-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlanguage_lua.c
316 lines (278 loc) · 7.91 KB
/
language_lua.c
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
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <errno.h>
#include "language.h"
typedef struct _lua_internal {
language_t*li;
lua_State* state;
int method_count;
} lua_internal_t;
static const luaL_reg lualibs[] =
{
{"base", luaopen_base}, // dofile etc.
{"table", luaopen_table},
{"math", luaopen_math},
{"string", luaopen_string},
{NULL, NULL}
};
static void openlualibs(lua_State *l)
{
const luaL_reg *lib;
for(lib = lualibs; lib->func != NULL; lib++)
{
lib->func(l);
lua_settop(l, 0);
}
}
static void dump_stack(lua_State *l) {
int i;
int len = lua_gettop(l);
for(i=1;i<=len;i++) {
printf("[%d] ", i);
switch(lua_type(l, i)) {
case LUA_TNONE:
printf("none");
break;
case LUA_TNIL:
printf("nil");
break;
case LUA_TBOOLEAN:
printf("%s", lua_toboolean(l, i) ? "true" : "false");
break;
case LUA_TLIGHTUSERDATA:
printf("<lightuserdata>");
break;
case LUA_TNUMBER:
printf("%.1f", lua_tonumber(l, i));
break;
case LUA_TSTRING:
printf("\"%s\"", lua_tostring(l, i));
break;
case LUA_TTABLE:
printf("<table>");
break;
case LUA_TFUNCTION:
printf("<function>");
break;
case LUA_TUSERDATA:
printf("<userdata>");
break;
case LUA_TTHREAD:
printf("<thread>");
break;
default:
printf("?");
break;
}
printf("\n");
}
}
static void show_error(language_t*li, lua_State *l)
{
const char *s = lua_tolstring(l, -1, NULL);
log_msg("%s", s);
language_error(li, s);
}
static bool initialize_lua(language_t*li, size_t mem_size)
{
if(li->internal)
return true; // already initialized
li->internal = calloc(1, sizeof(lua_internal_t));
lua_internal_t*lua = (lua_internal_t*)li->internal;
lua->li = li;
lua_State*l = lua->state = lua_open();
openlualibs(l);
return true;
}
static bool compile_script_lua(language_t*li, const char*script)
{
lua_internal_t*lua = (lua_internal_t*)li->internal;
lua_State*l = lua->state;
int error = luaL_loadbuffer(l, script, strlen(script), "@file.lua");
if(!error) {
error = lua_pcall(l, 0, LUA_MULTRET, 0);
}
if(error) {
show_error(li, l);
language_error(li, "Couldn't compile: %d\n", error);
return false;
}
return true;
}
static void push_value(lua_State*l, value_t*value)
{
int i;
switch(value->type) {
case TYPE_VOID:
lua_pushnil(l);
break;
case TYPE_FLOAT32:
lua_pushnumber(l, value->f32);
break;
case TYPE_INT32:
lua_pushinteger(l, value->i32);
break;
case TYPE_BOOLEAN:
lua_pushboolean(l, value->b);
break;
case TYPE_STRING: {
lua_pushstring(l, value->str);
}
break;
case TYPE_ARRAY: {
lua_newtable(l);
for(i=0;i<value->length;i++) {
lua_pushinteger(l, i);
push_value(l, value->data[i]);
lua_settable(l, -3);
}
}
break;
default: {
lua_pushnil(l);
}
}
}
static value_t* lua_to_value(language_t*li, int idx)
{
lua_internal_t*lua = (lua_internal_t*)li->internal;
lua_State*l = lua->state;
if(lua_gettop(l)+idx < 0) {
language_error(li, "[lua] Stack overflow: idx=%d, top=%d\n", idx, lua_gettop(l));
return NULL;
} else if(lua_isnoneornil(l, idx)) {
return value_new_void();
} else if(lua_isboolean(l, idx)) {
return value_new_boolean(lua_toboolean(l, idx));
} else if(lua_isnumber(l, idx)) {
return value_new_float32(lua_tonumber(l, idx));
} else if(lua_isnumber(l, idx)) {
return value_new_int32(lua_tointeger(l, idx));
} else if(lua_isstring(l, idx)) {
value_t*v = value_new_string(lua_tostring(l, idx));
return v;
} else if(lua_istable(l, idx)) {
value_t*array = array_new();
int i;
for(i=0;;i++) {
lua_pushinteger(l, i);
lua_gettable(l, idx<0?idx-1:idx);
if(lua_isnil(l, -1)) {
lua_pop(l, 1);
break;
}
array_append(array, lua_to_value(li, -1));
lua_pop(l, 1);
}
return array;
}
language_error(li, "Don't know how to process lua type: %d\n");
return NULL;
}
static void define_constant_lua(struct _language*li, const char*name, value_t*value)
{
lua_internal_t*lua = (lua_internal_t*)li->internal;
lua_State*l = lua->state;
push_value(l, value);
lua_setglobal(l, name);
}
typedef struct {
language_t*li;
value_t*f;
const char*name;
} function_data_t;
static int lua_function_proxy(lua_State*l)
{
assert(lua_islightuserdata(l, lua_upvalueindex(1)));
function_data_t*data = (function_data_t*)lua_touserdata(l, lua_upvalueindex(1));
value_t*f = data->f;
int i;
log_dbg("[lua] lua calls function %s (%d parameters)", data->name, f->num_params);
value_t*args = array_new();
int j = -f->num_params;
for(i=0;i<f->num_params;i++) {
value_t*a = lua_to_value(data->li, j++);
if(a == NULL) {
luaL_argerror(l, i+1, "invalid or missing value");
}
array_append(args, a);
}
value_t*ret = f->call(f, args);
value_destroy(args);
push_value(l, ret);
value_destroy(ret);
return 1;
}
static void define_function_lua(struct _language*li, const char*name, function_t*f)
{
lua_internal_t*lua = (lua_internal_t*)li->internal;
lua_State*l = lua->state;
printf("[lua] defining function %s (%d parameters)\n", name, f->num_params);
function_data_t*data = calloc(sizeof(function_data_t),1);
data->f = f;
data->name = name;
data->li = li;
lua_pushlightuserdata(l, (void*)data);
lua_pushcclosure(l, lua_function_proxy, 1);
lua_setglobal(l, name);
}
static bool is_function_lua(language_t*li, const char*name)
{
lua_internal_t*lua = (lua_internal_t*)li->internal;
lua_State*l = lua->state;
printf("[lua] is_function(%s)\n", name);
lua_getfield(l, LUA_GLOBALSINDEX, name);
bool ret = lua_isfunction(l, -1);
lua_pop(l, 1);
return ret;
}
static value_t* call_function_lua(language_t*li, const char*name, value_t*args)
{
lua_internal_t*lua = (lua_internal_t*)li->internal;
lua_State*l = lua->state;
lua_getfield(l, LUA_GLOBALSINDEX, name);
if(!lua_isfunction(l, -1)) {
language_error(li, "%s is not a function", name);
return NULL;
}
int i;
for(i=0;i<args->length;i++) {
push_value(l, args->data[i]);
}
int error = lua_pcall(l, /*nargs*/args->length, /*nresults*/1, 0);
if(error) {
show_error(li, l);
language_error(li, "Error calling function %s: %d\n", name, error);
return NULL;
}
value_t*ret = lua_to_value(li, -1);
lua_pop(l, 1);
return ret;
}
static void destroy_lua(language_t* li)
{
if(li->internal) {
lua_internal_t*lua = (lua_internal_t*)li->internal;
lua_close(lua->state);
free(lua);
}
free(li);
}
language_t* lua_interpreter_new()
{
language_t * li = calloc(1, sizeof(language_t));
li->name = "lua";
li->initialize = initialize_lua;
li->compile_script = compile_script_lua;
li->is_function = is_function_lua;
li->call_function = call_function_lua;
li->define_function = define_function_lua;
li->define_constant = define_constant_lua;
li->destroy = destroy_lua;
return li;
}