diff options
Diffstat (limited to 'src/lib_ffi.c')
-rw-r--r-- | src/lib_ffi.c | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/src/lib_ffi.c b/src/lib_ffi.c index 2a674b88..f4097041 100644 --- a/src/lib_ffi.c +++ b/src/lib_ffi.c | |||
@@ -35,7 +35,7 @@ | |||
35 | /* -- C type checks ------------------------------------------------------- */ | 35 | /* -- C type checks ------------------------------------------------------- */ |
36 | 36 | ||
37 | /* Check first argument for a C type and returns its ID. */ | 37 | /* Check first argument for a C type and returns its ID. */ |
38 | static CTypeID ffi_checkctype(lua_State *L, CTState *cts) | 38 | static CTypeID ffi_checkctype(lua_State *L, CTState *cts, TValue *param) |
39 | { | 39 | { |
40 | TValue *o = L->base; | 40 | TValue *o = L->base; |
41 | if (!(o < L->top)) { | 41 | if (!(o < L->top)) { |
@@ -50,6 +50,7 @@ static CTypeID ffi_checkctype(lua_State *L, CTState *cts) | |||
50 | cp.cts = cts; | 50 | cp.cts = cts; |
51 | cp.srcname = strdata(s); | 51 | cp.srcname = strdata(s); |
52 | cp.p = strdata(s); | 52 | cp.p = strdata(s); |
53 | cp.param = param; | ||
53 | cp.mode = CPARSE_MODE_ABSTRACT|CPARSE_MODE_NOIMPLICIT; | 54 | cp.mode = CPARSE_MODE_ABSTRACT|CPARSE_MODE_NOIMPLICIT; |
54 | errcode = lj_cparse(&cp); | 55 | errcode = lj_cparse(&cp); |
55 | if (errcode) lj_err_throw(L, errcode); /* Propagate errors. */ | 56 | if (errcode) lj_err_throw(L, errcode); /* Propagate errors. */ |
@@ -57,6 +58,7 @@ static CTypeID ffi_checkctype(lua_State *L, CTState *cts) | |||
57 | } else { | 58 | } else { |
58 | GCcdata *cd; | 59 | GCcdata *cd; |
59 | if (!tviscdata(o)) goto err_argtype; | 60 | if (!tviscdata(o)) goto err_argtype; |
61 | if (param && param < L->top) lj_err_arg(L, 1, LJ_ERR_FFI_NUMPARAM); | ||
60 | cd = cdataV(o); | 62 | cd = cdataV(o); |
61 | return cd->typeid == CTID_CTYPEID ? *(CTypeID *)cdataptr(cd) : cd->typeid; | 63 | return cd->typeid == CTID_CTYPEID ? *(CTypeID *)cdataptr(cd) : cd->typeid; |
62 | } | 64 | } |
@@ -442,6 +444,7 @@ LJLIB_CF(ffi_cdef) | |||
442 | cp.cts = ctype_cts(L); | 444 | cp.cts = ctype_cts(L); |
443 | cp.srcname = strdata(s); | 445 | cp.srcname = strdata(s); |
444 | cp.p = strdata(s); | 446 | cp.p = strdata(s); |
447 | cp.param = L->base+1; | ||
445 | cp.mode = CPARSE_MODE_MULTI|CPARSE_MODE_DIRECT; | 448 | cp.mode = CPARSE_MODE_MULTI|CPARSE_MODE_DIRECT; |
446 | errcode = lj_cparse(&cp); | 449 | errcode = lj_cparse(&cp); |
447 | if (errcode) lj_err_throw(L, errcode); /* Propagate errors. */ | 450 | if (errcode) lj_err_throw(L, errcode); /* Propagate errors. */ |
@@ -452,7 +455,7 @@ LJLIB_CF(ffi_cdef) | |||
452 | LJLIB_CF(ffi_new) LJLIB_REC(.) | 455 | LJLIB_CF(ffi_new) LJLIB_REC(.) |
453 | { | 456 | { |
454 | CTState *cts = ctype_cts(L); | 457 | CTState *cts = ctype_cts(L); |
455 | CTypeID id = ffi_checkctype(L, cts); | 458 | CTypeID id = ffi_checkctype(L, cts, NULL); |
456 | CType *ct = ctype_raw(cts, id); | 459 | CType *ct = ctype_raw(cts, id); |
457 | CTSize sz; | 460 | CTSize sz; |
458 | CTInfo info = lj_ctype_info(cts, id, &sz); | 461 | CTInfo info = lj_ctype_info(cts, id, &sz); |
@@ -492,7 +495,7 @@ LJLIB_CF(ffi_new) LJLIB_REC(.) | |||
492 | LJLIB_CF(ffi_cast) LJLIB_REC(ffi_new) | 495 | LJLIB_CF(ffi_cast) LJLIB_REC(ffi_new) |
493 | { | 496 | { |
494 | CTState *cts = ctype_cts(L); | 497 | CTState *cts = ctype_cts(L); |
495 | CTypeID id = ffi_checkctype(L, cts); | 498 | CTypeID id = ffi_checkctype(L, cts, NULL); |
496 | CType *d = ctype_raw(cts, id); | 499 | CType *d = ctype_raw(cts, id); |
497 | TValue *o = lj_lib_checkany(L, 2); | 500 | TValue *o = lj_lib_checkany(L, 2); |
498 | L->top = o+1; /* Make sure this is the last item on the stack. */ | 501 | L->top = o+1; /* Make sure this is the last item on the stack. */ |
@@ -510,7 +513,7 @@ LJLIB_CF(ffi_cast) LJLIB_REC(ffi_new) | |||
510 | LJLIB_CF(ffi_typeof) | 513 | LJLIB_CF(ffi_typeof) |
511 | { | 514 | { |
512 | CTState *cts = ctype_cts(L); | 515 | CTState *cts = ctype_cts(L); |
513 | CTypeID id = ffi_checkctype(L, cts); | 516 | CTypeID id = ffi_checkctype(L, cts, L->base+1); |
514 | GCcdata *cd = lj_cdata_new(cts, CTID_CTYPEID, 4); | 517 | GCcdata *cd = lj_cdata_new(cts, CTID_CTYPEID, 4); |
515 | *(CTypeID *)cdataptr(cd) = id; | 518 | *(CTypeID *)cdataptr(cd) = id; |
516 | setcdataV(L, L->top-1, cd); | 519 | setcdataV(L, L->top-1, cd); |
@@ -521,7 +524,7 @@ LJLIB_CF(ffi_typeof) | |||
521 | LJLIB_CF(ffi_istype) LJLIB_REC(ffi_istype) | 524 | LJLIB_CF(ffi_istype) LJLIB_REC(ffi_istype) |
522 | { | 525 | { |
523 | CTState *cts = ctype_cts(L); | 526 | CTState *cts = ctype_cts(L); |
524 | CTypeID id1 = ffi_checkctype(L, cts); | 527 | CTypeID id1 = ffi_checkctype(L, cts, NULL); |
525 | TValue *o = lj_lib_checkany(L, 2); | 528 | TValue *o = lj_lib_checkany(L, 2); |
526 | int b = 0; | 529 | int b = 0; |
527 | if (tviscdata(o)) { | 530 | if (tviscdata(o)) { |
@@ -551,7 +554,7 @@ LJLIB_CF(ffi_istype) LJLIB_REC(ffi_istype) | |||
551 | LJLIB_CF(ffi_sizeof) | 554 | LJLIB_CF(ffi_sizeof) |
552 | { | 555 | { |
553 | CTState *cts = ctype_cts(L); | 556 | CTState *cts = ctype_cts(L); |
554 | CTypeID id = ffi_checkctype(L, cts); | 557 | CTypeID id = ffi_checkctype(L, cts, NULL); |
555 | CTSize sz; | 558 | CTSize sz; |
556 | if (LJ_UNLIKELY(tviscdata(L->base) && cdataisv(cdataV(L->base)))) { | 559 | if (LJ_UNLIKELY(tviscdata(L->base) && cdataisv(cdataV(L->base)))) { |
557 | sz = cdatavlen(cdataV(L->base)); | 560 | sz = cdatavlen(cdataV(L->base)); |
@@ -573,7 +576,7 @@ LJLIB_CF(ffi_sizeof) | |||
573 | LJLIB_CF(ffi_alignof) | 576 | LJLIB_CF(ffi_alignof) |
574 | { | 577 | { |
575 | CTState *cts = ctype_cts(L); | 578 | CTState *cts = ctype_cts(L); |
576 | CTypeID id = ffi_checkctype(L, cts); | 579 | CTypeID id = ffi_checkctype(L, cts, NULL); |
577 | CTSize sz = 0; | 580 | CTSize sz = 0; |
578 | CTInfo info = lj_ctype_info(cts, id, &sz); | 581 | CTInfo info = lj_ctype_info(cts, id, &sz); |
579 | setintV(L->top-1, 1 << ctype_align(info)); | 582 | setintV(L->top-1, 1 << ctype_align(info)); |
@@ -583,7 +586,7 @@ LJLIB_CF(ffi_alignof) | |||
583 | LJLIB_CF(ffi_offsetof) | 586 | LJLIB_CF(ffi_offsetof) |
584 | { | 587 | { |
585 | CTState *cts = ctype_cts(L); | 588 | CTState *cts = ctype_cts(L); |
586 | CTypeID id = ffi_checkctype(L, cts); | 589 | CTypeID id = ffi_checkctype(L, cts, NULL); |
587 | GCstr *name = lj_lib_checkstr(L, 2); | 590 | GCstr *name = lj_lib_checkstr(L, 2); |
588 | CType *ct = lj_ctype_rawref(cts, id); | 591 | CType *ct = lj_ctype_rawref(cts, id); |
589 | CTSize ofs; | 592 | CTSize ofs; |
@@ -700,7 +703,7 @@ LJLIB_PUSH(top-8) LJLIB_SET(!) /* Store reference to miscmap table. */ | |||
700 | LJLIB_CF(ffi_metatype) | 703 | LJLIB_CF(ffi_metatype) |
701 | { | 704 | { |
702 | CTState *cts = ctype_cts(L); | 705 | CTState *cts = ctype_cts(L); |
703 | CTypeID id = ffi_checkctype(L, cts); | 706 | CTypeID id = ffi_checkctype(L, cts, NULL); |
704 | GCtab *mt = lj_lib_checktab(L, 2); | 707 | GCtab *mt = lj_lib_checktab(L, 2); |
705 | GCtab *t = cts->miscmap; | 708 | GCtab *t = cts->miscmap; |
706 | CType *ct = ctype_get(cts, id); /* Only allow raw types. */ | 709 | CType *ct = ctype_get(cts, id); /* Only allow raw types. */ |