aboutsummaryrefslogtreecommitdiff
path: root/src/lib_ffi.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib_ffi.c')
-rw-r--r--src/lib_ffi.c16
1 files changed, 10 insertions, 6 deletions
diff --git a/src/lib_ffi.c b/src/lib_ffi.c
index 85a4e9c1..72128574 100644
--- a/src/lib_ffi.c
+++ b/src/lib_ffi.c
@@ -425,17 +425,21 @@ LJLIB_CF(ffi_cast)
425 return 1; 425 return 1;
426} 426}
427 427
428LJLIB_CF(ffi_string) 428LJLIB_CF(ffi_string) LJLIB_REC(.)
429{ 429{
430 CTState *cts = ctype_cts(L); 430 CTState *cts = ctype_cts(L);
431 TValue *o = lj_lib_checkany(L, 1); 431 TValue *o = lj_lib_checkany(L, 1);
432 size_t sz = (size_t)(CTSize)lj_lib_optint(L, 2, (int32_t)CTSIZE_INVALID);
433 CType *ct = ctype_get(cts, sz==CTSIZE_INVALID ? CTID_P_CVOID : CTID_P_CCHAR);
434 const char *p; 432 const char *p;
433 size_t len;
434 if (o+1 < L->top) {
435 len = (size_t)lj_lib_checkint(L, 2);
436 lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CVOID), (uint8_t *)&p, o, 0);
437 } else {
438 lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CCHAR), (uint8_t *)&p, o, 0);
439 len = strlen(p);
440 }
435 L->top = o+1; /* Make sure this is the last item on the stack. */ 441 L->top = o+1; /* Make sure this is the last item on the stack. */
436 lj_cconv_ct_tv(cts, ct, (uint8_t *)&p, o, 0); 442 setstrV(L, o, lj_str_new(L, p, len));
437 if (sz == CTSIZE_INVALID) sz = strlen(p);
438 setstrV(L, o, lj_str_new(L, p, sz));
439 lj_gc_check(L); 443 lj_gc_check(L);
440 return 1; 444 return 1;
441} 445}