diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lua/luadyncall/src/dynstruct.lua	Thu Mar 19 22:26:28 2015 +0100
@@ -0,0 +1,37 @@
+require "ldynstruct"
+require "dyntype"
+
+dynstruct_metatable = {
+  __index = function(s, f)
+    local typeinfo = rawget(s, "typeinfo")
+    local fieldinfo = typeinfo.fields[f]
+    if not fieldinfo then error("unknown field "..f.." for type "..typeinfo.name) end
+    return ldynstruct.dynpeek( rawget(s, "pointer"), fieldinfo.offset, fieldinfo.typeinfo.signature )
+  end,
+  __newindex = function(s, f, v)
+    local typeinfo = rawget(s, "typeinfo")
+    local fieldinfo = typeinfo.fields[f]
+    if not fieldinfo then error("unknown field "..f.." for type "..typeinfo.name) end
+    ldynstruct.dynpoke( rawget(s, "pointer"), fieldinfo.offset, fieldinfo.typeinfo.signature, v )
+  end
+}
+
+function newdynstruct(typename)
+  local typeinfo = gettypeinfo(typename)
+  local object = { pointer = ldynstruct.newstruct( typeinfo.size ), typeinfo = typeinfo }
+  setmetatable(object, dynstruct_metatable)
+  return object
+end
+
+function dyncast(object, typeinfo)
+  local pointer
+  if type(object) == "userdata" then
+    pointer = object
+  elseif type(object) == "table" then
+    pointer = rawgeti(object, "pointer")
+  end
+  local object = { pointer = pointer, typeinfo = gettypeinfo(typeinfo) }
+  setmetatable(object, dynstruct_metatable)
+  return object
+end
+