-
Notifications
You must be signed in to change notification settings - Fork 0
/
structs.lua
44 lines (39 loc) · 969 Bytes
/
structs.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
if _G.structs then
error('structs cannot be loaded more than once')
end
local ffi = require('ffi')
local structs = {
['DumpHeader'] = {
ffi.cdef [[
typedef struct {
uint32_t signature;
uint8_t version;
bool format;
bool endianness;
uint8_t s_int;
uint8_t s_size_t;
uint8_t s_instruction;
uint8_t s_lua_Number;
uint8_t integral;
} DumpHeader; ]]
},
}
_G.structs = structs
return setmetatable({}, {
__index = function(self, key)
if structs[key] then
-- print 'Fetching struct'
return key
else
return error('Struct not defined.')
end
end,
__newindex = function(self, key, value)
if structs[key] then
return error(('Struct %s already defined'):format(key))
end
local t = type(value)
structs[key] = t == 'string' and {ffi.cdef(value)} or t == 'table' and t or error(('Invalid type for struct %s'):format(key))
end
})
-- basically augement