3 local ba = require("flatbuffers.binaryarray")
6 local bunpack = ba.Unpack
10 function type_mt:Pack(value)
11 return bpack(self.packFmt, value)
14 function type_mt:Unpack(buf, pos)
15 return bunpack(self.packFmt, buf, pos)
18 function type_mt:ValidNumber(n)
19 if not self.min_value and not self.max_value then return true end
20 return self.min_value <= n and n <= self.max_value
23 function type_mt:EnforceNumber(n)
24 -- duplicate code since the overhead of function calls
25 -- for such a popular method is time consuming
26 if not self.min_value and not self.max_value then
30 if self.min_value <= n and n <= self.max_value then
34 error("Number is not in the valid range")
37 function type_mt:EnforceNumberAndPack(n)
38 return bpack(self.packFmt, n)
41 function type_mt:ConvertType(n, otherType)
42 assert(self.bytewidth == otherType.bytewidth, "Cannot convert between types of different widths")
43 if self == otherType then
46 return otherType:Unpack(self:Pack(n))
54 lua_type = type(true),
144 lua_type = type(1.0),
154 lua_type = type(1.0),
159 -- register the base class
160 setmetatable(bool_mt, {__index = type_mt})
161 setmetatable(uint8_mt, {__index = type_mt})
162 setmetatable(uint16_mt, {__index = type_mt})
163 setmetatable(uint32_mt, {__index = type_mt})
164 setmetatable(uint64_mt, {__index = type_mt})
165 setmetatable(int8_mt, {__index = type_mt})
166 setmetatable(int16_mt, {__index = type_mt})
167 setmetatable(int32_mt, {__index = type_mt})
168 setmetatable(int64_mt, {__index = type_mt})
169 setmetatable(float32_mt, {__index = type_mt})
170 setmetatable(float64_mt, {__index = type_mt})
182 m.Float32 = float32_mt
183 m.Float64 = float64_mt
185 m.UOffsetT = uint32_mt
186 m.VOffsetT = uint16_mt
187 m.SOffsetT = int32_mt
189 function GenerateTypes(listOfTypes)
190 for _,t in pairs(listOfTypes) do
191 t.Pack = function(self, value) return bpack(self.packFmt, value) end
192 t.Unpack = function(self, buf, pos) return bunpack(self.packFmt, buf, pos) end