/
lang_lua.lua
327 lines (314 loc) · 11.5 KB
/
lang_lua.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
-- Copyright (c) 2010 by Robert G. Jakabosky <bobby@neoawareness.com>
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.
--
-- C to Lua Base types
--
basetype "bool" "boolean" "0"
basetype "char" "integer" "0"
basetype "unsigned char" "integer" "0"
basetype "short" "integer" "0"
basetype "unsigned short" "integer" "0"
basetype "int" "integer" "0"
basetype "unsigned" "integer" "0"
basetype "unsigned int" "integer" "0"
basetype "long" "integer" "0"
basetype "unsigned long" "integer" "0"
-- stdint types.
basetype "int8_t" "integer" "0"
basetype "int16_t" "integer" "0"
basetype "int32_t" "integer" "0"
basetype "int64_t" "integer" "0"
basetype "uint8_t" "integer" "0"
basetype "uint16_t" "integer" "0"
basetype "uint32_t" "integer" "0"
basetype "uint64_t" "integer" "0"
basetype "size_t" "integer" "0"
basetype "ssize_t" "integer" "0"
basetype "off_t" "integer" "0"
basetype "time_t" "integer" "0"
basetype "float" "number" "0.0"
basetype "double" "number" "0.0"
basetype "char *" "string" "NULL"
basetype "unsigned char *" "string" "NULL"
basetype "void *" "lightuserdata" "NULL"
basetype "uint8_t *" "lightuserdata" "NULL"
basetype "lua_State *" "thread" "NULL"
basetype "void" "nil" "NULL"
basetype "<any>" "nil" "NULL"
basetype "<table>" "table" "NULL"
--
-- to/check/push/delete methods
--
print"============ create Lua to/check/push/delete methods ================="
local lua_base_types = {
['nil'] = { push = 'lua_pushnil' },
['number'] = { to = 'lua_tonumber', opt = 'luaL_optnumber', check = 'luaL_checknumber',
push = 'lua_pushnumber' },
['integer'] = { to = 'lua_tointeger', opt = 'luaL_optinteger', check = 'luaL_checkinteger',
push = 'lua_pushinteger' },
['string'] = { to = 'lua_tolstring', opt = 'luaL_optlstring', check = 'luaL_checklstring',
push = 'lua_pushstring', push_len = 'lua_pushlstring' },
['boolean'] = { to = 'lua_toboolean', check = 'lua_toboolean', push = 'lua_pushboolean' },
['thread'] = { to = 'lua_tothread', check = 'lua_tothread', push = 'lua_pushthread' },
['lightuserdata'] =
{ to = 'lua_touserdata', check = 'lua_touserdata', push = 'lua_pushlightuserdata' },
}
reg_stage_parser("lang_type_process", {
basetype = function(self, rec, parent)
local l_type = lua_base_types[rec.lang_type]
if l_type == nil then return end
rec._ffi_push = function(self, var, flags, unwrap)
local wrap = var.ffi_wrap
if wrap then
return wrap .. '(${' .. var.name .. '})' .. (unwrap or '')
else
return '${' .. var.name .. '}' .. (unwrap or '')
end
end
if rec.lang_type == 'string' then
local cast = ''
if rec.c_type ~= 'const char *' and rec.c_type ~= 'char *' then
cast = '(' .. rec.c_type .. ')'
end
rec._to = function(self, var)
return '${' .. var.name .. '} = ' .. cast ..
l_type.to .. '(L,${' .. var.name .. '::idx},&(${' .. var.name .. '_len}));\n'
end
rec._define = function(self, var)
return 'size_t ${' .. var.name .. '_len};\n' ..
' ' .. var.c_type .. ' ${' .. var.name .. '};\n'
end
rec._check = function(self, var)
return '${' .. var.name .. '} = ' .. cast ..
l_type.check .. '(L,${' .. var.name .. '::idx},&(${' .. var.name .. '_len}));\n'
end
rec._opt = function(self, var, default)
if default then
default = '"' .. default .. '"'
else
default = 'NULL'
end
return '${' .. var.name .. '} = ' .. cast ..
l_type.opt .. '(L,${' .. var.name .. '::idx},' .. default ..
',&(${' .. var.name .. '_len}));\n'
end
rec._push = function(self, var)
if var.has_length then
return
' if(${' .. var.name .. '} == NULL) lua_pushnil(L);' ..
' else ' .. l_type.push_len .. '(L, ${' .. var.name .. '},' ..
'${' .. var.name .. '_len});\n'
end
return ' ' .. l_type.push .. '(L, ${' .. var.name .. '});\n'
end
rec._ffi_define = function(self, var)
return ''
end
rec._ffi_push = function(self, var)
if var.has_length then
return 'ffi_string_len(${' .. var.name .. '},${' .. var.name .. '_len})'
end
return 'ffi_string(${' .. var.name .. '})'
end
rec._ffi_check = function(self, var)
return 'local ${' .. var.name .. '_len} = #${' .. var.name .. '}\n'
end
rec._ffi_opt = function(self, var, default)
if default then
default = (' or %q'):format(tostring(default))
else
default = ''
end
return
'${' .. var.name .. '} = ${' .. var.name .. '}' .. default .. '\n' ..
' local ${' .. var.name .. '_len} = #${' .. var.name .. '}\n'
end
else
rec._to = function(self, var)
return '${' .. var.name .. '} = ' .. l_type.to .. '(L,${' .. var.name .. '::idx});\n'
end
rec._define = function(self, var)
return var.c_type .. ' ${' .. var.name .. '};\n'
end
rec._check = function(self, var)
return '${' .. var.name .. '} = ' .. l_type.check .. '(L,${' .. var.name .. '::idx});\n'
end
rec._opt = function(self, var, default)
default = default or '0'
if l_type.opt then
return '${' .. var.name .. '} = ' ..
l_type.opt .. '(L,${' .. var.name .. '::idx},' .. default .. ');\n'
end
return '${' .. var.name .. '} = ' ..
l_type.to .. '(L,${' .. var.name .. '::idx});\n'
end
rec._push = function(self, var)
return ' ' .. l_type.push .. '(L, ${' .. var.name .. '});\n'
end
rec._ffi_define = function(self, var)
return ''
end
rec._ffi_check = function(self, var)
return '\n'
end
rec._ffi_opt = function(self, var, default)
default = tostring(default or '0')
return '${' .. var.name .. '} = ${' .. var.name .. '} or ' .. default .. '\n'
end
end
end,
error_code = function(self, rec, parent)
local func_name = 'error_code__' .. rec.name .. '__push'
rec.func_name = func_name
-- create _push_error & _push function
rec._push = function(self, var)
return ' ' .. func_name ..'(L, ${' .. var.name .. '});\n'
end
rec._push_error = rec._push
rec._ffi_push = function(self, var, flags, unwrap)
return func_name ..'(${' .. var.name .. '})' .. (unwrap or '')
end
rec._ffi_push_error = rec._ffi_push
end,
object = function(self, rec, parent)
rec.lang_type = 'userdata'
local type_name = 'obj_type_' .. rec.name
rec._obj_type_name = type_name
-- create _check/_delete/_push functions
rec._define = function(self, var)
return var.c_type .. ' ${'..var.name..'};\n'
end
rec._check = function(self, var)
return '${'..var.name..'} = '..type_name..'_check(L,${'..var.name..'::idx});\n'
end
rec._opt = function(self, var)
return '${'..var.name..'} = '..type_name..'_optional(L,${'..var.name..'::idx});\n'
end
rec._delete = function(self, var, flags)
if not flags then
return '${'..var.name..'} = '..type_name..'_delete(L,${'..var.name..'::idx});\n'
end
return '${'..var.name..'} = '..type_name..'_delete(L,${'..var.name..'::idx},'..flags..');\n'
end
rec._to = rec._check
rec._push = function(self, var, flags)
if flags == false then
return ' '..type_name..'_push(L, ${'..var.name..'});\n'
end
if flags == nil then flags = '0' end
return ' '..type_name..'_push(L, ${'..var.name..'}, ' .. flags .. ');\n'
end
rec._ffi_define = function(self, var)
return ''
end
rec._ffi_check = function(self, var)
if not rec.subs then
-- no sub-classes
return rec._ffi_check_fast(self, var)
end
-- has sub-classes do extra casting if needed.
if var.is_this then
return 'local ${' .. var.name .. '} = '..type_name..'_check(self)\n'
end
local name = '${' .. var.name .. '}'
return name .. ' = '..type_name..'_check('..name..')\n'
end
rec._ffi_opt = function(self, var)
if var.is_this then
return 'local ${' .. var.name .. '} = '..type_name..'_check(self)\n'
end
local name = '${' .. var.name .. '}'
return name .. ' = '..name..' and '..type_name..'_check('..name..') or nil\n'
end
rec._ffi_delete = function(self, var, has_flags)
if not has_flags then
return 'local ${'..var.name..'} = '..type_name..'_delete(self)\n'
end
return 'local ${'..var.name..'},${'..var.name..'_flags} = '..type_name..'_delete(self)\n'
end
rec._ffi_push = function(self, var, flags, unwrap)
if flags == false then
return type_name..'_push(${'..var.name..'})' .. (unwrap or '')
end
if flags == nil then flags = '0' end
return type_name..'_push(${'..var.name..'}, ' .. flags .. ')' .. (unwrap or '')
end
if rec.error_on_null then
rec._push_error = function(self, var)
return ' lua_pushstring(L, ' .. rec.error_on_null .. ');\n'
end
rec._ffi_push_error = function(self, var)
return rec.error_on_null
end
end
end,
interface = function(self, rec, parent)
rec.lang_type = 'userdata'
local if_name = rec.name
-- create _check/_delete/_push functions
rec._define = function(self, var)
return if_name..'IF_VAR(${'..var.name..'});\n'
end
rec._check = function(self, var)
return if_name..'IF_LUA_CHECK(L,${'..var.name..'::idx}, ${'..var.name..'});\n'
end
rec._opt = function(self, var)
return if_name..'IF_LUA_OPTIONAL(L,${'..var.name..'::idx}, ${'..var.name..'});\n'
end
rec._delete = function(self, var, flags)
error("Can't delete an interface object.")
end
rec._to = rec._check
rec._push = function(self, var, flags)
error("Can't push an interface object.")
end
rec._ffi_define = function(self, var)
return 'local ${' .. var.name .. '}_if'
end
rec._ffi_check = function(self, var)
local name = '${' .. var.name .. '}'
return name .. '_if = '..name..'.NOBJ_get_'..if_name..'IF'
..' or obj_type_'..if_name..'_check('..name..')\n'
end
rec._ffi_opt = function(self, var)
local name = '${' .. var.name .. '}'
return name .. '_if = '..name..' and ('..name..'.NOBJ_get_'..if_name..'IF'
..' or obj_type_'..if_name..'_check('..name..')) or nil\n'
end
rec._ffi_delete = function(self, var, has_flags)
error("Can't delete an interface object.")
end
rec._ffi_push = function(self, var, flags, unwrap)
error("Can't push an interface object.")
end
end,
callback_func = function(self, rec, parent)
rec.lang_type = 'function'
-- create _check/_delete/_push functions
rec._check = function(self, var)
return 'lua_checktype_ref(L, ${' .. var.name .. '::idx}, LUA_TFUNCTION);\n'
end
rec._delete = nil
rec._to = rec._check
rec._push = function(self, var)
return 'lua_rawgeti(L, LUA_REGISTRYINDEX, ' .. var .. ');\n'
end
end,
})