local m = {} local ba = require("flatbuffers.binaryarray") local bpack = ba.Pack local bunpack = ba.Unpack local type_mt = {} function type_mt:Pack(value) return bpack(self.packFmt, value) end function type_mt:Unpack(buf, pos) return bunpack(self.packFmt, buf, pos) end function type_mt:ValidNumber(n) if not self.min_value and not self.max_value then return true end return self.min_value <= n and n <= self.max_value end function type_mt:EnforceNumber(n) -- duplicate code since the overhead of function calls -- for such a popular method is time consuming if not self.min_value and not self.max_value then return end if self.min_value <= n and n <= self.max_value then return end error("Number is not in the valid range") end function type_mt:EnforceNumbers(a,b) -- duplicate code since the overhead of function calls -- for such a popular method is time consuming if not self.min_value and not self.max_value then return end if self.min_value <= a and a <= self.max_value and self.min_value <= b and b <= self.max_value then return end error("Number is not in the valid range") end function type_mt:EnforceNumberAndPack(n) return bpack(self.packFmt, n) end function type_mt:ConvertType(n, otherType) assert(self.bytewidth == otherType.bytewidth, "Cannot convert between types of different widths") if self == otherType then return n end return otherType:Unpack(self:Pack(n)) end local bool_mt = { bytewidth = 1, min_value = false, max_value = true, lua_type = type(true), name = "bool", packFmt = "