This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
ffi.c
3552 lines (2964 loc) · 99.7 KB
/
ffi.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vim: ts=4 sw=4 sts=4 et tw=78
* Portions copyright (c) 2015-present, Facebook, Inc. All rights reserved.
* Portions copyright (c) 2011 James R. McKaskill.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#include "ffi.h"
#include <math.h>
#include <inttypes.h>
/* Set to 1 to get extra debugging on print */
#define DEBUG_TOSTRING 0
int jit_key;
int ctype_mt_key;
int cdata_mt_key;
int callback_mt_key;
int cmodule_mt_key;
int constants_key;
int types_key;
int gc_key;
int callbacks_key;
int functions_key;
int abi_key;
int next_unnamed_key;
int niluv_key;
int asmname_key;
void push_upval(lua_State* L, int* key)
{
lua_pushlightuserdata(L, key);
lua_rawget(L, LUA_REGISTRYINDEX);
}
void set_upval(lua_State* L, int* key)
{
lua_pushlightuserdata(L, key);
lua_insert(L, -2);
lua_rawset(L, LUA_REGISTRYINDEX);
}
int equals_upval(lua_State* L, int idx, int* key)
{
int ret;
lua_pushvalue(L, idx);
push_upval(L, key);
ret = lua_rawequal(L, -2, -1);
lua_pop(L, 2);
return ret;
}
struct jit* get_jit(lua_State* L)
{
struct jit* jit;
push_upval(L, &jit_key);
jit = (struct jit*) lua_touserdata(L, -1);
jit->L = L;
lua_pop(L, 1); /* still in registry */
return jit;
}
static int type_error(lua_State* L, int idx, const char* to_type, int to_usr, const struct ctype* to_ct)
{
luaL_Buffer B;
struct ctype ft;
assert(to_type || (to_usr && to_ct));
if (to_usr) {
to_usr = lua_absindex(L, to_usr);
}
idx = lua_absindex(L, idx);
luaL_buffinit(L, &B);
to_cdata(L, idx, &ft);
if (ft.type != INVALID_TYPE) {
push_type_name(L, -1, &ft);
lua_pushfstring(L, "unable to convert argument %d from cdata<%s> to cdata<", idx, lua_tostring(L, -1));
lua_remove(L, -2);
luaL_addvalue(&B);
} else {
lua_pushfstring(L, "unable to convert argument %d from lua<%s> to cdata<", idx, luaL_typename(L, idx));
luaL_addvalue(&B);
}
if (to_ct) {
push_type_name(L, to_usr, to_ct);
luaL_addvalue(&B);
} else {
luaL_addstring(&B, to_type);
}
luaL_addchar(&B, '>');
luaL_pushresult(&B);
return lua_error(L);
}
static void* userdata_toptr(lua_State* L, int idx)
{
void* ptr = lua_touserdata(L, idx);
// check for FILE*
lua_getmetatable(L, idx);
luaL_getmetatable(L, LUA_FILEHANDLE);
int isfile = lua_rawequal(L, -1, -2);
lua_pop(L, 2);
if (isfile) {
#if LUA_VERSION_NUM == 501
FILE** stream = (FILE**) ptr;
return *stream;
#else
luaL_Stream* stream = (luaL_Stream*) ptr;
return stream->f;
#endif
}
return ptr;
}
static int cdata_tointeger(lua_State* L, int idx, ptrdiff_t* val)
{
struct ctype ct;
void* addr = to_cdata(L, idx, &ct);
lua_pop(L, 1);
if (ct.pointers) {
return 0;
}
switch (ct.type) {
case INT8_TYPE:
*val = *(int8_t*)addr;
return 1;
case INT16_TYPE:
*val = *(int16_t*)addr;
return 1;
case INT32_TYPE:
*val = *(int32_t*)addr;
return 1;
case INT64_TYPE:
*val = *(int64_t*)addr;
return 1;
default:
return 0;
}
}
static int64_t check_intptr(lua_State* L, int idx, void* p, struct ctype* ct)
{
if (ct->type == INVALID_TYPE) {
int64_t ret;
memset(ct, 0, sizeof(*ct));
ct->base_size = 8;
ct->type = INT64_TYPE;
ct->is_defined = 1;
ret = luaL_checknumber(L, idx);
return ret;
} else if (ct->pointers) {
return (intptr_t) p;
}
switch (ct->type) {
case INTPTR_TYPE:
case FUNCTION_PTR_TYPE:
return *(intptr_t*) p;
case INT64_TYPE:
return *(int64_t*) p;
case INT32_TYPE:
return ct->is_unsigned ? (int64_t) *(uint32_t*) p : (int64_t) *(int32_t*) p;
case INT16_TYPE:
return ct->is_unsigned ? (int64_t) *(uint16_t*) p : (int64_t) *(int16_t*) p;
case INT8_TYPE:
return ct->is_unsigned ? (int64_t) *(uint8_t*) p : (int64_t) *(int8_t*) p;
default:
type_error(L, idx, "intptr_t", 0, NULL);
return 0;
}
}
static int get_cfunction_address(lua_State* L, int idx, cfunction* addr);
#define TO_NUMBER(TYPE, ALLOW_POINTERS, LUA_TONUMBER) \
TYPE ret = 0; \
void* p; \
struct ctype ct; \
cfunction f; \
\
switch (lua_type(L, idx)) { \
case LUA_TBOOLEAN: \
ret = (TYPE) lua_toboolean(L, idx); \
break; \
\
case LUA_TNUMBER: \
ret = (TYPE) LUA_TONUMBER(L, idx); \
break; \
\
case LUA_TSTRING: \
if (!ALLOW_POINTERS) { \
type_error(L, idx, #TYPE, 0, NULL); \
} \
ret = (TYPE) (intptr_t) lua_tostring(L, idx); \
break; \
\
case LUA_TLIGHTUSERDATA: \
if (!ALLOW_POINTERS) { \
type_error(L, idx, #TYPE, 0, NULL); \
} \
ret = (TYPE) (intptr_t) lua_topointer(L, idx); \
break; \
\
case LUA_TFUNCTION: \
if (!ALLOW_POINTERS) { \
type_error(L, idx, #TYPE, 0, NULL); \
} \
if (!get_cfunction_address(L, idx, &f)) { \
type_error(L, idx, #TYPE, 0, NULL); \
} \
ret = (TYPE) (intptr_t) f; \
break; \
\
case LUA_TUSERDATA: \
p = to_cdata(L, idx, &ct); \
\
if (ct.type == INVALID_TYPE) { \
if (!ALLOW_POINTERS) { \
type_error(L, idx, #TYPE, 0, NULL); \
} \
ret = (TYPE) (intptr_t) userdata_toptr(L, idx); \
} else if (ct.pointers || ct.type == STRUCT_TYPE || ct.type == UNION_TYPE) {\
if (!ALLOW_POINTERS) { \
type_error(L, idx, #TYPE, 0, NULL); \
} \
ret = (TYPE) (intptr_t) p; \
} else if (ct.type == COMPLEX_DOUBLE_TYPE) { \
ret = (TYPE) creal(*(complex_double*) p); \
} else if (ct.type == COMPLEX_FLOAT_TYPE) { \
ret = (TYPE) crealf(*(complex_float*) p); \
} else if (ct.type == DOUBLE_TYPE) { \
ret = (TYPE) *(double*) p; \
} else if (ct.type == FLOAT_TYPE) { \
ret = (TYPE) *(float*) p; \
} else { \
ret = check_intptr(L, idx, p, &ct); \
} \
lua_pop(L, 1); \
break; \
\
case LUA_TNIL: \
ret = (TYPE) 0; \
break; \
\
default: \
type_error(L, idx, #TYPE, 0, NULL); \
} \
static int64_t cast_int64(lua_State* L, int idx, int is_cast)
{ TO_NUMBER(int64_t, is_cast, lua_tointeger); return ret; }
static uint64_t cast_uint64(lua_State* L, int idx, int is_cast)
{ TO_NUMBER(uint64_t, is_cast, lua_tointeger); return ret; }
int32_t check_int32(lua_State* L, int idx)
{ return (int32_t) cast_int64(L, idx, 0); }
uint32_t check_uint32(lua_State* L, int idx)
{ return (uint32_t) cast_uint64(L, idx, 0); }
int64_t check_int64(lua_State* L, int idx)
{ return cast_int64(L, idx, 0); }
uint64_t check_uint64(lua_State* L, int idx)
{ return cast_uint64(L, idx, 0); }
double check_double(lua_State* L, int idx)
{ TO_NUMBER(double, 0, lua_tonumber); return ret; }
float check_float(lua_State* L, int idx)
{ TO_NUMBER(double, 0, lua_tonumber); return ret; }
uintptr_t check_uintptr(lua_State* L, int idx)
{ TO_NUMBER(uintptr_t, 1, lua_tointeger); return ret; }
complex_double check_complex_double(lua_State* L, int idx)
{
double real = 0, imag = 0;
void* p;
struct ctype ct;
switch (lua_type(L, idx)) {
case LUA_TNUMBER:
real = (double) lua_tonumber(L, idx);
break;
case LUA_TTABLE:
lua_rawgeti(L, idx, 1);
real = check_double(L, -1);
lua_pop(L, 1);
lua_rawgeti(L, idx, 2);
if (lua_isnil(L, -1)) {
imag = real;
} else {
imag = check_double(L, -1);
}
lua_pop(L, 1);
break;
case LUA_TUSERDATA:
p = to_cdata(L, idx, &ct);
if (ct.type == COMPLEX_DOUBLE_TYPE) {
real = creal(*(complex_double*) p);
imag = cimag(*(complex_double*) p);
} else if (ct.type == COMPLEX_FLOAT_TYPE) {
real = crealf(*(complex_float*) p);
imag = cimagf(*(complex_float*) p);
} else if (ct.type == DOUBLE_TYPE) {
real = *(double*) p;
} else if (ct.type == FLOAT_TYPE) {
real = *(float*) p;
} else {
real = check_intptr(L, idx, p, &ct);
}
lua_pop(L, 1);
break;
default:
type_error(L, idx, "complex", 0, NULL);
}
return mk_complex_double(real, imag);
}
complex_float check_complex_float(lua_State* L, int idx)
{
complex_double d = check_complex_double(L, idx);
return mk_complex_float(creal(d), cimag(d));
}
static size_t unpack_vararg(lua_State* L, int i, char* to)
{
void* p;
struct ctype ct;
switch (lua_type(L, i)) {
case LUA_TBOOLEAN:
*(int*) to = lua_toboolean(L, i);
return sizeof(int);
case LUA_TNUMBER:
*(double*) to = lua_tonumber(L, i); // TODO in Lua 5.3: lua_tointeger sometimes should be here
return sizeof(double);
case LUA_TSTRING:
*(const char**) to = lua_tostring(L, i);
return sizeof(const char*);
case LUA_TLIGHTUSERDATA:
*(void**) to = lua_touserdata(L, i);
return sizeof(void*);
case LUA_TUSERDATA:
p = to_cdata(L, i, &ct);
lua_pop(L, 1);
if (ct.type == INVALID_TYPE) {
*(void**) to = userdata_toptr(L, i);
return sizeof(void*);
} else if (ct.pointers || ct.type == INTPTR_TYPE) {
*(void**) to = p;
return sizeof(void*);
} else if (ct.type == INT32_TYPE) {
*(int32_t*) to = *(int32_t*) p;
return sizeof(int32_t);
} else if (ct.type == INT64_TYPE) {
*(int64_t*) to = *(int64_t*) p;
return sizeof(int64_t);
}
goto err;
case LUA_TNIL:
*(void**) to = NULL;
return sizeof(void*);
default:
goto err;
}
err:
return type_error(L, i, "vararg", 0, NULL);
}
void unpack_varargs_stack(lua_State* L, int first, int last, char* to)
{
int i;
for (i = first; i <= last; i++) {
to += unpack_vararg(L, i, to);
}
}
void unpack_varargs_stack_skip(lua_State* L, int first, int last, int ints_to_skip, int floats_to_skip, char* to)
{
int i;
for (i = first; i <= last; i++) {
int type = lua_type(L, i);
if (type == LUA_TNUMBER && --floats_to_skip >= 0) {
continue;
} else if (type != LUA_TNUMBER && --ints_to_skip >= 0) {
continue;
}
to += unpack_vararg(L, i, to);
}
}
void unpack_varargs_float(lua_State* L, int first, int last, int max, char* to)
{
int i;
for (i = first; i <= last && max > 0; i++) {
if (lua_type(L, i) == LUA_TNUMBER) {
unpack_vararg(L, i, to);
to += sizeof(double);
max--;
}
}
}
void unpack_varargs_int(lua_State* L, int first, int last, int max, char* to)
{
int i;
for (i = first; i <= last && max > 0; i++) {
if (lua_type(L, i) != LUA_TNUMBER) {
unpack_vararg(L, i, to);
to += sizeof(void*);
max--;
}
}
}
void unpack_varargs_reg(lua_State* L, int first, int last, char* to)
{
int i;
for (i = first; i <= last; i++) {
unpack_vararg(L, i, to);
to += sizeof(double);
}
}
/* to_enum tries to convert a value at idx to the enum type indicated by to_ct
* and uv to_usr. For strings this means it will do a string lookup for the
* enum type. It leaves the stack unchanged. Will throw an error if the type
* at idx can't be conerted.
*/
int32_t check_enum(lua_State* L, int idx, int to_usr, const struct ctype* to_ct)
{
int32_t ret;
switch (lua_type(L, idx)) {
case LUA_TSTRING:
/* lookup string in to_usr to find value */
to_usr = lua_absindex(L, to_usr);
lua_pushvalue(L, idx);
lua_rawget(L, to_usr);
if (lua_isnil(L, -1)) {
goto err;
}
ret = (int32_t) lua_tointeger(L, -1);
lua_pop(L, 1);
return ret;
case LUA_TUSERDATA:
return check_int32(L, idx);
case LUA_TNIL:
return (int32_t) 0;
case LUA_TNUMBER:
return (int32_t) lua_tointeger(L, idx);
default:
goto err;
}
err:
return type_error(L, idx, NULL, to_usr, to_ct);
}
/* to_pointer tries converts a value at idx to a pointer. It fills out ct and
* pushes the uv of the found type. It will throw a lua error if it can not
* convert the value to a pointer. */
static void* check_pointer(lua_State* L, int idx, struct ctype* ct)
{
void* p;
memset(ct, 0, sizeof(*ct));
idx = lua_absindex(L, idx);
switch (lua_type(L, idx)) {
case LUA_TNIL:
ct->type = VOID_TYPE;
ct->pointers = 1;
ct->is_null = 1;
lua_pushnil(L);
return NULL;
case LUA_TNUMBER:
ct->type = INTPTR_TYPE;
ct->is_unsigned = 1;
ct->pointers = 0;
lua_pushnil(L);
return (void*) (uintptr_t) lua_tonumber(L, idx); // TODO in Lua 5.3: maybe change to lua_tointeger
case LUA_TLIGHTUSERDATA:
ct->type = VOID_TYPE;
ct->pointers = 1;
lua_pushnil(L);
return lua_touserdata(L, idx);
case LUA_TSTRING:
ct->type = INT8_TYPE;
ct->pointers = 1;
ct->is_unsigned = IS_CHAR_UNSIGNED;
ct->is_array = 1;
ct->base_size = 1;
ct->const_mask = 2;
lua_pushnil(L);
return (void*) lua_tolstring(L, idx, &ct->array_size);
case LUA_TUSERDATA:
p = to_cdata(L, idx, ct);
if (ct->type == INVALID_TYPE) {
/* some other type of user data */
ct->type = VOID_TYPE;
ct->pointers = 1;
return userdata_toptr(L, idx);
} else if (ct->type == STRUCT_TYPE || ct->type == UNION_TYPE) {
return p;
} else {
return (void*) (intptr_t) check_intptr(L, idx, p, ct);
}
break;
}
type_error(L, idx, "pointer", 0, NULL);
return NULL;
}
static int is_void_ptr(const struct ctype* ct)
{
return ct->type == VOID_TYPE
&& ct->pointers == 1;
}
static int is_same_type(lua_State* L, int usr1, int usr2, const struct ctype* t1, const struct ctype* t2)
{
if (t1->type != t2->type) {
return 0;
}
#if LUA_VERSION_NUM == 501
if (lua_isnil(L, usr1) != lua_isnil(L, usr2)) {
int ret;
usr1 = lua_absindex(L, usr1);
usr2 = lua_absindex(L, usr2);
push_upval(L, &niluv_key);
ret = lua_rawequal(L, usr1, -1)
|| lua_rawequal(L, usr2, -1);
lua_pop(L, 1);
if (ret) {
return 1;
}
}
#endif
return lua_rawequal(L, usr1, usr2);
}
static void set_struct(lua_State* L, int idx, void* to, int to_usr, const struct ctype* tt, int check_pointers);
/* to_typed_pointer converts a value at idx to a type tt with target uv to_usr
* checking all types. May push a temporary value so that it can create
* structs on the fly. */
void* check_typed_pointer(lua_State* L, int idx, int to_usr, const struct ctype* tt)
{
struct ctype ft;
void* p;
to_usr = lua_absindex(L, to_usr);
idx = lua_absindex(L, idx);
if (tt->pointers == 1 && (tt->type == STRUCT_TYPE || tt->type == UNION_TYPE) && lua_type(L, idx) == LUA_TTABLE) {
/* need to construct a struct of the target type */
struct ctype ct = *tt;
ct.pointers = ct.is_array = 0;
p = push_cdata(L, to_usr, &ct);
set_struct(L, idx, p, to_usr, &ct, 1);
return p;
}
p = check_pointer(L, idx, &ft);
if (tt->pointers == 1 && ft.pointers == 0 && (ft.type == STRUCT_TYPE || ft.type == UNION_TYPE)) {
/* auto dereference structs */
ft.pointers = 1;
ft.const_mask <<= 1;
}
if (is_void_ptr(tt)) {
/* any pointer can convert to void* */
goto suc;
} else if (is_void_ptr(&ft) && (ft.pointers || ft.is_reference)) {
/* void* can convert to any pointer */
goto suc;
} else if (ft.is_null) {
/* NULL can convert to any pointer */
goto suc;
} else if (!is_same_type(L, to_usr, -1, tt, &ft)) {
/* the base type is different */
goto err;
} else if (tt->pointers != ft.pointers) {
goto err;
} else if (ft.const_mask & ~tt->const_mask) {
/* for every const in from it must be in to, there are further rules
* for const casting (see the c++ spec), but they are hard to test
* quickly */
goto err;
}
suc:
return p;
err:
type_error(L, idx, NULL, to_usr, tt);
return NULL;
}
/**
* gets the address of the wrapped C function for the lua function value at idx
* and returns 1 if it exists; otherwise returns 0 and nothing is pushed */
static int get_cfunction_address(lua_State* L, int idx, cfunction* addr)
{
if (!lua_isfunction(L, idx)) return 0;
int top = lua_gettop(L);
// Get the last upvalue
int n = 2;
while (lua_getupvalue(L, idx, n)) {
lua_pop(L, 1);
n++;
}
if (!lua_getupvalue(L, idx, n - 1))
return 0;
if (!lua_isuserdata(L, -1) || !lua_getmetatable(L, -1)) {
lua_pop(L, 1);
return 0;
}
push_upval(L, &callback_mt_key);
if (!lua_rawequal(L, -1, -2)) {
lua_pop(L, 3);
return 0;
}
/* stack is:
* userdata upval
* metatable
* callback_mt
*/
cfunction* f = lua_touserdata(L, -3);
*addr = f[1];
lua_pop(L, 3);
return 1;
}
/* to_cfunction converts a value at idx with usr table at to_usr and type tt
* into a function. Leaves the stack unchanged. */
static cfunction check_cfunction(lua_State* L, int idx, int to_usr, const struct ctype* tt, int check_pointers)
{
void* p;
struct ctype ft;
cfunction f;
int top = lua_gettop(L);
idx = lua_absindex(L, idx);
to_usr = lua_absindex(L, to_usr);
switch (lua_type(L, idx)) {
case LUA_TFUNCTION:
if (get_cfunction_address(L, idx, &f)) {
return f;
}
/* Function cdatas are pinned and must be manually cleaned up by
* calling func:free(). */
push_upval(L, &callbacks_key);
f = compile_callback(L, idx, to_usr, tt);
lua_pushboolean(L, 1);
lua_rawset(L, -3);
lua_pop(L, 1); /* callbacks tbl */
return f;
case LUA_TNIL:
return NULL;
case LUA_TLIGHTUSERDATA:
if (check_pointers) {
goto err;
} else {
return (cfunction) lua_touserdata(L, idx);
}
case LUA_TUSERDATA:
p = to_cdata(L, idx, &ft);
assert(lua_gettop(L) == top + 1);
if (ft.type == INVALID_TYPE) {
if (check_pointers) {
goto err;
} else {
lua_pop(L, 1);
return (cfunction) lua_touserdata(L, idx);
}
} else if (ft.is_null) {
lua_pop(L, 1);
return NULL;
} else if (!check_pointers && (ft.pointers || ft.type == INTPTR_TYPE)) {
lua_pop(L, 1);
return (cfunction) *(void**) p;
} else if (ft.type != FUNCTION_PTR_TYPE) {
goto err;
} else if (!check_pointers) {
lua_pop(L, 1);
return *(cfunction*) p;
} else if (ft.calling_convention != tt->calling_convention) {
goto err;
} else if (!is_same_type(L, -1, to_usr, &ft, tt)) {
goto err;
} else {
lua_pop(L, 1);
return *(cfunction*) p;
}
default:
goto err;
}
err:
type_error(L, idx, NULL, to_usr, tt);
return NULL;
}
/* to_type_cfunction converts a value at idx with uv at to_usr and type tt to
* a cfunction. Leaves the stack unchanged. */
cfunction check_typed_cfunction(lua_State* L, int idx, int to_usr, const struct ctype* tt)
{ return check_cfunction(L, idx, to_usr, tt, 1); }
static void set_value(lua_State* L, int idx, void* to, int to_usr, const struct ctype* tt, int check_pointers);
static void set_array(lua_State* L, int idx, void* to, int to_usr, const struct ctype* tt, int check_pointers)
{
size_t i, sz, esz;
struct ctype et;
idx = lua_absindex(L, idx);
to_usr = lua_absindex(L, to_usr);
switch (lua_type(L, idx)) {
case LUA_TSTRING:
if (tt->pointers == 1 && tt->type == INT8_TYPE) {
const char* str = lua_tolstring(L, idx, &sz);
if (!tt->is_variable_array && sz >= tt->array_size) {
memcpy(to, str, tt->array_size);
} else {
/* include nul terminator */
memcpy(to, str, sz+1);
}
} else {
goto err;
}
break;
case LUA_TTABLE:
et = *tt;
et.pointers--;
et.const_mask >>= 1;
et.is_array = 0;
esz = et.pointers ? sizeof(void*) : et.base_size;
lua_rawgeti(L, idx, 2);
if (tt->is_variable_array) {
/* we have no idea how big the array is, so set values based off
* how many items were given to us */
lua_pop(L, 1);
for (i = 0; i < lua_rawlen(L, idx); i++) {
lua_rawgeti(L, idx, (int) i + 1);
set_value(L, -1, (char*) to + esz * i, to_usr, &et, check_pointers);
lua_pop(L, 1);
}
} else if (lua_isnil(L, -1)) {
/* there is no second element, so we set the whole array to the
* first element (or nil - ie 0) if there is no first element) */
lua_pop(L, 1);
lua_rawgeti(L, idx, 1);
if (lua_isnil(L, -1)) {
memset(to, 0, ctype_size(L, tt));
} else {
/* if its still variable we have no idea how many values to set */
for (i = 0; i < tt->array_size; i++) {
set_value(L, -1, (char*) to + esz * i, to_usr, &et, check_pointers);
}
}
lua_pop(L, 1);
} else {
/* there is a second element, so we set each element using the
* equiv index in the table initializer */
lua_pop(L, 1);
for (i = 0; i < tt->array_size; i++) {
lua_rawgeti(L, idx, (int) (i+1));
if (lua_isnil(L, -1)) {
/* we've hit the end of the values provided in the
* initializer, so memset the rest to zero */
lua_pop(L, 1);
memset((char*) to + esz * i, 0, (tt->array_size - i) * esz);
break;
} else {
set_value(L, -1, (char*) to + esz * i, to_usr, &et, check_pointers);
lua_pop(L, 1);
}
}
}
break;
default:
goto err;
}
return;
err:
type_error(L, idx, NULL, to_usr, tt);
}
/* pops the member key from the stack, leaves the member user value on the
* stack. Returns the member offset. Returns -ve if the member can not be
* found. */
static ptrdiff_t get_member(lua_State* L, int usr, const struct ctype* ct, struct ctype* mt)
{
ptrdiff_t off;
lua_rawget(L, usr);
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
return -1;
}
*mt = *(const struct ctype*) lua_touserdata(L, -1);
lua_getuservalue(L, -1);
lua_replace(L, -2);
if (mt->is_variable_array && ct->variable_size_known) {
/* eg char mbr[?] */
size_t sz = (mt->pointers > 1) ? sizeof(void*) : mt->base_size;
assert(ct->is_variable_struct && mt->is_array);
mt->array_size = ct->variable_increment / sz;
mt->is_variable_array = 0;
} else if (mt->is_variable_struct && ct->variable_size_known) {
/* eg struct {char a; char b[?]} mbr; */
assert(ct->is_variable_struct);
mt->variable_size_known = 1;
mt->variable_increment = ct->variable_increment;
}
off = mt->offset;
mt->offset = 0;
return off;
}
static void set_struct(lua_State* L, int idx, void* to, int to_usr, const struct ctype* tt, int check_pointers)
{
int have_first = 0;
int have_other = 0;
struct ctype mt;
void* p;
to_usr = lua_absindex(L, to_usr);
idx = lua_absindex(L, idx);
switch (lua_type(L, idx)) {
case LUA_TTABLE:
/* match up to the members based off the table initializers key - this
* will match both numbered and named members in the user table
* we need a special case for when no entries in the initializer -
* zero initialize the c struct, and only one entry in the initializer
* - set all members to this value */
memset(to, 0, ctype_size(L, tt));
lua_pushnil(L);
while (lua_next(L, idx)) {
ptrdiff_t off;
if (!have_first && lua_tonumber(L, -2) == 1 && lua_tonumber(L, -1) != 0) {
have_first = 1;
} else if (!have_other && (lua_type(L, -2) != LUA_TNUMBER || lua_tonumber(L, -2) != 1)) {
have_other = 1;
}
lua_pushvalue(L, -2);
off = get_member(L, to_usr, tt, &mt);
assert(off >= 0);
set_value(L, -2, (char*) to + off, -1, &mt, check_pointers);
/* initializer value, mt usr */
lua_pop(L, 2);
}
/* if we only had a single non zero value then initialize all members to that value */
if (!have_other && have_first && tt->type != UNION_TYPE) {
size_t i, sz;
ptrdiff_t off;
lua_rawgeti(L, idx, 1);
sz = lua_rawlen(L, to_usr);
for (i = 2; i < sz; i++) {
lua_pushinteger(L, i);
off = get_member(L, to_usr, tt, &mt);
assert(off >= 0);
set_value(L, -2, (char*) to + off, -1, &mt, check_pointers);
lua_pop(L, 1); /* mt usr */
}
lua_pop(L, 1); /* initializer table */
}
break;
case LUA_TUSERDATA:
if (check_pointers) {
p = check_typed_pointer(L, idx, to_usr, tt);
} else {
struct ctype ct;
p = check_pointer(L, idx, &ct);
}
memcpy(to, p, tt->base_size);
lua_pop(L, 1);
break;
default:
goto err;
}
return;
err:
type_error(L, idx, NULL, to_usr, tt);
}