2 ** FFI C callback handling.
3 ** Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h
18 #include "lj_ccallback.h"
19 #include "lj_target.h"
24 /* -- Target-specific handling of callback slots -------------------------- */
26 #define CALLBACK_MCODE_SIZE (LJ_PAGESIZE * LJ_NUM_CBPAGE)
30 /* Disabled callback support. */
31 #define CALLBACK_SLOT2OFS(slot) (0*(slot))
32 #define CALLBACK_OFS2SLOT(ofs) (0*(ofs))
33 #define CALLBACK_MAX_SLOT 0
35 #elif LJ_TARGET_X86ORX64
37 #define CALLBACK_MCODE_HEAD (LJ_64 ? 8 : 0)
38 #define CALLBACK_MCODE_GROUP (-2+1+2+5+(LJ_64 ? 6 : 5))
40 #define CALLBACK_SLOT2OFS(slot) \
41 (CALLBACK_MCODE_HEAD + CALLBACK_MCODE_GROUP*((slot)/32) + 4*(slot))
43 static MSize
CALLBACK_OFS2SLOT(MSize ofs
)
46 ofs
-= CALLBACK_MCODE_HEAD
;
47 group
= ofs
/ (32*4 + CALLBACK_MCODE_GROUP
);
48 return (ofs
% (32*4 + CALLBACK_MCODE_GROUP
))/4 + group
*32;
51 #define CALLBACK_MAX_SLOT \
52 (((CALLBACK_MCODE_SIZE-CALLBACK_MCODE_HEAD)/(CALLBACK_MCODE_GROUP+4*32))*32)
56 #define CALLBACK_MCODE_HEAD 32
57 #define CALLBACK_SLOT2OFS(slot) (CALLBACK_MCODE_HEAD + 8*(slot))
58 #define CALLBACK_OFS2SLOT(ofs) (((ofs)-CALLBACK_MCODE_HEAD)/8)
59 #define CALLBACK_MAX_SLOT (CALLBACK_OFS2SLOT(CALLBACK_MCODE_SIZE))
63 #define CALLBACK_MCODE_HEAD 24
64 #define CALLBACK_SLOT2OFS(slot) (CALLBACK_MCODE_HEAD + 8*(slot))
65 #define CALLBACK_OFS2SLOT(ofs) (((ofs)-CALLBACK_MCODE_HEAD)/8)
66 #define CALLBACK_MAX_SLOT (CALLBACK_OFS2SLOT(CALLBACK_MCODE_SIZE))
70 #define CALLBACK_MCODE_HEAD 24
71 #define CALLBACK_SLOT2OFS(slot) (CALLBACK_MCODE_HEAD + 8*(slot))
72 #define CALLBACK_OFS2SLOT(ofs) (((ofs)-CALLBACK_MCODE_HEAD)/8)
73 #define CALLBACK_MAX_SLOT (CALLBACK_OFS2SLOT(CALLBACK_MCODE_SIZE))
77 /* Missing support for this architecture. */
78 #define CALLBACK_SLOT2OFS(slot) (0*(slot))
79 #define CALLBACK_OFS2SLOT(ofs) (0*(ofs))
80 #define CALLBACK_MAX_SLOT 0
84 /* Convert callback slot number to callback function pointer. */
85 static void *callback_slot2ptr(CTState
*cts
, MSize slot
)
87 return (uint8_t *)cts
->cb
.mcode
+ CALLBACK_SLOT2OFS(slot
);
90 /* Convert callback function pointer to slot number. */
91 MSize
lj_ccallback_ptr2slot(CTState
*cts
, void *p
)
93 uintptr_t ofs
= (uintptr_t)((uint8_t *)p
-(uint8_t *)cts
->cb
.mcode
);
94 if (ofs
< CALLBACK_MCODE_SIZE
) {
95 MSize slot
= CALLBACK_OFS2SLOT((MSize
)ofs
);
96 if (CALLBACK_SLOT2OFS(slot
) == (MSize
)ofs
)
99 return ~0u; /* Not a known callback function pointer. */
102 /* Initialize machine code for callback function pointers. */
104 /* Disabled callback support. */
105 #define callback_mcode_init(g, p) UNUSED(p)
106 #elif LJ_TARGET_X86ORX64
107 static void callback_mcode_init(global_State
*g
, uint8_t *page
)
110 uint8_t *target
= (uint8_t *)(void *)lj_vm_ffi_callback
;
113 *(void **)p
= target
; p
+= 8;
115 for (slot
= 0; slot
< CALLBACK_MAX_SLOT
; slot
++) {
116 /* mov al, slot; jmp group */
117 *p
++ = XI_MOVrib
| RID_EAX
; *p
++ = (uint8_t)slot
;
118 if ((slot
& 31) == 31 || slot
== CALLBACK_MAX_SLOT
-1) {
119 /* push ebp/rbp; mov ah, slot>>8; mov ebp, &g. */
120 *p
++ = XI_PUSH
+ RID_EBP
;
121 *p
++ = XI_MOVrib
| (RID_EAX
+4); *p
++ = (uint8_t)(slot
>> 8);
122 *p
++ = XI_MOVri
| RID_EBP
;
123 *(int32_t *)p
= i32ptr(g
); p
+= 4;
125 /* jmp [rip-pageofs] where lj_vm_ffi_callback is stored. */
126 *p
++ = XI_GROUP5
; *p
++ = XM_OFS0
+ (XOg_JMP
<<3) + RID_EBP
;
127 *(int32_t *)p
= (int32_t)(page
-(p
+4)); p
+= 4;
129 /* jmp lj_vm_ffi_callback. */
130 *p
++ = XI_JMP
; *(int32_t *)p
= target
-(p
+4); p
+= 4;
133 *p
++ = XI_JMPs
; *p
++ = (uint8_t)((2+2)*(31-(slot
&31)) - 2);
136 lua_assert(p
- page
<= CALLBACK_MCODE_SIZE
);
139 static void callback_mcode_init(global_State
*g
, uint32_t *page
)
142 void *target
= (void *)lj_vm_ffi_callback
;
144 /* This must match with the saveregs macro in buildvm_arm.dasc. */
145 *p
++ = ARMI_SUB
|ARMF_D(RID_R12
)|ARMF_N(RID_R12
)|ARMF_M(RID_PC
);
146 *p
++ = ARMI_PUSH
|ARMF_N(RID_SP
)|RSET_RANGE(RID_R4
,RID_R11
+1)|RID2RSET(RID_LR
);
147 *p
++ = ARMI_SUB
|ARMI_K12
|ARMF_D(RID_R12
)|ARMF_N(RID_R12
)|CALLBACK_MCODE_HEAD
;
148 *p
++ = ARMI_STR
|ARMI_LS_P
|ARMI_LS_W
|ARMF_D(RID_R12
)|ARMF_N(RID_SP
)|(CFRAME_SIZE
-4*9);
149 *p
++ = ARMI_LDR
|ARMI_LS_P
|ARMI_LS_U
|ARMF_D(RID_R12
)|ARMF_N(RID_PC
);
150 *p
++ = ARMI_LDR
|ARMI_LS_P
|ARMI_LS_U
|ARMF_D(RID_PC
)|ARMF_N(RID_PC
);
152 *p
++ = u32ptr(target
);
153 for (slot
= 0; slot
< CALLBACK_MAX_SLOT
; slot
++) {
154 *p
++ = ARMI_MOV
|ARMF_D(RID_R12
)|ARMF_M(RID_PC
);
155 *p
= ARMI_B
| ((page
-p
-2) & 0x00ffffffu
);
158 lua_assert(p
- page
<= CALLBACK_MCODE_SIZE
);
161 static void callback_mcode_init(global_State
*g
, uint32_t *page
)
164 void *target
= (void *)lj_vm_ffi_callback
;
166 *p
++ = PPCI_LIS
| PPCF_T(RID_TMP
) | (u32ptr(target
) >> 16);
167 *p
++ = PPCI_LIS
| PPCF_T(RID_R12
) | (u32ptr(g
) >> 16);
168 *p
++ = PPCI_ORI
| PPCF_A(RID_TMP
)|PPCF_T(RID_TMP
) | (u32ptr(target
) & 0xffff);
169 *p
++ = PPCI_ORI
| PPCF_A(RID_R12
)|PPCF_T(RID_R12
) | (u32ptr(g
) & 0xffff);
170 *p
++ = PPCI_MTCTR
| PPCF_T(RID_TMP
);
172 for (slot
= 0; slot
< CALLBACK_MAX_SLOT
; slot
++) {
173 *p
++ = PPCI_LI
| PPCF_T(RID_R11
) | slot
;
174 *p
= PPCI_B
| (((page
-p
) & 0x00ffffffu
) << 2);
177 lua_assert(p
- page
<= CALLBACK_MCODE_SIZE
);
180 static void callback_mcode_init(global_State
*g
, uint32_t *page
)
183 void *target
= (void *)lj_vm_ffi_callback
;
185 *p
++ = MIPSI_SW
| MIPSF_T(RID_R1
)|MIPSF_S(RID_SP
) | 0;
186 *p
++ = MIPSI_LUI
| MIPSF_T(RID_R3
) | (u32ptr(target
) >> 16);
187 *p
++ = MIPSI_LUI
| MIPSF_T(RID_R2
) | (u32ptr(g
) >> 16);
188 *p
++ = MIPSI_ORI
| MIPSF_T(RID_R3
)|MIPSF_S(RID_R3
) |(u32ptr(target
)&0xffff);
189 *p
++ = MIPSI_JR
| MIPSF_S(RID_R3
);
190 *p
++ = MIPSI_ORI
| MIPSF_T(RID_R2
)|MIPSF_S(RID_R2
) | (u32ptr(g
)&0xffff);
191 for (slot
= 0; slot
< CALLBACK_MAX_SLOT
; slot
++) {
192 *p
= MIPSI_B
| ((page
-p
-1) & 0x0000ffffu
);
194 *p
++ = MIPSI_LI
| MIPSF_T(RID_R1
) | slot
;
196 lua_assert(p
- page
<= CALLBACK_MCODE_SIZE
);
199 /* Missing support for this architecture. */
200 #define callback_mcode_init(g, p) UNUSED(p)
203 /* -- Machine code management --------------------------------------------- */
205 #if LJ_TARGET_WINDOWS
207 #define WIN32_LEAN_AND_MEAN
210 #elif LJ_TARGET_POSIX
212 #include <sys/mman.h>
213 #ifndef MAP_ANONYMOUS
214 #define MAP_ANONYMOUS MAP_ANON
219 /* Allocate and initialize area for callback function pointers. */
220 static void callback_mcode_new(CTState
*cts
)
222 size_t sz
= (size_t)CALLBACK_MCODE_SIZE
;
224 if (CALLBACK_MAX_SLOT
== 0)
225 lj_err_caller(cts
->L
, LJ_ERR_FFI_CBACKOV
);
226 #if LJ_TARGET_WINDOWS
227 p
= VirtualAlloc(NULL
, sz
, MEM_RESERVE
|MEM_COMMIT
, PAGE_READWRITE
);
229 lj_err_caller(cts
->L
, LJ_ERR_FFI_CBACKOV
);
230 #elif LJ_TARGET_POSIX
231 p
= mmap(NULL
, sz
, (PROT_READ
|PROT_WRITE
), MAP_PRIVATE
|MAP_ANONYMOUS
,
234 lj_err_caller(cts
->L
, LJ_ERR_FFI_CBACKOV
);
236 /* Fallback allocator. Fails if memory is not executable by default. */
237 p
= lj_mem_new(cts
->L
, sz
);
240 callback_mcode_init(cts
->g
, p
);
241 lj_mcode_sync(p
, (char *)p
+ sz
);
242 #if LJ_TARGET_WINDOWS
245 VirtualProtect(p
, sz
, PAGE_EXECUTE_READ
, &oprot
);
247 #elif LJ_TARGET_POSIX
248 mprotect(p
, sz
, (PROT_READ
|PROT_EXEC
));
252 /* Free area for callback function pointers. */
253 void lj_ccallback_mcode_free(CTState
*cts
)
255 size_t sz
= (size_t)CALLBACK_MCODE_SIZE
;
256 void *p
= cts
->cb
.mcode
;
257 if (p
== NULL
) return;
258 #if LJ_TARGET_WINDOWS
259 VirtualFree(p
, 0, MEM_RELEASE
);
261 #elif LJ_TARGET_POSIX
264 lj_mem_free(cts
->g
, p
, sz
);
268 /* -- C callback entry ---------------------------------------------------- */
270 /* Target-specific handling of register arguments. Similar to lj_ccall.c. */
273 #define CALLBACK_HANDLE_REGARG \
274 if (!isfp) { /* Only non-FP values may be passed in registers. */ \
275 if (n > 1) { /* Anything > 32 bit is passed on the stack. */ \
276 if (!LJ_ABI_WIN) ngpr = maxgpr; /* Prevent reordering. */ \
277 } else if (ngpr + 1 <= maxgpr) { \
278 sp = &cts->cb.gpr[ngpr]; \
284 #elif LJ_TARGET_X64 && LJ_ABI_WIN
286 /* Windows/x64 argument registers are strictly positional (use ngpr). */
287 #define CALLBACK_HANDLE_REGARG \
289 if (ngpr < 4) { sp = &cts->cb.fpr[ngpr++]; nfpr = ngpr; goto done; } \
291 if (ngpr < 4) { sp = &cts->cb.gpr[ngpr++]; goto done; } \
296 #define CALLBACK_HANDLE_REGARG \
298 if (nfpr + n <= CCALL_NARG_FPR) { \
299 sp = &cts->cb.fpr[nfpr]; \
304 if (ngpr + n <= maxgpr) { \
305 sp = &cts->cb.gpr[ngpr]; \
315 #define CALLBACK_HANDLE_REGARG_FP1 UNUSED(isfp);
316 #define CALLBACK_HANDLE_REGARG_FP2
320 #define CALLBACK_HANDLE_REGARG_FP1 \
324 sp = &cts->cb.fpr[fprodd-1]; \
327 } else if (nfpr + 1 <= CCALL_NARG_FPR) { \
328 sp = &cts->cb.fpr[nfpr++]; \
333 if (nfpr + 1 <= CCALL_NARG_FPR) { \
334 sp = &cts->cb.fpr[nfpr++]; \
338 fprodd = 0; /* No reordering after the first FP value is on stack. */ \
341 #define CALLBACK_HANDLE_REGARG_FP2 }
345 #define CALLBACK_HANDLE_REGARG \
346 CALLBACK_HANDLE_REGARG_FP1 \
347 if (n > 1) ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \
348 if (ngpr + n <= maxgpr) { \
349 sp = &cts->cb.gpr[ngpr]; \
352 } CALLBACK_HANDLE_REGARG_FP2
356 #define CALLBACK_HANDLE_REGARG \
358 if (nfpr + 1 <= CCALL_NARG_FPR) { \
359 sp = &cts->cb.fpr[nfpr++]; \
360 cta = ctype_get(cts, CTID_DOUBLE); /* FPRs always hold doubles. */ \
363 } else { /* Try to pass argument in GPRs. */ \
365 lua_assert(ctype_isinteger(cta->info) && n == 2); /* int64_t. */ \
366 ngpr = (ngpr + 1u) & ~1u; /* Align int64_t to regpair. */ \
368 if (ngpr + n <= maxgpr) { \
369 sp = &cts->cb.gpr[ngpr]; \
375 #define CALLBACK_HANDLE_RET \
376 if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \
377 *(double *)dp = *(float *)dp; /* FPRs always hold doubles. */
381 #define CALLBACK_HANDLE_REGARG \
382 if (isfp && nfpr < CCALL_NARG_FPR) { /* Try to pass argument in FPRs. */ \
383 sp = (void *)((uint8_t *)&cts->cb.fpr[nfpr] + ((LJ_BE && n==1) ? 4 : 0)); \
386 } else { /* Try to pass argument in GPRs. */ \
387 nfpr = CCALL_NARG_FPR; \
388 if (n > 1) ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \
389 if (ngpr + n <= maxgpr) { \
390 sp = &cts->cb.gpr[ngpr]; \
396 #define CALLBACK_HANDLE_RET \
397 if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \
398 ((float *)dp)[1] = *(float *)dp;
401 #error "Missing calling convention definitions for this architecture"
404 /* Convert and push callback arguments to Lua stack. */
405 static void callback_conv_args(CTState
*cts
, lua_State
*L
)
408 intptr_t *stack
= cts
->cb
.stack
;
409 MSize slot
= cts
->cb
.slot
;
410 CTypeID id
= 0, rid
, fid
;
413 MSize ngpr
= 0, nsp
= 0, maxgpr
= CCALL_NARG_GPR
;
421 if (slot
< cts
->cb
.sizeid
&& (id
= cts
->cb
.cbid
[slot
]) != 0) {
422 ct
= ctype_get(cts
, id
);
423 rid
= ctype_cid(ct
->info
);
424 fn
= funcV(lj_tab_getint(cts
->miscmap
, (int32_t)slot
));
425 } else { /* Must set up frame first, before throwing the error. */
430 o
->u32
.lo
= LJ_CONT_FFI_CALLBACK
; /* Continuation returns from callback. */
431 o
->u32
.hi
= rid
; /* Return type. x86: +(spadj<<16). */
433 setframe_gc(o
, obj2gco(fn
));
434 setframe_ftsz(o
, (int)((char *)(o
+1) - (char *)L
->base
) + FRAME_CONT
);
435 L
->top
= L
->base
= ++o
;
437 lj_err_caller(cts
->L
, LJ_ERR_FFI_BADCBACK
);
439 setcframe_pc(L
->cframe
, proto_bc(funcproto(fn
))+1);
440 lj_state_checkstack(L
, LUA_MINSTACK
); /* May throw. */
441 o
= L
->base
; /* Might have been reallocated. */
444 /* x86 has several different calling conventions. */
445 switch (ctype_cconv(ct
->info
)) {
446 case CTCC_FASTCALL
: maxgpr
= 2; break;
447 case CTCC_THISCALL
: maxgpr
= 1; break;
448 default: maxgpr
= 0; break;
454 CType
*ctf
= ctype_get(cts
, fid
);
455 if (!ctype_isattrib(ctf
->info
)) {
461 lua_assert(ctype_isfield(ctf
->info
));
462 cta
= ctype_rawchild(cts
, ctf
);
463 isfp
= ctype_isfp(cta
->info
);
464 sz
= (cta
->size
+ CTSIZE_PTR
-1) & ~(CTSIZE_PTR
-1);
465 n
= sz
/ CTSIZE_PTR
; /* Number of GPRs or stack slots needed. */
467 CALLBACK_HANDLE_REGARG
/* Handle register arguments. */
469 /* Otherwise pass argument on stack. */
470 if (CCALL_ALIGN_STACKARG
&& LJ_32
&& sz
== 8)
471 nsp
= (nsp
+ 1) & ~1u; /* Align 64 bit argument on stack. */
476 if (LJ_BE
&& cta
->size
< CTSIZE_PTR
)
477 sp
= (void *)((uint8_t *)sp
+ CTSIZE_PTR
-cta
->size
);
478 lj_cconv_tv_ct(cts
, cta
, 0, o
++, sp
);
484 /* Store stack adjustment for returns from non-cdecl callbacks. */
485 if (ctype_cconv(ct
->info
) != CTCC_CDECL
)
486 (L
->base
-2)->u32
.hi
|= (nsp
<< (16+2));
490 /* Convert Lua object to callback result. */
491 static void callback_conv_result(CTState
*cts
, lua_State
*L
, TValue
*o
)
493 CType
*ctr
= ctype_raw(cts
, (uint16_t)(L
->base
-2)->u32
.hi
);
497 if (!ctype_isvoid(ctr
->info
)) {
498 uint8_t *dp
= (uint8_t *)&cts
->cb
.gpr
[0];
500 if (ctype_isfp(ctr
->info
))
501 dp
= (uint8_t *)&cts
->cb
.fpr
[0];
503 lj_cconv_ct_tv(cts
, ctr
, dp
, o
, 0);
504 #ifdef CALLBACK_HANDLE_RET
507 /* Extend returned integers to (at least) 32 bits. */
508 if (ctype_isinteger_or_bool(ctr
->info
) && ctr
->size
< 4) {
509 if (ctr
->info
& CTF_UNSIGNED
)
510 *(uint32_t *)dp
= ctr
->size
== 1 ? (uint32_t)*(uint8_t *)dp
:
511 (uint32_t)*(uint16_t *)dp
;
513 *(int32_t *)dp
= ctr
->size
== 1 ? (int32_t)*(int8_t *)dp
:
514 (int32_t)*(int16_t *)dp
;
517 if (ctype_isfp(ctr
->info
))
518 cts
->cb
.gpr
[2] = ctr
->size
== sizeof(float) ? 1 : 2;
523 /* Enter callback. */
524 lua_State
* LJ_FASTCALL
lj_ccallback_enter(CTState
*cts
, void *cf
)
526 lua_State
*L
= cts
->L
;
527 global_State
*g
= cts
->g
;
528 lua_assert(L
!= NULL
);
529 if (gcref(g
->jit_L
)) {
530 setstrV(L
, L
->top
++, lj_err_str(L
, LJ_ERR_FFI_BADCBACK
));
531 if (g
->panic
) g
->panic(L
);
534 lj_trace_abort(g
); /* Never record across callback. */
536 cframe_prev(cf
) = L
->cframe
;
538 cframe_errfunc(cf
) = -1;
541 callback_conv_args(cts
, L
);
542 return L
; /* Now call the function on this stack. */
545 /* Leave callback. */
546 void LJ_FASTCALL
lj_ccallback_leave(CTState
*cts
, TValue
*o
)
548 lua_State
*L
= cts
->L
;
550 TValue
*obase
= L
->base
;
551 L
->base
= L
->top
; /* Keep continuation frame for throwing errors. */
553 /* PC of RET* is lost. Point to last line for result conv. errors. */
556 GCproto
*pt
= funcproto(fn
);
557 setcframe_pc(L
->cframe
, proto_bc(pt
)+pt
->sizebc
+1);
560 callback_conv_result(cts
, L
, o
);
561 /* Finally drop C frame and continuation frame. */
562 L
->cframe
= cframe_prev(L
->cframe
);
565 cts
->cb
.slot
= 0; /* Blacklist C function that called the callback. */
568 /* -- C callback management ----------------------------------------------- */
570 /* Get an unused slot in the callback slot table. */
571 static MSize
callback_slot_new(CTState
*cts
, CType
*ct
)
573 CTypeID id
= ctype_typeid(cts
, ct
);
574 CTypeID1
*cbid
= cts
->cb
.cbid
;
576 for (top
= cts
->cb
.topid
; top
< cts
->cb
.sizeid
; top
++)
577 if (LJ_LIKELY(cbid
[top
] == 0))
579 #if CALLBACK_MAX_SLOT
580 if (top
>= CALLBACK_MAX_SLOT
)
582 lj_err_caller(cts
->L
, LJ_ERR_FFI_CBACKOV
);
584 callback_mcode_new(cts
);
585 lj_mem_growvec(cts
->L
, cbid
, cts
->cb
.sizeid
, CALLBACK_MAX_SLOT
, CTypeID1
);
587 memset(cbid
+top
, 0, (cts
->cb
.sizeid
-top
)*sizeof(CTypeID1
));
590 cts
->cb
.topid
= top
+1;
594 /* Check for function pointer and supported argument/result types. */
595 static CType
*callback_checkfunc(CTState
*cts
, CType
*ct
)
598 if (!ctype_isptr(ct
->info
) || (LJ_64
&& ct
->size
!= CTSIZE_PTR
))
600 ct
= ctype_rawchild(cts
, ct
);
601 if (ctype_isfunc(ct
->info
)) {
602 CType
*ctr
= ctype_rawchild(cts
, ct
);
603 CTypeID fid
= ct
->sib
;
604 if (!(ctype_isvoid(ctr
->info
) || ctype_isenum(ctr
->info
) ||
605 ctype_isptr(ctr
->info
) || (ctype_isnum(ctr
->info
) && ctr
->size
<= 8)))
607 if ((ct
->info
& CTF_VARARG
))
610 CType
*ctf
= ctype_get(cts
, fid
);
611 if (!ctype_isattrib(ctf
->info
)) {
613 lua_assert(ctype_isfield(ctf
->info
));
614 cta
= ctype_rawchild(cts
, ctf
);
615 if (!(ctype_isenum(cta
->info
) || ctype_isptr(cta
->info
) ||
616 (ctype_isnum(cta
->info
) && cta
->size
<= 8)) ||
617 ++narg
>= LUA_MINSTACK
-3)
627 /* Create a new callback and return the callback function pointer. */
628 void *lj_ccallback_new(CTState
*cts
, CType
*ct
, GCfunc
*fn
)
630 ct
= callback_checkfunc(cts
, ct
);
632 MSize slot
= callback_slot_new(cts
, ct
);
633 GCtab
*t
= cts
->miscmap
;
634 setfuncV(cts
->L
, lj_tab_setint(cts
->L
, t
, (int32_t)slot
), fn
);
635 lj_gc_anybarriert(cts
->L
, t
);
636 return callback_slot2ptr(cts
, slot
);
638 return NULL
; /* Bad conversion. */