-
Notifications
You must be signed in to change notification settings - Fork 23
/
types.lua
183 lines (150 loc) · 5.32 KB
/
types.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
local argtypes = {}
local function interpretdefaultvalue(arg)
local default = arg.default
if type(default) == 'boolean' then
if default then
return '1'
else
return '0'
end
elseif type(default) == 'number' then
return tostring(default)
elseif type(default) == 'string' then
return default
elseif type(default) == 'function' then
default = default(arg)
assert(type(default) == 'string', 'a default function must return a string')
return default
elseif type(default) == 'nil' then
return nil
else
error('unknown default type value')
end
end
argtypes.index = {
helpname = function(arg)
return "index"
end,
declare = function(arg)
-- if it is a number we initialize here
local default = tonumber(interpretdefaultvalue(arg)) or 1
return string.format("long arg%d = %d;", arg.i, tonumber(default)-1)
end,
check = function(arg, idx)
return string.format("lua_isnumber(L, %d)", idx)
end,
read = function(arg, idx)
return string.format("arg%d = (long)lua_tonumber(L, %d)-1;", arg.i, idx)
end,
init = function(arg)
-- otherwise do it here
if arg.default then
local default = interpretdefaultvalue(arg)
if not tonumber(default) then
return string.format("arg%d = %s-1;", arg.i, default)
end
end
end,
carg = function(arg)
return string.format('arg%d', arg.i)
end,
creturn = function(arg)
return string.format('arg%d', arg.i)
end,
precall = function(arg)
if arg.returned then
return string.format('lua_pushnumber(L, (lua_Number)arg%d+1);', arg.i)
end
end,
postcall = function(arg)
if arg.creturned then
return string.format('lua_pushnumber(L, (lua_Number)arg%d+1);', arg.i)
end
end
}
for _,typename in ipairs({"real", "unsigned char", "char", "short", "int", "long", "float", "double"}) do
argtypes[typename] = {
helpname = function(arg)
return typename
end,
declare = function(arg)
-- if it is a number we initialize here
local default = tonumber(interpretdefaultvalue(arg)) or 0
return string.format("%s arg%d = %g;", typename, arg.i, default)
end,
check = function(arg, idx)
return string.format("lua_isnumber(L, %d)", idx)
end,
read = function(arg, idx)
return string.format("arg%d = (%s)lua_tonumber(L, %d);", arg.i, typename, idx)
end,
init = function(arg)
-- otherwise do it here
if arg.default then
local default = interpretdefaultvalue(arg)
if not tonumber(default) then
return string.format("arg%d = %s;", arg.i, default)
end
end
end,
carg = function(arg)
return string.format('arg%d', arg.i)
end,
creturn = function(arg)
return string.format('arg%d', arg.i)
end,
precall = function(arg)
if arg.returned then
return string.format('lua_pushnumber(L, (lua_Number)arg%d);', arg.i)
end
end,
postcall = function(arg)
if arg.creturned then
return string.format('lua_pushnumber(L, (lua_Number)arg%d);', arg.i)
end
end
}
end
argtypes.byte = argtypes['unsigned char']
argtypes.boolean = {
helpname = function(arg)
return "boolean"
end,
declare = function(arg)
-- if it is a number we initialize here
local default = tonumber(interpretdefaultvalue(arg)) or 0
return string.format("int arg%d = %d;", arg.i, tonumber(default))
end,
check = function(arg, idx)
return string.format("lua_isboolean(L, %d)", idx)
end,
read = function(arg, idx)
return string.format("arg%d = lua_toboolean(L, %d);", arg.i, idx)
end,
init = function(arg)
-- otherwise do it here
if arg.default then
local default = interpretdefaultvalue(arg)
if not tonumber(default) then
return string.format("arg%d = %s;", arg.i, default)
end
end
end,
carg = function(arg)
return string.format('arg%d', arg.i)
end,
creturn = function(arg)
return string.format('arg%d', arg.i)
end,
precall = function(arg)
if arg.returned then
return string.format('lua_pushboolean(L, arg%d);', arg.i)
end
end,
postcall = function(arg)
if arg.creturned then
return string.format('lua_pushboolean(L, arg%d);', arg.i)
end
end
}
return argtypes