Lua (5.3) Language addition (#4804)
[platform/upstream/flatbuffers.git] / lua / flatbuffers / numTypes.lua
1 local m = {}
2
3 local ba = require("flatbuffers.binaryarray")
4
5 local bpack = ba.Pack
6 local bunpack = ba.Unpack
7
8 local type_mt =  {}
9
10 function type_mt:Pack(value)
11     return bpack(self.packFmt, value)
12 end
13
14 function type_mt:Unpack(buf, pos)    
15     return bunpack(self.packFmt, buf, pos)
16 end
17
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
21 end
22
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 
27         return 
28     end
29     
30     if self.min_value <= n and n <= self.max_value then 
31         return
32     end    
33     
34     error("Number is not in the valid range") 
35 end
36
37 function type_mt:EnforceNumberAndPack(n)
38     return bpack(self.packFmt, n)    
39 end
40
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
44         return n
45     end
46     return otherType:Unpack(self:Pack(n))
47 end
48
49 local bool_mt =
50 {
51     bytewidth = 1,
52     min_value = false,
53     max_value = true,
54     lua_type = type(true),
55     name = "bool",
56     packFmt = "<b"
57 }
58
59 local uint8_mt = 
60 {
61     bytewidth = 1,
62     min_value = 0,
63     max_value = 2^8-1,
64     lua_type = type(1),
65     name = "uint8",
66     packFmt = "<I1"
67 }
68
69 local uint16_mt = 
70 {
71     bytewidth = 2,
72     min_value = 0,
73     max_value = 2^16-1,
74     lua_type = type(1),
75     name = "uint16",
76     packFmt = "<I2"
77 }
78
79 local uint32_mt = 
80 {
81     bytewidth = 4,
82     min_value = 0,
83     max_value = 2^32-1,
84     lua_type = type(1),
85     name = "uint32",
86     packFmt = "<I4"
87 }
88
89 local uint64_mt = 
90 {
91     bytewidth = 8,
92     min_value = 0,
93     max_value = 2^64-1,
94     lua_type = type(1),
95     name = "uint64",
96     packFmt = "<I8"
97 }
98
99 local int8_mt = 
100 {
101     bytewidth = 1,
102     min_value = -2^7,
103     max_value = 2^7-1,
104     lua_type = type(1),
105     name = "int8",
106     packFmt = "<i1"
107 }
108
109 local int16_mt = 
110 {
111     bytewidth = 2,
112     min_value = -2^15,
113     max_value = 2^15-1,
114     lua_type = type(1),
115     name = "int16",
116     packFmt = "<i2"
117 }
118
119 local int32_mt = 
120 {
121     bytewidth = 4,
122     min_value = -2^15,
123     max_value = 2^15-1,
124     lua_type = type(1),
125     name = "int32",
126     packFmt = "<i4"
127 }
128
129 local int64_mt = 
130 {
131     bytewidth = 8,
132     min_value = -2^63,
133     max_value = 2^63-1,
134     lua_type = type(1),
135     name = "int64",
136     packFmt = "<i8"
137 }
138
139 local float32_mt = 
140 {
141     bytewidth = 4,
142     min_value = nil,
143     max_value = nil,
144     lua_type = type(1.0),
145     name = "float32",
146     packFmt = "<f"
147 }
148
149 local float64_mt = 
150 {
151     bytewidth = 8,
152     min_value = nil,
153     max_value = nil,
154     lua_type = type(1.0),
155     name = "float64",
156     packFmt = "<d"
157 }
158
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})
171
172
173 m.Bool      = bool_mt
174 m.Uint8     = uint8_mt
175 m.Uint16    = uint16_mt
176 m.Uint32    = uint32_mt
177 m.Uint64    = uint64_mt
178 m.Int8      = int8_mt
179 m.Int16     = int16_mt
180 m.Int32     = int32_mt
181 m.Int64     = int64_mt
182 m.Float32   = float32_mt
183 m.Float64   = float64_mt
184
185 m.UOffsetT  = uint32_mt
186 m.VOffsetT  = uint16_mt
187 m.SOffsetT  = int32_mt
188
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
193     end
194 end
195
196 GenerateTypes(m)
197
198 return m