comparison lua/luadyncall/src/dynstruct.lua @ 0:0cfcc391201f

initial from svn dyncall-1745
author Daniel Adler
date Thu, 19 Mar 2015 22:26:28 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:0cfcc391201f
1 require "ldynstruct"
2 require "dyntype"
3
4 dynstruct_metatable = {
5 __index = function(s, f)
6 local typeinfo = rawget(s, "typeinfo")
7 local fieldinfo = typeinfo.fields[f]
8 if not fieldinfo then error("unknown field "..f.." for type "..typeinfo.name) end
9 return ldynstruct.dynpeek( rawget(s, "pointer"), fieldinfo.offset, fieldinfo.typeinfo.signature )
10 end,
11 __newindex = function(s, f, v)
12 local typeinfo = rawget(s, "typeinfo")
13 local fieldinfo = typeinfo.fields[f]
14 if not fieldinfo then error("unknown field "..f.." for type "..typeinfo.name) end
15 ldynstruct.dynpoke( rawget(s, "pointer"), fieldinfo.offset, fieldinfo.typeinfo.signature, v )
16 end
17 }
18
19 function newdynstruct(typename)
20 local typeinfo = gettypeinfo(typename)
21 local object = { pointer = ldynstruct.newstruct( typeinfo.size ), typeinfo = typeinfo }
22 setmetatable(object, dynstruct_metatable)
23 return object
24 end
25
26 function dyncast(object, typeinfo)
27 local pointer
28 if type(object) == "userdata" then
29 pointer = object
30 elseif type(object) == "table" then
31 pointer = rawgeti(object, "pointer")
32 end
33 local object = { pointer = pointer, typeinfo = gettypeinfo(typeinfo) }
34 setmetatable(object, dynstruct_metatable)
35 return object
36 end
37