-
Notifications
You must be signed in to change notification settings - Fork 22
/
luabind.c
473 lines (433 loc) · 8.81 KB
/
luabind.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#include "luabind.h"
#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct lvar {
int type;
union {
lua_Integer i;
lua_Number f;
const char * s;
int b;
void *p;
// struct larray *a;
// struct lmap *m;
} v;
};
struct vars {
int n;
int cap;
struct lvar *v;
};
struct vars *
lbind_new() {
struct vars *v = malloc(sizeof(*v));
if (v == NULL)
return NULL;
v->n = 0;
v->cap = 0;
v->v = NULL;
return v;
}
void
lbind_delete(struct vars *v) {
if (v == NULL)
return;
if (v->v) {
free(v->v);
}
free(v);
}
void
lbind_clear(struct vars *v) {
v->n = 0;
}
int
lbind_type(struct vars *v, int index) {
if (index < 0 || index >=v->n) {
return LT_NONE;
}
return v->v[index].type;
}
int
lbind_tointeger(struct vars *v, int index) {
if (index < 0 || index >=v->n) {
return 0;
}
return v->v[index].v.i;
}
double
lbind_toreal(struct vars *v, int index) {
if (index < 0 || index >=v->n) {
return 0;
}
return v->v[index].v.f;
}
const char *
lbind_tostring(struct vars *v, int index) {
if (index < 0 || index >=v->n) {
return 0;
}
return v->v[index].v.s;
}
int
lbind_toboolean(struct vars *v, int index) {
if (index < 0 || index >=v->n) {
return 0;
}
return v->v[index].v.b;
}
void *
lbind_topointer(struct vars *v, int index) {
if (index < 0 || index >=v->n) {
return 0;
}
return v->v[index].v.p;
}
static struct lvar *
newvalue(struct vars *v) {
if (v->n >= v->cap) {
int cap = v->cap * 2;
if (cap == 0) {
cap = 16;
}
struct lvar * nv = malloc(cap * sizeof(*nv));
if (nv == NULL)
return NULL;
memcpy(nv, v->v, v->n * sizeof(*nv));
free(v->v);
v->v = nv;
v->cap = cap;
}
struct lvar * ret = &v->v[v->n];
++v->n;
return ret;
}
int
lbind_pushinteger(struct vars *v, int i) {
struct lvar * s = newvalue(v);
if (s == NULL)
return -1;
s->type = LT_INTEGER;
s->v.i = i;
return 0;
}
int
lbind_pushreal(struct vars *v, double f) {
struct lvar * s = newvalue(v);
if (s == NULL)
return -1;
s->type = LT_REAL;
s->v.f = f;
return 0;
}
int
lbind_pushstring(struct vars *v, const char *str) {
struct lvar * s = newvalue(v);
if (s == NULL)
return -1;
s->type = LT_STRING;
s->v.s = str;
return 0;
}
int
lbind_pushboolean(struct vars *v, int b) {
struct lvar * s = newvalue(v);
if (s == NULL)
return -1;
s->type = LT_BOOLEAN;
s->v.b = b;
return 0;
}
int
lbind_pushnil(struct vars *v) {
struct lvar * s = newvalue(v);
if (s == NULL)
return -1;
s->type = LT_NIL;
return 0;
}
int
lbind_pushpointer(struct vars *v, void *p) {
struct lvar * s = newvalue(v);
if (s == NULL)
return -1;
s->type = LT_POINTER;
s->v.p = p;
return 0;
}
static int
ldelvars(lua_State *L) {
struct vars ** box = (struct vars **)lua_touserdata(L,1);
if (*box) {
lbind_delete(*box);
*box = NULL;
}
return 0;
}
static struct vars *
newvarsobject(lua_State *L) {
struct vars ** box = (struct vars **)lua_newuserdata(L, sizeof(*box));
*box = NULL;
struct vars * v = lbind_new();
if (v == NULL) {
return NULL;
}
*box = NULL;
if (luaL_newmetatable(L, "luabindvars")) {
lua_pushcfunction(L, ldelvars);
lua_setfield(L, -2, "__gc");
}
lua_setmetatable(L, -2);
*box = v;
return v;
}
// v -> lua stack
static int
pushargs(lua_State *L, struct vars *vars) {
if (vars == NULL)
return 0;
int n = vars->n;
luaL_checkstack(L, n, NULL);
int i;
for (i=0;i<n;i++) {
struct lvar *v = &vars->v[i];
switch(v->type) {
case LT_NIL:
lua_pushnil(L);
break;
case LT_INTEGER:
lua_pushinteger(L, v->v.i);
break;
case LT_REAL:
lua_pushnumber(L, v->v.f);
break;
case LT_STRING:
lua_pushstring(L, v->v.s);
break;
case LT_BOOLEAN:
lua_pushboolean(L, v->v.b);
break;
case LT_POINTER:
lua_pushlightuserdata(L, v->v.p);
break;
case LT_ARRAY : //todo
case LT_MAP : //todo
default :
luaL_error(L, "unsupport type %d", v->type);
}
}
return n;
}
// lua stack -> v
static void
genargs(lua_State *L, struct vars *v) {
int n = lua_gettop(L);
int i;
for (i=1;i<=n;i++) {
int err = 0;
int t = lua_type(L, i);
switch(t) {
case LUA_TNIL:
err = lbind_pushnil(v);
break;
case LUA_TBOOLEAN:
err = lbind_pushboolean(v, lua_toboolean(L, i));
break;
case LUA_TNUMBER:
if (lua_isinteger(L, i)) {
err = lbind_pushinteger(v, lua_tointeger(L, i));
} else {
err = lbind_pushreal(v, lua_tonumber(L, i));
}
break;
case LUA_TSTRING:
err = lbind_pushstring(v, lua_tostring(L, i));
break;
case LUA_TLIGHTUSERDATA:
err = lbind_pushpointer(v, lua_touserdata(L, i));
break;
case LUA_TTABLE: // todo
default:
luaL_error(L, "unsupport type %s", lua_typename(L, t));
}
if (err) {
luaL_error(L, "push arg %d error", i);
}
}
}
static struct vars *
getvars(lua_State *L, void *p) {
struct vars *args = NULL;
lua_rawgetp(L, LUA_REGISTRYINDEX, p);
if (lua_isuserdata(L, -1)) {
struct vars ** box = (struct vars **)lua_touserdata(L, -1);
args = *box;
lua_pop(L,1);
} else {
lua_pop(L,1);
args = newvarsobject(L);
if (args == NULL)
luaL_error(L, "new result object failed");
lua_rawsetp(L, LUA_REGISTRYINDEX, p);
}
lbind_clear(args);
return args;
}
static struct vars *
resultvars(lua_State *L) {
static int result = 0;
return getvars(L, &result);
}
static struct vars *
argvars(lua_State *L, int onlyfetch) {
static int args = 0;
if (onlyfetch) {
lua_rawgetp(L, LUA_REGISTRYINDEX, &args);
if (lua_isuserdata(L, -1)) {
struct vars ** box = (struct vars **)lua_touserdata(L, -1);
lua_pop(L,1);
return *box;
} else {
lua_pop(L,1);
return NULL;
}
} else {
return getvars(L, &args);
}
}
static int
lnewargs(lua_State *L) {
struct vars * args = argvars(L, 0);
lua_pushlightuserdata(L, args);
return 1;
}
static void
errorlog(lua_State *L) {
fprintf(stderr, "Error: %s", lua_tostring(L, -1));
lua_pop(L, 1);
}
struct vars *
lbind_args(lua_State *L) {
struct vars * args = argvars(L,1); // try fetch args, never raise error
if (args)
return args;
lua_pushcfunction(L, lnewargs);
if (lua_pcall(L,0,1,0) != LUA_OK) {
errorlog(L);
return NULL;
}
args = lua_touserdata(L, -1);
lua_pop(L, 1);
return args;
}
static int
lcall(lua_State *L) {
const char * funcname = lua_touserdata(L, 1);
struct vars *args = lua_touserdata(L,2);
lua_settop(L, 0);
if (lua_getglobal(L, funcname) != LUA_TFUNCTION) {
return luaL_error(L, "%s is not a function", funcname);
}
lua_call(L, pushargs(L, args), LUA_MULTRET);
luaL_checkstack(L, LUA_MINSTACK, NULL);
args = resultvars(L);
genargs(L, args);
lua_pushlightuserdata(L, args);
return 1;
}
struct vars *
lbind_call(lua_State *L, const char * funcname, struct vars *args) {
lua_pushcfunction(L, lcall);
lua_pushlightuserdata(L, (void *)funcname);
lua_pushlightuserdata(L, args);
int ret = lua_pcall(L, 2, 1, 0);
if (ret == LUA_OK) {
struct vars * result = lua_touserdata(L,-1);
lua_pop(L, 1);
return result;
} else {
// ignore the error message, If you need log error message, use lua_tostring(L, -1)
errorlog(L);
return NULL;
}
}
static int
lfunc(lua_State *L) {
lbind_function f = (lbind_function)lua_touserdata(L, lua_upvalueindex(1));
struct vars * args = argvars(L,0);
struct vars * result = resultvars(L);
genargs(L, args);
f(args, result);
lbind_clear(args);
lua_settop(L, 0);
int n = pushargs(L, result);
lbind_clear(result);
return n;
}
static int
lregister(lua_State *L) {
const char * funcname = lua_touserdata(L, 1);
luaL_checktype(L, 2, LUA_TLIGHTUSERDATA);
if (lua_getglobal(L, "C") != LUA_TTABLE) {
lua_newtable(L);
lua_pushvalue(L, -1);
lua_setglobal(L, "C");
}
lua_pushvalue(L, 2);
lua_pushcclosure(L, lfunc, 1);
lua_setfield(L, -2, funcname);
return 0;
}
void
lbind_register(lua_State *L, const char * funcname, lbind_function f) {
lua_pushcfunction(L, lregister);
lua_pushlightuserdata(L, (void *)funcname);
lua_pushlightuserdata(L, f);
int ret = lua_pcall(L, 2, 0, 0);
if (ret != LUA_OK) {
// ignore the error message, If you need log error message, use lua_tostring(L, -1)
errorlog(L);
}
}
static int
ldofile(lua_State *L) {
const char * filename = (const char *)lua_touserdata(L, 1);
int ret = luaL_loadfile(L, filename);
if (ret != LUA_OK) {
// ignore the error message
errorlog(L);
if (ret == LUA_ERRFILE) {
lua_pushinteger(L, LF_NOTFOUND);
} else {
lua_pushinteger(L, LF_ERRPARSE);
}
return 1;
}
ret = lua_pcall(L, 0, 0, 0);
if (ret != LUA_OK) {
// ignore the error message
errorlog(L);
lua_pushinteger(L, LF_ERRRUN);
return 1;
}
lua_pushinteger(L, 0);
return 1;
}
int
lbind_dofile(lua_State *L, const char * filename) {
lua_pushcfunction(L, ldofile);
lua_pushlightuserdata(L, (void *)filename);
int ret = lua_pcall(L, 1, 1, 0);
if (ret != LUA_OK) {
// ignore the error message, If you need log error message, use lua_tostring(L, -1)
errorlog(L);
return -1;
} else {
int ret = lua_tointeger(L, -1);
lua_pop(L, 1);
return ret;
}
}