-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_luatypes.c
457 lines (388 loc) · 13.6 KB
/
go_luatypes.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
#include "go_luajit.h"
void free_table_entry(lua_State *L, lua_table_entry *entry, _Bool deletePermanent) {
if (entry == NULL)
return;
free_lua_value_impl(L, entry->key, deletePermanent);
entry->key = NULL;
free_lua_value_impl(L, entry->value, deletePermanent);
entry->value = NULL;
return_lua_table_entry(L, entry);
}
void free_unrolled_table(lua_State *L, lua_unrolled_table *table, _Bool deletePermanent) {
if (table == NULL)
return;
table->last = NULL;
lua_table_entry *next = table->first;
while (next != NULL) {
table->first = next->next;
free_table_entry(L, next, deletePermanent);
next = table->first;
}
table->first = NULL;
return_lua_unrolled_table(L, table);
}
void free_temporary_lua_value(lua_State *L, lua_value *value) {
free_lua_value_impl(L, value, 0);
}
void free_temporary_lua_value_array(lua_State *L, lua_value *value_array[], int count) {
for (int i = 0; i < count; i++) {
lua_value *value = value_array[i];
if (value != NULL) {
free_temporary_lua_value(L, value);
}
}
}
void free_lua_value(lua_State *L, lua_value *value) {
free_lua_value_impl(L, value, 1);
}
void free_lua_value_impl(lua_State *L, lua_value *value, _Bool deletePermanent) {
if (value == NULL)
return;
if (!value->temporary && !deletePermanent)
return;
switch(value->valueType) {
case LUA_TSTRING:
chfree(value->data.pointerVal);
break;
case LUA_TFUNCTION:
if (value->dataArg.isCFunction)
break;
//Intentionally falling through- lua functions need to be stored as refs
case LUA_TUSERDATA:
case LUA_TTHREAD:
case LUA_TLIGHTUSERDATA:
case LUA_TTABLE:
luaL_unref(L, LUA_REGISTRYINDEX, value->data.luaRefVal);
break;
case LUA_TUNROLLEDTABLE:
free_unrolled_table(L, (lua_unrolled_table*)value->data.pointerVal, deletePermanent);
break;
default:
break;
}
return_lua_value(L, value);
}
void free_temporary_lua_return(lua_State *_L, lua_return retVal, _Bool freeValues) {
free_lua_return_impl(_L, retVal, freeValues, 0);
}
void free_lua_return(lua_State *_L, lua_return retVal, _Bool freeValues) {
free_lua_return_impl(_L, retVal, freeValues, 1);
}
void free_lua_return_impl(lua_State *_L, lua_return retVal, _Bool freeValues, _Bool deletePermanent) {
if (retVal.err != NULL)
free_lua_error(retVal.err);
if (freeValues) {
for (int i = 0; i < retVal.valueCount; i++) {
free_lua_value_impl(_L, retVal.values[i], deletePermanent);
}
}
if (retVal.valueCount > 0) {
chfree(retVal.values);
}
}
void free_lua_args(lua_State *_L, lua_args args, _Bool freeValues) {
free_lua_args_impl(_L, args, freeValues, 1);
}
void free_temporary_lua_args(lua_State *_L, lua_args args, _Bool freeValues) {
free_lua_args_impl(_L, args, freeValues, 0);
}
void free_lua_args_impl(lua_State *_L, lua_args args, _Bool freeValues, _Bool deletePermanent) {
if (freeValues) {
for (int i = 0; i < args.valueCount; i++) {
free_lua_value_impl(_L, args.values[i], deletePermanent);
}
}
chfree(args.values);
}
lua_result unroll_table(lua_State *_L, lua_value *table) {
int luaRefVal = table->data.luaRefVal;
lua_result retVal;
lua_unrolled_table *unrolled = make_lua_unrolled_table(_L);
retVal.err = NULL;
retVal.value = make_lua_value(_L);
retVal.value->valueType = LUA_TUNROLLEDTABLE;
retVal.value->data.pointerVal = NULL;
retVal.value->temporary = 0;
//Push rolled table to top of stack
lua_rawgeti(_L, LUA_REGISTRYINDEX, luaRefVal);
unrolled->first = NULL;
unrolled->last = NULL;
unrolled->arraySize = 0;
unrolled->hashSize = 0;
lua_pushnil(_L); // First key to start iteration
while (lua_next(_L, -2)) {
lua_result key;
lua_result value = convert_stack_value(_L);
if (value.err == NULL && value.value != NULL && value.value->valueType == LUA_TTABLE) {
lua_result nextValue = unroll_table(_L, value.value);
free_lua_value(_L, value.value);
value = nextValue;
}
if (value.err == NULL) {
key = convert_stack_value_impl(_L, 1);
if (key.err == NULL && key.value != NULL && key.value->valueType == LUA_TTABLE) {
lua_result nextKey = unroll_table(_L, key.value);
free_lua_value(_L, key.value);
key = nextKey;
}
}
if (value.err != NULL || key.err != NULL) {
//Move error over to output
if (value.err != NULL) {
retVal.err = value.err;
} else {
retVal.err = key.err;
}
//Free allocated values
if (key.value != NULL) free_lua_value(_L, key.value);
if (value.value != NULL) free_lua_value(_L, value.value);
//Free the unrolled table in progress
free_unrolled_table(_L, unrolled, 1);
retVal.value = NULL;
//Pop table from stack
lua_pop(_L, 1);
return retVal;
}
lua_table_entry *entry = make_lua_table_entry(_L);
//We should increment the array count or hash count
if (key.value->valueType == LUA_TNUMBER) {
float number = key.value->data.numberVal;
if (number > 0) {
//We can be off by a little if we need to, so don't bohter figuring out
//if number is an integer
unrolled->arraySize = unrolled->arraySize+1;
} else {
unrolled->hashSize = unrolled->hashSize+1;
}
} else {
unrolled->hashSize = unrolled->hashSize+1;
}
entry->key = key.value;
entry->value = value.value;
entry->next = NULL;
if (unrolled->first == NULL) {
unrolled->first = entry;
unrolled->last = entry;
} else {
unrolled->last->next = entry;
unrolled->last = entry;
}
}
retVal.value->data.pointerVal = (void*)unrolled;
//remove table to cleanup
lua_pop(_L, 1);
return retVal;
}
_Bool isUData(lua_State *_L, const char *name) {
luaL_getmetatable(_L, name);
int equal = lua_rawequal(_L, -1, -2);
lua_pop(_L, 1);
return (_Bool)equal;
}
lua_result convert_stack_value(lua_State *L) {
return convert_stack_value_impl(L, 0);
}
lua_result convert_stack_value_impl(lua_State *L, _Bool suppressPop) {
int type = lua_type(L, -1);
lua_result retVal = {};
retVal.err = NULL;
retVal.value = NULL;
if (type == LUA_TNIL) {
lua_pop(L, 1);
return retVal;
}
retVal.value = make_lua_value(L);
retVal.value->valueType = type;
retVal.value->dataArg.isCFunction = 0;
retVal.value->data.pointerVal = 0;
retVal.value->temporary = 0;
retVal.err = NULL;
_Bool needsPop = !suppressPop;
switch(type) {
case LUA_TNUMBER:
retVal.value->data.numberVal = (double)lua_tonumber(L, -1);
break;
case LUA_TBOOLEAN:
retVal.value->data.booleanVal = (_Bool)lua_toboolean(L, -1);
break;
case LUA_TSTRING:
{
const char *luaStr = lua_tolstring(L, -1, &(retVal.value->dataArg.stringLen));
char *outStr = chmalloc(sizeof(char)*(retVal.value->dataArg.stringLen+1));
strncpy(outStr, luaStr, retVal.value->dataArg.stringLen+1);
retVal.value->data.pointerVal = (void*)outStr;
break;
}
case LUA_TFUNCTION:
{
if (lua_iscfunction(L, -1)) {
retVal.value->dataArg.isCFunction = 1;
retVal.value->data.pointerVal = (void*)lua_tocfunction(L, -1);
break;
}
//Intentionally falling through- lua functions need to be stored as refs
}
case LUA_TUSERDATA:
{
if (type == LUA_TUSERDATA) {
//For UData's we should try to provide the type to give golang an easier time
retVal.value->dataArg.userDataType = 0;
int gotMeta = lua_getmetatable(L, -1);
if (gotMeta) {
if (isUData(L, MT_GOCALLBACK))
retVal.value->dataArg.userDataType = META_GOCALLBACK;
lua_pop(L, 1);
}
}
//Intentional fallthrough
}
case LUA_TTHREAD:
case LUA_TLIGHTUSERDATA:
case LUA_TTABLE:
retVal.value->data.luaRefVal = luaL_ref(L, LUA_REGISTRYINDEX);
needsPop = 0;
break;
default:
retVal.err = create_lua_error("CANNOT POP FROM STACK - INVALID STACK VALUE");
needsPop = 0;
break;
}
if (needsPop)
lua_pop(L, 1);
return retVal;
}
lua_return pop_lua_values(lua_State *_L, int valueCount) {
lua_return retVal = {};
retVal.valueCount = valueCount;
retVal.err = NULL;
retVal.values = chmalloc(valueCount * sizeof(lua_value*));
for (int i = 0; i < valueCount; i++) {
lua_result result = convert_stack_value(_L);
if (result.err != NULL) {
//Just return error- free all allocations made until this point
retVal.err = result.err;
for (int j = 0; j < i; j++) {
free_lua_value(_L, retVal.values[valueCount-j-1]);
}
chfree(retVal.values);
retVal.values = NULL;
retVal.valueCount = 0;
return retVal;
}
retVal.values[valueCount-i-1] = result.value;
}
return retVal;
}
lua_err *push_unrolled_table(lua_State *_L, lua_unrolled_table *table) {
lua_createtable(_L, table->arraySize, table->hashSize);
lua_table_entry *next = table->first;
while (next != NULL) {
lua_err *err = push_lua_value(_L, next->key);
if (err != NULL) return err;
err = push_lua_value(_L, next->value);
if (err != NULL) return err;
lua_rawset(_L, -3);
next = next->next;
}
return NULL;
}
lua_err *push_lua_value(lua_State *_L, lua_value *value) {
if (value == NULL) {
lua_pushnil(_L);
return NULL;
}
switch(value->valueType) {
case LUA_TUNLOADEDCALLBACK:
{
//This came from golang, it's a cgo handle for a go function
void **userData = (void**)lua_newuserdata(_L, sizeof(void*));
*userData = value->data.pointerVal;
luaL_getmetatable(_L, MT_GOCALLBACK);
lua_setmetatable(_L, -2);
break;
}
case LUA_TNUMBER:
lua_pushnumber(_L, (lua_Number)value->data.numberVal);
break;
case LUA_TBOOLEAN:
lua_pushboolean(_L, (int)value->data.booleanVal);
break;
case LUA_TSTRING:
lua_pushlstring(_L, (const char*)value->data.pointerVal, value->dataArg.stringLen);
break;
case LUA_TFUNCTION:
{
if (value->dataArg.isCFunction) {
lua_pushcfunction(_L, (lua_CFunction)value->data.pointerVal);
break;
}
//Intentionally falling through- lua functions need to be stored as refs
}
case LUA_TUSERDATA:
case LUA_TTHREAD:
case LUA_TLIGHTUSERDATA:
case LUA_TTABLE:
lua_rawgeti(_L, LUA_REGISTRYINDEX, value->data.luaRefVal);
break;
case LUA_TUNROLLEDTABLE:
return push_unrolled_table(_L, (lua_unrolled_table*)value->data.pointerVal);
default:
return create_lua_error("CANNOT PUSH TO STACK - INVALID VALUE");
}
return NULL;
}
lua_err *push_lua_args(lua_State *_L, lua_args args) {
int alreadyPushed = 0;
for (int i = 0; i < args.valueCount; i++) {
lua_err *err = push_lua_value(_L, args.values[args.valueCount-i-1]);
if (err != NULL) {
if (alreadyPushed > 0)
lua_pop(_L, alreadyPushed);
return err;
}
alreadyPushed++;
}
return NULL;
}
lua_err *push_lua_return(lua_State *_L, lua_return retVal) {
int alreadyPushed = 0;
for (int i = 0; i < retVal.valueCount; i++) {
lua_err *err = push_lua_value(_L, retVal.values[i]);
if (err != NULL) {
if (alreadyPushed > 0)
lua_pop(_L, alreadyPushed);
return err;
}
alreadyPushed++;
}
return NULL;
}
lua_unrolled_table *build_unrolled_table(lua_State *L, int entries) {
lua_unrolled_table *table = make_lua_unrolled_table(L);
table->first = NULL;
table->last = NULL;
table->arraySize = 0;
table->hashSize = 0;
lua_table_entry *entry = NULL;
for (int i = 0; i < entries; i++) {
entry = make_lua_table_entry(L);
entry->key = make_lua_value(L);
entry->value = make_lua_value(L);
entry->next = NULL;
if (table->first == NULL) {
table->first = entry;
table->last = entry;
} else {
table->last->next = entry;
table->last = entry;
}
}
return table;
}
lua_value **build_values(lua_State *_L, int slots, int allocs) {
lua_value** valueList = chmalloc(sizeof(lua_value*)*slots);
for (int i = 0; i < allocs; i++) {
valueList[i] = make_lua_value(_L);
}
return valueList;
}