annotate lua/luadyncall/src/dynstruct.lua @ 19:5b51738793c6

- layout restructuring
author cslag
date Wed, 30 Mar 2016 02:06:16 +0200
parents 0cfcc391201f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
1 require "ldynstruct"
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
2 require "dyntype"
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
3
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4 dynstruct_metatable = {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
5 __index = function(s, f)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
6 local typeinfo = rawget(s, "typeinfo")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
7 local fieldinfo = typeinfo.fields[f]
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8 if not fieldinfo then error("unknown field "..f.." for type "..typeinfo.name) end
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
9 return ldynstruct.dynpeek( rawget(s, "pointer"), fieldinfo.offset, fieldinfo.typeinfo.signature )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
10 end,
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
11 __newindex = function(s, f, v)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
12 local typeinfo = rawget(s, "typeinfo")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13 local fieldinfo = typeinfo.fields[f]
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14 if not fieldinfo then error("unknown field "..f.." for type "..typeinfo.name) end
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15 ldynstruct.dynpoke( rawget(s, "pointer"), fieldinfo.offset, fieldinfo.typeinfo.signature, v )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16 end
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19 function newdynstruct(typename)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20 local typeinfo = gettypeinfo(typename)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21 local object = { pointer = ldynstruct.newstruct( typeinfo.size ), typeinfo = typeinfo }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
22 setmetatable(object, dynstruct_metatable)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
23 return object
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24 end
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26 function dyncast(object, typeinfo)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27 local pointer
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
28 if type(object) == "userdata" then
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
29 pointer = object
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
30 elseif type(object) == "table" then
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
31 pointer = rawgeti(object, "pointer")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
32 end
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
33 local object = { pointer = pointer, typeinfo = gettypeinfo(typeinfo) }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
34 setmetatable(object, dynstruct_metatable)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
35 return object
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
36 end
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
37