w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

ffi.c
Go to the documentation of this file.
1 /* vim: ts=4 sw=4 sts=4 et tw=78
2  * Portions copyright (c) 2015-present, Facebook, Inc. All rights reserved.
3  * Portions copyright (c) 2011 James R. McKaskill.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  */
9 #include "ffi.h"
10 #include <math.h>
11 #include <inttypes.h>
12 
13 /* Set to 1 to get extra debugging on print */
14 #define DEBUG_TOSTRING 0
15 
16 int jit_key;
23 int gc_key;
26 int abi_key;
30 
31 void push_upval(lua_State* L, int* key)
32 {
35 }
36 
37 void set_upval(lua_State* L, int* key)
38 {
40  lua_insert(L, -2);
42 }
43 
44 int equals_upval(lua_State* L, int idx, int* key)
45 {
46  int ret;
48  push_upval(L, key);
49  ret = lua_rawequal(L, -2, -1);
50  lua_pop(L, 2);
51  return ret;
52 }
53 
55 {
56  struct jit* jit;
57  push_upval(L, &jit_key);
58  jit = (struct jit*) lua_touserdata(L, -1);
59  jit->L = L;
60  lua_pop(L, 1); /* still in registry */
61  return jit;
62 }
63 
64 static int type_error(lua_State* L, int idx, const char* to_type, int to_usr, const struct ctype* to_ct)
65 {
66  luaL_Buffer B;
67  struct ctype ft;
68 
69  assert(to_type || (to_usr && to_ct));
70  if (to_usr) {
71  to_usr = lua_absindex(L, to_usr);
72  }
73 
74  idx = lua_absindex(L, idx);
75 
76  luaL_buffinit(L, &B);
77  to_cdata(L, idx, &ft);
78 
79  if (ft.type != INVALID_TYPE) {
80  push_type_name(L, -1, &ft);
81  lua_pushfstring(L, "unable to convert argument %d from cdata<%s> to cdata<", idx, lua_tostring(L, -1));
82  lua_remove(L, -2);
83  luaL_addvalue(&B);
84  } else {
85  lua_pushfstring(L, "unable to convert argument %d from lua<%s> to cdata<", idx, luaL_typename(L, idx));
86  luaL_addvalue(&B);
87  }
88 
89  if (to_ct) {
90  push_type_name(L, to_usr, to_ct);
91  luaL_addvalue(&B);
92  } else {
93  luaL_addstring(&B, to_type);
94  }
95 
96  luaL_addchar(&B, '>');
97 
99  return lua_error(L);
100 }
101 
102 static void* userdata_toptr(lua_State* L, int idx)
103 {
104  void* ptr = lua_touserdata(L, idx);
105  int isfile;
106 
107  // check for FILE*
110  isfile = lua_rawequal(L, -1, -2);
111  lua_pop(L, 2);
112 
113  if (isfile) {
114 #if LUA_VERSION_NUM == 501
115  FILE** stream = (FILE**) ptr;
116  return *stream;
117 #else
119  return stream->f;
120 #endif
121  }
122 
123  return ptr;
124 }
125 
127 {
128  struct ctype ct;
129  void* addr = to_cdata(L, idx, &ct);
130  lua_pop(L, 1);
131 
132  if (ct.pointers) {
133  return 0;
134  }
135 
136  switch (ct.type) {
137  case INT8_TYPE:
138  *val = *(int8_t*)addr;
139  return 1;
140  case INT16_TYPE:
141  *val = *(int16_t*)addr;
142  return 1;
143  case INT32_TYPE:
144  *val = *(int32_t*)addr;
145  return 1;
146  case INT64_TYPE:
147  *val = *(int64_t*)addr;
148  return 1;
149  default:
150  return 0;
151  }
152 }
153 
154 static int64_t check_intptr(lua_State* L, int idx, void* p, struct ctype* ct)
155 {
156  if (ct->type == INVALID_TYPE) {
157  int64_t ret;
158  memset(ct, 0, sizeof(*ct));
159  ct->base_size = 8;
160  ct->type = INT64_TYPE;
161  ct->is_defined = 1;
163  return ret;
164 
165  } else if (ct->pointers) {
166  return (intptr_t) p;
167  }
168 
169  switch (ct->type) {
170  case INTPTR_TYPE:
171  case FUNCTION_PTR_TYPE:
172  return *(intptr_t*) p;
173 
174  case INT64_TYPE:
175  return *(int64_t*) p;
176 
177  case INT32_TYPE:
178  return ct->is_unsigned ? (int64_t) *(uint32_t*) p : (int64_t) *(int32_t*) p;
179 
180  case INT16_TYPE:
181  return ct->is_unsigned ? (int64_t) *(uint16_t*) p : (int64_t) *(int16_t*) p;
182 
183  case INT8_TYPE:
184  return ct->is_unsigned ? (int64_t) *(uint8_t*) p : (int64_t) *(int8_t*) p;
185 
186  default:
187  type_error(L, idx, "intptr_t", 0, NULL);
188  return 0;
189  }
190 }
191 
192 static int get_cfunction_address(lua_State* L, int idx, cfunction* addr);
193 
194 #define TO_NUMBER(TYPE, ALLOW_POINTERS, LUA_TONUMBER) \
195  TYPE ret = 0; \
196  void* p; \
197  struct ctype ct; \
198  cfunction f; \
199  \
200  switch (lua_type(L, idx)) { \
201  case LUA_TBOOLEAN: \
202  ret = (TYPE) lua_toboolean(L, idx); \
203  break; \
204  \
205  case LUA_TNUMBER: \
206  ret = (TYPE) LUA_TONUMBER(L, idx); \
207  break; \
208  \
209  case LUA_TSTRING: \
210  if (!ALLOW_POINTERS) { \
211  type_error(L, idx, #TYPE, 0, NULL); \
212  } \
213  ret = (TYPE) (intptr_t) lua_tostring(L, idx); \
214  break; \
215  \
216  case LUA_TLIGHTUSERDATA: \
217  if (!ALLOW_POINTERS) { \
218  type_error(L, idx, #TYPE, 0, NULL); \
219  } \
220  ret = (TYPE) (intptr_t) lua_topointer(L, idx); \
221  break; \
222  \
223  case LUA_TFUNCTION: \
224  if (!ALLOW_POINTERS) { \
225  type_error(L, idx, #TYPE, 0, NULL); \
226  } \
227  if (!get_cfunction_address(L, idx, &f)) { \
228  type_error(L, idx, #TYPE, 0, NULL); \
229  } \
230  ret = (TYPE) (intptr_t) f; \
231  break; \
232  \
233  case LUA_TUSERDATA: \
234  p = to_cdata(L, idx, &ct); \
235  \
236  if (ct.type == INVALID_TYPE) { \
237  if (!ALLOW_POINTERS) { \
238  type_error(L, idx, #TYPE, 0, NULL); \
239  } \
240  ret = (TYPE) (intptr_t) userdata_toptr(L, idx); \
241  } else if (ct.pointers || ct.type == STRUCT_TYPE || ct.type == UNION_TYPE) {\
242  if (!ALLOW_POINTERS) { \
243  type_error(L, idx, #TYPE, 0, NULL); \
244  } \
245  ret = (TYPE) (intptr_t) p; \
246  } else if (ct.type == COMPLEX_DOUBLE_TYPE) { \
247  ret = (TYPE) creal(*(complex_double*) p); \
248  } else if (ct.type == COMPLEX_FLOAT_TYPE) { \
249  ret = (TYPE) crealf(*(complex_float*) p); \
250  } else if (ct.type == DOUBLE_TYPE) { \
251  ret = (TYPE) *(double*) p; \
252  } else if (ct.type == FLOAT_TYPE) { \
253  ret = (TYPE) *(float*) p; \
254  } else { \
255  ret = check_intptr(L, idx, p, &ct); \
256  } \
257  lua_pop(L, 1); \
258  break; \
259  \
260  case LUA_TNIL: \
261  ret = (TYPE) 0; \
262  break; \
263  \
264  default: \
265  type_error(L, idx, #TYPE, 0, NULL); \
266  } \
267 
268 static int64_t cast_int64(lua_State* L, int idx, int is_cast)
269 { TO_NUMBER(int64_t, is_cast, lua_tointeger); return ret; }
270 
271 static uint64_t cast_uint64(lua_State* L, int idx, int is_cast)
272 { TO_NUMBER(uint64_t, is_cast, lua_tointeger); return ret; }
273 
275 { return (int32_t) cast_int64(L, idx, 0); }
276 
278 { return (uint32_t) cast_uint64(L, idx, 0); }
279 
281 { return cast_int64(L, idx, 0); }
282 
284 { return cast_uint64(L, idx, 0); }
285 
287 { TO_NUMBER(double, 0, lua_tonumber); return ret; }
288 
290 { TO_NUMBER(double, 0, lua_tonumber); return ret; }
291 
293 { TO_NUMBER(uintptr_t, 1, lua_tointeger); return ret; }
294 
296 {
297  double real = 0, imag = 0;
298  void* p;
299  struct ctype ct;
300 
301  switch (lua_type(L, idx)) {
302  case LUA_TNUMBER:
303  real = (double) lua_tonumber(L, idx);
304  break;
305  case LUA_TTABLE:
306  lua_rawgeti(L, idx, 1);
307  real = check_double(L, -1);
308  lua_pop(L, 1);
309 
310  lua_rawgeti(L, idx, 2);
311  if (lua_isnil(L, -1)) {
312  imag = real;
313  } else {
314  imag = check_double(L, -1);
315  }
316  lua_pop(L, 1);
317  break;
318  case LUA_TUSERDATA:
319  p = to_cdata(L, idx, &ct);
320  if (ct.type == COMPLEX_DOUBLE_TYPE) {
321  real = creal(*(complex_double*) p);
322  imag = cimag(*(complex_double*) p);
323  } else if (ct.type == COMPLEX_FLOAT_TYPE) {
324  real = crealf(*(complex_float*) p);
325  imag = cimagf(*(complex_float*) p);
326  } else if (ct.type == DOUBLE_TYPE) {
327  real = *(double*) p;
328  } else if (ct.type == FLOAT_TYPE) {
329  real = *(float*) p;
330  } else {
331  real = check_intptr(L, idx, p, &ct);
332  }
333  lua_pop(L, 1);
334  break;
335 
336  default:
337  type_error(L, idx, "complex", 0, NULL);
338  }
339 
340  return mk_complex_double(real, imag);
341 }
342 
344 {
346  return mk_complex_float(creal(d), cimag(d));
347 }
348 
349 static size_t unpack_vararg(lua_State* L, int i, char* to)
350 {
351  void* p;
352  struct ctype ct;
353 
354  switch (lua_type(L, i)) {
355  case LUA_TBOOLEAN:
356  *(int*) to = lua_toboolean(L, i);
357  return sizeof(int);
358 
359  case LUA_TNUMBER:
360  *(double*) to = lua_tonumber(L, i); // TODO in Lua 5.3: lua_tointeger sometimes should be here
361  return sizeof(double);
362 
363  case LUA_TSTRING:
364  *(const char**) to = lua_tostring(L, i);
365  return sizeof(const char*);
366 
367  case LUA_TLIGHTUSERDATA:
368  *(void**) to = lua_touserdata(L, i);
369  return sizeof(void*);
370 
371  case LUA_TUSERDATA:
372  p = to_cdata(L, i, &ct);
373  lua_pop(L, 1);
374 
375  if (ct.type == INVALID_TYPE) {
376  *(void**) to = userdata_toptr(L, i);
377  return sizeof(void*);
378 
379  } else if (ct.pointers || ct.type == INTPTR_TYPE) {
380  *(void**) to = p;
381  return sizeof(void*);
382 
383  } else if (ct.type == INT32_TYPE) {
384  *(int32_t*) to = *(int32_t*) p;
385  return sizeof(int32_t);
386 
387  } else if (ct.type == INT64_TYPE) {
388  *(int64_t*) to = *(int64_t*) p;
389  return sizeof(int64_t);
390 
391  } else if (ct.type == FUNCTION_PTR_TYPE) {
392  *(cfunction *) to = *(cfunction *)p;
393  return sizeof(cfunction);
394  }
395 
396  goto err;
397 
398  case LUA_TNIL:
399  *(void**) to = NULL;
400  return sizeof(void*);
401 
402  default:
403  goto err;
404  }
405 
406 err:
407  return type_error(L, i, "vararg", 0, NULL);
408 }
409 
410 void unpack_varargs_stack(lua_State* L, int first, int last, char* to)
411 {
412  int i;
413 
414  for (i = first; i <= last; i++) {
415  to += unpack_vararg(L, i, to);
416  }
417 }
418 
419 void unpack_varargs_stack_skip(lua_State* L, int first, int last, int ints_to_skip, int floats_to_skip, char* to)
420 {
421  int i;
422 
423  for (i = first; i <= last; i++) {
424  int type = lua_type(L, i);
425 
426  if (type == LUA_TNUMBER && --floats_to_skip >= 0) {
427  continue;
428  } else if (type != LUA_TNUMBER && --ints_to_skip >= 0) {
429  continue;
430  }
431 
432  to += unpack_vararg(L, i, to);
433  }
434 }
435 
436 void unpack_varargs_float(lua_State* L, int first, int last, int max, char* to)
437 {
438  int i;
439 
440  for (i = first; i <= last && max > 0; i++) {
441  if (lua_type(L, i) == LUA_TNUMBER) {
442  unpack_vararg(L, i, to);
443  to += sizeof(double);
444  max--;
445  }
446  }
447 }
448 
449 void unpack_varargs_int(lua_State* L, int first, int last, int max, char* to)
450 {
451  int i;
452 
453  for (i = first; i <= last && max > 0; i++) {
454  if (lua_type(L, i) != LUA_TNUMBER) {
455  unpack_vararg(L, i, to);
456  to += sizeof(void*);
457  max--;
458  }
459  }
460 }
461 
462 void unpack_varargs_reg(lua_State* L, int first, int last, char* to)
463 {
464  int i;
465 
466  for (i = first; i <= last; i++) {
467  unpack_vararg(L, i, to);
468  to += sizeof(double);
469  }
470 }
471 
472 /* to_enum tries to convert a value at idx to the enum type indicated by to_ct
473  * and uv to_usr. For strings this means it will do a string lookup for the
474  * enum type. It leaves the stack unchanged. Will throw an error if the type
475  * at idx can't be conerted.
476  */
477 int32_t check_enum(lua_State* L, int idx, int to_usr, const struct ctype* to_ct)
478 {
479  int32_t ret;
480 
481  switch (lua_type(L, idx)) {
482  case LUA_TSTRING:
483  /* lookup string in to_usr to find value */
484  to_usr = lua_absindex(L, to_usr);
485  lua_pushvalue(L, idx);
486  lua_rawget(L, to_usr);
487 
488  if (lua_isnil(L, -1)) {
489  goto err;
490  }
491 
492  ret = (int32_t) lua_tointeger(L, -1);
493  lua_pop(L, 1);
494  return ret;
495 
496  case LUA_TUSERDATA:
497  return check_int32(L, idx);
498 
499  case LUA_TNIL:
500  return (int32_t) 0;
501 
502  case LUA_TNUMBER:
503  return (int32_t) lua_tointeger(L, idx);
504 
505  default:
506  goto err;
507  }
508 
509 err:
510  return type_error(L, idx, NULL, to_usr, to_ct);
511 }
512 
513 /* to_pointer tries converts a value at idx to a pointer. It fills out ct and
514  * pushes the uv of the found type. It will throw a lua error if it can not
515  * convert the value to a pointer. */
516 static void* check_pointer(lua_State* L, int idx, struct ctype* ct)
517 {
518  void* p;
519  memset(ct, 0, sizeof(*ct));
520  idx = lua_absindex(L, idx);
521 
522  switch (lua_type(L, idx)) {
523  case LUA_TNIL:
524  ct->type = VOID_TYPE;
525  ct->pointers = 1;
526  ct->is_null = 1;
527  lua_pushnil(L);
528  return NULL;
529 
530  case LUA_TNUMBER:
531  ct->type = INTPTR_TYPE;
532  ct->is_unsigned = 1;
533  ct->pointers = 0;
534  lua_pushnil(L);
535  return (void*) (uintptr_t) lua_tonumber(L, idx); // TODO in Lua 5.3: maybe change to lua_tointeger
536 
537  case LUA_TLIGHTUSERDATA:
538  ct->type = VOID_TYPE;
539  ct->pointers = 1;
540  lua_pushnil(L);
541  return lua_touserdata(L, idx);
542 
543  case LUA_TSTRING:
544  ct->type = INT8_TYPE;
545  ct->pointers = 1;
547  ct->is_array = 1;
548  ct->base_size = 1;
549  ct->const_mask = 2;
550  lua_pushnil(L);
551  return (void*) lua_tolstring(L, idx, &ct->array_size);
552 
553  case LUA_TUSERDATA:
554  p = to_cdata(L, idx, ct);
555 
556  if (ct->type == INVALID_TYPE) {
557  /* some other type of user data */
558  ct->type = VOID_TYPE;
559  ct->pointers = 1;
560  return userdata_toptr(L, idx);
561  } else if (ct->type == STRUCT_TYPE || ct->type == UNION_TYPE) {
562  return p;
563  } else {
564  return (void*) (intptr_t) check_intptr(L, idx, p, ct);
565  }
566  break;
567  }
568 
569  type_error(L, idx, "pointer", 0, NULL);
570  return NULL;
571 }
572 
573 static int is_void_ptr(const struct ctype* ct)
574 {
575  return ct->type == VOID_TYPE
576  && ct->pointers == 1;
577 }
578 
579 static int is_same_type(lua_State* L, int usr1, int usr2, const struct ctype* t1, const struct ctype* t2)
580 {
581  if (t1->type != t2->type) {
582  return 0;
583  }
584 
585 #if LUA_VERSION_NUM == 501
586  if (lua_isnil(L, usr1) != lua_isnil(L, usr2)) {
587  int ret;
588  usr1 = lua_absindex(L, usr1);
589  usr2 = lua_absindex(L, usr2);
591 
592  ret = lua_rawequal(L, usr1, -1)
593  || lua_rawequal(L, usr2, -1);
594 
595  lua_pop(L, 1);
596 
597  if (ret) {
598  return 1;
599  }
600  }
601 #endif
602 
603  return lua_rawequal(L, usr1, usr2);
604 }
605 
606 static void set_struct(lua_State* L, int idx, void* to, int to_usr, const struct ctype* tt, int check_pointers);
607 
608 /* to_typed_pointer converts a value at idx to a type tt with target uv to_usr
609  * checking all types. May push a temporary value so that it can create
610  * structs on the fly. */
611 void* check_typed_pointer(lua_State* L, int idx, int to_usr, const struct ctype* tt)
612 {
613  struct ctype ft;
614  void* p;
615 
616  to_usr = lua_absindex(L, to_usr);
617  idx = lua_absindex(L, idx);
618 
619  if (tt->pointers == 1 && (tt->type == STRUCT_TYPE || tt->type == UNION_TYPE) && lua_type(L, idx) == LUA_TTABLE) {
620  /* need to construct a struct of the target type */
621  struct ctype ct = *tt;
622  ct.pointers = ct.is_array = 0;
623  p = push_cdata(L, to_usr, &ct);
624  set_struct(L, idx, p, to_usr, &ct, 1);
625  return p;
626  }
627 
628  p = check_pointer(L, idx, &ft);
629 
630  if (tt->pointers == 1 && ft.pointers == 0 && (ft.type == STRUCT_TYPE || ft.type == UNION_TYPE)) {
631  /* auto dereference structs */
632  ft.pointers = 1;
633  ft.const_mask <<= 1;
634  }
635 
636  if (is_void_ptr(tt)) {
637  /* any pointer can convert to void* */
638  goto suc;
639 
640  } else if (is_void_ptr(&ft) && (ft.pointers || ft.is_reference)) {
641  /* void* can convert to any pointer */
642  goto suc;
643 
644  } else if (ft.is_null) {
645  /* NULL can convert to any pointer */
646  goto suc;
647 
648  } else if (!is_same_type(L, to_usr, -1, tt, &ft)) {
649  /* the base type is different */
650  goto err;
651 
652  } else if (tt->pointers != ft.pointers) {
653  goto err;
654 
655  } else if (ft.const_mask & ~tt->const_mask) {
656  /* for every const in from it must be in to, there are further rules
657  * for const casting (see the c++ spec), but they are hard to test
658  * quickly */
659  goto err;
660  }
661 
662 suc:
663  return p;
664 
665 err:
666  type_error(L, idx, NULL, to_usr, tt);
667  return NULL;
668 }
669 
670 /**
671  * gets the address of the wrapped C function for the lua function value at idx
672  * and returns 1 if it exists; otherwise returns 0 and nothing is pushed */
674 {
675  int top, n;
676  cfunction* f;
677  if (!lua_isfunction(L, idx)) return 0;
678 
679  top = lua_gettop(L);
680 
681  // Get the last upvalue
682  n = 2;
683  while (lua_getupvalue(L, idx, n)) {
684  lua_pop(L, 1);
685  n++;
686  }
687 
688  if (!lua_getupvalue(L, idx, n - 1))
689  return 0;
690 
691  if (!lua_isuserdata(L, -1) || !lua_getmetatable(L, -1)) {
692  lua_pop(L, 1);
693  return 0;
694  }
695 
697  if (!lua_rawequal(L, -1, -2)) {
698  lua_pop(L, 3);
699  return 0;
700  }
701 
702  /* stack is:
703  * userdata upval
704  * metatable
705  * callback_mt
706  */
707 
708  f = lua_touserdata(L, -3);
709  *addr = f[1];
710  lua_pop(L, 3);
711  return 1;
712 }
713 
714 /* to_cfunction converts a value at idx with usr table at to_usr and type tt
715  * into a function. Leaves the stack unchanged. */
716 static cfunction check_cfunction(lua_State* L, int idx, int to_usr, const struct ctype* tt, int check_pointers)
717 {
718  void* p;
719  struct ctype ft;
720  cfunction f;
721  int top = lua_gettop(L);
722 
723  idx = lua_absindex(L, idx);
724  to_usr = lua_absindex(L, to_usr);
725 
726  switch (lua_type(L, idx)) {
727  case LUA_TFUNCTION:
728  if (get_cfunction_address(L, idx, &f)) {
729  return f;
730  }
731 
732  /* Function cdatas are pinned and must be manually cleaned up by
733  * calling func:free(). */
735  f = compile_callback(L, idx, to_usr, tt);
736  lua_pushboolean(L, 1);
737  lua_rawset(L, -3);
738  lua_pop(L, 1); /* callbacks tbl */
739  return f;
740 
741  case LUA_TNIL:
742  return NULL;
743 
744  case LUA_TLIGHTUSERDATA:
745  if (check_pointers) {
746  goto err;
747  } else {
748  return (cfunction) lua_touserdata(L, idx);
749  }
750 
751  case LUA_TUSERDATA:
752  p = to_cdata(L, idx, &ft);
753  assert(lua_gettop(L) == top + 1);
754 
755  if (ft.type == INVALID_TYPE) {
756  if (check_pointers) {
757  goto err;
758  } else {
759  lua_pop(L, 1);
760  return (cfunction) lua_touserdata(L, idx);
761  }
762 
763  } else if (ft.is_null) {
764  lua_pop(L, 1);
765  return NULL;
766 
767  } else if (!check_pointers && (ft.pointers || ft.type == INTPTR_TYPE)) {
768  lua_pop(L, 1);
769  return (cfunction) *(void**) p;
770 
771  } else if (ft.type != FUNCTION_PTR_TYPE) {
772  goto err;
773 
774  } else if (!check_pointers) {
775  lua_pop(L, 1);
776  return *(cfunction*) p;
777 
778  } else if (ft.calling_convention != tt->calling_convention) {
779  goto err;
780 
781  } else if (!is_same_type(L, -1, to_usr, &ft, tt)) {
782  goto err;
783 
784  } else {
785  lua_pop(L, 1);
786  return *(cfunction*) p;
787  }
788 
789  default:
790  goto err;
791  }
792 
793 err:
794  type_error(L, idx, NULL, to_usr, tt);
795  return NULL;
796 }
797 
798 /* to_type_cfunction converts a value at idx with uv at to_usr and type tt to
799  * a cfunction. Leaves the stack unchanged. */
800 cfunction check_typed_cfunction(lua_State* L, int idx, int to_usr, const struct ctype* tt)
801 { return check_cfunction(L, idx, to_usr, tt, 1); }
802 
803 static void set_value(lua_State* L, int idx, void* to, int to_usr, const struct ctype* tt, int check_pointers);
804 
805 static void set_array(lua_State* L, int idx, void* to, int to_usr, const struct ctype* tt, int check_pointers)
806 {
807  size_t i, sz, esz;
808  struct ctype et;
809 
810  idx = lua_absindex(L, idx);
811  to_usr = lua_absindex(L, to_usr);
812 
813  switch (lua_type(L, idx)) {
814  case LUA_TSTRING:
815  if (tt->pointers == 1 && tt->type == INT8_TYPE) {
816  const char* str = lua_tolstring(L, idx, &sz);
817 
818  if (!tt->is_variable_array && sz >= tt->array_size) {
819  memcpy(to, str, tt->array_size);
820  } else {
821  /* include nul terminator */
822  memcpy(to, str, sz+1);
823  }
824  } else {
825  goto err;
826  }
827  break;
828 
829  case LUA_TTABLE:
830  et = *tt;
831  et.pointers--;
832  et.const_mask >>= 1;
833  et.is_array = 0;
834  esz = et.pointers ? sizeof(void*) : et.base_size;
835 
836  lua_rawgeti(L, idx, 2);
837 
838  if (tt->is_variable_array) {
839  /* we have no idea how big the array is, so set values based off
840  * how many items were given to us */
841  lua_pop(L, 1);
842  for (i = 0; i < lua_rawlen(L, idx); i++) {
843  lua_rawgeti(L, idx, (int) i + 1);
844  set_value(L, -1, (char*) to + esz * i, to_usr, &et, check_pointers);
845  lua_pop(L, 1);
846  }
847 
848  } else if (lua_isnil(L, -1)) {
849  /* there is no second element, so we set the whole array to the
850  * first element (or nil - ie 0) if there is no first element) */
851  lua_pop(L, 1);
852  lua_rawgeti(L, idx, 1);
853 
854  if (lua_isnil(L, -1)) {
855  memset(to, 0, ctype_size(L, tt));
856  } else {
857  /* if its still variable we have no idea how many values to set */
858  for (i = 0; i < tt->array_size; i++) {
859  set_value(L, -1, (char*) to + esz * i, to_usr, &et, check_pointers);
860  }
861  }
862 
863  lua_pop(L, 1);
864 
865  } else {
866  /* there is a second element, so we set each element using the
867  * equiv index in the table initializer */
868  lua_pop(L, 1);
869  for (i = 0; i < tt->array_size; i++) {
870  lua_rawgeti(L, idx, (int) (i+1));
871 
872  if (lua_isnil(L, -1)) {
873  /* we've hit the end of the values provided in the
874  * initializer, so memset the rest to zero */
875  lua_pop(L, 1);
876  memset((char*) to + esz * i, 0, (tt->array_size - i) * esz);
877  break;
878 
879  } else {
880  set_value(L, -1, (char*) to + esz * i, to_usr, &et, check_pointers);
881  lua_pop(L, 1);
882  }
883  }
884  }
885  break;
886 
887  default:
888  goto err;
889  }
890 
891  return;
892 
893 err:
894  type_error(L, idx, NULL, to_usr, tt);
895 }
896 
897 /* pops the member key from the stack, leaves the member user value on the
898  * stack. Returns the member offset. Returns -ve if the member can not be
899  * found. */
900 static ptrdiff_t get_member(lua_State* L, int usr, const struct ctype* ct, struct ctype* mt)
901 {
902  ptrdiff_t off;
903  lua_rawget(L, usr);
904 
905  if (lua_isnil(L, -1)) {
906  lua_pop(L, 1);
907  return -1;
908  }
909 
910  *mt = *(const struct ctype*) lua_touserdata(L, -1);
911  lua_getuservalue(L, -1);
912  lua_replace(L, -2);
913 
914  if (mt->is_variable_array && ct->variable_size_known) {
915  /* eg char mbr[?] */
916  size_t sz = (mt->pointers > 1) ? sizeof(void*) : mt->base_size;
917  assert(ct->is_variable_struct && mt->is_array);
918  mt->array_size = ct->variable_increment / sz;
919  mt->is_variable_array = 0;
920 
921  } else if (mt->is_variable_struct && ct->variable_size_known) {
922  /* eg struct {char a; char b[?]} mbr; */
924  mt->variable_size_known = 1;
925  mt->variable_increment = ct->variable_increment;
926  }
927 
928  off = mt->offset;
929  mt->offset = 0;
930  return off;
931 }
932 
933 static void set_struct(lua_State* L, int idx, void* to, int to_usr, const struct ctype* tt, int check_pointers)
934 {
935  int have_first = 0;
936  int have_other = 0;
937  struct ctype mt;
938  void* p;
939 
940  to_usr = lua_absindex(L, to_usr);
941  idx = lua_absindex(L, idx);
942 
943  switch (lua_type(L, idx)) {
944  case LUA_TTABLE:
945  /* match up to the members based off the table initializers key - this
946  * will match both numbered and named members in the user table
947  * we need a special case for when no entries in the initializer -
948  * zero initialize the c struct, and only one entry in the initializer
949  * - set all members to this value */
950  memset(to, 0, ctype_size(L, tt));
951  lua_pushnil(L);
952  while (lua_next(L, idx)) {
953  ptrdiff_t off;
954 
955  if (!have_first && lua_tonumber(L, -2) == 1 && lua_tonumber(L, -1) != 0) {
956  have_first = 1;
957  } else if (!have_other && (lua_type(L, -2) != LUA_TNUMBER || lua_tonumber(L, -2) != 1)) {
958  have_other = 1;
959  }
960 
961  lua_pushvalue(L, -2);
962  off = get_member(L, to_usr, tt, &mt);
963  assert(off >= 0);
964  set_value(L, -2, (char*) to + off, -1, &mt, check_pointers);
965 
966  /* initializer value, mt usr */
967  lua_pop(L, 2);
968  }
969 
970  /* if we only had a single non zero value then initialize all members to that value */
971  if (!have_other && have_first && tt->type != UNION_TYPE) {
972  size_t i, sz;
973  ptrdiff_t off;
974  lua_rawgeti(L, idx, 1);
975  sz = lua_rawlen(L, to_usr);
976 
977  for (i = 2; i < sz; i++) {
978  lua_pushinteger(L, i);
979  off = get_member(L, to_usr, tt, &mt);
980  assert(off >= 0);
981  set_value(L, -2, (char*) to + off, -1, &mt, check_pointers);
982  lua_pop(L, 1); /* mt usr */
983  }
984 
985  lua_pop(L, 1); /* initializer table */
986  }
987  break;
988 
989  case LUA_TUSERDATA:
990  if (check_pointers) {
991  p = check_typed_pointer(L, idx, to_usr, tt);
992  } else {
993  struct ctype ct;
994  p = check_pointer(L, idx, &ct);
995  }
996  memcpy(to, p, tt->base_size);
997  lua_pop(L, 1);
998  break;
999 
1000  default:
1001  goto err;
1002  }
1003 
1004  return;
1005 
1006 err:
1007  type_error(L, idx, NULL, to_usr, tt);
1008 }
1009 
1010 static void set_value(lua_State* L, int idx, void* to, int to_usr, const struct ctype* tt, int check_pointers)
1011 {
1012  int top = lua_gettop(L);
1013 
1014  if (tt->is_array) {
1015  set_array(L, idx, to, to_usr, tt, check_pointers);
1016 
1017  } else if (tt->pointers || tt->is_reference) {
1018  union {
1019  uint8_t c[sizeof(void*)];
1020  void* p;
1021  } u;
1022 
1023  if (lua_istable(L, idx)) {
1024  luaL_error(L, "Can't set a pointer member to a struct that's about to be freed");
1025  }
1026 
1027  if (check_pointers) {
1028  u.p = check_typed_pointer(L, idx, to_usr, tt);
1029  } else {
1030  struct ctype ct;
1031  u.p = check_pointer(L, idx, &ct);
1032  }
1033 
1034 #ifndef ALLOW_MISALIGNED_ACCESS
1035  if ((uintptr_t) to & PTR_ALIGN_MASK) {
1036  memcpy(to, u.c, sizeof(void*));
1037  } else
1038 #endif
1039  {
1040  *(void**) to = u.p;
1041  }
1042 
1043  lua_pop(L, 1);
1044 
1045  } else if (tt->is_bitfield) {
1046 
1047  uint64_t hi_mask = UINT64_C(0) - (UINT64_C(1) << (tt->bit_offset + tt->bit_size));
1048  uint64_t low_mask = (UINT64_C(1) << tt->bit_offset) - UINT64_C(1);
1050  val &= (UINT64_C(1) << tt->bit_size) - 1;
1051  val <<= tt->bit_offset;
1052  *(uint64_t*) to = val | (*(uint64_t*) to & (hi_mask | low_mask));
1053 
1054  } else if (tt->type == STRUCT_TYPE || tt->type == UNION_TYPE) {
1055  set_struct(L, idx, to, to_usr, tt, check_pointers);
1056 
1057  } else {
1058 
1059 #ifndef ALLOW_MISALIGNED_ACCESS
1060  union {
1061  uint8_t c[8];
1062  _Bool b;
1063  uint64_t u64;
1064  float f;
1065  double d;
1066  cfunction func;
1067  } misalign;
1068 
1069  void* origto = to;
1070 
1071  if ((uintptr_t) origto & (tt->base_size - 1)) {
1072  to = misalign.c;
1073  }
1074 #endif
1075 
1076  switch (tt->type) {
1077  case BOOL_TYPE:
1078  *(_Bool*) to = (cast_int64(L, idx, !check_pointers) != 0);
1079  break;
1080  case INT8_TYPE:
1081  if (tt->is_unsigned) {
1082  *(uint8_t*) to = (uint8_t) cast_uint64(L, idx, !check_pointers);
1083  } else {
1084  *(int8_t*) to = (int8_t) cast_int64(L, idx, !check_pointers);
1085  }
1086  break;
1087  case INT16_TYPE:
1088  if (tt->is_unsigned) {
1089  *(uint16_t*) to = (uint16_t) cast_uint64(L, idx, !check_pointers);
1090  } else {
1091  *(int16_t*) to = (int16_t) cast_int64(L, idx, !check_pointers);
1092  }
1093  break;
1094  case INT32_TYPE:
1095  if (tt->is_unsigned) {
1096  *(uint32_t*) to = (uint32_t) cast_uint64(L, idx, !check_pointers);
1097  } else {
1098  *(int32_t*) to = (int32_t) cast_int64(L, idx, !check_pointers);
1099  }
1100  break;
1101  case INT64_TYPE:
1102  if (tt->is_unsigned) {
1103  *(uint64_t*) to = cast_uint64(L, idx, !check_pointers);
1104  } else {
1105  *(int64_t*) to = cast_int64(L, idx, !check_pointers);
1106  }
1107  break;
1108  case FLOAT_TYPE:
1109  *(float*) to = (float) check_double(L, idx);
1110  break;
1111  case DOUBLE_TYPE:
1112  *(double*) to = check_double(L, idx);
1113  break;
1114  case COMPLEX_FLOAT_TYPE:
1116  break;
1117  case COMPLEX_DOUBLE_TYPE:
1119  break;
1120  case INTPTR_TYPE:
1121  *(uintptr_t*) to = check_uintptr(L, idx);
1122  break;
1123  case ENUM_TYPE:
1124  *(int32_t*) to = check_enum(L, idx, to_usr, tt);
1125  break;
1126  case FUNCTION_PTR_TYPE:
1127  *(cfunction*) to = check_cfunction(L, idx, to_usr, tt, check_pointers);
1128  break;
1129  default:
1130  goto err;
1131  }
1132 
1133 #ifndef ALLOW_MISALIGNED_ACCESS
1134  if ((uintptr_t) origto & (tt->base_size - 1)) {
1135  memcpy(origto, misalign.c, tt->base_size);
1136  }
1137 #endif
1138  }
1139 
1140  assert(lua_gettop(L) == top);
1141  return;
1142 err:
1143  type_error(L, idx, NULL, to_usr, tt);
1144 }
1145 
1146 static int ffi_typeof(lua_State* L)
1147 {
1148  struct ctype ct;
1149  check_ctype(L, 1, &ct);
1150  push_ctype(L, -1, &ct);
1151  return 1;
1152 }
1153 
1154 static void setmintop(lua_State* L, int idx)
1155 {
1156  if (lua_gettop(L) < idx) {
1157  lua_settop(L, idx);
1158  }
1159 }
1160 
1161 /* warning: in the case that it finds an array size, it removes that index */
1162 static void get_variable_array_size(lua_State* L, int idx, struct ctype* ct)
1163 {
1164  /* we only care about the variable buisness for the variable array
1165  * directly ie ffi.new('char[?]') or the struct that contains the variable
1166  * array ffi.new('struct {char v[?]}'). A pointer to the struct doesn't
1167  * care about the variable size (it treats it as a zero sized array). */
1168 
1169  if (ct->is_variable_array) {
1170  assert(ct->is_array);
1172  ct->is_variable_array = 0;
1173  lua_remove(L, idx);
1174 
1175  } else if (ct->is_variable_struct && !ct->variable_size_known) {
1176  assert(ct->type == STRUCT_TYPE && !ct->is_array);
1178  ct->variable_size_known = 1;
1179  lua_remove(L, idx);
1180  }
1181 }
1182 
1183 static int is_scalar(struct ctype* ct)
1184 {
1185  int type = ct->type;
1186  if (ct->pointers || ct->is_reference) {
1187  return !ct->is_array;
1188  }
1189  return type != STRUCT_TYPE && type != UNION_TYPE && !IS_COMPLEX(type);
1190 }
1191 
1192 static int should_pack(lua_State *L, int ct_usr, struct ctype* ct, int idx)
1193 {
1194  struct ctype argt;
1195  int same;
1196  ct_usr = lua_absindex(L, ct_usr);
1197 
1198  if (IS_COMPLEX(ct->type)) {
1199  return 0;
1200  }
1201 
1202  switch (lua_type(L, idx)) {
1203  case LUA_TTABLE:
1204  return 0;
1205  case LUA_TSTRING:
1206  return ct->type == STRUCT_TYPE;
1207  case LUA_TUSERDATA:
1208  // don't pack if the argument is a cdata with the same type
1209  to_cdata(L, idx, &argt);
1210  same = is_same_type(L, ct_usr, -1, ct, &argt);
1211  lua_pop(L, 1);
1212  return !same;
1213  default:
1214  return 1;
1215  }
1216 }
1217 
1218 static int do_new(lua_State* L, int is_cast)
1219 {
1220  int cargs, i, scalar;
1221  void* p;
1222  struct ctype ct;
1223  int check_ptrs = !is_cast;
1224 
1225  check_ctype(L, 1, &ct);
1226 
1227  /* don't push a callback when we have a c function, as cb:set needs a
1228  * compiled callback from a lua function to work */
1229  if (!ct.pointers && ct.type == FUNCTION_PTR_TYPE && (lua_isnil(L, 2) || lua_isfunction(L, 2))) {
1230  // Get the bound C function if this is a ffi lua function
1231  cfunction func;
1232  if (get_cfunction_address(L, 2, &func)) {
1233  p = push_cdata(L, -1, &ct);
1234  *(cfunction*) p = func;
1235  return 1;
1236  }
1237 
1238  /* Function cdatas are pinned and must be manually cleaned up by
1239  * calling func:free(). */
1240  compile_callback(L, 2, -1, &ct);
1242  lua_pushvalue(L, -2);
1243  lua_pushboolean(L, 1);
1244  lua_rawset(L, -3);
1245  lua_pop(L, 1); /* callbacks tbl */
1246  return 1;
1247  }
1248 
1249  /* this removes the vararg argument if its needed, and errors if its invalid */
1250  if (!is_cast) {
1251  get_variable_array_size(L, 2, &ct);
1252  }
1253 
1254  p = push_cdata(L, -1, &ct);
1255 
1256  /* if the user mt has a __gc function then call ffi.gc on this value */
1257  if (push_user_mt(L, -2, &ct)) {
1258  push_upval(L, &gc_key);
1259  lua_pushvalue(L, -3);
1260 
1261  /* user_mt.__gc */
1262  lua_pushliteral(L, "__gc");
1263  lua_rawget(L, -4);
1264 
1265  lua_rawset(L, -3); /* gc_upval[cdata] = user_mt.__gc */
1266  lua_pop(L, 2); /* user_mt and gc_upval */
1267  }
1268 
1269  /* stack is:
1270  * ctype arg
1271  * ctor args ... 0+
1272  * ctype usr
1273  * cdata
1274  */
1275 
1276  cargs = lua_gettop(L) - 3;
1277 
1278  if (cargs == 0) {
1279  return 1;
1280  }
1281 
1282  scalar = is_scalar(&ct);
1283  if (scalar && cargs > 1) {
1284  return luaL_error(L, "too many initializers");
1285  }
1286 
1287  if (cargs > 1 || (!scalar && should_pack(L, -2, &ct, 2))) {
1288  lua_createtable(L, cargs, 0);
1289  lua_replace(L, 1);
1290  for (i = 1; i <= cargs; i++) {
1291  lua_pushvalue(L, i + 1);
1292  lua_rawseti(L, 1, i);
1293  }
1294  assert(lua_gettop(L) == cargs + 3);
1295  set_value(L, 1, p, -2, &ct, check_ptrs);
1296  return 1;
1297  }
1298 
1299  set_value(L, 2, p, -2, &ct, check_ptrs);
1300  return 1;
1301 }
1302 
1303 static int ffi_new(lua_State* L)
1304 { return do_new(L, 0); }
1305 
1306 static int ffi_cast(lua_State* L)
1307 { return do_new(L, 1); }
1308 
1309 static int ctype_new(lua_State* L)
1310 { return do_new(L, 0); }
1311 
1312 static int ctype_call(lua_State* L)
1313 {
1314  struct ctype ct;
1315  int top = lua_gettop(L);
1316 
1317  check_ctype(L, 1, &ct);
1318 
1319  if (push_user_mt(L, -1, &ct)) {
1320  lua_pushstring(L, "__new");
1321  lua_rawget(L, -2);
1322  if (!lua_isnil(L, -1)) {
1323  lua_insert(L, 1); // function at bottom of stack under args
1324  lua_pop(L, 2);
1325  lua_call(L, top, 1);
1326  return 1;
1327  }
1328  lua_pop(L, 2);
1329  }
1330  lua_pop(L, 1);
1331 
1332  assert(lua_gettop(L) == top);
1333  return do_new(L, 0);
1334 }
1335 
1336 static int ffi_sizeof(lua_State* L)
1337 {
1338  struct ctype ct;
1339  check_ctype(L, 1, &ct);
1340  get_variable_array_size(L, 2, &ct);
1341  lua_pushinteger(L, ctype_size(L, &ct));
1342  return 1;
1343 }
1344 
1346 {
1347  struct ctype ct, mt;
1348  lua_settop(L, 2);
1349  check_ctype(L, 1, &ct);
1350 
1351  /* if no member is specified then we return the alignment of the type */
1352  if (lua_isnil(L, 2)) {
1353  lua_pushinteger(L, ct.align_mask + 1);
1354  return 1;
1355  }
1356 
1357  /* get the alignment of the member */
1358  lua_pushvalue(L, 2);
1359  if (get_member(L, -2, &ct, &mt) < 0) {
1360  push_type_name(L, 3, &ct);
1361  return luaL_error(L, "type %s has no member %s", lua_tostring(L, -1), lua_tostring(L, 2));
1362  }
1363 
1364  lua_pushinteger(L, mt.align_mask + 1);
1365  return 1;
1366 }
1367 
1369 {
1370  ptrdiff_t off;
1371  struct ctype ct, mt;
1372  lua_settop(L, 2);
1373  check_ctype(L, 1, &ct);
1374 
1375  lua_pushvalue(L, 2);
1376  off = get_member(L, -2, &ct, &mt); /* this replaces the member key at -1 with the mbr usr value */
1377  if (off < 0) {
1378  push_type_name(L, 3, &ct);
1379  return luaL_error(L, "type %s has no member %s", lua_tostring(L, -1), lua_tostring(L, 2));
1380  }
1381 
1382  lua_pushinteger(L, off);
1383 
1384  if (!mt.is_bitfield) {
1385  return 1;
1386  }
1387 
1388  lua_pushinteger(L, mt.bit_offset);
1389  lua_pushinteger(L, mt.bit_size);
1390  return 3;
1391 }
1392 
1393 static int ffi_istype(lua_State* L)
1394 {
1395  struct ctype tt, ft;
1396  check_ctype(L, 1, &tt);
1397  to_cdata(L, 2, &ft);
1398 
1399  if (ft.type == INVALID_TYPE) {
1400  goto fail;
1401  }
1402 
1403  if (!is_same_type(L, 3, 4, &tt, &ft)) {
1404  goto fail;
1405  }
1406 
1407  if (tt.pointers != ft.pointers) {
1408  goto fail;
1409  }
1410 
1411  if (tt.is_array != ft.is_array) {
1412  goto fail;
1413  }
1414 
1415  if (tt.is_array && tt.array_size != ft.array_size) {
1416  goto fail;
1417  }
1418 
1419  if (tt.calling_convention != ft.calling_convention) {
1420  goto fail;
1421  }
1422 
1423  lua_pushboolean(L, 1);
1424  return 1;
1425 
1426 fail:
1427  lua_pushboolean(L, 0);
1428  return 1;
1429 }
1430 
1431 static int cdata_gc(lua_State* L)
1432 {
1433  struct ctype ct;
1434  check_cdata(L, 1, &ct);
1435  lua_settop(L, 1);
1436 
1437  /* call the gc func if there is any registered */
1438  lua_pushvalue(L, 1);
1440  if (!lua_isnil(L, -1)) {
1441  lua_pushvalue(L, 1);
1442  lua_pcall(L, 1, 0, 0);
1443  }
1444 
1445  /* unset the closure */
1446  lua_pushvalue(L, 1);
1447  lua_pushnil(L);
1449 
1450  return 0;
1451 }
1452 
1454 {
1455  cfunction* p = (cfunction*) lua_touserdata(L, 1);
1456  // FIXME: temporarily disabled to prevent SIGTRAP on exit
1457  // free_code(get_jit(L), L, *p);
1458  return 0;
1459 }
1460 
1461 static int cdata_free(lua_State* L)
1462 {
1463  struct ctype ct;
1464  cfunction* p = (cfunction*) check_cdata(L, 1, &ct);
1465  lua_settop(L, 1);
1466 
1467  /* unset the closure */
1468  lua_pushvalue(L, 1);
1469  lua_pushnil(L);
1471 
1472  if (ct.is_jitted) {
1473  free_code(get_jit(L), L, *p);
1474  *p = NULL;
1475  }
1476 
1477  return 0;
1478 }
1479 
1480 static int cdata_set(lua_State* L)
1481 {
1482  struct ctype ct;
1483  cfunction* p = (cfunction*) check_cdata(L, 1, &ct);
1485 
1486  if (!ct.is_jitted) {
1487  luaL_error(L, "can't set the function for a non-lua callback");
1488  }
1489 
1490  if (*p == NULL) {
1491  luaL_error(L, "can't set the function for a free'd callback");
1492  }
1493 
1494  push_func_ref(L, *p);
1495  lua_pushvalue(L, 2);
1497 
1498  /* remove the closure for this callback as it embeds the function pointer
1499  * value */
1500  lua_pushvalue(L, 1);
1501  lua_pushboolean(L, 1);
1503 
1504  return 0;
1505 }
1506 
1507 static int cdata_call(lua_State* L)
1508 {
1509  struct ctype ct;
1510  int top = lua_gettop(L);
1511  cfunction* p = (cfunction*) check_cdata(L, 1, &ct);
1512 
1513  if (push_user_mt(L, -1, &ct)) {
1514  lua_pushliteral(L, "__call");
1515  lua_rawget(L, -2);
1516 
1517  if (!lua_isnil(L, -1)) {
1518  lua_insert(L, 1);
1519  lua_pop(L, 2); /* ct_usr, user_mt */
1520  lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
1521  return lua_gettop(L);
1522  }
1523  }
1524  if (ct.pointers || ct.type != FUNCTION_PTR_TYPE) {
1525  return luaL_error(L, "only function callbacks are callable");
1526  }
1527 
1528  lua_pushvalue(L, 1);
1530 
1531  if (!lua_isfunction(L, -1)) {
1532  lua_pop(L, 1);
1533  compile_function(L, *p, -1, &ct);
1534 
1535  assert(lua_gettop(L) == top + 2); /* uv, closure */
1536 
1537  /* closures[func] = closure */
1538  lua_pushvalue(L, 1);
1539  lua_pushvalue(L, -2);
1541 
1542  lua_replace(L, 1);
1543  } else {
1544  lua_replace(L, 1);
1545  }
1546 
1547  lua_pop(L, 1); /* uv */
1548  assert(lua_gettop(L) == top);
1549 
1550  lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
1551  return lua_gettop(L);
1552 }
1553 
1554 static int user_mt_key;
1555 
1557 {
1558  struct ctype ct;
1559  lua_settop(L, 2);
1560 
1561  check_ctype(L, 1, &ct);
1562  if (lua_type(L, 2) != LUA_TTABLE && lua_type(L, 2) != LUA_TNIL) {
1563  return luaL_argerror(L, 2, "metatable must be a table or nil");
1564  }
1565 
1567  lua_pushvalue(L, 2);
1568  lua_rawset(L, 3); /* user[user_mt_key] = mt */
1569 
1570  /* return the passed in ctype */
1571  push_ctype(L, 3, &ct);
1572  return 1;
1573 }
1574 
1575 /* push_user_mt returns 1 if the type has a user metatable and pushes it onto
1576  * the stack, otherwise it returns 0 and pushes nothing */
1577 int push_user_mt(lua_State* L, int ct_usr, const struct ctype* ct)
1578 {
1579  if (ct->type != STRUCT_TYPE && ct->type != UNION_TYPE && !IS_COMPLEX(ct->type)) {
1580  return 0;
1581  }
1582  if (!lua_istable(L, ct_usr)) {
1583  return 0;
1584  }
1585 
1586  ct_usr = lua_absindex(L, ct_usr);
1588  lua_rawget(L, ct_usr);
1589 
1590  if (lua_isnil(L, -1)) {
1591  lua_pop(L, 1);
1592  return 0;
1593  }
1594  return 1;
1595 }
1596 
1597 static int ffi_gc(lua_State* L)
1598 {
1599  struct ctype ct;
1600  lua_settop(L, 2);
1601  check_cdata(L, 1, &ct);
1602 
1603  push_upval(L, &gc_key);
1604  lua_pushvalue(L, 1);
1605  lua_pushvalue(L, 2);
1606  lua_rawset(L, -3);
1607 
1608  /* return the cdata back */
1609  lua_settop(L, 1);
1610  return 1;
1611 }
1612 
1613 /* lookup_cdata_index returns the offset of the found type and user value on
1614  * the stack if valid. Otherwise returns -ve and doesn't touch the stack.
1615  */
1616 static ptrdiff_t lookup_cdata_index(lua_State* L, int idx, int ct_usr, struct ctype* ct)
1617 {
1618  struct ctype mt;
1619  ptrdiff_t off;
1620  int type;
1621 
1622  ct_usr = lua_absindex(L, ct_usr);
1623  type = lua_type(L, idx);
1624 
1625  switch (type) {
1626  case LUA_TNUMBER:
1627  case LUA_TUSERDATA:
1628  /* possibilities are array, pointer */
1629 
1630  if (!ct->pointers || is_void_ptr(ct)) {
1631  return -1;
1632  }
1633 
1634  // unbox cdata
1635  if (type == LUA_TUSERDATA) {
1636  if (!cdata_tointeger(L, idx, &off)) {
1637  return -1;
1638  }
1639  } else {
1640  off = lua_tointeger(L, idx);
1641  }
1642 
1643  ct->is_array = 0;
1644  ct->pointers--;
1645  ct->const_mask >>= 1;
1646  ct->is_reference = 0;
1647 
1648  lua_pushvalue(L, ct_usr);
1649 
1650  return (ct->pointers ? sizeof(void*) : ct->base_size) * off;
1651 
1652  case LUA_TSTRING:
1653  /* possibilities are struct/union, pointer to struct/union */
1654 
1655  if ((ct->type != STRUCT_TYPE && ct->type != UNION_TYPE) || ct->is_array || ct->pointers > 1) {
1656  return -1;
1657  }
1658 
1659  lua_pushvalue(L, idx);
1660  off = get_member(L, ct_usr, ct, &mt);
1661  if (off < 0) {
1662  return -1;
1663  }
1664 
1665  *ct = mt;
1666  return off;
1667 
1668  default:
1669  return -1;
1670  }
1671 }
1672 
1674 {
1675  struct ctype tt;
1676  char* to;
1677  ptrdiff_t off;
1678 
1679  lua_settop(L, 3);
1680 
1681  to = (char*) check_cdata(L, 1, &tt);
1682  off = lookup_cdata_index(L, 2, -1, &tt);
1683 
1684  if (off < 0) {
1685  if (!push_user_mt(L, -1, &tt)) {
1686  goto err;
1687  }
1688 
1689  lua_pushliteral(L, "__newindex");
1690  lua_rawget(L, -2);
1691 
1692  if (lua_isnil(L, -1)) {
1693  goto err;
1694  }
1695 
1696  lua_insert(L, 1);
1697  lua_settop(L, 4);
1698  lua_call(L, 3, LUA_MULTRET);
1699  return lua_gettop(L);
1700  }
1701 
1702  if (tt.const_mask & 1) {
1703  return luaL_error(L, "can't set const data");
1704  }
1705 
1706  set_value(L, 3, to + off, -1, &tt, 1);
1707  return 0;
1708 
1709 err:
1710  push_type_name(L, 4, &tt);
1711  return luaL_error(L, "type %s has no member %s", lua_tostring(L, -1), lua_tostring(L, 2));
1712 }
1713 
1715 {
1716  void* to;
1717  struct ctype ct;
1718  char* data;
1719  ptrdiff_t off;
1720 
1721  lua_settop(L, 2);
1722  data = (char*) check_cdata(L, 1, &ct);
1723  assert(lua_gettop(L) == 3);
1724 
1725  if (!ct.pointers) {
1726  switch (ct.type) {
1727  case FUNCTION_PTR_TYPE:
1728  /* Callbacks use the same metatable as standard cdata values, but have set
1729  * and free members. So instead of mt.__index = mt, we do the equiv here. */
1730  lua_getmetatable(L, 1);
1731  lua_pushvalue(L, 2);
1732  lua_rawget(L, -2);
1733  return 1;
1734 
1735  /* This provides the .re and .im virtual members */
1736  case COMPLEX_DOUBLE_TYPE:
1737  case COMPLEX_FLOAT_TYPE:
1738  if (!lua_isstring(L, 2)) {
1739  luaL_error(L, "invalid member for complex number");
1740 
1741  } else if (strcmp(lua_tostring(L, 2), "re") == 0) {
1742  lua_pushnumber(L, ct.type == COMPLEX_DOUBLE_TYPE ? creal(*(complex_double*) data) : crealf(*(complex_float*) data));
1743 
1744  } else if (strcmp(lua_tostring(L, 2), "im") == 0) {
1745  lua_pushnumber(L, ct.type == COMPLEX_DOUBLE_TYPE ? cimag(*(complex_double*) data) : cimagf(*(complex_float*) data));
1746 
1747  } else {
1748  luaL_error(L, "invalid member for complex number");
1749  }
1750  return 1;
1751  }
1752  }
1753 
1754  off = lookup_cdata_index(L, 2, -1, &ct);
1755 
1756  if (off < 0) {
1757  assert(lua_gettop(L) == 3);
1758  if (!push_user_mt(L, -1, &ct)) {
1759  goto err;
1760  }
1761 
1762  lua_pushliteral(L, "__index");
1763  lua_rawget(L, -2);
1764 
1765  if (lua_isnil(L, -1)) {
1766  goto err;
1767  }
1768 
1769  if (lua_istable(L, -1)) {
1770  lua_pushvalue(L, 2);
1771  lua_gettable(L, -2);
1772  return 1;
1773  }
1774 
1775  lua_insert(L, 1);
1776  lua_settop(L, 3);
1777  lua_call(L, 2, LUA_MULTRET);
1778  return lua_gettop(L);
1779 
1780 err:
1781  push_type_name(L, 3, &ct);
1782  return luaL_error(L, "type %s has no member %s", lua_tostring(L, -1), lua_tostring(L, 2));
1783  }
1784 
1785  assert(lua_gettop(L) == 4); /* ct, key, ct_usr, mbr_usr */
1786  data += off;
1787 
1788  if (ct.is_array) {
1789  /* push a reference to the array */
1790  ct.is_reference = 1;
1791  to = push_cdata(L, -1, &ct);
1792  *(void**) to = data;
1793  return 1;
1794 
1795  } else if (ct.is_bitfield) {
1796 
1797  if (ct.type == INT64_TYPE) {
1798  struct ctype rt;
1799  uint64_t val = *(uint64_t*) data;
1800  val >>= ct.bit_offset;
1801  val &= (UINT64_C(1) << ct.bit_size) - 1;
1802 
1803  memset(&rt, 0, sizeof(rt));
1804  rt.base_size = 8;
1805  rt.type = INT64_TYPE;
1806  rt.is_unsigned = 1;
1807  rt.is_defined = 1;
1808 
1809  to = push_cdata(L, 0, &rt);
1810  *(uint64_t*) to = val;
1811 
1812  return 1;
1813 
1814  } else if (ct.type == BOOL_TYPE) {
1815  uint64_t val = *(uint64_t*) data;
1816  lua_pushboolean(L, (int) (val & (UINT64_C(1) << ct.bit_offset)));
1817  return 1;
1818 
1819  } else {
1820  uint64_t val = *(uint64_t*) data;
1821  val >>= ct.bit_offset;
1822  val &= (UINT64_C(1) << ct.bit_size) - 1;
1823  lua_pushinteger(L, val);
1824  return 1;
1825  }
1826 
1827  } else if (ct.pointers) {
1828 #ifndef ALLOW_MISALIGNED_ACCESS
1829  union {
1830  uint8_t c[8];
1831  void* p;
1832  } misalignbuf;
1833 
1834  if ((uintptr_t) data & PTR_ALIGN_MASK) {
1835  memcpy(misalignbuf.c, data, sizeof(void*));
1836  data = misalignbuf.c;
1837  }
1838 #endif
1839  to = push_cdata(L, -1, &ct);
1840  *(void**) to = *(void**) data;
1841  return 1;
1842 
1843  } else if (ct.type == STRUCT_TYPE || ct.type == UNION_TYPE) {
1844  /* push a reference to the member */
1845  ct.is_reference = 1;
1846  to = push_cdata(L, -1, &ct);
1847  *(void**) to = data;
1848  return 1;
1849 
1850  } else if (ct.type == FUNCTION_PTR_TYPE) {
1851  cfunction* pf = (cfunction*) push_cdata(L, -1, &ct);
1852  *pf = *(cfunction*) data;
1853  return 1;
1854 
1855  } else {
1856 #ifndef ALLOW_MISALIGNED_ACCESS
1857  union {
1858  uint8_t c[8];
1859  double d;
1860  float f;
1861  uint64_t u64;
1862  } misalignbuf;
1863 
1864  assert(ct.base_size <= 8);
1865 
1866  if ((uintptr_t) data & (ct.base_size - 1)) {
1867  memcpy(misalignbuf.c, data, ct.base_size);
1868  data = misalignbuf.c;
1869  }
1870 #endif
1871 
1872  switch (ct.type) {
1873  case BOOL_TYPE:
1874  lua_pushboolean(L, *(_Bool*) data);
1875  break;
1876  case INT8_TYPE:
1878  break;
1879  case INT16_TYPE:
1881  break;
1882  case ENUM_TYPE:
1883  case INT32_TYPE:
1885  break;
1886  case INT64_TYPE:
1887  to = push_cdata(L, -1, &ct);
1888  *(int64_t*) to = *(int64_t*) data;
1889  break;
1890  case INTPTR_TYPE:
1891  to = push_cdata(L, -1, &ct);
1892  *(intptr_t*) to = *(intptr_t*) data;
1893  break;
1894  case FLOAT_TYPE:
1895  lua_pushnumber(L, *(float*) data);
1896  break;
1897  case DOUBLE_TYPE:
1898  lua_pushnumber(L, *(double*) data);
1899  break;
1900  case COMPLEX_DOUBLE_TYPE:
1901  to = push_cdata(L, -1, &ct);
1903  break;
1904  case COMPLEX_FLOAT_TYPE:
1905  to = push_cdata(L, -1, &ct);
1906  *(complex_float*) to = *(complex_float*) data;
1907  break;
1908  default:
1909  luaL_error(L, "internal error: invalid member type");
1910  }
1911 
1912  return 1;
1913  }
1914 }
1915 
1916 static complex_double check_complex(lua_State* L, int idx, void* p, struct ctype* ct)
1917 {
1918  if (ct->type == INVALID_TYPE) {
1919  double d = luaL_checknumber(L, idx);
1920 #ifdef HAVE_COMPLEX
1921  return d;
1922 #else
1923  complex_double c;
1924  c.real = d;
1925  c.imag = 0;
1926  return c;
1927 #endif
1928  } else if (ct->type == COMPLEX_DOUBLE_TYPE) {
1929  return *(complex_double*) p;
1930  } else if (ct->type == COMPLEX_FLOAT_TYPE) {
1932 #ifdef HAVE_COMPLEX
1933  return *f;
1934 #else
1935  complex_double d;
1936  d.real = f->real;
1937  d.imag = f->imag;
1938  return d;
1939 #endif
1940  } else {
1942  type_error(L, idx, "complex", 0, NULL);
1943  memset(&dummy, 0, sizeof(dummy));
1944  return dummy;
1945  }
1946 }
1947 
1948 static int rank(const struct ctype* ct)
1949 {
1950  if (ct->pointers) {
1951  return 5;
1952  }
1953 
1954  switch (ct->type) {
1955  case COMPLEX_DOUBLE_TYPE:
1956  return 7;
1957  case COMPLEX_FLOAT_TYPE:
1958  return 6;
1959  case INTPTR_TYPE:
1960  return sizeof(intptr_t) >= sizeof(int64_t) ? 4 : 1;
1961  case INT64_TYPE:
1962  return ct->is_unsigned ? 3 : 2;
1963  case INT32_TYPE:
1964  case INT16_TYPE:
1965  case INT8_TYPE:
1966  return 2;
1967  default:
1968  return 0;
1969  }
1970 }
1971 
1972 static void push_complex(lua_State* L, complex_double res, int ct_usr, const struct ctype* ct)
1973 {
1974  if (ct->type == COMPLEX_DOUBLE_TYPE) {
1975  complex_double* p = (complex_double*) push_cdata(L, ct_usr, ct);
1976  *p = res;
1977  } else {
1978  complex_float* p = (complex_float*) push_cdata(L, ct_usr, ct);
1979 #ifdef HAVE_COMPLEX
1980  *p = (complex float) res;
1981 #else
1982  p->real = (float) res.real;
1983  p->imag = (float) res.imag;
1984 #endif
1985  }
1986 }
1987 
1988 static void push_number(lua_State* L, int64_t val, int ct_usr, const struct ctype* ct)
1989 {
1990  if ((ct->pointers || ct->type == INTPTR_TYPE) && sizeof(intptr_t) != sizeof(int64_t)) {
1991  intptr_t* p = (intptr_t*) push_cdata(L, ct_usr, ct);
1992  *p = val;
1993  } else {
1994  int64_t* p = (int64_t*) push_cdata(L, ct_usr, ct);
1995  *p = val;
1996  }
1997 }
1998 
1999 static int call_user_op(lua_State* L, const char* opfield, int idx, int ct_usr, const struct ctype* ct)
2000 {
2001  idx = lua_absindex(L, idx);
2002 
2003  if (push_user_mt(L, ct_usr, ct)) {
2004  lua_pushstring(L, opfield);
2005  lua_rawget(L, -2);
2006  if (!lua_isnil(L, -1)) {
2007  int top = lua_gettop(L);
2008  lua_pushvalue(L, idx);
2009  lua_call(L, 1, LUA_MULTRET);
2010  return lua_gettop(L) - top + 1;
2011  }
2012  lua_pop(L, 2);
2013  }
2014  return -1;
2015 }
2016 
2017 static int cdata_unm(lua_State* L)
2018 {
2019  struct ctype ct;
2020  void* p;
2021  int64_t val;
2022  int ret;
2023 
2024  lua_settop(L, 1);
2025  p = to_cdata(L, 1, &ct);
2026 
2027  ret = call_user_op(L, "__unm", 1, 2, &ct);
2028  if (ret >= 0) {
2029  return ret;
2030  }
2031 
2032  val = check_intptr(L, 1, p, &ct);
2033 
2034  if (ct.pointers) {
2035  luaL_error(L, "can't negate a pointer value");
2036  } else {
2037  memset(&ct, 0, sizeof(ct));
2038  ct.type = INT64_TYPE;
2039  ct.base_size = 8;
2040  ct.is_defined = 1;
2041  push_number(L, -val, 0, &ct);
2042  }
2043 
2044  return 1;
2045 }
2046 
2047 /* returns -ve if no binop was called otherwise returns the number of return
2048  * arguments */
2049 static int call_user_binop(lua_State* L, const char* opfield, int lidx, int lusr, const struct ctype* lt, int ridx, int rusr, const struct ctype* rt)
2050 {
2051  lidx = lua_absindex(L, lidx);
2052  ridx = lua_absindex(L, ridx);
2053 
2054  if (push_user_mt(L, lusr, lt)) {
2055  lua_pushstring(L, opfield);
2056  lua_rawget(L, -2);
2057 
2058  if (!lua_isnil(L, -1)) {
2059  int top = lua_gettop(L);
2060  lua_pushvalue(L, lidx);
2061  lua_pushvalue(L, ridx);
2062  lua_call(L, 2, LUA_MULTRET);
2063  return lua_gettop(L) - top + 1;
2064  }
2065 
2066  lua_pop(L, 2); /* user_mt and user_mt.op */
2067  }
2068 
2069  if (push_user_mt(L, rusr, rt)) {
2070  lua_pushstring(L, opfield);
2071  lua_rawget(L, -2);
2072 
2073  if (!lua_isnil(L, -1)) {
2074  int top = lua_gettop(L);
2075  lua_pushvalue(L, lidx);
2076  lua_pushvalue(L, ridx);
2077  lua_call(L, 2, LUA_MULTRET);
2078  return lua_gettop(L) - top + 1;
2079  }
2080 
2081  lua_pop(L, 2); /* user_mt and user_mt.op */
2082  }
2083 
2084  return -1;
2085 }
2086 
2088 {
2089  struct ctype lt, rt;
2090  int ret;
2091 
2092  lua_settop(L, 2);
2093  to_cdata(L, 1, &lt);
2094  to_cdata(L, 2, &rt);
2095 
2096  ret = call_user_binop(L, "__concat", 1, 3, &lt, 2, 4, &rt);
2097  if (ret >= 0) {
2098  return ret;
2099  }
2100 
2101  return luaL_error(L, "NYI");
2102 }
2103 
2104 static int cdata_len(lua_State* L)
2105 {
2106  struct ctype ct;
2107  int ret;
2108 
2109  lua_settop(L, 1);
2110  to_cdata(L, 1, &ct);
2111 
2112  ret = call_user_op(L, "__len", 1, 2, &ct);
2113  if (ret >= 0) {
2114  return ret;
2115  }
2116 
2117  push_type_name(L, 2, &ct);
2118  return luaL_error(L, "type %s does not implement the __len metamethod", lua_tostring(L, -1));
2119 }
2120 
2122 {
2123  struct ctype ct;
2124  int ret;
2125 
2126  lua_settop(L, 1);
2127  to_cdata(L, 1, &ct);
2128 
2129  ret = call_user_op(L, "__pairs", 1, 2, &ct);
2130  if (ret >= 0) {
2131  return ret;
2132  }
2133 
2134  push_type_name(L, 2, &ct);
2135  return luaL_error(L, "type %s does not implement the __pairs metamethod", lua_tostring(L, -1));
2136 }
2137 
2139 {
2140  struct ctype ct;
2141  int ret;
2142 
2143  lua_settop(L, 1);
2144  to_cdata(L, 1, &ct);
2145 
2146  ret = call_user_op(L, "__ipairs", 1, 2, &ct);
2147  if (ret >= 0) {
2148  return ret;
2149  }
2150 
2151  push_type_name(L, 2, &ct);
2152  return luaL_error(L, "type %s does not implement the __ipairs metamethod", lua_tostring(L, -1));
2153 }
2154 
2155 static int cdata_add(lua_State* L)
2156 {
2157  struct ctype lt, rt, ct;
2158  void *lp, *rp;
2159  int ct_usr;
2160  int ret;
2161 
2162  lua_settop(L, 2);
2163 
2164  lp = to_cdata(L, 1, &lt);
2165  rp = to_cdata(L, 2, &rt);
2166  assert(lua_gettop(L) == 4);
2167 
2168  ret = call_user_binop(L, "__add", 1, 3, &lt, 2, 4, &rt);
2169  if (ret >= 0) {
2170  return ret;
2171  }
2172  assert(lua_gettop(L) == 4);
2173 
2174  ct_usr = rank(&lt) > rank(&rt) ? 3 : 4;
2175  ct = rank(&lt) > rank(&rt) ? lt : rt;
2176 
2177  if (IS_COMPLEX(ct.type)) {
2179 
2180  left = check_complex(L, 1, lp, &lt);
2181  right = check_complex(L, 2, rp, &rt);
2182  assert(lua_gettop(L) == 4);
2183 
2184 #ifdef HAVE_COMPLEX
2185  res = left + right;
2186 #else
2187  res.real = left.real + right.real;
2188  res.imag = left.imag + right.imag;
2189 #endif
2190 
2191  push_complex(L, res, ct_usr, &ct);
2192  return 1;
2193 
2194  } else {
2195  int64_t left = check_intptr(L, 1, lp, &lt);
2196  int64_t right = check_intptr(L, 2, rp, &rt);
2197  assert(lua_gettop(L) == 4);
2198 
2199  /* note due to 2s complement it doesn't matter if we do the addition as int or uint,
2200  * but the result needs to be uint64_t if either of the sources are */
2201 
2202  if (lt.pointers && rt.pointers) {
2203  luaL_error(L, "can't add two pointers");
2204 
2205  } else if (lt.pointers) {
2206  int64_t res = left + (lt.pointers > 1 ? sizeof(void*) : lt.base_size) * right;
2207  lt.is_array = 0;
2208  push_number(L, res, 3, &lt);
2209 
2210  } else if (rt.pointers) {
2211  int64_t res = right + (rt.pointers > 1 ? sizeof(void*) : rt.base_size) * left;
2212  rt.is_array = 0;
2213  push_number(L, res, 4, &rt);
2214 
2215  } else {
2216  push_number(L, left + right, ct_usr, &ct);
2217  }
2218 
2219  return 1;
2220  }
2221 }
2222 
2223 static int cdata_sub(lua_State* L)
2224 {
2225  struct ctype lt, rt, ct;
2226  void *lp, *rp;
2227  int ct_usr;
2228  int ret;
2229 
2230  lua_settop(L, 2);
2231 
2232  lp = to_cdata(L, 1, &lt);
2233  rp = to_cdata(L, 2, &rt);
2234 
2235  ret = call_user_binop(L, "__sub", 1, 3, &lt, 2, 4, &rt);
2236  if (ret >= 0) {
2237  return ret;
2238  }
2239 
2240  ct_usr = rank(&lt) > rank(&rt) ? 3 : 4;
2241  ct = rank(&lt) > rank(&rt) ? lt : rt;
2242 
2243  if (IS_COMPLEX(ct.type)) {
2245 
2246  left = check_complex(L, 1, lp, &lt);
2247  right = check_complex(L, 2, rp, &rt);
2248 
2249 #ifdef HAVE_COMPLEX
2250  res = left - right;
2251 #else
2252  res.real = left.real - right.real;
2253  res.imag = left.imag - right.imag;
2254 #endif
2255 
2256  push_complex(L, res, ct_usr, &ct);
2257  return 1;
2258 
2259  } else {
2260  int64_t left = check_intptr(L, 1, lp, &lt);
2261  int64_t right = check_intptr(L, 2, rp, &rt);
2262 
2263  if (rt.pointers) {
2264  luaL_error(L, "NYI: can't subtract a pointer value");
2265 
2266  } else if (lt.pointers) {
2267  int64_t res = left - (lt.pointers > 1 ? sizeof(void*) : lt.base_size) * right;
2268  lt.is_array = 0;
2269  push_number(L, res, 3, &lt);
2270 
2271  } else {
2272  int64_t res = left - right;
2273  push_number(L, res, ct_usr, &ct);
2274  }
2275 
2276  return 1;
2277  }
2278 }
2279 
2280 /* TODO fix for unsigned */
2281 #define NUMBER_ONLY_BINOP(OPSTR, DO_NORMAL, DO_COMPLEX) \
2282  struct ctype lt, rt, ct; \
2283  void *lp, *rp; \
2284  int ct_usr; \
2285  int ret; \
2286  \
2287  lua_settop(L, 2); \
2288  \
2289  lp = to_cdata(L, 1, &lt); \
2290  rp = to_cdata(L, 2, &rt); \
2291  \
2292  ret = call_user_binop(L, OPSTR, 1, 3, &lt, 2, 4, &rt); \
2293  if (ret >= 0) { \
2294  return ret; \
2295  } \
2296  \
2297  ct_usr = rank(&lt) > rank(&rt) ? 3 : 4; \
2298  ct = rank(&lt) > rank(&rt) ? lt : rt; \
2299  \
2300  if (IS_COMPLEX(ct.type)) { \
2301  complex_double res; \
2302  complex_double left = check_complex(L, 1, lp, &lt); \
2303  complex_double right = check_complex(L, 2, rp, &rt); \
2304  \
2305  DO_COMPLEX(left, right, res); \
2306  push_complex(L, res, ct_usr, &ct); \
2307  \
2308  } else if (lt.pointers || rt.pointers) { \
2309  luaL_error(L, "can't operate on a pointer value"); \
2310  \
2311  } else { \
2312  int64_t res; \
2313  int64_t left = check_intptr(L, 1, lp, &lt); \
2314  int64_t right = check_intptr(L, 2, rp, &rt); \
2315  \
2316  DO_NORMAL(left, right, res); \
2317  push_number(L, res, ct_usr, &ct); \
2318  } \
2319  \
2320  return 1
2321 
2322 #define MUL(l,r,s) s = l * r
2323 #define DIV(l,r,s) s = l / r
2324 #define MOD(l,r,s) s = l % r
2325 #define POW(l,r,s) s = pow(l, r)
2326 
2327 #ifdef HAVE_COMPLEX
2328 #define MULC(l,r,s) s = l * r
2329 #define DIVC(l,r,s) s = l / r
2330 #define MODC(l,r,s) (void) l, (void) r, memset(&s, 0, sizeof(s)), luaL_error(L, "NYI: complex mod")
2331 #define POWC(l,r,s) s = cpow(l, r)
2332 #else
2333 #define MULC(l,r,s) s.real = l.real * r.real - l.imag * r.imag, s.imag = l.real * r.imag + l.imag * r.real
2334 #define DIVC(l,r,s) s.real = (l.real * r.real + l.imag * r.imag) / (r.real * r.real + r.imag * r.imag), \
2335  s.imag = (l.imag * r.real - l.real * r.imag) / (r.real * r.real + r.imag * r.imag)
2336 #define MODC(l,r,s) (void) l, (void) r, memset(&s, 0, sizeof(s)), luaL_error(L, "NYI: complex mod")
2337 #define POWC(l,r,s) (void) l, (void) r, memset(&s, 0, sizeof(s)), luaL_error(L, "NYI: complex pow")
2338 #endif
2339 
2340 static int cdata_mul(lua_State* L)
2341 { NUMBER_ONLY_BINOP("__mul", MUL, MULC); }
2342 
2343 static int cdata_div(lua_State* L)
2344 { NUMBER_ONLY_BINOP("__div", DIV, DIVC); }
2345 
2346 static int cdata_mod(lua_State* L)
2347 { NUMBER_ONLY_BINOP("__mod", MOD, MODC); }
2348 
2349 static int cdata_pow(lua_State* L)
2350 { NUMBER_ONLY_BINOP("__pow", POW, POWC); }
2351 
2352 #define COMPARE_BINOP(OPSTR, OP, OPC) \
2353  struct ctype lt, rt; \
2354  void *lp, *rp; \
2355  int ret, res; \
2356  \
2357  lua_settop(L, 2); \
2358  \
2359  lp = to_cdata(L, 1, &lt); \
2360  rp = to_cdata(L, 2, &rt); \
2361  \
2362  ret = call_user_binop(L, OPSTR, 1, 3, &lt, 2, 4, &rt); \
2363  if (ret >= 0) { \
2364  return ret; \
2365  } \
2366  \
2367  if (IS_COMPLEX(lt.type) || IS_COMPLEX(rt.type)) { \
2368  complex_double left = check_complex(L, 1, lp, &lt); \
2369  complex_double right = check_complex(L, 2, rp, &rt); \
2370  \
2371  res = OPC(left, right); \
2372  \
2373  lua_pushboolean(L, res); \
2374  \
2375  } else { \
2376  int64_t left = check_intptr(L, 1, lp, &lt); \
2377  int64_t right = check_intptr(L, 2, rp, &rt); \
2378  \
2379  if (lt.pointers && rt.pointers) { \
2380  if (is_void_ptr(&lt) || is_void_ptr(&rt) || is_same_type(L, 3, 4, &lt, &rt)) { \
2381  res = OP((uint64_t) left, (uint64_t) right); \
2382  } else { \
2383  goto err; \
2384  } \
2385  \
2386  } else if (lt.is_null && rt.type == FUNCTION_PTR_TYPE) { \
2387  res = OP((uint64_t) left, (uint64_t) right); \
2388  \
2389  } else if (rt.is_null && lt.type == FUNCTION_PTR_TYPE) { \
2390  res = OP((uint64_t) left, (uint64_t) right); \
2391  \
2392  } else if (lt.pointers && rt.type == INTPTR_TYPE && rt.is_unsigned) {\
2393  res = OP((uint64_t) left, (uint64_t) right); \
2394  \
2395  } else if (rt.pointers && lt.type == INTPTR_TYPE && lt.is_unsigned) {\
2396  res = OP((uint64_t) left, (uint64_t) right); \
2397  \
2398  } else if (rt.pointers || lt.pointers) { \
2399  goto err; \
2400  \
2401  } else if (lt.is_unsigned && rt.is_unsigned) { \
2402  res = OP((uint64_t) left, (uint64_t) right); \
2403  \
2404  } else if (lt.is_unsigned) { \
2405  res = OP((int64_t) (uint64_t) left, right); \
2406  \
2407  } else if (rt.is_unsigned) { \
2408  res = OP(left, (int64_t) (uint64_t) right); \
2409  \
2410  } else { \
2411  res = OP(left, right); \
2412  } \
2413  \
2414  lua_pushboolean(L, res); \
2415  } \
2416  return 1
2417 
2418 #define EQ(l, r) (l) == (r)
2419 #define LT(l, r) (l) < (r)
2420 #define LE(l, r) (l) <= (r)
2421 
2422 #ifdef HAVE_COMPLEX
2423 #define EQC(l, r) (l) == (r)
2424 #else
2425 #define EQC(l, r) (l).real == (r).real && (l).imag == (r).imag
2426 #endif
2427 
2428 #define LEC(l, r) EQC(l, r), luaL_error(L, "complex numbers are non-orderable")
2429 #define LTC(l, r) EQC(l, r), luaL_error(L, "complex numbers are non-orderable")
2430 
2431 static int cdata_eq(lua_State* L)
2432 {
2433  COMPARE_BINOP("__eq", EQ, EQC);
2434 err:
2435  lua_pushboolean(L, 0);
2436  return 1;
2437 }
2438 
2439 static int cdata_lt(lua_State* L)
2440 {
2441  COMPARE_BINOP("__lt", LT, LTC);
2442 err:
2443  lua_getuservalue(L, 1);
2444  lua_getuservalue(L, 2);
2445  push_type_name(L, -2, &lt);
2446  push_type_name(L, -2, &lt);
2447  return luaL_error(L, "trying to compare incompatible types %s and %s", lua_tostring(L, -2), lua_tostring(L, -1));
2448 }
2449 
2450 static int cdata_le(lua_State* L)
2451 {
2452  COMPARE_BINOP("__le", LE, LEC);
2453 err:
2454  lua_getuservalue(L, 1);
2455  lua_getuservalue(L, 2);
2456  push_type_name(L, -2, &lt);
2457  push_type_name(L, -2, &lt);
2458  return luaL_error(L, "trying to compare incompatible types %s and %s", lua_tostring(L, -2), lua_tostring(L, -1));
2459 }
2460 
2461 static const char* etype_tostring(int type)
2462 {
2463  switch (type) {
2464  case VOID_TYPE: return "void";
2465  case DOUBLE_TYPE: return "double";
2466  case FLOAT_TYPE: return "float";
2467  case COMPLEX_DOUBLE_TYPE: return "complex double";
2468  case COMPLEX_FLOAT_TYPE: return "complex float";
2469  case BOOL_TYPE: return "bool";
2470  case INT8_TYPE: return "int8";
2471  case INT16_TYPE: return "int16";
2472  case INT32_TYPE: return "int32";
2473  case INT64_TYPE: return "int64";
2474  case INTPTR_TYPE: return "intptr";
2475  case ENUM_TYPE: return "enum";
2476  case UNION_TYPE: return "union";
2477  case STRUCT_TYPE: return "struct";
2478  case FUNCTION_PTR_TYPE: return "function ptr";
2479  case FUNCTION_TYPE: return "function";
2480  default: return "invalid";
2481  }
2482 }
2483 
2484 static void print_type(lua_State* L, const struct ctype* ct)
2485 {
2486  lua_pushfstring(L, " sz %d %d %d align %d ptr %d %d %d type %s%s %d %d %d name %d call %d %d var %d %d %d bit %d %d %d %d jit %d",
2487  /* sz */
2488  ct->base_size,
2489  ct->array_size,
2490  ct->offset,
2491  /* align */
2492  ct->align_mask,
2493  /* ptr */
2494  ct->is_array,
2495  ct->pointers,
2496  ct->const_mask,
2497  /* type */
2498  ct->is_unsigned ? "u" : "",
2499  etype_tostring(ct->type),
2500  ct->is_reference,
2501  ct->is_defined,
2502  ct->is_null,
2503  /* name */
2504  ct->has_member_name,
2505  /* call */
2506  ct->calling_convention,
2507  ct->has_var_arg,
2508  /* var */
2509  ct->is_variable_array,
2510  ct->is_variable_struct,
2511  ct->variable_size_known,
2512  /* bit */
2513  ct->is_bitfield,
2514  ct->has_bitfield,
2515  ct->bit_offset,
2516  ct->bit_size,
2517  /* jit */
2518  ct->is_jitted);
2519 }
2520 
2522 {
2523  struct ctype ct;
2524  assert(lua_type(L, 1) == LUA_TUSERDATA);
2525  lua_settop(L, 1);
2526  check_ctype(L, 1, &ct);
2527  assert(lua_gettop(L) == 2);
2528  push_type_name(L, -1, &ct);
2529  lua_pushfstring(L, "ctype<%s>", lua_tostring(L, -1));
2530 
2531  if (DEBUG_TOSTRING) {
2532  print_type(L, &ct);
2533  lua_concat(L, 2);
2534  }
2535 
2536  return 1;
2537 }
2538 
2540 {
2541  struct ctype ct;
2542  char buf[64];
2543  void* p;
2544  int ret;
2545 
2546  lua_settop(L, 1);
2547  p = to_cdata(L, 1, &ct);
2548 
2549  ret = call_user_op(L, "__tostring", 1, 2, &ct);
2550  if (ret >= 0) {
2551  return ret;
2552  }
2553 
2554  if (ct.pointers > 0 || ct.is_reference || ct.type == STRUCT_TYPE || ct.type == UNION_TYPE) {
2555  push_type_name(L, -1, &ct);
2556  lua_pushfstring(L, "cdata<%s>: %p", lua_tostring(L, -1), p);
2557 
2558  if (DEBUG_TOSTRING) {
2559  print_type(L, &ct);
2560  lua_concat(L, 2);
2561  }
2562 
2563  return 1;
2564  }
2565 
2566  switch (ct.type) {
2567  case COMPLEX_DOUBLE_TYPE:
2568  {
2570  lua_pushfstring(L, "%f+%fi", creal(c), cimag(c));
2571  }
2572  return 1;
2573 
2574  case COMPLEX_FLOAT_TYPE:
2575  {
2577  lua_pushfstring(L, "%f+%fi", crealf(c), cimagf(c));
2578  }
2579  return 1;
2580 
2581  case FUNCTION_PTR_TYPE:
2582  p = *(void**) p;
2583  push_type_name(L, -1, &ct);
2584  lua_pushfstring(L, "cdata<%s>: %p", lua_tostring(L, -1), *(void**) p);
2585  return 1;
2586 
2587  case INTPTR_TYPE:
2588  lua_pushfstring(L, "%p", *(uintptr_t*) p);
2589  return 1;
2590 
2591  case INT64_TYPE:
2592  sprintf(buf, ct.is_unsigned ? "%"PRIu64 : "%"PRId64, *(uint64_t*) p);
2593  lua_pushstring(L, buf);
2594  return 1;
2595 
2596  default:
2597  sprintf(buf, ct.is_unsigned ? "%"PRId64 : "%"PRId64, (int64_t) check_intptr(L, 1, p, &ct));
2598  lua_pushstring(L, buf);
2599  return 1;
2600  }
2601 }
2602 
2603 static int ffi_errno(lua_State* L)
2604 {
2605  struct jit* jit = get_jit(L);
2606 
2607  if (!lua_isnoneornil(L, 1)) {
2610  } else {
2612  }
2613 
2614  return 1;
2615 }
2616 
2617 static int ffi_type(lua_State* L)
2618 {
2619  if (lua_isuserdata(L, 1) && lua_getmetatable(L, 1)) {
2620  if (equals_upval(L, -1, &cdata_mt_key) || equals_upval(L, -1, &ctype_mt_key)) {
2621  lua_pushstring(L, "cdata");
2622  return 1;
2623  }
2624  lua_pop(L, 1); /* mt */
2625  }
2626 
2627  /* call the old _G.type, we use an upvalue as _G.type is set
2628  * to this function */
2630  lua_insert(L, 1);
2632  return lua_gettop(L);
2633 }
2634 
2635 static int ffi_number(lua_State* L)
2636 {
2637  struct ctype ct;
2638  void* data = to_cdata(L, 1, &ct);
2639 
2640  if (ct.type != INVALID_TYPE) {
2641  lua_pushinteger(L, check_intptr(L, 1, data, &ct));
2642  return 1;
2643  } else {
2644  /* call the old _G.tonumber, we use an upvalue as _G.tonumber is set
2645  * to this function */
2647  lua_insert(L, 1);
2649  return lua_gettop(L);
2650  }
2651 }
2652 
2653 static int ffi_string(lua_State* L)
2654 {
2655  struct ctype ct;
2656  char* data;
2657  lua_settop(L, 2);
2658 
2659  data = (char*) check_cdata(L, 1, &ct);
2660 
2661  if (is_void_ptr(&ct)) {
2662  lua_pushlstring(L, data, (size_t) luaL_checknumber(L, 2));
2663  return 1;
2664 
2665  } else if (ct.type == INT8_TYPE && ct.pointers == 1) {
2666  size_t sz;
2667 
2668  if (lua_isuserdata(L, 2)) {
2669  ptrdiff_t val;
2670  if (!cdata_tointeger(L, 2, &val)) {
2671  type_error(L, 2, "int", 0, NULL);
2672  }
2673  sz = (size_t) val;
2674  } else if (!lua_isnil(L, 2)) {
2675  sz = (size_t) luaL_checknumber(L, 2);
2676 
2677  } else if (ct.is_array && !ct.is_variable_array) {
2678  char* nul = memchr(data, '\0', ct.array_size);
2679  sz = nul ? nul - data : ct.array_size;
2680 
2681  } else {
2682  sz = strlen(data);
2683  }
2684 
2685  lua_pushlstring(L, data, sz);
2686  return 1;
2687  }
2688 
2689  return luaL_error(L, "unable to convert cdata to string");
2690 }
2691 
2692 static int ffi_copy(lua_State* L)
2693 {
2694  struct ctype ft, tt;
2695  char *to, *from;
2696 
2697  setmintop(L, 3);
2698  to = (char*) check_pointer(L, 1, &tt);
2699  from = (char*) check_pointer(L, 2, &ft);
2700 
2701  if (!lua_isnoneornil(L, 3)) {
2702  memcpy(to, from, (size_t) luaL_checknumber(L, 3));
2703 
2704  } else if (ft.type == INT8_TYPE && ft.pointers == 1) {
2705  size_t sz = ft.is_array ? ft.array_size : strlen(from);
2706  memcpy(to, from, sz);
2707  to[sz] = '\0';
2708  }
2709 
2710  return 0;
2711 }
2712 
2713 static int ffi_fill(lua_State* L)
2714 {
2715  struct ctype ct;
2716  void* to;
2717  size_t sz;
2718  int val = 0;
2719 
2720  setmintop(L, 3);
2721  to = check_pointer(L, 1, &ct);
2722  sz = (size_t) luaL_checknumber(L, 2);
2723 
2724  if (!lua_isnoneornil(L, 3)) {
2725  val = (int) luaL_checkinteger(L, 3);
2726  }
2727 
2728  memset(to, val, sz);
2729  return 0;
2730 }
2731 
2732 static int ffi_abi(lua_State* L)
2733 {
2734  luaL_checkstring(L, 1);
2735  push_upval(L, &abi_key);
2736  lua_pushvalue(L, 1);
2737  lua_rawget(L, -2);
2739  return 1;
2740 }
2741 
2742 static int ffi_load(lua_State* L)
2743 {
2744  const char* libname = luaL_checkstring(L, 1);
2745  void** lib = (void**) lua_newuserdata(L, sizeof(void*));
2746 
2747  *lib = LoadLibraryA(libname);
2748 
2749 #ifdef LIB_FORMAT_1
2750  if (!*lib) {
2751  libname = lua_pushfstring(L, LIB_FORMAT_1, lua_tostring(L, 1));
2752  *lib = LoadLibraryA(libname);
2753  lua_pop(L, 1);
2754  }
2755 #endif
2756 
2757 #ifdef LIB_FORMAT_2
2758  if (!*lib) {
2759  libname = lua_pushfstring(L, LIB_FORMAT_2, lua_tostring(L, 1));
2760  *lib = LoadLibraryA(libname);
2761  lua_pop(L, 1);
2762  }
2763 #endif
2764 
2765  if (!*lib) {
2766  return luaL_error(L, "could not load library %s", lua_tostring(L, 1));
2767  }
2768 
2769  lua_newtable(L);
2770  lua_setuservalue(L, -2);
2771 
2773  lua_setmetatable(L, -2);
2774  return 1;
2775 }
2776 
2777 static void* find_symbol(lua_State* L, int modidx, const char* asmname)
2778 {
2779  size_t i;
2780  void** libs;
2781  size_t num;
2782  void* sym = NULL;
2783 
2784  libs = (void**) lua_touserdata(L, modidx);
2785  num = lua_rawlen(L, modidx) / sizeof(void*);
2786 
2787  for (i = 0; i < num && sym == NULL; i++) {
2788  if (libs[i]) {
2789  sym = GetProcAddressA(libs[i], asmname);
2790  }
2791  }
2792 
2793  return sym;
2794 }
2795 
2796 /* pushes the user table */
2797 static void* lookup_global(lua_State* L, int modidx, int nameidx, const char** pname, struct ctype* ct)
2798 {
2799  int top = lua_gettop(L);
2800  void* sym;
2801 
2802  modidx = lua_absindex(L, modidx);
2803  nameidx = lua_absindex(L, nameidx);
2804 
2805  *pname = luaL_checkstring(L, nameidx);
2806 
2807  /* get the ctype */
2809  lua_pushvalue(L, nameidx);
2810  lua_rawget(L, -2);
2811  if (lua_isnil(L, -1)) {
2812  luaL_error(L, "missing declaration for function/global %s", *pname);
2813  return NULL;
2814  }
2815 
2816  /* leave just the ct_usr on the stack */
2817  *ct = *(const struct ctype*) lua_touserdata(L, -1);
2818  lua_getuservalue(L, -1);
2819  lua_replace(L, top + 1);
2820  lua_pop(L, 1);
2821 
2822  assert(lua_gettop(L) == top + 1);
2823 
2824  /* get the assembly name */
2826  lua_pushvalue(L, nameidx);
2827  lua_rawget(L, -2);
2828  if (lua_isstring(L, -1)) {
2829  *pname = lua_tostring(L, -1);
2830  }
2831  lua_pop(L, 2);
2832 
2833  sym = find_symbol(L, modidx, *pname);
2834 
2835  assert(lua_gettop(L) == top + 1);
2836  return sym;
2837 }
2838 
2840 {
2841  const char* asmname;
2842  struct ctype ct;
2843  void *sym;
2844 
2845  lua_settop(L, 2);
2846 
2847  /* see if we have already loaded the function */
2848  lua_getuservalue(L, 1);
2849  lua_pushvalue(L, 2);
2850  lua_rawget(L, -2);
2851  if (!lua_isnil(L, -1)) {
2852  return 1;
2853  }
2854  lua_pop(L, 2);
2855 
2856  /* check the constants table */
2858  lua_pushvalue(L, 2);
2859  lua_rawget(L, -2);
2860  if (!lua_isnil(L, -1)) {
2861  return 1;
2862  }
2863  lua_pop(L, 2);
2864 
2865  /* lookup_global pushes the ct_usr */
2866  sym = lookup_global(L, 1, 2, &asmname, &ct);
2867 
2868 #if defined _WIN32 && !defined _WIN64 && (defined __i386__ || defined _M_IX86)
2869  if (!sym && ct.type == FUNCTION_TYPE) {
2871  lua_pushfstring(L, "_%s@%d", asmname, x86_return_size(L, -1, &ct));
2872  sym = find_symbol(L, 1, lua_tostring(L, -1));
2873  lua_pop(L, 1);
2874  }
2875 
2876  if (!sym && ct.type == FUNCTION_TYPE) {
2878  lua_pushfstring(L, "@%s@%d", asmname, x86_return_size(L, -1, &ct));
2879  sym = find_symbol(L, 1, lua_tostring(L, -1));
2880  lua_pop(L, 1);
2881  }
2882 #endif
2883 
2884  if (!sym) {
2885  return luaL_error(L, "failed to find function/global %s", asmname);
2886  }
2887 
2888  assert(lua_gettop(L) == 3); /* module, name, ct_usr */
2889 
2890  if (ct.type == FUNCTION_TYPE) {
2891  compile_function(L, (cfunction) sym, -1, &ct);
2892  assert(lua_gettop(L) == 4); /* module, name, ct_usr, function */
2893 
2894  /* set module usr value[luaname] = function to cache for next time */
2895  lua_getuservalue(L, 1);
2896  lua_pushvalue(L, 2);
2897  lua_pushvalue(L, -3);
2898  lua_rawset(L, -3);
2899  lua_pop(L, 1); /* module uv */
2900  return 1;
2901  }
2902 
2903  /* extern const char* foo; and extern const char foo[]; */
2904  if (ct.pointers == 1 && ct.type == INT8_TYPE) {
2905  char* str = (char*) sym;
2906  if (!ct.is_array) {
2907  str = *(char**) sym;
2908  }
2909  lua_pushstring(L, str);
2910  return 1;
2911  }
2912 
2913  /* extern struct foo foo[], extern void* foo[]; and extern struct foo foo; */
2914  if (ct.is_array || (!ct.pointers && (ct.type == UNION_TYPE || ct.type == STRUCT_TYPE))) {
2915  void* p;
2916  ct.is_reference = 1;
2917  p = push_cdata(L, -1, &ct);
2918  *(void**) p = sym;
2919  return 1;
2920  }
2921 
2922  /* extern void* foo; and extern void (*foo)(); */
2923  if (ct.pointers || ct.type == FUNCTION_PTR_TYPE) {
2924  void* p = push_cdata(L, -1, &ct);
2925  *(void**) p = *(void**) sym;
2926  return 1;
2927  }
2928 
2929  switch (ct.type) {
2930  case COMPLEX_DOUBLE_TYPE:
2931  case COMPLEX_FLOAT_TYPE:
2932  case INTPTR_TYPE:
2933  case INT64_TYPE:
2934  {
2935  /* TODO: complex float/double need to be references if .re and
2936  * .imag are setable */
2937  void* p = push_cdata(L, -1, &ct);
2938  memcpy(p, sym, ct.base_size);
2939  return 1;
2940  }
2941 
2942  case DOUBLE_TYPE:
2943  lua_pushnumber(L, *(double*) sym);
2944  return 1;
2945 
2946  case FLOAT_TYPE:
2947  lua_pushnumber(L, *(float*) sym);
2948  return 1;
2949 
2950  case BOOL_TYPE:
2951  lua_pushboolean(L, *(bool*) sym);
2952  return 1;
2953 
2954  case INT8_TYPE:
2956  return 1;
2957 
2958  case INT16_TYPE:
2960  return 1;
2961 
2962  case INT32_TYPE:
2963  case ENUM_TYPE:
2965  return 1;
2966  }
2967 
2968  return luaL_error(L, "NYI - global value type");
2969 }
2970 
2972 {
2973  const char* name;
2974  void* sym;
2975  struct ctype ct;
2976 
2977  lua_settop(L, 3);
2978 
2979  /* pushes the ct_usr */
2980  sym = lookup_global(L, 1, 2, &name, &ct);
2981  assert(lua_gettop(L) == 4); /* module, name, value, ct_usr */
2982 
2983  if (sym == NULL) {
2984  return luaL_error(L, "failed to find global %s", name);
2985  }
2986 
2987  if (ct.type == FUNCTION_TYPE || ct.is_array || (ct.const_mask & 1)) {
2988  return luaL_error(L, "can not set global %s", name);
2989  }
2990 
2991  set_value(L, 3, sym, -1, &ct, 1);
2992  return 0;
2993 }
2994 
2995 static int jit_gc(lua_State* L)
2996 {
2997  size_t i;
2998  struct jit* jit = get_jit(L);
2999  dasm_free(jit);
3000  for (i = 0; i < jit->pagenum; i++) {
3001  FreePage(jit->pages[i], jit->pages[i]->size);
3002  }
3003  free(jit->pages);
3004  free(jit->globals);
3005  return 0;
3006 }
3007 
3008 static int ffi_debug(lua_State* L)
3009 {
3010  lua_newtable(L);
3012  lua_setfield(L, -2, "ctype_mt");
3014  lua_setfield(L, -2, "cdata_mt");
3016  lua_setfield(L, -2, "cmodule_mt");
3018  lua_setfield(L, -2, "constants");
3019  push_upval(L, &types_key);
3020  lua_setfield(L, -2, "types");
3021  push_upval(L, &jit_key);
3022  lua_setfield(L, -2, "jit");
3023  push_upval(L, &gc_key);
3024  lua_setfield(L, -2, "gc");
3026  lua_setfield(L, -2, "callbacks");
3028  lua_setfield(L, -2, "functions");
3029  push_upval(L, &abi_key);
3030  lua_setfield(L, -2, "abi");
3032  lua_setfield(L, -2, "next_unnamed");
3033  return 1;
3034 }
3035 
3036 static int do64(lua_State* L, int is_unsigned)
3037 {
3038  lua_Number low, high;
3039  struct ctype ct;
3040  int64_t val;
3041 
3042  lua_settop(L, 2);
3043 
3044  if (!lua_isnil(L, 2)) {
3045  high = luaL_checknumber(L, 1);
3046  low = luaL_checknumber(L, 2);
3047  } else {
3048  high = 0;
3049  low = luaL_checknumber(L, 1);
3050  }
3051 
3052  val = ((int64_t) (uint32_t) high << 32) | (int64_t) (uint32_t) low;
3053 
3054  if (!is_unsigned && (high < 0 || low < 0)) {
3055  val = -val;
3056  }
3057 
3058  memset(&ct, 0, sizeof(ct));
3059  ct.type = INT64_TYPE;
3060  ct.is_unsigned = is_unsigned;
3061  ct.is_defined = 1;
3062  ct.base_size = sizeof(int64_t);
3063  push_number(L, (int64_t) val, 0, &ct);
3064 
3065  return 1;
3066 }
3067 
3068 static int ffi_i64(lua_State* L)
3069 { return do64(L, 0); }
3070 
3071 static int ffi_u64(lua_State* L)
3072 { return do64(L, 1); }
3073 
3074 static const luaL_Reg cdata_mt[] = {
3075  {"__gc", &cdata_gc},
3076  {"__call", &cdata_call},
3077  {"free", &cdata_free},
3078  {"set", &cdata_set},
3079  {"__index", &cdata_index},
3080  {"__newindex", &cdata_newindex},
3081  {"__add", &cdata_add},
3082  {"__sub", &cdata_sub},
3083  {"__mul", &cdata_mul},
3084  {"__div", &cdata_div},
3085  {"__mod", &cdata_mod},
3086  {"__pow", &cdata_pow},
3087  {"__unm", &cdata_unm},
3088  {"__eq", &cdata_eq},
3089  {"__lt", &cdata_lt},
3090  {"__le", &cdata_le},
3091  {"__tostring", &cdata_tostring},
3092  {"__concat", &cdata_concat},
3093  {"__len", &cdata_len},
3094  {"__pairs", &cdata_pairs},
3095  {"__ipairs", &cdata_ipairs},
3096  {NULL, NULL}
3097 };
3098 
3099 static const luaL_Reg callback_mt[] = {
3100  {"__gc", &callback_free},
3101  {NULL, NULL}
3102 };
3103 
3104 static const luaL_Reg ctype_mt[] = {
3105  {"__call", &ctype_call},
3106  {"__new", &ctype_new},
3107  {"__tostring", &ctype_tostring},
3108  {NULL, NULL}
3109 };
3110 
3111 static const luaL_Reg cmodule_mt[] = {
3112  {"__index", &cmodule_index},
3113  {"__newindex", &cmodule_newindex},
3114  {NULL, NULL}
3115 };
3116 
3117 static const luaL_Reg jit_mt[] = {
3118  {"__gc", &jit_gc},
3119  {NULL, NULL}
3120 };
3121 
3122 static const luaL_Reg ffi_reg[] = {
3123  {"cdef", &ffi_cdef},
3124  {"load", &ffi_load},
3125  {"new", &ffi_new},
3126  {"typeof", &ffi_typeof},
3127  {"cast", &ffi_cast},
3128  {"metatype", &ffi_metatype},
3129  {"gc", &ffi_gc},
3130  {"sizeof", &ffi_sizeof},
3131  {"alignof", &ffi_alignof},
3132  {"offsetof", &ffi_offsetof},
3133  {"istype", &ffi_istype},
3134  {"errno", &ffi_errno},
3135  {"string", &ffi_string},
3136  {"copy", &ffi_copy},
3137  {"fill", &ffi_fill},
3138  {"abi", &ffi_abi},
3139  {"debug", &ffi_debug},
3140  {"i64", &ffi_i64},
3141  {"u64", &ffi_u64},
3142  {NULL, NULL}
3143 };
3144 
3145 /* leaves the usr table on the stack */
3146 static void push_builtin(lua_State* L, struct ctype* ct, const char* name, int type, int size, int align, int is_unsigned)
3147 {
3148  memset(ct, 0, sizeof(*ct));
3149  ct->type = type;
3150  ct->base_size = size;
3151  ct->align_mask = align;
3152  ct->is_defined = 1;
3153  ct->is_unsigned = is_unsigned;
3154 
3155  if (IS_COMPLEX(type)) {
3156  lua_newtable(L);
3157  } else {
3158  lua_pushnil(L);
3159  }
3160 
3161  push_upval(L, &types_key);
3162  push_ctype(L, -2, ct);
3163  lua_setfield(L, -2, name);
3164  lua_pop(L, 2); /* types, usr table */
3165 }
3166 
3167 static void push_builtin_undef(lua_State* L, struct ctype* ct, const char* name, int type)
3168 {
3169  memset(ct, 0, sizeof(*ct));
3170  ct->type = type;
3171 
3172  push_upval(L, &types_key);
3173  push_ctype(L, 0, ct);
3174  lua_setfield(L, -2, name);
3175  lua_pop(L, 1); /* types */
3176 }
3177 
3178 static void add_typedef(lua_State* L, const char* from, const char* to)
3179 {
3180  struct ctype ct;
3181  struct parser P;
3182  P.line = 1;
3183  P.align_mask = DEFAULT_ALIGN_MASK;
3184  P.next = P.prev = from;
3185 
3186  push_upval(L, &types_key);
3187  parse_type(L, &P, &ct);
3188  parse_argument(L, &P, -1, &ct, NULL, NULL);
3189  push_ctype(L, -1, &ct);
3190 
3191  /* stack is at +4: types, type usr, arg usr, ctype */
3192 
3193  lua_setfield(L, -4, to);
3194  lua_pop(L, 3); /* types, type usr, arg usr */
3195 }
3196 
3198 {
3199  struct jit* jit = get_jit(L);
3200 
3201  /* jit setup */
3202  {
3203  dasm_init(jit, 64);
3204 #ifdef _WIN32
3205  {
3206  SYSTEM_INFO si;
3207  GetSystemInfo(&si);
3208  jit->align_page_size = si.dwAllocationGranularity - 1;
3209  }
3210 #else
3211  jit->align_page_size = sysconf(_SC_PAGE_SIZE) - 1;
3212 #endif
3213  jit->globals = (void**) malloc(64 * sizeof(void*));
3215  compile_globals(jit, L);
3216  }
3217 
3218  /* ffi.C */
3219  {
3220 #ifdef _WIN32
3221  size_t sz = sizeof(HMODULE) * 6;
3222  HMODULE* libs = lua_newuserdata(L, sz);
3223  memset(libs, 0, sz);
3224 
3225  /* exe */
3226  GetModuleHandle(NULL);
3227  /* lua dll */
3228 #ifdef LUA_DLL_NAME
3229 #define STR2(tok) #tok
3230 #define STR(tok) STR2(tok)
3231  libs[1] = LoadLibraryA(STR(LUA_DLL_NAME));
3232 #undef STR
3233 #undef STR2
3234 #endif
3235 
3236  /* crt */
3237 #ifdef UNDER_CE
3238  libs[2] = LoadLibraryA("coredll.dll");
3239 #else
3240  GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (char*) &_fmode, &libs[2]);
3241  libs[3] = LoadLibraryA("kernel32.dll");
3242  libs[4] = LoadLibraryA("user32.dll");
3243  libs[5] = LoadLibraryA("gdi32.dll");
3244 #endif
3245 
3246  jit->lua_dll = libs[1];
3247  jit->kernel32_dll = libs[3];
3248 
3249 #else /* !_WIN32 */
3250  size_t sz = sizeof(void*) * 5;
3251  void** libs = lua_newuserdata(L, sz);
3252  memset(libs, 0, sz);
3253 
3254  libs[0] = LoadLibraryA(NULL); /* exe */
3255  libs[1] = LoadLibraryA("libc.so");
3256 #ifdef __GNUC__
3257  libs[2] = LoadLibraryA("libgcc.so");
3258 #endif
3259  libs[3] = LoadLibraryA("libm.so");
3260  libs[4] = LoadLibraryA("libdl.so");
3261 #endif
3262 
3263  lua_newtable(L);
3264  lua_setuservalue(L, -2);
3265 
3267  lua_setmetatable(L, -2);
3268 
3269  lua_setfield(L, 1, "C");
3270  }
3271 
3272  /* setup builtin types */
3273  {
3274  complex_double* pc;
3275  struct {char ch; uint16_t v;} a16;
3276  struct {char ch; uint32_t v;} a32;
3277  struct {char ch; uint64_t v;} a64;
3278  struct {char ch; float v;} af;
3279  struct {char ch; double v;} ad;
3280 #ifdef HAVE_LONG_DOUBLE
3281  struct {char ch; long double v;} ald;
3282 #endif
3283  struct {char ch; uintptr_t v;} aptr;
3284  struct ctype ct;
3285  struct {char ch; complex_float v;} cf;
3286  struct {char ch; complex_double v;} cd;
3287 #if defined HAVE_LONG_DOUBLE && defined HAVE_COMPLEX
3288  struct {char ch; complex long double v;} cld;
3289 #endif
3290 
3291  push_builtin(L, &ct, "void", VOID_TYPE, 0, 0, 0);
3292  push_builtin(L, &ct, "bool", BOOL_TYPE, sizeof(_Bool), sizeof(_Bool) -1, 1);
3293  push_builtin(L, &ct, "uint8_t", INT8_TYPE, sizeof(uint8_t), 0, 1);
3294  push_builtin(L, &ct, "int8_t", INT8_TYPE, sizeof(int8_t), 0, 0);
3295  push_builtin(L, &ct, "uint16_t", INT16_TYPE, sizeof(uint16_t), ALIGNOF(a16), 1);
3296  push_builtin(L, &ct, "int16_t", INT16_TYPE, sizeof(int16_t), ALIGNOF(a16), 0);
3297  push_builtin(L, &ct, "uint32_t", INT32_TYPE, sizeof(uint32_t), ALIGNOF(a32), 1);
3298  push_builtin(L, &ct, "int32_t", INT32_TYPE, sizeof(int32_t), ALIGNOF(a32), 0);
3299  push_builtin(L, &ct, "uint64_t", INT64_TYPE, sizeof(uint64_t), ALIGNOF(a64), 1);
3300  push_builtin(L, &ct, "int64_t", INT64_TYPE, sizeof(int64_t), ALIGNOF(a64), 0);
3301  push_builtin(L, &ct, "float", FLOAT_TYPE, sizeof(float), ALIGNOF(af), 0);
3302  push_builtin(L, &ct, "double", DOUBLE_TYPE, sizeof(double), ALIGNOF(ad), 0);
3303 #ifdef HAVE_LONG_DOUBLE
3304  push_builtin(L, &ct, "long double", LONG_DOUBLE_TYPE, sizeof(long double), ALIGNOF(ald), 0);
3305 #else
3306  push_builtin_undef(L, &ct, "long double", LONG_DOUBLE_TYPE);
3307 #endif
3308  push_builtin(L, &ct, "uintptr_t", INTPTR_TYPE, sizeof(uintptr_t), ALIGNOF(aptr), 1);
3309  push_builtin(L, &ct, "intptr_t", INTPTR_TYPE, sizeof(uintptr_t), ALIGNOF(aptr), 0);
3310  push_builtin(L, &ct, "complex float", COMPLEX_FLOAT_TYPE, sizeof(complex_float), ALIGNOF(cf), 0);
3311  push_builtin(L, &ct, "complex double", COMPLEX_DOUBLE_TYPE, sizeof(complex_double), ALIGNOF(cd), 0);
3312 #if defined HAVE_LONG_DOUBLE && defined HAVE_COMPLEX
3313  push_builtin(L, &ct, "complex long double", COMPLEX_LONG_DOUBLE_TYPE, sizeof(complex long double), ALIGNOF(cld), 0);
3314 #else
3315  push_builtin_undef(L, &ct, "complex long double", COMPLEX_LONG_DOUBLE_TYPE);
3316 #endif
3317 
3318  /* add NULL and i constants */
3320 
3321  memset(&ct, 0, sizeof(ct));
3322  ct.type = VOID_TYPE;
3323  ct.is_defined = 1;
3324  ct.pointers = 1;
3325  ct.is_null = 1;
3326 
3327  /* add ffi.C.NULL */
3328  push_cdata(L, 0, &ct);
3329  lua_setfield(L, -2, "NULL");
3330 
3331  /* add ffi.NULL */
3332  push_cdata(L, 0, &ct);
3333  lua_setfield(L, 1, "NULL");
3334 
3335  memset(&ct, 0, sizeof(ct));
3337  ct.is_defined = 1;
3338  ct.base_size = sizeof(complex_double);
3339  pc = (complex_double*) push_cdata(L, 0, &ct);
3340 #ifdef HAVE_COMPLEX
3341  *pc = 1i;
3342 #else
3343  pc->real = 0;
3344  pc->imag = 1;
3345 #endif
3346  lua_setfield(L, -2, "i");
3347 
3348  lua_pop(L, 1); /* constants */
3349  }
3350 
3351  assert(lua_gettop(L) == 1);
3352 
3353  /* setup builtin typedefs */
3354  {
3355  add_typedef(L, "bool", "_Bool");
3356 
3357  if (sizeof(uint32_t) == sizeof(size_t)) {
3358  add_typedef(L, "uint32_t", "size_t");
3359  add_typedef(L, "int32_t", "ssize_t");
3360  } else if (sizeof(uint64_t) == sizeof(size_t)) {
3361  add_typedef(L, "uint64_t", "size_t");
3362  add_typedef(L, "int64_t", "ssize_t");
3363  }
3364 
3365  if (sizeof(int32_t) == sizeof(intptr_t)) {
3366  add_typedef(L, "int32_t", "intptr_t");
3367  add_typedef(L, "int32_t", "ptrdiff_t");
3368  } else if (sizeof(int64_t) == sizeof(intptr_t)) {
3369  add_typedef(L, "int64_t", "intptr_t");
3370  add_typedef(L, "int64_t", "ptrdiff_t");
3371  }
3372 
3373  if (sizeof(uint8_t) == sizeof(wchar_t)) {
3374  add_typedef(L, "uint8_t", "wchar_t");
3375  } else if (sizeof(uint16_t) == sizeof(wchar_t)) {
3376  add_typedef(L, "uint16_t", "wchar_t");
3377  } else if (sizeof(uint32_t) == sizeof(wchar_t)) {
3378  add_typedef(L, "uint32_t", "wchar_t");
3379  }
3380 
3381  if (sizeof(va_list) == sizeof(char*)) {
3382  add_typedef(L, "char*", "va_list");
3383  } else {
3384  struct {char ch; va_list v;} av;
3385  lua_pushfstring(L, "struct {char data[%d] __attribute__((align(%d)));}", (int) sizeof(va_list), (int) ALIGNOF(av) + 1);
3386  add_typedef(L, lua_tostring(L, -1), "va_list");
3387  lua_pop(L, 1);
3388  }
3389 
3390  add_typedef(L, "va_list", "__builtin_va_list");
3391  add_typedef(L, "va_list", "__gnuc_va_list");
3392  }
3393 
3394  assert(lua_gettop(L) == 1);
3395 
3396  /* setup ABI params table */
3397  push_upval(L, &abi_key);
3398 
3399 #if defined ARCH_X86 || defined ARCH_ARM
3400  lua_pushboolean(L, 1);
3401  lua_setfield(L, -2, "32bit");
3402 #elif defined ARCH_X64 || defined ARCH_PPC64
3403  lua_pushboolean(L, 1);
3404  lua_setfield(L, -2, "64bit");
3405 #else
3406 # define UNSUPPORTED_ARCH /* error */
3407 #endif
3408 
3409 #if defined ARCH_X86 || defined ARCH_X64 || defined ARCH_ARM || defined ARCH_PPC64
3410  lua_pushboolean(L, 1);
3411  lua_setfield(L, -2, "le");
3412 #else
3413 # define UNSUPPORTED_ARCH /*error*/
3414 #endif
3415 
3416 #if defined ARCH_X86 || defined ARCH_X64 || defined ARCH_PPC64
3417  lua_pushboolean(L, 1);
3418  lua_setfield(L, -2, "fpu");
3419 #elif defined ARCH_ARM
3420  lua_pushboolean(L, 1);
3421  lua_setfield(L, -2, "softfp");
3422 #else
3423 # define UNSUPPORTED_ARCH /*error*/
3424 #endif
3425  lua_pop(L, 1); /* abi tbl */
3426 
3427 
3428  /* GC table - shouldn't pin cdata values */
3429  push_upval(L, &gc_key);
3430  lua_newtable(L);
3431  lua_pushliteral(L, "k");
3432  lua_setfield(L, -2, "__mode");
3433  lua_setmetatable(L, -2);
3434  lua_pop(L, 1); /* gc table */
3435 
3436 
3437  /* ffi.os */
3438 #if defined OS_CE
3439  lua_pushliteral(L, "WindowsCE");
3440 #elif defined OS_WIN
3441  lua_pushliteral(L, "Windows");
3442 #elif defined OS_OSX
3443  lua_pushliteral(L, "OSX");
3444 #elif defined OS_LINUX
3445  lua_pushliteral(L, "Linux");
3446 #elif defined OS_BSD
3447  lua_pushliteral(L, "BSD");
3448 #elif defined OS_POSIX
3449  lua_pushliteral(L, "POSIX");
3450 #else
3451  lua_pushliteral(L, "Other");
3452 #endif
3453  lua_setfield(L, 1, "os");
3454 
3455 
3456  /* ffi.arch */
3457 #if defined ARCH_X86
3458  lua_pushliteral(L, "x86");
3459 #elif defined ARCH_X64
3460  lua_pushliteral(L, "x64");
3461 #elif defined ARCH_ARM
3462  lua_pushliteral(L, "arm");
3463 #elif defined ARCH_PPC64
3464  lua_pushliteral(L, "ppc64");
3465 #else
3466 # define UNSUPPORTED_ARCH /* error */
3467 #endif
3468  lua_setfield(L, 1, "arch");
3469 
3470  assert(lua_gettop(L) == 1);
3471 
3472  return 0;
3473 }
3474 
3475 static void setup_mt(lua_State* L, const luaL_Reg* mt, int upvals)
3476 {
3477  lua_pushboolean(L, 1);
3478  lua_setfield(L, -upvals-2, "__metatable");
3479  luaL_setfuncs(L, mt, upvals);
3480 }
3481 
3482 
3483 #if defined FFI_ENABLE_LUATEX_INTERFACE
3484 int luaopen_ffi(lua_State* L)
3485 {
3486  lua_settop(L, 0);
3487 
3488  lua_newtable(L);
3489  set_upval(L, &niluv_key);
3490 
3491  lua_newtable(L);
3492  setup_mt(L, ctype_mt, 0);
3494 
3495  lua_newtable(L);
3497 
3498  lua_newtable(L);
3499  set_upval(L, &gc_key);
3500 
3501  lua_newtable(L);
3503  push_upval(L, &gc_key);
3504  setup_mt(L, cdata_mt, 2);
3506 
3507  lua_newtable(L);
3508  setup_mt(L, callback_mt, 0);
3510 
3511  lua_newtable(L);
3512  setup_mt(L, cmodule_mt, 0);
3514 
3515  memset(lua_newuserdata(L, sizeof(struct jit)), 0, sizeof(struct jit));
3516  lua_newtable(L);
3517  setup_mt(L, jit_mt, 0);
3518  lua_setmetatable(L, -2);
3519  set_upval(L, &jit_key);
3520 
3521  lua_newtable(L);
3523 
3524  lua_newtable(L);
3525  set_upval(L, &types_key);
3526 
3527  lua_newtable(L);
3529 
3530  lua_newtable(L);
3531  set_upval(L, &asmname_key);
3532 
3533  lua_newtable(L);
3534  set_upval(L, &abi_key);
3535 
3536  lua_pushinteger(L, 1);
3538 
3539  assert(lua_gettop(L) == 0);
3540 
3541  /* ffi table */
3542  lua_newtable(L);
3543  luaL_setfuncs(L, ffi_reg, 0);
3544 
3545  /* setup_upvals(ffi tbl) */
3547  lua_pushvalue(L, 1);
3548  lua_call(L, 1, 0);
3549 
3550  assert(lua_gettop(L) == 1);
3551 
3552  lua_getglobal(L, "tonumber");
3554  lua_pushvalue(L, -1);
3555  lua_setglobal(L, "tonumber");
3556  lua_setfield(L, -2, "number"); /* ffi.number */
3557 
3558  lua_getglobal(L, "type");
3559  lua_pushcclosure(L, &ffi_type, 1);
3560  lua_pushvalue(L, -1);
3561  lua_setglobal(L, "type");
3562  lua_setfield(L, -2, "type"); /* ffi.type */
3563  return 1;
3564 }
3565 #else
3566 /* This is a stub for OS/ARCH that at this moment don't support ffi */
3568 {
3569  int k ;
3570 
3571  k = luaL_dostring(L, "local info = [[\n"
3572 "The ffi module is available for:\n"
3573 "\n"
3574 " archictures : ARCH_X86 and ARCH_X64,\n"
3575 " operating systems : OS_CE, OS_WIN, OS_LINUX, OS_BSD and OS_POSIX\n"
3576 "\n"
3577 "The ARM processor is currently not supported. There are subtle\n"
3578 "differences between this module and the one in luajitTeX \n"
3579 "and we hope to be in sync around TeXLive 2022.\n"
3580 "Different OS can have different interfaces,\n"
3581 "for instance OS_WIN has not 'complex.h'. If you want portable\n"
3582 "code, stick to the most common concepts.\n"
3583 "]]\n"
3584 "\n"
3585 "local function stub()\n"
3586 " texio.write_nl(info)\n"
3587 "end\n"
3588 "\n"
3589 "return {\n"
3590 " fill = stub,\n"
3591 " cast = stub,\n"
3592 " offsetof = stub,\n"
3593 " copy = stub,\n"
3594 " string = stub,\n"
3595 " abi = stub,\n"
3596 " cdef = stub,\n"
3597 " typeof = stub,\n"
3598 " sizeof = stub,\n"
3599 " type = stub,\n"
3600 " number = stub,\n"
3601 " gc = stub,\n"
3602 " metatype = stub,\n"
3603 " errno = stub,\n"
3604 " debug = stub,\n"
3605 " os = '' ,\n"
3606 " arch = '' ,\n"
3607 " NULL = nil ,\n"
3608 " alignof = stub,\n"
3609 " new = stub,\n"
3610 " u64 = stub,\n"
3611 " i64 = stub,\n"
3612 " istype = stub,\n"
3613 " load = stub,\n"
3614 " C = nil ,\n"
3615 "}");
3616 
3617  return k==0 ? 1 : k;
3618 }
3619 #endif
int ptrdiff_t
Definition: CPAL.d:3845
static int pf(int(*writeFunc)(void *stream, const char *data, int size), void *stream, const char *fmt,...)
Definition: HTMLGen.cc:254
rp
Definition: action.c:992
#define type(a)
Definition: aptex-macros.h:171
#define name
#define _Bool
Definition: autosp.c:99
void push_func_ref(lua_State *L, cfunction func)
Definition: call.c:237
void free_code(struct jit *jit, lua_State *L, cfunction func)
Definition: call.c:243
void compile_function(lua_State *L, cfunction func, int ct_usr, const struct ctype *ct)
Definition: call_arm.h:830
cfunction compile_callback(lua_State *L, int fidx, int ct_usr, const struct ctype *ct)
Definition: call_arm.h:630
void compile_globals(struct jit *jit, lua_State *L)
Definition: call_arm.h:625
int x86_return_size(lua_State *L, int usr, const struct ctype *ct)
Definition: call_x64.h:244
#define n
Definition: t4ht.c:1290
#define b
Definition: jpegint.h:372
void * check_cdata(lua_State *L, int idx, struct ctype *ct)
Definition: ctype.c:266
void * to_cdata(lua_State *L, int idx, struct ctype *ct)
Definition: ctype.c:230
struct ctype * push_ctype(lua_State *L, int ct_usr, const struct ctype *ct)
Definition: ctype.c:86
void check_ctype(lua_State *L, int idx, struct ctype *ct)
Definition: ctype.c:196
void * push_cdata(lua_State *L, int ct_usr, const struct ctype *ct)
Definition: ctype.c:136
size_t ctype_size(lua_State *L, const struct ctype *ct)
Definition: ctype.c:116
#define free(a)
Definition: decNumber.cpp:310
#define dummy
Definition: devnag.c:313
long pc
Definition: disdvi.c:114
int dummy
Definition: dummy.c:29
int v
Definition: dviconv.c:10
double real
Definition: dvips.h:66
int strcmp()
Definition: coll.cpp:143
struct rect data
Definition: dvipdfm.c:64
static int cdata_index(lua_State *L)
Definition: ffi.c:1714
#define COMPARE_BINOP(OPSTR, OP, OPC)
Definition: ffi.c:2352
#define DIVC(l, r, s)
Definition: ffi.c:2329
static void get_variable_array_size(lua_State *L, int idx, struct ctype *ct)
Definition: ffi.c:1162
int32_t check_int32(lua_State *L, int idx)
Definition: ffi.c:274
uint32_t check_uint32(lua_State *L, int idx)
Definition: ffi.c:277
int functions_key
Definition: ffi.c:25
static void * check_pointer(lua_State *L, int idx, struct ctype *ct)
Definition: ffi.c:516
static complex_double check_complex(lua_State *L, int idx, void *p, struct ctype *ct)
Definition: ffi.c:1916
static int should_pack(lua_State *L, int ct_usr, struct ctype *ct, int idx)
Definition: ffi.c:1192
static const luaL_Reg ffi_reg[]
Definition: ffi.c:3122
static int cdata_ipairs(lua_State *L)
Definition: ffi.c:2138
static void add_typedef(lua_State *L, const char *from, const char *to)
Definition: ffi.c:3178
static int cdata_eq(lua_State *L)
Definition: ffi.c:2431
int callback_mt_key
Definition: ffi.c:19
static const char * etype_tostring(int type)
Definition: ffi.c:2461
static int cdata_mul(lua_State *L)
Definition: ffi.c:2340
static uint64_t cast_uint64(lua_State *L, int idx, int is_cast)
Definition: ffi.c:271
static const luaL_Reg jit_mt[]
Definition: ffi.c:3117
static int cdata_le(lua_State *L)
Definition: ffi.c:2450
static int ffi_number(lua_State *L)
Definition: ffi.c:2635
static int cdata_tointeger(lua_State *L, int idx, ptrdiff_t *val)
Definition: ffi.c:126
static const luaL_Reg cmodule_mt[]
Definition: ffi.c:3111
static void push_builtin(lua_State *L, struct ctype *ct, const char *name, int type, int size, int align, int is_unsigned)
Definition: ffi.c:3146
static int is_same_type(lua_State *L, int usr1, int usr2, const struct ctype *t1, const struct ctype *t2)
Definition: ffi.c:579
int cmodule_mt_key
Definition: ffi.c:20
void set_upval(lua_State *L, int *key)
Definition: ffi.c:37
int32_t check_enum(lua_State *L, int idx, int to_usr, const struct ctype *to_ct)
Definition: ffi.c:477
static int ffi_string(lua_State *L)
Definition: ffi.c:2653
static int ffi_debug(lua_State *L)
Definition: ffi.c:3008
static int cdata_mod(lua_State *L)
Definition: ffi.c:2346
float check_float(lua_State *L, int idx)
Definition: ffi.c:289
static const luaL_Reg ctype_mt[]
Definition: ffi.c:3104
static int jit_gc(lua_State *L)
Definition: ffi.c:2995
#define DIV(l, r, s)
Definition: ffi.c:2323
int equals_upval(lua_State *L, int idx, int *key)
Definition: ffi.c:44
static int is_void_ptr(const struct ctype *ct)
Definition: ffi.c:573
static int ctype_new(lua_State *L)
Definition: ffi.c:1309
int cdata_mt_key
Definition: ffi.c:18
void unpack_varargs_stack_skip(lua_State *L, int first, int last, int ints_to_skip, int floats_to_skip, char *to)
Definition: ffi.c:419
#define TO_NUMBER(TYPE, ALLOW_POINTERS, LUA_TONUMBER)
Definition: ffi.c:194
static int cdata_div(lua_State *L)
Definition: ffi.c:2343
double check_double(lua_State *L, int idx)
Definition: ffi.c:286
static void setmintop(lua_State *L, int idx)
Definition: ffi.c:1154
static int ffi_errno(lua_State *L)
Definition: ffi.c:2603
static int cdata_set(lua_State *L)
Definition: ffi.c:1480
static int do64(lua_State *L, int is_unsigned)
Definition: ffi.c:3036
static int ffi_abi(lua_State *L)
Definition: ffi.c:2732
#define POW(l, r, s)
Definition: ffi.c:2325
static int cmodule_newindex(lua_State *L)
Definition: ffi.c:2971
int ctype_mt_key
Definition: ffi.c:17
static int cdata_free(lua_State *L)
Definition: ffi.c:1461
static int user_mt_key
Definition: ffi.c:1554
static void * lookup_global(lua_State *L, int modidx, int nameidx, const char **pname, struct ctype *ct)
Definition: ffi.c:2797
static int ffi_fill(lua_State *L)
Definition: ffi.c:2713
static int64_t cast_int64(lua_State *L, int idx, int is_cast)
Definition: ffi.c:268
static int cdata_pow(lua_State *L)
Definition: ffi.c:2349
void unpack_varargs_stack(lua_State *L, int first, int last, char *to)
Definition: ffi.c:410
static int ctype_call(lua_State *L)
Definition: ffi.c:1312
int next_unnamed_key
Definition: ffi.c:27
static int ffi_load(lua_State *L)
Definition: ffi.c:2742
int types_key
Definition: ffi.c:22
static int is_scalar(struct ctype *ct)
Definition: ffi.c:1183
static int cdata_call(lua_State *L)
Definition: ffi.c:1507
void push_upval(lua_State *L, int *key)
Definition: ffi.c:31
static int cdata_tostring(lua_State *L)
Definition: ffi.c:2539
static int ffi_type(lua_State *L)
Definition: ffi.c:2617
static int cdata_len(lua_State *L)
Definition: ffi.c:2104
static int ffi_typeof(lua_State *L)
Definition: ffi.c:1146
#define MODC(l, r, s)
Definition: ffi.c:2330
static void push_builtin_undef(lua_State *L, struct ctype *ct, const char *name, int type)
Definition: ffi.c:3167
static void print_type(lua_State *L, const struct ctype *ct)
Definition: ffi.c:2484
static int ffi_gc(lua_State *L)
Definition: ffi.c:1597
static void set_array(lua_State *L, int idx, void *to, int to_usr, const struct ctype *tt, int check_pointers)
Definition: ffi.c:805
#define POWC(l, r, s)
Definition: ffi.c:2331
static const luaL_Reg callback_mt[]
Definition: ffi.c:3099
static int ffi_u64(lua_State *L)
Definition: ffi.c:3071
#define LEC(l, r)
Definition: ffi.c:2428
complex_float check_complex_float(lua_State *L, int idx)
Definition: ffi.c:343
static int ffi_new(lua_State *L)
Definition: ffi.c:1303
static int callback_free(lua_State *L)
Definition: ffi.c:1453
static int cdata_add(lua_State *L)
Definition: ffi.c:2155
#define LE(l, r)
Definition: ffi.c:2420
#define DEBUG_TOSTRING
Definition: ffi.c:14
static int ffi_metatype(lua_State *L)
Definition: ffi.c:1556
static int cdata_concat(lua_State *L)
Definition: ffi.c:2087
static int ffi_cast(lua_State *L)
Definition: ffi.c:1306
static int ffi_sizeof(lua_State *L)
Definition: ffi.c:1336
int gc_key
Definition: ffi.c:23
#define MOD(l, r, s)
Definition: ffi.c:2324
cfunction check_typed_cfunction(lua_State *L, int idx, int to_usr, const struct ctype *tt)
Definition: ffi.c:800
static ptrdiff_t get_member(lua_State *L, int usr, const struct ctype *ct, struct ctype *mt)
Definition: ffi.c:900
int constants_key
Definition: ffi.c:21
static int rank(const struct ctype *ct)
Definition: ffi.c:1948
#define NUMBER_ONLY_BINOP(OPSTR, DO_NORMAL, DO_COMPLEX)
Definition: ffi.c:2281
static size_t unpack_vararg(lua_State *L, int i, char *to)
Definition: ffi.c:349
static int call_user_op(lua_State *L, const char *opfield, int idx, int ct_usr, const struct ctype *ct)
Definition: ffi.c:1999
void unpack_varargs_int(lua_State *L, int first, int last, int max, char *to)
Definition: ffi.c:449
static void push_complex(lua_State *L, complex_double res, int ct_usr, const struct ctype *ct)
Definition: ffi.c:1972
void unpack_varargs_reg(lua_State *L, int first, int last, char *to)
Definition: ffi.c:462
static void * find_symbol(lua_State *L, int modidx, const char *asmname)
Definition: ffi.c:2777
static const luaL_Reg cdata_mt[]
Definition: ffi.c:3074
static void push_number(lua_State *L, int64_t val, int ct_usr, const struct ctype *ct)
Definition: ffi.c:1988
int callbacks_key
Definition: ffi.c:24
static int get_cfunction_address(lua_State *L, int idx, cfunction *addr)
Definition: ffi.c:673
void * check_typed_pointer(lua_State *L, int idx, int to_usr, const struct ctype *tt)
Definition: ffi.c:611
static int cdata_gc(lua_State *L)
Definition: ffi.c:1431
static void setup_mt(lua_State *L, const luaL_Reg *mt, int upvals)
Definition: ffi.c:3475
int64_t check_int64(lua_State *L, int idx)
Definition: ffi.c:280
int niluv_key
Definition: ffi.c:28
#define LTC(l, r)
Definition: ffi.c:2429
static int setup_upvals(lua_State *L)
Definition: ffi.c:3197
struct jit * get_jit(lua_State *L)
Definition: ffi.c:54
static int ffi_alignof(lua_State *L)
Definition: ffi.c:1345
#define MULC(l, r, s)
Definition: ffi.c:2328
static void set_struct(lua_State *L, int idx, void *to, int to_usr, const struct ctype *tt, int check_pointers)
Definition: ffi.c:933
#define EQC(l, r)
Definition: ffi.c:2423
static int do_new(lua_State *L, int is_cast)
Definition: ffi.c:1218
uintptr_t check_uintptr(lua_State *L, int idx)
Definition: ffi.c:292
static void set_value(lua_State *L, int idx, void *to, int to_usr, const struct ctype *tt, int check_pointers)
Definition: ffi.c:1010
static int64_t check_intptr(lua_State *L, int idx, void *p, struct ctype *ct)
Definition: ffi.c:154
static int cdata_sub(lua_State *L)
Definition: ffi.c:2223
static int ffi_offsetof(lua_State *L)
Definition: ffi.c:1368
static void * userdata_toptr(lua_State *L, int idx)
Definition: ffi.c:102
static int cdata_pairs(lua_State *L)
Definition: ffi.c:2121
#define MUL(l, r, s)
Definition: ffi.c:2322
static ptrdiff_t lookup_cdata_index(lua_State *L, int idx, int ct_usr, struct ctype *ct)
Definition: ffi.c:1616
complex_double check_complex_double(lua_State *L, int idx)
Definition: ffi.c:295
#define LT(l, r)
Definition: ffi.c:2419
static int cdata_unm(lua_State *L)
Definition: ffi.c:2017
static int ffi_i64(lua_State *L)
Definition: ffi.c:3068
static int ctype_tostring(lua_State *L)
Definition: ffi.c:2521
void unpack_varargs_float(lua_State *L, int first, int last, int max, char *to)
Definition: ffi.c:436
#define EQ(l, r)
Definition: ffi.c:2418
static int cdata_lt(lua_State *L)
Definition: ffi.c:2439
static int cmodule_index(lua_State *L)
Definition: ffi.c:2839
static int type_error(lua_State *L, int idx, const char *to_type, int to_usr, const struct ctype *to_ct)
Definition: ffi.c:64
int asmname_key
Definition: ffi.c:29
int abi_key
Definition: ffi.c:26
uint64_t check_uint64(lua_State *L, int idx)
Definition: ffi.c:283
int luaopen_ffi(lua_State *L)
Definition: ffi.c:3567
static int call_user_binop(lua_State *L, const char *opfield, int lidx, int lusr, const struct ctype *lt, int ridx, int rusr, const struct ctype *rt)
Definition: ffi.c:2049
static int cdata_newindex(lua_State *L)
Definition: ffi.c:1673
int push_user_mt(lua_State *L, int ct_usr, const struct ctype *ct)
Definition: ffi.c:1577
int jit_key
Definition: ffi.c:16
static int ffi_istype(lua_State *L)
Definition: ffi.c:1393
static cfunction check_cfunction(lua_State *L, int idx, int to_usr, const struct ctype *tt, int check_pointers)
Definition: ffi.c:716
static int ffi_copy(lua_State *L)
Definition: ffi.c:2692
#define LIB_FORMAT_2
Definition: ffi.h:192
#define CALLBACK_FUNC_USR_IDX
Definition: ffi.h:424
#define LoadLibraryA(name)
Definition: ffi.h:194
#define LIB_FORMAT_1
Definition: ffi.h:191
void(* cfunction)(void)
Definition: ffi.h:379
#define lua_absindex(L, idx)
Definition: ffi.h:82
#define DEFAULT_ALIGN_MASK
Definition: ffi.h:243
@ STD_CALL
Definition: ffi.h:286
@ FAST_CALL
Definition: ffi.h:287
void parse_argument(lua_State *L, struct parser *P, int ct_usr, struct ctype *type, struct token *name, struct parser *asmname)
Definition: parser.c:1842
#define PTR_ALIGN_MASK
Definition: ffi.h:241
#define IS_CHAR_UNSIGNED
Definition: ffi.h:312
int parse_type(lua_State *L, struct parser *P, struct ctype *type)
Definition: parser.c:1180
#define ALIGNOF(S)
Definition: ffi.h:318
float complex complex_float
Definition: ffi.h:383
#define FreePage(data, size)
Definition: ffi.h:197
static complex_double mk_complex_double(double real, double imag)
Definition: ffi.h:384
#define IS_COMPLEX(type)
Definition: ffi.h:313
static complex_double mk_complex_float(double real, double imag)
Definition: ffi.h:387
int ffi_cdef(lua_State *L)
Definition: parser.c:2189
double complex complex_double
Definition: ffi.h:382
void push_type_name(lua_State *L, int usr, const struct ctype *ct)
Definition: parser.c:1400
#define GetProcAddressA(lib, name)
Definition: ffi.h:195
@ ENUM_TYPE
Definition: ffi.h:305
@ INT64_TYPE
Definition: ffi.h:303
@ COMPLEX_FLOAT_TYPE
Definition: ffi.h:296
@ VOID_TYPE
Definition: ffi.h:292
@ COMPLEX_LONG_DOUBLE_TYPE
Definition: ffi.h:298
@ UNION_TYPE
Definition: ffi.h:306
@ INT32_TYPE
Definition: ffi.h:302
@ BOOL_TYPE
Definition: ffi.h:299
@ INT16_TYPE
Definition: ffi.h:301
@ FUNCTION_TYPE
Definition: ffi.h:308
@ INVALID_TYPE
Definition: ffi.h:291
@ LONG_DOUBLE_TYPE
Definition: ffi.h:295
@ INTPTR_TYPE
Definition: ffi.h:304
@ FLOAT_TYPE
Definition: ffi.h:293
@ COMPLEX_DOUBLE_TYPE
Definition: ffi.h:297
@ FUNCTION_PTR_TYPE
Definition: ffi.h:309
@ DOUBLE_TYPE
Definition: ffi.h:294
@ INT8_TYPE
Definition: ffi.h:300
@ STRUCT_TYPE
Definition: ffi.h:307
mpz_t * f
Definition: gen-fib.c:34
paragraph P
#define c(n)
Definition: gpos-common.c:150
#define d(n)
Definition: gpos-common.c:151
#define memcpy(d, s, n)
Definition: gsftopk.c:64
assert(pcxLoadImage24((char *)((void *) 0), fp, pinfo, hdr))
#define u64
Definition: jbig2arith.cc:23
#define NULL
Definition: ftobjs.h:61
small capitals from c petite p scientific f u
Definition: afcover.h:88
small capitals from c petite p
Definition: afcover.h:72
small capitals from c petite p scientific i
Definition: afcover.h:80
FT_UInt idx
Definition: cffcmap.c:135
@ right
Definition: annotate.c:15
int int double double double char double char * top
Definition: gdfx.h:19
#define PRIu64
Definition: inttypes.h:132
#define PRId64
Definition: inttypes.h:78
signed short int16_t
Definition: stdint.h:76
unsigned short uint16_t
Definition: stdint.h:79
signed __int64 int64_t
Definition: stdint.h:89
unsigned int uint32_t
Definition: stdint.h:80
unsigned int uintptr_t
Definition: stdint.h:119
#define UINT64_C(val)
Definition: stdint.h:238
signed int intptr_t
Definition: stdint.h:118
signed int int32_t
Definition: stdint.h:77
unsigned char uint8_t
Definition: stdint.h:78
unsigned __int64 uint64_t
Definition: stdint.h:90
signed char int8_t
Definition: stdint.h:75
int num
Definition: disdvi.c:621
#define buf
static unsigned long mt[624]
Definition: mt19937ar.c:53
static int ret
Definition: convert.c:72
#define malloc
Definition: alloca.c:91
long sysconf()
#define sprintf
Definition: snprintf.c:44
int low
Definition: combiners.h:904
int high
Definition: combiners.h:904
static luaL_Reg func[]
Definition: except.c:32
#define size_t
Definition: glob.c:257
char * memchr(char *s, int c, size_t n) const
Definition: memchr.c:58
@ err
Definition: mtxline.h:24
dictionary off
Definition: fc-lang.py:226
#define align(x, k)
Definition: obcommon.h:49
int k
Definition: otp-parser.c:70
#define max(a, b)
Definition: pbmto4425.c:11
static int sz
Definition: pdftocairo.cc:114
#define res(length)
Definition: picttoppm.c:287
set set set set set set set set set set set set set set set set set set set set *set set set macro pixldst op &r &cond WK op &r &cond WK op &r &cond WK else op &m &cond &ia op &r &cond WK else op &m &cond &ia elseif elseif else error unsupported base if elseif elseif else error unsupported unaligned pixldst unaligned endm macro pixst base base else pixldst base endif endm macro PF ptr
#define t1
#define t2
real to[600]
Definition: pmxab.c:87
static int32_t last
Definition: ppagelist.c:29
static int32_t first
Definition: ppagelist.c:29
#define B(x, y)
static int size
Definition: ppmlabel.c:24
bstring c int memset(void *s, int c, int length)
struct stream_s stream
Definition: pts_fax.h:93
#define real
Definition: splinefont.h:48
void dasm_free(Dst_DECL)
Definition: dasm_arm.h:110
void dasm_init(Dst_DECL, int maxsection)
Definition: dasm_arm.h:86
void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl)
Definition: dasm_arm.h:123
#define lua_setglobal(L, s)
Definition: minilua.c:189
#define lua_rawlen
Definition: lua.h:363
#define lua_getglobal(L, s)
Definition: lua.h:280
void lua_pushcclosure(lua_State *L, lua_CFunction fn, int n)
Definition: lapi.c:532
int lua_setmetatable(lua_State *L, int objindex)
Definition: lapi.c:846
int lua_getmetatable(lua_State *L, int objindex)
Definition: lapi.c:697
int lua_getuservalue(lua_State *L, int idx)
Definition: lapi.c:724
const char * lua_getupvalue(lua_State *L, int funcindex, int n)
Definition: lapi.c:1222
void * lua_touserdata(lua_State *L, int idx)
Definition: lapi.c:413
int lua_gettop(lua_State *L)
Definition: lapi.c:167
int lua_isstring(lua_State *L, int idx)
Definition: lapi.c:283
void lua_pushinteger(lua_State *L, lua_Integer n)
Definition: lapi.c:466
void lua_concat(lua_State *L, int n)
Definition: lapi.c:1140
void lua_setfield(lua_State *L, int idx, const char *k)
Definition: lapi.c:777
const char * lua_pushfstring(lua_State *L, const char *fmt,...)
Definition: lapi.c:519
void lua_pushnumber(lua_State *L, lua_Number n)
Definition: lapi.c:458
void lua_pushvalue(lua_State *L, int idx)
Definition: lapi.c:237
int lua_rawget(lua_State *L, int idx)
Definition: lapi.c:647
int lua_type(lua_State *L, int idx)
Definition: lapi.c:251
void lua_pushnil(lua_State *L)
Definition: lapi.c:450
void lua_settop(lua_State *L, int idx)
Definition: lapi.c:172
const char * lua_pushstring(lua_State *L, const char *s)
Definition: lapi.c:491
const char * lua_tolstring(lua_State *L, int idx, size_t *len)
Definition: lapi.c:373
int lua_isuserdata(lua_State *L, int idx)
Definition: lapi.c:289
void lua_setuservalue(lua_State *L, int idx)
Definition: lapi.c:886
int lua_error(lua_State *L)
Definition: lapi.c:1114
void lua_createtable(lua_State *L, int narray, int nrec)
Definition: lapi.c:684
int lua_rawgeti(lua_State *L, int idx, lua_Integer n)
Definition: lapi.c:658
void lua_rawset(lua_State *L, int idx)
Definition: lapi.c:801
void lua_rawseti(lua_State *L, int idx, lua_Integer n)
Definition: lapi.c:817
void lua_pushboolean(lua_State *L, int b)
Definition: lapi.c:557
int lua_gettable(lua_State *L, int idx)
Definition: lapi.c:612
void * lua_newuserdata(lua_State *L, size_t size)
Definition: lapi.c:1184
int lua_toboolean(lua_State *L, int idx)
Definition: lapi.c:367
int lua_rawequal(lua_State *L, int index1, int index2)
Definition: lapi.c:295
void lua_pushlightuserdata(lua_State *L, void *p)
Definition: lapi.c:565
const char * lua_pushlstring(lua_State *L, const char *s, size_t len)
Definition: lapi.c:479
int lua_next(lua_State *L, int idx)
Definition: lapi.c:1123
int luaL_argerror(lua_State *L, int arg, const char *extramsg)
Definition: lauxlib.c:164
void luaL_addstring(luaL_Buffer *B, const char *s)
Definition: lauxlib.c:537
int luaL_error(lua_State *L, const char *fmt,...)
Definition: lauxlib.c:223
void luaL_pushresult(luaL_Buffer *B)
Definition: lauxlib.c:542
lua_Integer luaL_checkinteger(lua_State *L, int arg)
Definition: lauxlib.c:430
lua_Number luaL_checknumber(lua_State *L, int arg)
Definition: lauxlib.c:408
void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup)
Definition: lauxlib.c:934
void luaL_addvalue(luaL_Buffer *B)
Definition: lauxlib.c:558
void luaL_checktype(lua_State *L, int arg, int t)
Definition: lauxlib.c:378
void luaL_buffinit(lua_State *L, luaL_Buffer *B)
Definition: lauxlib.c:569
#define luaL_dostring(L, s)
Definition: lauxlib.h:132
#define luaL_getmetatable(L, n)
Definition: lauxlib.h:135
#define luaL_typename(L, i)
Definition: lauxlib.h:127