diff --git a/src/bindings/lua/hammer.lua b/src/bindings/lua/hammer.lua index 29bc8699124582df5a40f940f89d8e13f2d12b46..0247924599b9e4f299bd6f5307c38c408601c53f 100644 --- a/src/bindings/lua/hammer.lua +++ b/src/bindings/lua/hammer.lua @@ -189,6 +189,19 @@ local hammer = {} hammer.parser = ffi.metatype("HParser", mt) local counted_array + + + +local function _castCountedArray(array) + if array == nil then return nil end + if array.elements == nil then return nil end + local ret = {} + for i = 0, tonumber(array.used) do + ret[#ret+1] = array.elements[i]() + end + return ret +end + local arr_mt = { __index = function(table, key) return table.elements[key] @@ -203,13 +216,7 @@ local arr_mt = { end end end, - __call = function(self) - ret = {} - for i, v in ipairs(self) - do ret[#ret+1] = v() - end - return ret - end + __call = _castCountedArray, } counted_array = ffi.metatype("HCountedArray", arr_mt) @@ -235,9 +242,11 @@ local parse_result = ffi.metatype("HParseResult", result_mt) local token_types = ffi.new("HTokenType") + local parsed_token local tok_mt = { __call = function(self) + if self == nil then return nil end if self.token_type == ffi.C.TT_BYTES then return self.bytes() elseif self.token_type == ffi.C.TT_SINT then @@ -247,6 +256,7 @@ local tok_mt = { elseif self.token_type == ffi.C.TT_SEQUENCE then return self.seq() end + return nil end } parsed_token = ffi.metatype("HParsedToken", tok_mt)