You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by zw...@apache.org on 2015/07/29 01:40:00 UTC
[22/62] [abbrv] trafficserver git commit: TS-3783 TS-3030 Add luajit
v2.0.4 as a subtree
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1f27b840/lib/luajit/src/lj_ccallback.c
----------------------------------------------------------------------
diff --git a/lib/luajit/src/lj_ccallback.c b/lib/luajit/src/lj_ccallback.c
new file mode 100644
index 0000000..b210641
--- /dev/null
+++ b/lib/luajit/src/lj_ccallback.c
@@ -0,0 +1,644 @@
+/*
+** FFI C callback handling.
+** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
+*/
+
+#include "lj_obj.h"
+
+#if LJ_HASFFI
+
+#include "lj_gc.h"
+#include "lj_err.h"
+#include "lj_tab.h"
+#include "lj_state.h"
+#include "lj_frame.h"
+#include "lj_ctype.h"
+#include "lj_cconv.h"
+#include "lj_ccall.h"
+#include "lj_ccallback.h"
+#include "lj_target.h"
+#include "lj_mcode.h"
+#include "lj_trace.h"
+#include "lj_vm.h"
+
+/* -- Target-specific handling of callback slots -------------------------- */
+
+#define CALLBACK_MCODE_SIZE (LJ_PAGESIZE * LJ_NUM_CBPAGE)
+
+#if LJ_OS_NOJIT
+
+/* Disabled callback support. */
+#define CALLBACK_SLOT2OFS(slot) (0*(slot))
+#define CALLBACK_OFS2SLOT(ofs) (0*(ofs))
+#define CALLBACK_MAX_SLOT 0
+
+#elif LJ_TARGET_X86ORX64
+
+#define CALLBACK_MCODE_HEAD (LJ_64 ? 8 : 0)
+#define CALLBACK_MCODE_GROUP (-2+1+2+5+(LJ_64 ? 6 : 5))
+
+#define CALLBACK_SLOT2OFS(slot) \
+ (CALLBACK_MCODE_HEAD + CALLBACK_MCODE_GROUP*((slot)/32) + 4*(slot))
+
+static MSize CALLBACK_OFS2SLOT(MSize ofs)
+{
+ MSize group;
+ ofs -= CALLBACK_MCODE_HEAD;
+ group = ofs / (32*4 + CALLBACK_MCODE_GROUP);
+ return (ofs % (32*4 + CALLBACK_MCODE_GROUP))/4 + group*32;
+}
+
+#define CALLBACK_MAX_SLOT \
+ (((CALLBACK_MCODE_SIZE-CALLBACK_MCODE_HEAD)/(CALLBACK_MCODE_GROUP+4*32))*32)
+
+#elif LJ_TARGET_ARM
+
+#define CALLBACK_MCODE_HEAD 32
+#define CALLBACK_SLOT2OFS(slot) (CALLBACK_MCODE_HEAD + 8*(slot))
+#define CALLBACK_OFS2SLOT(ofs) (((ofs)-CALLBACK_MCODE_HEAD)/8)
+#define CALLBACK_MAX_SLOT (CALLBACK_OFS2SLOT(CALLBACK_MCODE_SIZE))
+
+#elif LJ_TARGET_PPC
+
+#define CALLBACK_MCODE_HEAD 24
+#define CALLBACK_SLOT2OFS(slot) (CALLBACK_MCODE_HEAD + 8*(slot))
+#define CALLBACK_OFS2SLOT(ofs) (((ofs)-CALLBACK_MCODE_HEAD)/8)
+#define CALLBACK_MAX_SLOT (CALLBACK_OFS2SLOT(CALLBACK_MCODE_SIZE))
+
+#elif LJ_TARGET_MIPS
+
+#define CALLBACK_MCODE_HEAD 24
+#define CALLBACK_SLOT2OFS(slot) (CALLBACK_MCODE_HEAD + 8*(slot))
+#define CALLBACK_OFS2SLOT(ofs) (((ofs)-CALLBACK_MCODE_HEAD)/8)
+#define CALLBACK_MAX_SLOT (CALLBACK_OFS2SLOT(CALLBACK_MCODE_SIZE))
+
+#else
+
+/* Missing support for this architecture. */
+#define CALLBACK_SLOT2OFS(slot) (0*(slot))
+#define CALLBACK_OFS2SLOT(ofs) (0*(ofs))
+#define CALLBACK_MAX_SLOT 0
+
+#endif
+
+/* Convert callback slot number to callback function pointer. */
+static void *callback_slot2ptr(CTState *cts, MSize slot)
+{
+ return (uint8_t *)cts->cb.mcode + CALLBACK_SLOT2OFS(slot);
+}
+
+/* Convert callback function pointer to slot number. */
+MSize lj_ccallback_ptr2slot(CTState *cts, void *p)
+{
+ uintptr_t ofs = (uintptr_t)((uint8_t *)p -(uint8_t *)cts->cb.mcode);
+ if (ofs < CALLBACK_MCODE_SIZE) {
+ MSize slot = CALLBACK_OFS2SLOT((MSize)ofs);
+ if (CALLBACK_SLOT2OFS(slot) == (MSize)ofs)
+ return slot;
+ }
+ return ~0u; /* Not a known callback function pointer. */
+}
+
+/* Initialize machine code for callback function pointers. */
+#if LJ_OS_NOJIT
+/* Disabled callback support. */
+#define callback_mcode_init(g, p) UNUSED(p)
+#elif LJ_TARGET_X86ORX64
+static void callback_mcode_init(global_State *g, uint8_t *page)
+{
+ uint8_t *p = page;
+ uint8_t *target = (uint8_t *)(void *)lj_vm_ffi_callback;
+ MSize slot;
+#if LJ_64
+ *(void **)p = target; p += 8;
+#endif
+ for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) {
+ /* mov al, slot; jmp group */
+ *p++ = XI_MOVrib | RID_EAX; *p++ = (uint8_t)slot;
+ if ((slot & 31) == 31 || slot == CALLBACK_MAX_SLOT-1) {
+ /* push ebp/rbp; mov ah, slot>>8; mov ebp, &g. */
+ *p++ = XI_PUSH + RID_EBP;
+ *p++ = XI_MOVrib | (RID_EAX+4); *p++ = (uint8_t)(slot >> 8);
+ *p++ = XI_MOVri | RID_EBP;
+ *(int32_t *)p = i32ptr(g); p += 4;
+#if LJ_64
+ /* jmp [rip-pageofs] where lj_vm_ffi_callback is stored. */
+ *p++ = XI_GROUP5; *p++ = XM_OFS0 + (XOg_JMP<<3) + RID_EBP;
+ *(int32_t *)p = (int32_t)(page-(p+4)); p += 4;
+#else
+ /* jmp lj_vm_ffi_callback. */
+ *p++ = XI_JMP; *(int32_t *)p = target-(p+4); p += 4;
+#endif
+ } else {
+ *p++ = XI_JMPs; *p++ = (uint8_t)((2+2)*(31-(slot&31)) - 2);
+ }
+ }
+ lua_assert(p - page <= CALLBACK_MCODE_SIZE);
+}
+#elif LJ_TARGET_ARM
+static void callback_mcode_init(global_State *g, uint32_t *page)
+{
+ uint32_t *p = page;
+ void *target = (void *)lj_vm_ffi_callback;
+ MSize slot;
+ /* This must match with the saveregs macro in buildvm_arm.dasc. */
+ *p++ = ARMI_SUB|ARMF_D(RID_R12)|ARMF_N(RID_R12)|ARMF_M(RID_PC);
+ *p++ = ARMI_PUSH|ARMF_N(RID_SP)|RSET_RANGE(RID_R4,RID_R11+1)|RID2RSET(RID_LR);
+ *p++ = ARMI_SUB|ARMI_K12|ARMF_D(RID_R12)|ARMF_N(RID_R12)|CALLBACK_MCODE_HEAD;
+ *p++ = ARMI_STR|ARMI_LS_P|ARMI_LS_W|ARMF_D(RID_R12)|ARMF_N(RID_SP)|(CFRAME_SIZE-4*9);
+ *p++ = ARMI_LDR|ARMI_LS_P|ARMI_LS_U|ARMF_D(RID_R12)|ARMF_N(RID_PC);
+ *p++ = ARMI_LDR|ARMI_LS_P|ARMI_LS_U|ARMF_D(RID_PC)|ARMF_N(RID_PC);
+ *p++ = u32ptr(g);
+ *p++ = u32ptr(target);
+ for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) {
+ *p++ = ARMI_MOV|ARMF_D(RID_R12)|ARMF_M(RID_PC);
+ *p = ARMI_B | ((page-p-2) & 0x00ffffffu);
+ p++;
+ }
+ lua_assert(p - page <= CALLBACK_MCODE_SIZE);
+}
+#elif LJ_TARGET_PPC
+static void callback_mcode_init(global_State *g, uint32_t *page)
+{
+ uint32_t *p = page;
+ void *target = (void *)lj_vm_ffi_callback;
+ MSize slot;
+ *p++ = PPCI_LIS | PPCF_T(RID_TMP) | (u32ptr(target) >> 16);
+ *p++ = PPCI_LIS | PPCF_T(RID_R12) | (u32ptr(g) >> 16);
+ *p++ = PPCI_ORI | PPCF_A(RID_TMP)|PPCF_T(RID_TMP) | (u32ptr(target) & 0xffff);
+ *p++ = PPCI_ORI | PPCF_A(RID_R12)|PPCF_T(RID_R12) | (u32ptr(g) & 0xffff);
+ *p++ = PPCI_MTCTR | PPCF_T(RID_TMP);
+ *p++ = PPCI_BCTR;
+ for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) {
+ *p++ = PPCI_LI | PPCF_T(RID_R11) | slot;
+ *p = PPCI_B | (((page-p) & 0x00ffffffu) << 2);
+ p++;
+ }
+ lua_assert(p - page <= CALLBACK_MCODE_SIZE);
+}
+#elif LJ_TARGET_MIPS
+static void callback_mcode_init(global_State *g, uint32_t *page)
+{
+ uint32_t *p = page;
+ void *target = (void *)lj_vm_ffi_callback;
+ MSize slot;
+ *p++ = MIPSI_SW | MIPSF_T(RID_R1)|MIPSF_S(RID_SP) | 0;
+ *p++ = MIPSI_LUI | MIPSF_T(RID_R3) | (u32ptr(target) >> 16);
+ *p++ = MIPSI_LUI | MIPSF_T(RID_R2) | (u32ptr(g) >> 16);
+ *p++ = MIPSI_ORI | MIPSF_T(RID_R3)|MIPSF_S(RID_R3) |(u32ptr(target)&0xffff);
+ *p++ = MIPSI_JR | MIPSF_S(RID_R3);
+ *p++ = MIPSI_ORI | MIPSF_T(RID_R2)|MIPSF_S(RID_R2) | (u32ptr(g)&0xffff);
+ for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) {
+ *p = MIPSI_B | ((page-p-1) & 0x0000ffffu);
+ p++;
+ *p++ = MIPSI_LI | MIPSF_T(RID_R1) | slot;
+ }
+ lua_assert(p - page <= CALLBACK_MCODE_SIZE);
+}
+#else
+/* Missing support for this architecture. */
+#define callback_mcode_init(g, p) UNUSED(p)
+#endif
+
+/* -- Machine code management --------------------------------------------- */
+
+#if LJ_TARGET_WINDOWS
+
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+
+#elif LJ_TARGET_POSIX
+
+#include <sys/mman.h>
+#ifndef MAP_ANONYMOUS
+#define MAP_ANONYMOUS MAP_ANON
+#endif
+
+#endif
+
+/* Allocate and initialize area for callback function pointers. */
+static void callback_mcode_new(CTState *cts)
+{
+ size_t sz = (size_t)CALLBACK_MCODE_SIZE;
+ void *p;
+ if (CALLBACK_MAX_SLOT == 0)
+ lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV);
+#if LJ_TARGET_WINDOWS
+ p = VirtualAlloc(NULL, sz, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
+ if (!p)
+ lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV);
+#elif LJ_TARGET_POSIX
+ p = mmap(NULL, sz, (PROT_READ|PROT_WRITE), MAP_PRIVATE|MAP_ANONYMOUS,
+ -1, 0);
+ if (p == MAP_FAILED)
+ lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV);
+#else
+ /* Fallback allocator. Fails if memory is not executable by default. */
+ p = lj_mem_new(cts->L, sz);
+#endif
+ cts->cb.mcode = p;
+ callback_mcode_init(cts->g, p);
+ lj_mcode_sync(p, (char *)p + sz);
+#if LJ_TARGET_WINDOWS
+ {
+ DWORD oprot;
+ VirtualProtect(p, sz, PAGE_EXECUTE_READ, &oprot);
+ }
+#elif LJ_TARGET_POSIX
+ mprotect(p, sz, (PROT_READ|PROT_EXEC));
+#endif
+}
+
+/* Free area for callback function pointers. */
+void lj_ccallback_mcode_free(CTState *cts)
+{
+ size_t sz = (size_t)CALLBACK_MCODE_SIZE;
+ void *p = cts->cb.mcode;
+ if (p == NULL) return;
+#if LJ_TARGET_WINDOWS
+ VirtualFree(p, 0, MEM_RELEASE);
+ UNUSED(sz);
+#elif LJ_TARGET_POSIX
+ munmap(p, sz);
+#else
+ lj_mem_free(cts->g, p, sz);
+#endif
+}
+
+/* -- C callback entry ---------------------------------------------------- */
+
+/* Target-specific handling of register arguments. Similar to lj_ccall.c. */
+#if LJ_TARGET_X86
+
+#define CALLBACK_HANDLE_REGARG \
+ if (!isfp) { /* Only non-FP values may be passed in registers. */ \
+ if (n > 1) { /* Anything > 32 bit is passed on the stack. */ \
+ if (!LJ_ABI_WIN) ngpr = maxgpr; /* Prevent reordering. */ \
+ } else if (ngpr + 1 <= maxgpr) { \
+ sp = &cts->cb.gpr[ngpr]; \
+ ngpr += n; \
+ goto done; \
+ } \
+ }
+
+#elif LJ_TARGET_X64 && LJ_ABI_WIN
+
+/* Windows/x64 argument registers are strictly positional (use ngpr). */
+#define CALLBACK_HANDLE_REGARG \
+ if (isfp) { \
+ if (ngpr < maxgpr) { sp = &cts->cb.fpr[ngpr++]; UNUSED(nfpr); goto done; } \
+ } else { \
+ if (ngpr < maxgpr) { sp = &cts->cb.gpr[ngpr++]; goto done; } \
+ }
+
+#elif LJ_TARGET_X64
+
+#define CALLBACK_HANDLE_REGARG \
+ if (isfp) { \
+ if (nfpr + n <= CCALL_NARG_FPR) { \
+ sp = &cts->cb.fpr[nfpr]; \
+ nfpr += n; \
+ goto done; \
+ } \
+ } else { \
+ if (ngpr + n <= maxgpr) { \
+ sp = &cts->cb.gpr[ngpr]; \
+ ngpr += n; \
+ goto done; \
+ } \
+ }
+
+#elif LJ_TARGET_ARM
+
+#if LJ_ABI_SOFTFP
+
+#define CALLBACK_HANDLE_REGARG_FP1 UNUSED(isfp);
+#define CALLBACK_HANDLE_REGARG_FP2
+
+#else
+
+#define CALLBACK_HANDLE_REGARG_FP1 \
+ if (isfp) { \
+ if (n == 1) { \
+ if (fprodd) { \
+ sp = &cts->cb.fpr[fprodd-1]; \
+ fprodd = 0; \
+ goto done; \
+ } else if (nfpr + 1 <= CCALL_NARG_FPR) { \
+ sp = &cts->cb.fpr[nfpr++]; \
+ fprodd = nfpr; \
+ goto done; \
+ } \
+ } else { \
+ if (nfpr + 1 <= CCALL_NARG_FPR) { \
+ sp = &cts->cb.fpr[nfpr++]; \
+ goto done; \
+ } \
+ } \
+ fprodd = 0; /* No reordering after the first FP value is on stack. */ \
+ } else {
+
+#define CALLBACK_HANDLE_REGARG_FP2 }
+
+#endif
+
+#define CALLBACK_HANDLE_REGARG \
+ CALLBACK_HANDLE_REGARG_FP1 \
+ if (n > 1) ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \
+ if (ngpr + n <= maxgpr) { \
+ sp = &cts->cb.gpr[ngpr]; \
+ ngpr += n; \
+ goto done; \
+ } CALLBACK_HANDLE_REGARG_FP2
+
+#elif LJ_TARGET_PPC
+
+#define CALLBACK_HANDLE_REGARG \
+ if (isfp) { \
+ if (nfpr + 1 <= CCALL_NARG_FPR) { \
+ sp = &cts->cb.fpr[nfpr++]; \
+ cta = ctype_get(cts, CTID_DOUBLE); /* FPRs always hold doubles. */ \
+ goto done; \
+ } \
+ } else { /* Try to pass argument in GPRs. */ \
+ if (n > 1) { \
+ lua_assert(ctype_isinteger(cta->info) && n == 2); /* int64_t. */ \
+ ngpr = (ngpr + 1u) & ~1u; /* Align int64_t to regpair. */ \
+ } \
+ if (ngpr + n <= maxgpr) { \
+ sp = &cts->cb.gpr[ngpr]; \
+ ngpr += n; \
+ goto done; \
+ } \
+ }
+
+#define CALLBACK_HANDLE_RET \
+ if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \
+ *(double *)dp = *(float *)dp; /* FPRs always hold doubles. */
+
+#elif LJ_TARGET_MIPS
+
+#define CALLBACK_HANDLE_REGARG \
+ if (isfp && nfpr < CCALL_NARG_FPR) { /* Try to pass argument in FPRs. */ \
+ sp = (void *)((uint8_t *)&cts->cb.fpr[nfpr] + ((LJ_BE && n==1) ? 4 : 0)); \
+ nfpr++; ngpr += n; \
+ goto done; \
+ } else { /* Try to pass argument in GPRs. */ \
+ nfpr = CCALL_NARG_FPR; \
+ if (n > 1) ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \
+ if (ngpr + n <= maxgpr) { \
+ sp = &cts->cb.gpr[ngpr]; \
+ ngpr += n; \
+ goto done; \
+ } \
+ }
+
+#define CALLBACK_HANDLE_RET \
+ if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \
+ ((float *)dp)[1] = *(float *)dp;
+
+#else
+#error "Missing calling convention definitions for this architecture"
+#endif
+
+/* Convert and push callback arguments to Lua stack. */
+static void callback_conv_args(CTState *cts, lua_State *L)
+{
+ TValue *o = L->top;
+ intptr_t *stack = cts->cb.stack;
+ MSize slot = cts->cb.slot;
+ CTypeID id = 0, rid, fid;
+ int gcsteps = 0;
+ CType *ct;
+ GCfunc *fn;
+ MSize ngpr = 0, nsp = 0, maxgpr = CCALL_NARG_GPR;
+#if CCALL_NARG_FPR
+ MSize nfpr = 0;
+#if LJ_TARGET_ARM
+ MSize fprodd = 0;
+#endif
+#endif
+
+ if (slot < cts->cb.sizeid && (id = cts->cb.cbid[slot]) != 0) {
+ ct = ctype_get(cts, id);
+ rid = ctype_cid(ct->info);
+ fn = funcV(lj_tab_getint(cts->miscmap, (int32_t)slot));
+ } else { /* Must set up frame first, before throwing the error. */
+ ct = NULL;
+ rid = 0;
+ fn = (GCfunc *)L;
+ }
+ o->u32.lo = LJ_CONT_FFI_CALLBACK; /* Continuation returns from callback. */
+ o->u32.hi = rid; /* Return type. x86: +(spadj<<16). */
+ o++;
+ setframe_gc(o, obj2gco(fn));
+ setframe_ftsz(o, (int)((char *)(o+1) - (char *)L->base) + FRAME_CONT);
+ L->top = L->base = ++o;
+ if (!ct)
+ lj_err_caller(cts->L, LJ_ERR_FFI_BADCBACK);
+ if (isluafunc(fn))
+ setcframe_pc(L->cframe, proto_bc(funcproto(fn))+1);
+ lj_state_checkstack(L, LUA_MINSTACK); /* May throw. */
+ o = L->base; /* Might have been reallocated. */
+
+#if LJ_TARGET_X86
+ /* x86 has several different calling conventions. */
+ switch (ctype_cconv(ct->info)) {
+ case CTCC_FASTCALL: maxgpr = 2; break;
+ case CTCC_THISCALL: maxgpr = 1; break;
+ default: maxgpr = 0; break;
+ }
+#endif
+
+ fid = ct->sib;
+ while (fid) {
+ CType *ctf = ctype_get(cts, fid);
+ if (!ctype_isattrib(ctf->info)) {
+ CType *cta;
+ void *sp;
+ CTSize sz;
+ int isfp;
+ MSize n;
+ lua_assert(ctype_isfield(ctf->info));
+ cta = ctype_rawchild(cts, ctf);
+ isfp = ctype_isfp(cta->info);
+ sz = (cta->size + CTSIZE_PTR-1) & ~(CTSIZE_PTR-1);
+ n = sz / CTSIZE_PTR; /* Number of GPRs or stack slots needed. */
+
+ CALLBACK_HANDLE_REGARG /* Handle register arguments. */
+
+ /* Otherwise pass argument on stack. */
+ if (CCALL_ALIGN_STACKARG && LJ_32 && sz == 8)
+ nsp = (nsp + 1) & ~1u; /* Align 64 bit argument on stack. */
+ sp = &stack[nsp];
+ nsp += n;
+
+ done:
+ if (LJ_BE && cta->size < CTSIZE_PTR)
+ sp = (void *)((uint8_t *)sp + CTSIZE_PTR-cta->size);
+ gcsteps += lj_cconv_tv_ct(cts, cta, 0, o++, sp);
+ }
+ fid = ctf->sib;
+ }
+ L->top = o;
+#if LJ_TARGET_X86
+ /* Store stack adjustment for returns from non-cdecl callbacks. */
+ if (ctype_cconv(ct->info) != CTCC_CDECL)
+ (L->base-2)->u32.hi |= (nsp << (16+2));
+#endif
+ while (gcsteps-- > 0)
+ lj_gc_check(L);
+}
+
+/* Convert Lua object to callback result. */
+static void callback_conv_result(CTState *cts, lua_State *L, TValue *o)
+{
+ CType *ctr = ctype_raw(cts, (uint16_t)(L->base-2)->u32.hi);
+#if LJ_TARGET_X86
+ cts->cb.gpr[2] = 0;
+#endif
+ if (!ctype_isvoid(ctr->info)) {
+ uint8_t *dp = (uint8_t *)&cts->cb.gpr[0];
+#if CCALL_NUM_FPR
+ if (ctype_isfp(ctr->info))
+ dp = (uint8_t *)&cts->cb.fpr[0];
+#endif
+ lj_cconv_ct_tv(cts, ctr, dp, o, 0);
+#ifdef CALLBACK_HANDLE_RET
+ CALLBACK_HANDLE_RET
+#endif
+ /* Extend returned integers to (at least) 32 bits. */
+ if (ctype_isinteger_or_bool(ctr->info) && ctr->size < 4) {
+ if (ctr->info & CTF_UNSIGNED)
+ *(uint32_t *)dp = ctr->size == 1 ? (uint32_t)*(uint8_t *)dp :
+ (uint32_t)*(uint16_t *)dp;
+ else
+ *(int32_t *)dp = ctr->size == 1 ? (int32_t)*(int8_t *)dp :
+ (int32_t)*(int16_t *)dp;
+ }
+#if LJ_TARGET_X86
+ if (ctype_isfp(ctr->info))
+ cts->cb.gpr[2] = ctr->size == sizeof(float) ? 1 : 2;
+#endif
+ }
+}
+
+/* Enter callback. */
+lua_State * LJ_FASTCALL lj_ccallback_enter(CTState *cts, void *cf)
+{
+ lua_State *L = cts->L;
+ global_State *g = cts->g;
+ lua_assert(L != NULL);
+ if (gcref(g->jit_L)) {
+ setstrV(L, L->top++, lj_err_str(L, LJ_ERR_FFI_BADCBACK));
+ if (g->panic) g->panic(L);
+ exit(EXIT_FAILURE);
+ }
+ lj_trace_abort(g); /* Never record across callback. */
+ /* Setup C frame. */
+ cframe_prev(cf) = L->cframe;
+ setcframe_L(cf, L);
+ cframe_errfunc(cf) = -1;
+ cframe_nres(cf) = 0;
+ L->cframe = cf;
+ callback_conv_args(cts, L);
+ return L; /* Now call the function on this stack. */
+}
+
+/* Leave callback. */
+void LJ_FASTCALL lj_ccallback_leave(CTState *cts, TValue *o)
+{
+ lua_State *L = cts->L;
+ GCfunc *fn;
+ TValue *obase = L->base;
+ L->base = L->top; /* Keep continuation frame for throwing errors. */
+ if (o >= L->base) {
+ /* PC of RET* is lost. Point to last line for result conv. errors. */
+ fn = curr_func(L);
+ if (isluafunc(fn)) {
+ GCproto *pt = funcproto(fn);
+ setcframe_pc(L->cframe, proto_bc(pt)+pt->sizebc+1);
+ }
+ }
+ callback_conv_result(cts, L, o);
+ /* Finally drop C frame and continuation frame. */
+ L->cframe = cframe_prev(L->cframe);
+ L->top -= 2;
+ L->base = obase;
+ cts->cb.slot = 0; /* Blacklist C function that called the callback. */
+}
+
+/* -- C callback management ----------------------------------------------- */
+
+/* Get an unused slot in the callback slot table. */
+static MSize callback_slot_new(CTState *cts, CType *ct)
+{
+ CTypeID id = ctype_typeid(cts, ct);
+ CTypeID1 *cbid = cts->cb.cbid;
+ MSize top;
+ for (top = cts->cb.topid; top < cts->cb.sizeid; top++)
+ if (LJ_LIKELY(cbid[top] == 0))
+ goto found;
+#if CALLBACK_MAX_SLOT
+ if (top >= CALLBACK_MAX_SLOT)
+#endif
+ lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV);
+ if (!cts->cb.mcode)
+ callback_mcode_new(cts);
+ lj_mem_growvec(cts->L, cbid, cts->cb.sizeid, CALLBACK_MAX_SLOT, CTypeID1);
+ cts->cb.cbid = cbid;
+ memset(cbid+top, 0, (cts->cb.sizeid-top)*sizeof(CTypeID1));
+found:
+ cbid[top] = id;
+ cts->cb.topid = top+1;
+ return top;
+}
+
+/* Check for function pointer and supported argument/result types. */
+static CType *callback_checkfunc(CTState *cts, CType *ct)
+{
+ int narg = 0;
+ if (!ctype_isptr(ct->info) || (LJ_64 && ct->size != CTSIZE_PTR))
+ return NULL;
+ ct = ctype_rawchild(cts, ct);
+ if (ctype_isfunc(ct->info)) {
+ CType *ctr = ctype_rawchild(cts, ct);
+ CTypeID fid = ct->sib;
+ if (!(ctype_isvoid(ctr->info) || ctype_isenum(ctr->info) ||
+ ctype_isptr(ctr->info) || (ctype_isnum(ctr->info) && ctr->size <= 8)))
+ return NULL;
+ if ((ct->info & CTF_VARARG))
+ return NULL;
+ while (fid) {
+ CType *ctf = ctype_get(cts, fid);
+ if (!ctype_isattrib(ctf->info)) {
+ CType *cta;
+ lua_assert(ctype_isfield(ctf->info));
+ cta = ctype_rawchild(cts, ctf);
+ if (!(ctype_isenum(cta->info) || ctype_isptr(cta->info) ||
+ (ctype_isnum(cta->info) && cta->size <= 8)) ||
+ ++narg >= LUA_MINSTACK-3)
+ return NULL;
+ }
+ fid = ctf->sib;
+ }
+ return ct;
+ }
+ return NULL;
+}
+
+/* Create a new callback and return the callback function pointer. */
+void *lj_ccallback_new(CTState *cts, CType *ct, GCfunc *fn)
+{
+ ct = callback_checkfunc(cts, ct);
+ if (ct) {
+ MSize slot = callback_slot_new(cts, ct);
+ GCtab *t = cts->miscmap;
+ setfuncV(cts->L, lj_tab_setint(cts->L, t, (int32_t)slot), fn);
+ lj_gc_anybarriert(cts->L, t);
+ return callback_slot2ptr(cts, slot);
+ }
+ return NULL; /* Bad conversion. */
+}
+
+#endif
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1f27b840/lib/luajit/src/lj_ccallback.h
----------------------------------------------------------------------
diff --git a/lib/luajit/src/lj_ccallback.h b/lib/luajit/src/lj_ccallback.h
new file mode 100644
index 0000000..83dbe04
--- /dev/null
+++ b/lib/luajit/src/lj_ccallback.h
@@ -0,0 +1,25 @@
+/*
+** FFI C callback handling.
+** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
+*/
+
+#ifndef _LJ_CCALLBACK_H
+#define _LJ_CCALLBACK_H
+
+#include "lj_obj.h"
+#include "lj_ctype.h"
+
+#if LJ_HASFFI
+
+/* Really belongs to lj_vm.h. */
+LJ_ASMF void lj_vm_ffi_callback(void);
+
+LJ_FUNC MSize lj_ccallback_ptr2slot(CTState *cts, void *p);
+LJ_FUNCA lua_State * LJ_FASTCALL lj_ccallback_enter(CTState *cts, void *cf);
+LJ_FUNCA void LJ_FASTCALL lj_ccallback_leave(CTState *cts, TValue *o);
+LJ_FUNC void *lj_ccallback_new(CTState *cts, CType *ct, GCfunc *fn);
+LJ_FUNC void lj_ccallback_mcode_free(CTState *cts);
+
+#endif
+
+#endif
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1f27b840/lib/luajit/src/lj_cconv.c
----------------------------------------------------------------------
diff --git a/lib/luajit/src/lj_cconv.c b/lib/luajit/src/lj_cconv.c
new file mode 100644
index 0000000..8a27076
--- /dev/null
+++ b/lib/luajit/src/lj_cconv.c
@@ -0,0 +1,752 @@
+/*
+** C type conversions.
+** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
+*/
+
+#include "lj_obj.h"
+
+#if LJ_HASFFI
+
+#include "lj_err.h"
+#include "lj_tab.h"
+#include "lj_ctype.h"
+#include "lj_cdata.h"
+#include "lj_cconv.h"
+#include "lj_ccallback.h"
+
+/* -- Conversion errors --------------------------------------------------- */
+
+/* Bad conversion. */
+LJ_NORET static void cconv_err_conv(CTState *cts, CType *d, CType *s,
+ CTInfo flags)
+{
+ const char *dst = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, d), NULL));
+ const char *src;
+ if ((flags & CCF_FROMTV))
+ src = lj_obj_typename[1+(ctype_isnum(s->info) ? LUA_TNUMBER :
+ ctype_isarray(s->info) ? LUA_TSTRING : LUA_TNIL)];
+ else
+ src = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, s), NULL));
+ if (CCF_GETARG(flags))
+ lj_err_argv(cts->L, CCF_GETARG(flags), LJ_ERR_FFI_BADCONV, src, dst);
+ else
+ lj_err_callerv(cts->L, LJ_ERR_FFI_BADCONV, src, dst);
+}
+
+/* Bad conversion from TValue. */
+LJ_NORET static void cconv_err_convtv(CTState *cts, CType *d, TValue *o,
+ CTInfo flags)
+{
+ const char *dst = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, d), NULL));
+ const char *src = lj_typename(o);
+ if (CCF_GETARG(flags))
+ lj_err_argv(cts->L, CCF_GETARG(flags), LJ_ERR_FFI_BADCONV, src, dst);
+ else
+ lj_err_callerv(cts->L, LJ_ERR_FFI_BADCONV, src, dst);
+}
+
+/* Initializer overflow. */
+LJ_NORET static void cconv_err_initov(CTState *cts, CType *d)
+{
+ const char *dst = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, d), NULL));
+ lj_err_callerv(cts->L, LJ_ERR_FFI_INITOV, dst);
+}
+
+/* -- C type compatibility checks ----------------------------------------- */
+
+/* Get raw type and qualifiers for a child type. Resolves enums, too. */
+static CType *cconv_childqual(CTState *cts, CType *ct, CTInfo *qual)
+{
+ ct = ctype_child(cts, ct);
+ for (;;) {
+ if (ctype_isattrib(ct->info)) {
+ if (ctype_attrib(ct->info) == CTA_QUAL) *qual |= ct->size;
+ } else if (!ctype_isenum(ct->info)) {
+ break;
+ }
+ ct = ctype_child(cts, ct);
+ }
+ *qual |= (ct->info & CTF_QUAL);
+ return ct;
+}
+
+/* Check for compatible types when converting to a pointer.
+** Note: these checks are more relaxed than what C99 mandates.
+*/
+int lj_cconv_compatptr(CTState *cts, CType *d, CType *s, CTInfo flags)
+{
+ if (!((flags & CCF_CAST) || d == s)) {
+ CTInfo dqual = 0, squal = 0;
+ d = cconv_childqual(cts, d, &dqual);
+ if (!ctype_isstruct(s->info))
+ s = cconv_childqual(cts, s, &squal);
+ if ((flags & CCF_SAME)) {
+ if (dqual != squal)
+ return 0; /* Different qualifiers. */
+ } else if (!(flags & CCF_IGNQUAL)) {
+ if ((dqual & squal) != squal)
+ return 0; /* Discarded qualifiers. */
+ if (ctype_isvoid(d->info) || ctype_isvoid(s->info))
+ return 1; /* Converting to/from void * is always ok. */
+ }
+ if (ctype_type(d->info) != ctype_type(s->info) ||
+ d->size != s->size)
+ return 0; /* Different type or different size. */
+ if (ctype_isnum(d->info)) {
+ if (((d->info ^ s->info) & (CTF_BOOL|CTF_FP)))
+ return 0; /* Different numeric types. */
+ } else if (ctype_ispointer(d->info)) {
+ /* Check child types for compatibility. */
+ return lj_cconv_compatptr(cts, d, s, flags|CCF_SAME);
+ } else if (ctype_isstruct(d->info)) {
+ if (d != s)
+ return 0; /* Must be exact same type for struct/union. */
+ } else if (ctype_isfunc(d->info)) {
+ /* NYI: structural equality of functions. */
+ }
+ }
+ return 1; /* Types are compatible. */
+}
+
+/* -- C type to C type conversion ----------------------------------------- */
+
+/* Convert C type to C type. Caveat: expects to get the raw CType!
+**
+** Note: This is only used by the interpreter and not optimized at all.
+** The JIT compiler will do a much better job specializing for each case.
+*/
+void lj_cconv_ct_ct(CTState *cts, CType *d, CType *s,
+ uint8_t *dp, uint8_t *sp, CTInfo flags)
+{
+ CTSize dsize = d->size, ssize = s->size;
+ CTInfo dinfo = d->info, sinfo = s->info;
+ void *tmpptr;
+
+ lua_assert(!ctype_isenum(dinfo) && !ctype_isenum(sinfo));
+ lua_assert(!ctype_isattrib(dinfo) && !ctype_isattrib(sinfo));
+
+ if (ctype_type(dinfo) > CT_MAYCONVERT || ctype_type(sinfo) > CT_MAYCONVERT)
+ goto err_conv;
+
+ /* Some basic sanity checks. */
+ lua_assert(!ctype_isnum(dinfo) || dsize > 0);
+ lua_assert(!ctype_isnum(sinfo) || ssize > 0);
+ lua_assert(!ctype_isbool(dinfo) || dsize == 1 || dsize == 4);
+ lua_assert(!ctype_isbool(sinfo) || ssize == 1 || ssize == 4);
+ lua_assert(!ctype_isinteger(dinfo) || (1u<<lj_fls(dsize)) == dsize);
+ lua_assert(!ctype_isinteger(sinfo) || (1u<<lj_fls(ssize)) == ssize);
+
+ switch (cconv_idx2(dinfo, sinfo)) {
+ /* Destination is a bool. */
+ case CCX(B, B):
+ /* Source operand is already normalized. */
+ if (dsize == 1) *dp = *sp; else *(int *)dp = *sp;
+ break;
+ case CCX(B, I): {
+ MSize i;
+ uint8_t b = 0;
+ for (i = 0; i < ssize; i++) b |= sp[i];
+ b = (b != 0);
+ if (dsize == 1) *dp = b; else *(int *)dp = b;
+ break;
+ }
+ case CCX(B, F): {
+ uint8_t b;
+ if (ssize == sizeof(double)) b = (*(double *)sp != 0);
+ else if (ssize == sizeof(float)) b = (*(float *)sp != 0);
+ else goto err_conv; /* NYI: long double. */
+ if (dsize == 1) *dp = b; else *(int *)dp = b;
+ break;
+ }
+
+ /* Destination is an integer. */
+ case CCX(I, B):
+ case CCX(I, I):
+ conv_I_I:
+ if (dsize > ssize) { /* Zero-extend or sign-extend LSB. */
+#if LJ_LE
+ uint8_t fill = (!(sinfo & CTF_UNSIGNED) && (sp[ssize-1]&0x80)) ? 0xff : 0;
+ memcpy(dp, sp, ssize);
+ memset(dp + ssize, fill, dsize-ssize);
+#else
+ uint8_t fill = (!(sinfo & CTF_UNSIGNED) && (sp[0]&0x80)) ? 0xff : 0;
+ memset(dp, fill, dsize-ssize);
+ memcpy(dp + (dsize-ssize), sp, ssize);
+#endif
+ } else { /* Copy LSB. */
+#if LJ_LE
+ memcpy(dp, sp, dsize);
+#else
+ memcpy(dp, sp + (ssize-dsize), dsize);
+#endif
+ }
+ break;
+ case CCX(I, F): {
+ double n; /* Always convert via double. */
+ conv_I_F:
+ /* Convert source to double. */
+ if (ssize == sizeof(double)) n = *(double *)sp;
+ else if (ssize == sizeof(float)) n = (double)*(float *)sp;
+ else goto err_conv; /* NYI: long double. */
+ /* Then convert double to integer. */
+ /* The conversion must exactly match the semantics of JIT-compiled code! */
+ if (dsize < 4 || (dsize == 4 && !(dinfo & CTF_UNSIGNED))) {
+ int32_t i = (int32_t)n;
+ if (dsize == 4) *(int32_t *)dp = i;
+ else if (dsize == 2) *(int16_t *)dp = (int16_t)i;
+ else *(int8_t *)dp = (int8_t)i;
+ } else if (dsize == 4) {
+ *(uint32_t *)dp = (uint32_t)n;
+ } else if (dsize == 8) {
+ if (!(dinfo & CTF_UNSIGNED))
+ *(int64_t *)dp = (int64_t)n;
+ else
+ *(uint64_t *)dp = lj_num2u64(n);
+ } else {
+ goto err_conv; /* NYI: conversion to >64 bit integers. */
+ }
+ break;
+ }
+ case CCX(I, C):
+ s = ctype_child(cts, s);
+ sinfo = s->info;
+ ssize = s->size;
+ goto conv_I_F; /* Just convert re. */
+ case CCX(I, P):
+ if (!(flags & CCF_CAST)) goto err_conv;
+ sinfo = CTINFO(CT_NUM, CTF_UNSIGNED);
+ goto conv_I_I;
+ case CCX(I, A):
+ if (!(flags & CCF_CAST)) goto err_conv;
+ sinfo = CTINFO(CT_NUM, CTF_UNSIGNED);
+ ssize = CTSIZE_PTR;
+ tmpptr = sp;
+ sp = (uint8_t *)&tmpptr;
+ goto conv_I_I;
+
+ /* Destination is a floating-point number. */
+ case CCX(F, B):
+ case CCX(F, I): {
+ double n; /* Always convert via double. */
+ conv_F_I:
+ /* First convert source to double. */
+ /* The conversion must exactly match the semantics of JIT-compiled code! */
+ if (ssize < 4 || (ssize == 4 && !(sinfo & CTF_UNSIGNED))) {
+ int32_t i;
+ if (ssize == 4) {
+ i = *(int32_t *)sp;
+ } else if (!(sinfo & CTF_UNSIGNED)) {
+ if (ssize == 2) i = *(int16_t *)sp;
+ else i = *(int8_t *)sp;
+ } else {
+ if (ssize == 2) i = *(uint16_t *)sp;
+ else i = *(uint8_t *)sp;
+ }
+ n = (double)i;
+ } else if (ssize == 4) {
+ n = (double)*(uint32_t *)sp;
+ } else if (ssize == 8) {
+ if (!(sinfo & CTF_UNSIGNED)) n = (double)*(int64_t *)sp;
+ else n = (double)*(uint64_t *)sp;
+ } else {
+ goto err_conv; /* NYI: conversion from >64 bit integers. */
+ }
+ /* Convert double to destination. */
+ if (dsize == sizeof(double)) *(double *)dp = n;
+ else if (dsize == sizeof(float)) *(float *)dp = (float)n;
+ else goto err_conv; /* NYI: long double. */
+ break;
+ }
+ case CCX(F, F): {
+ double n; /* Always convert via double. */
+ conv_F_F:
+ if (ssize == dsize) goto copyval;
+ /* Convert source to double. */
+ if (ssize == sizeof(double)) n = *(double *)sp;
+ else if (ssize == sizeof(float)) n = (double)*(float *)sp;
+ else goto err_conv; /* NYI: long double. */
+ /* Convert double to destination. */
+ if (dsize == sizeof(double)) *(double *)dp = n;
+ else if (dsize == sizeof(float)) *(float *)dp = (float)n;
+ else goto err_conv; /* NYI: long double. */
+ break;
+ }
+ case CCX(F, C):
+ s = ctype_child(cts, s);
+ sinfo = s->info;
+ ssize = s->size;
+ goto conv_F_F; /* Ignore im, and convert from re. */
+
+ /* Destination is a complex number. */
+ case CCX(C, I):
+ d = ctype_child(cts, d);
+ dinfo = d->info;
+ dsize = d->size;
+ memset(dp + dsize, 0, dsize); /* Clear im. */
+ goto conv_F_I; /* Convert to re. */
+ case CCX(C, F):
+ d = ctype_child(cts, d);
+ dinfo = d->info;
+ dsize = d->size;
+ memset(dp + dsize, 0, dsize); /* Clear im. */
+ goto conv_F_F; /* Convert to re. */
+
+ case CCX(C, C):
+ if (dsize != ssize) { /* Different types: convert re/im separately. */
+ CType *dc = ctype_child(cts, d);
+ CType *sc = ctype_child(cts, s);
+ lj_cconv_ct_ct(cts, dc, sc, dp, sp, flags);
+ lj_cconv_ct_ct(cts, dc, sc, dp + dc->size, sp + sc->size, flags);
+ return;
+ }
+ goto copyval; /* Otherwise this is easy. */
+
+ /* Destination is a vector. */
+ case CCX(V, I):
+ case CCX(V, F):
+ case CCX(V, C): {
+ CType *dc = ctype_child(cts, d);
+ CTSize esize;
+ /* First convert the scalar to the first element. */
+ lj_cconv_ct_ct(cts, dc, s, dp, sp, flags);
+ /* Then replicate it to the other elements (splat). */
+ for (sp = dp, esize = dc->size; dsize > esize; dsize -= esize) {
+ dp += esize;
+ memcpy(dp, sp, esize);
+ }
+ break;
+ }
+
+ case CCX(V, V):
+ /* Copy same-sized vectors, even for different lengths/element-types. */
+ if (dsize != ssize) goto err_conv;
+ goto copyval;
+
+ /* Destination is a pointer. */
+ case CCX(P, I):
+ if (!(flags & CCF_CAST)) goto err_conv;
+ dinfo = CTINFO(CT_NUM, CTF_UNSIGNED);
+ goto conv_I_I;
+
+ case CCX(P, F):
+ if (!(flags & CCF_CAST) || !(flags & CCF_FROMTV)) goto err_conv;
+ /* The signed conversion is cheaper. x64 really has 47 bit pointers. */
+ dinfo = CTINFO(CT_NUM, (LJ_64 && dsize == 8) ? 0 : CTF_UNSIGNED);
+ goto conv_I_F;
+
+ case CCX(P, P):
+ if (!lj_cconv_compatptr(cts, d, s, flags)) goto err_conv;
+ cdata_setptr(dp, dsize, cdata_getptr(sp, ssize));
+ break;
+
+ case CCX(P, A):
+ case CCX(P, S):
+ if (!lj_cconv_compatptr(cts, d, s, flags)) goto err_conv;
+ cdata_setptr(dp, dsize, sp);
+ break;
+
+ /* Destination is an array. */
+ case CCX(A, A):
+ if ((flags & CCF_CAST) || (d->info & CTF_VLA) || dsize != ssize ||
+ d->size == CTSIZE_INVALID || !lj_cconv_compatptr(cts, d, s, flags))
+ goto err_conv;
+ goto copyval;
+
+ /* Destination is a struct/union. */
+ case CCX(S, S):
+ if ((flags & CCF_CAST) || (d->info & CTF_VLA) || d != s)
+ goto err_conv; /* Must be exact same type. */
+copyval: /* Copy value. */
+ lua_assert(dsize == ssize);
+ memcpy(dp, sp, dsize);
+ break;
+
+ default:
+ err_conv:
+ cconv_err_conv(cts, d, s, flags);
+ }
+}
+
+/* -- C type to TValue conversion ----------------------------------------- */
+
+/* Convert C type to TValue. Caveat: expects to get the raw CType! */
+int lj_cconv_tv_ct(CTState *cts, CType *s, CTypeID sid,
+ TValue *o, uint8_t *sp)
+{
+ CTInfo sinfo = s->info;
+ if (ctype_isnum(sinfo)) {
+ if (!ctype_isbool(sinfo)) {
+ if (ctype_isinteger(sinfo) && s->size > 4) goto copyval;
+ if (LJ_DUALNUM && ctype_isinteger(sinfo)) {
+ int32_t i;
+ lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT32), s,
+ (uint8_t *)&i, sp, 0);
+ if ((sinfo & CTF_UNSIGNED) && i < 0)
+ setnumV(o, (lua_Number)(uint32_t)i);
+ else
+ setintV(o, i);
+ } else {
+ lj_cconv_ct_ct(cts, ctype_get(cts, CTID_DOUBLE), s,
+ (uint8_t *)&o->n, sp, 0);
+ /* Numbers are NOT canonicalized here! Beware of uninitialized data. */
+ lua_assert(tvisnum(o));
+ }
+ } else {
+ uint32_t b = s->size == 1 ? (*sp != 0) : (*(int *)sp != 0);
+ setboolV(o, b);
+ setboolV(&cts->g->tmptv2, b); /* Remember for trace recorder. */
+ }
+ return 0;
+ } else if (ctype_isrefarray(sinfo) || ctype_isstruct(sinfo)) {
+ /* Create reference. */
+ setcdataV(cts->L, o, lj_cdata_newref(cts, sp, sid));
+ return 1; /* Need GC step. */
+ } else {
+ GCcdata *cd;
+ CTSize sz;
+ copyval: /* Copy value. */
+ sz = s->size;
+ lua_assert(sz != CTSIZE_INVALID);
+ /* Attributes are stripped, qualifiers are kept (but mostly ignored). */
+ cd = lj_cdata_new(cts, ctype_typeid(cts, s), sz);
+ setcdataV(cts->L, o, cd);
+ memcpy(cdataptr(cd), sp, sz);
+ return 1; /* Need GC step. */
+ }
+}
+
+/* Convert bitfield to TValue. */
+int lj_cconv_tv_bf(CTState *cts, CType *s, TValue *o, uint8_t *sp)
+{
+ CTInfo info = s->info;
+ CTSize pos, bsz;
+ uint32_t val;
+ lua_assert(ctype_isbitfield(info));
+ /* NYI: packed bitfields may cause misaligned reads. */
+ switch (ctype_bitcsz(info)) {
+ case 4: val = *(uint32_t *)sp; break;
+ case 2: val = *(uint16_t *)sp; break;
+ case 1: val = *(uint8_t *)sp; break;
+ default: lua_assert(0); val = 0; break;
+ }
+ /* Check if a packed bitfield crosses a container boundary. */
+ pos = ctype_bitpos(info);
+ bsz = ctype_bitbsz(info);
+ lua_assert(pos < 8*ctype_bitcsz(info));
+ lua_assert(bsz > 0 && bsz <= 8*ctype_bitcsz(info));
+ if (pos + bsz > 8*ctype_bitcsz(info))
+ lj_err_caller(cts->L, LJ_ERR_FFI_NYIPACKBIT);
+ if (!(info & CTF_BOOL)) {
+ CTSize shift = 32 - bsz;
+ if (!(info & CTF_UNSIGNED)) {
+ setintV(o, (int32_t)(val << (shift-pos)) >> shift);
+ } else {
+ val = (val << (shift-pos)) >> shift;
+ if (!LJ_DUALNUM || (int32_t)val < 0)
+ setnumV(o, (lua_Number)(uint32_t)val);
+ else
+ setintV(o, (int32_t)val);
+ }
+ } else {
+ lua_assert(bsz == 1);
+ setboolV(o, (val >> pos) & 1);
+ }
+ return 0; /* No GC step needed. */
+}
+
+/* -- TValue to C type conversion ----------------------------------------- */
+
+/* Convert table to array. */
+static void cconv_array_tab(CTState *cts, CType *d,
+ uint8_t *dp, GCtab *t, CTInfo flags)
+{
+ int32_t i;
+ CType *dc = ctype_rawchild(cts, d); /* Array element type. */
+ CTSize size = d->size, esize = dc->size, ofs = 0;
+ for (i = 0; ; i++) {
+ TValue *tv = (TValue *)lj_tab_getint(t, i);
+ if (!tv || tvisnil(tv)) {
+ if (i == 0) continue; /* Try again for 1-based tables. */
+ break; /* Stop at first nil. */
+ }
+ if (ofs >= size)
+ cconv_err_initov(cts, d);
+ lj_cconv_ct_tv(cts, dc, dp + ofs, tv, flags);
+ ofs += esize;
+ }
+ if (size != CTSIZE_INVALID) { /* Only fill up arrays with known size. */
+ if (ofs == esize) { /* Replicate a single element. */
+ for (; ofs < size; ofs += esize) memcpy(dp + ofs, dp, esize);
+ } else { /* Otherwise fill the remainder with zero. */
+ memset(dp + ofs, 0, size - ofs);
+ }
+ }
+}
+
+/* Convert table to sub-struct/union. */
+static void cconv_substruct_tab(CTState *cts, CType *d, uint8_t *dp,
+ GCtab *t, int32_t *ip, CTInfo flags)
+{
+ CTypeID id = d->sib;
+ while (id) {
+ CType *df = ctype_get(cts, id);
+ id = df->sib;
+ if (ctype_isfield(df->info) || ctype_isbitfield(df->info)) {
+ TValue *tv;
+ int32_t i = *ip, iz = i;
+ if (!gcref(df->name)) continue; /* Ignore unnamed fields. */
+ if (i >= 0) {
+ retry:
+ tv = (TValue *)lj_tab_getint(t, i);
+ if (!tv || tvisnil(tv)) {
+ if (i == 0) { i = 1; goto retry; } /* 1-based tables. */
+ if (iz == 0) { *ip = i = -1; goto tryname; } /* Init named fields. */
+ break; /* Stop at first nil. */
+ }
+ *ip = i + 1;
+ } else {
+ tryname:
+ tv = (TValue *)lj_tab_getstr(t, gco2str(gcref(df->name)));
+ if (!tv || tvisnil(tv)) continue;
+ }
+ if (ctype_isfield(df->info))
+ lj_cconv_ct_tv(cts, ctype_rawchild(cts, df), dp+df->size, tv, flags);
+ else
+ lj_cconv_bf_tv(cts, df, dp+df->size, tv);
+ if ((d->info & CTF_UNION)) break;
+ } else if (ctype_isxattrib(df->info, CTA_SUBTYPE)) {
+ cconv_substruct_tab(cts, ctype_rawchild(cts, df),
+ dp+df->size, t, ip, flags);
+ } /* Ignore all other entries in the chain. */
+ }
+}
+
+/* Convert table to struct/union. */
+static void cconv_struct_tab(CTState *cts, CType *d,
+ uint8_t *dp, GCtab *t, CTInfo flags)
+{
+ int32_t i = 0;
+ memset(dp, 0, d->size); /* Much simpler to clear the struct first. */
+ cconv_substruct_tab(cts, d, dp, t, &i, flags);
+}
+
+/* Convert TValue to C type. Caveat: expects to get the raw CType! */
+void lj_cconv_ct_tv(CTState *cts, CType *d,
+ uint8_t *dp, TValue *o, CTInfo flags)
+{
+ CTypeID sid = CTID_P_VOID;
+ CType *s;
+ void *tmpptr;
+ uint8_t tmpbool, *sp = (uint8_t *)&tmpptr;
+ if (LJ_LIKELY(tvisint(o))) {
+ sp = (uint8_t *)&o->i;
+ sid = CTID_INT32;
+ flags |= CCF_FROMTV;
+ } else if (LJ_LIKELY(tvisnum(o))) {
+ sp = (uint8_t *)&o->n;
+ sid = CTID_DOUBLE;
+ flags |= CCF_FROMTV;
+ } else if (tviscdata(o)) {
+ sp = cdataptr(cdataV(o));
+ sid = cdataV(o)->ctypeid;
+ s = ctype_get(cts, sid);
+ if (ctype_isref(s->info)) { /* Resolve reference for value. */
+ lua_assert(s->size == CTSIZE_PTR);
+ sp = *(void **)sp;
+ sid = ctype_cid(s->info);
+ }
+ s = ctype_raw(cts, sid);
+ if (ctype_isfunc(s->info)) {
+ sid = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|sid), CTSIZE_PTR);
+ } else {
+ if (ctype_isenum(s->info)) s = ctype_child(cts, s);
+ goto doconv;
+ }
+ } else if (tvisstr(o)) {
+ GCstr *str = strV(o);
+ if (ctype_isenum(d->info)) { /* Match string against enum constant. */
+ CTSize ofs;
+ CType *cct = lj_ctype_getfield(cts, d, str, &ofs);
+ if (!cct || !ctype_isconstval(cct->info))
+ goto err_conv;
+ lua_assert(d->size == 4);
+ sp = (uint8_t *)&cct->size;
+ sid = ctype_cid(cct->info);
+ } else if (ctype_isrefarray(d->info)) { /* Copy string to array. */
+ CType *dc = ctype_rawchild(cts, d);
+ CTSize sz = str->len+1;
+ if (!ctype_isinteger(dc->info) || dc->size != 1)
+ goto err_conv;
+ if (d->size != 0 && d->size < sz)
+ sz = d->size;
+ memcpy(dp, strdata(str), sz);
+ return;
+ } else { /* Otherwise pass it as a const char[]. */
+ sp = (uint8_t *)strdata(str);
+ sid = CTID_A_CCHAR;
+ flags |= CCF_FROMTV;
+ }
+ } else if (tvistab(o)) {
+ if (ctype_isarray(d->info)) {
+ cconv_array_tab(cts, d, dp, tabV(o), flags);
+ return;
+ } else if (ctype_isstruct(d->info)) {
+ cconv_struct_tab(cts, d, dp, tabV(o), flags);
+ return;
+ } else {
+ goto err_conv;
+ }
+ } else if (tvisbool(o)) {
+ tmpbool = boolV(o);
+ sp = &tmpbool;
+ sid = CTID_BOOL;
+ } else if (tvisnil(o)) {
+ tmpptr = (void *)0;
+ flags |= CCF_FROMTV;
+ } else if (tvisudata(o)) {
+ GCudata *ud = udataV(o);
+ tmpptr = uddata(ud);
+ if (ud->udtype == UDTYPE_IO_FILE)
+ tmpptr = *(void **)tmpptr;
+ } else if (tvislightud(o)) {
+ tmpptr = lightudV(o);
+ } else if (tvisfunc(o)) {
+ void *p = lj_ccallback_new(cts, d, funcV(o));
+ if (p) {
+ *(void **)dp = p;
+ return;
+ }
+ goto err_conv;
+ } else {
+ err_conv:
+ cconv_err_convtv(cts, d, o, flags);
+ }
+ s = ctype_get(cts, sid);
+doconv:
+ if (ctype_isenum(d->info)) d = ctype_child(cts, d);
+ lj_cconv_ct_ct(cts, d, s, dp, sp, flags);
+}
+
+/* Convert TValue to bitfield. */
+void lj_cconv_bf_tv(CTState *cts, CType *d, uint8_t *dp, TValue *o)
+{
+ CTInfo info = d->info;
+ CTSize pos, bsz;
+ uint32_t val, mask;
+ lua_assert(ctype_isbitfield(info));
+ if ((info & CTF_BOOL)) {
+ uint8_t tmpbool;
+ lua_assert(ctype_bitbsz(info) == 1);
+ lj_cconv_ct_tv(cts, ctype_get(cts, CTID_BOOL), &tmpbool, o, 0);
+ val = tmpbool;
+ } else {
+ CTypeID did = (info & CTF_UNSIGNED) ? CTID_UINT32 : CTID_INT32;
+ lj_cconv_ct_tv(cts, ctype_get(cts, did), (uint8_t *)&val, o, 0);
+ }
+ pos = ctype_bitpos(info);
+ bsz = ctype_bitbsz(info);
+ lua_assert(pos < 8*ctype_bitcsz(info));
+ lua_assert(bsz > 0 && bsz <= 8*ctype_bitcsz(info));
+ /* Check if a packed bitfield crosses a container boundary. */
+ if (pos + bsz > 8*ctype_bitcsz(info))
+ lj_err_caller(cts->L, LJ_ERR_FFI_NYIPACKBIT);
+ mask = ((1u << bsz) - 1u) << pos;
+ val = (val << pos) & mask;
+ /* NYI: packed bitfields may cause misaligned reads/writes. */
+ switch (ctype_bitcsz(info)) {
+ case 4: *(uint32_t *)dp = (*(uint32_t *)dp & ~mask) | (uint32_t)val; break;
+ case 2: *(uint16_t *)dp = (*(uint16_t *)dp & ~mask) | (uint16_t)val; break;
+ case 1: *(uint8_t *)dp = (*(uint8_t *)dp & ~mask) | (uint8_t)val; break;
+ default: lua_assert(0); break;
+ }
+}
+
+/* -- Initialize C type with TValues -------------------------------------- */
+
+/* Initialize an array with TValues. */
+static void cconv_array_init(CTState *cts, CType *d, CTSize sz, uint8_t *dp,
+ TValue *o, MSize len)
+{
+ CType *dc = ctype_rawchild(cts, d); /* Array element type. */
+ CTSize ofs, esize = dc->size;
+ MSize i;
+ if (len*esize > sz)
+ cconv_err_initov(cts, d);
+ for (i = 0, ofs = 0; i < len; i++, ofs += esize)
+ lj_cconv_ct_tv(cts, dc, dp + ofs, o + i, 0);
+ if (ofs == esize) { /* Replicate a single element. */
+ for (; ofs < sz; ofs += esize) memcpy(dp + ofs, dp, esize);
+ } else { /* Otherwise fill the remainder with zero. */
+ memset(dp + ofs, 0, sz - ofs);
+ }
+}
+
+/* Initialize a sub-struct/union with TValues. */
+static void cconv_substruct_init(CTState *cts, CType *d, uint8_t *dp,
+ TValue *o, MSize len, MSize *ip)
+{
+ CTypeID id = d->sib;
+ while (id) {
+ CType *df = ctype_get(cts, id);
+ id = df->sib;
+ if (ctype_isfield(df->info) || ctype_isbitfield(df->info)) {
+ MSize i = *ip;
+ if (!gcref(df->name)) continue; /* Ignore unnamed fields. */
+ if (i >= len) break;
+ *ip = i + 1;
+ if (ctype_isfield(df->info))
+ lj_cconv_ct_tv(cts, ctype_rawchild(cts, df), dp+df->size, o + i, 0);
+ else
+ lj_cconv_bf_tv(cts, df, dp+df->size, o + i);
+ if ((d->info & CTF_UNION)) break;
+ } else if (ctype_isxattrib(df->info, CTA_SUBTYPE)) {
+ cconv_substruct_init(cts, ctype_rawchild(cts, df),
+ dp+df->size, o, len, ip);
+ if ((d->info & CTF_UNION)) break;
+ } /* Ignore all other entries in the chain. */
+ }
+}
+
+/* Initialize a struct/union with TValues. */
+static void cconv_struct_init(CTState *cts, CType *d, CTSize sz, uint8_t *dp,
+ TValue *o, MSize len)
+{
+ MSize i = 0;
+ memset(dp, 0, sz); /* Much simpler to clear the struct first. */
+ cconv_substruct_init(cts, d, dp, o, len, &i);
+ if (i < len)
+ cconv_err_initov(cts, d);
+}
+
+/* Check whether to use a multi-value initializer.
+** This is true if an aggregate is to be initialized with a value.
+** Valarrays are treated as values here so ct_tv handles (V|C, I|F).
+*/
+int lj_cconv_multi_init(CTState *cts, CType *d, TValue *o)
+{
+ if (!(ctype_isrefarray(d->info) || ctype_isstruct(d->info)))
+ return 0; /* Destination is not an aggregate. */
+ if (tvistab(o) || (tvisstr(o) && !ctype_isstruct(d->info)))
+ return 0; /* Initializer is not a value. */
+ if (tviscdata(o) && lj_ctype_rawref(cts, cdataV(o)->ctypeid) == d)
+ return 0; /* Source and destination are identical aggregates. */
+ return 1; /* Otherwise the initializer is a value. */
+}
+
+/* Initialize C type with TValues. Caveat: expects to get the raw CType! */
+void lj_cconv_ct_init(CTState *cts, CType *d, CTSize sz,
+ uint8_t *dp, TValue *o, MSize len)
+{
+ if (len == 0)
+ memset(dp, 0, sz);
+ else if (len == 1 && !lj_cconv_multi_init(cts, d, o))
+ lj_cconv_ct_tv(cts, d, dp, o, 0);
+ else if (ctype_isarray(d->info)) /* Also handles valarray init with len>1. */
+ cconv_array_init(cts, d, sz, dp, o, len);
+ else if (ctype_isstruct(d->info))
+ cconv_struct_init(cts, d, sz, dp, o, len);
+ else
+ cconv_err_initov(cts, d);
+}
+
+#endif
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1f27b840/lib/luajit/src/lj_cconv.h
----------------------------------------------------------------------
diff --git a/lib/luajit/src/lj_cconv.h b/lib/luajit/src/lj_cconv.h
new file mode 100644
index 0000000..2bd50ff
--- /dev/null
+++ b/lib/luajit/src/lj_cconv.h
@@ -0,0 +1,70 @@
+/*
+** C type conversions.
+** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
+*/
+
+#ifndef _LJ_CCONV_H
+#define _LJ_CCONV_H
+
+#include "lj_obj.h"
+#include "lj_ctype.h"
+
+#if LJ_HASFFI
+
+/* Compressed C type index. ORDER CCX. */
+enum {
+ CCX_B, /* Bool. */
+ CCX_I, /* Integer. */
+ CCX_F, /* Floating-point number. */
+ CCX_C, /* Complex. */
+ CCX_V, /* Vector. */
+ CCX_P, /* Pointer. */
+ CCX_A, /* Refarray. */
+ CCX_S /* Struct/union. */
+};
+
+/* Convert C type info to compressed C type index. ORDER CT. ORDER CCX. */
+static LJ_AINLINE uint32_t cconv_idx(CTInfo info)
+{
+ uint32_t idx = ((info >> 26) & 15u); /* Dispatch bits. */
+ lua_assert(ctype_type(info) <= CT_MAYCONVERT);
+#if LJ_64
+ idx = ((uint32_t)(U64x(f436fff5,fff7f021) >> 4*idx) & 15u);
+#else
+ idx = (((idx < 8 ? 0xfff7f021u : 0xf436fff5) >> 4*(idx & 7u)) & 15u);
+#endif
+ lua_assert(idx < 8);
+ return idx;
+}
+
+#define cconv_idx2(dinfo, sinfo) \
+ ((cconv_idx((dinfo)) << 3) + cconv_idx((sinfo)))
+
+#define CCX(dst, src) ((CCX_##dst << 3) + CCX_##src)
+
+/* Conversion flags. */
+#define CCF_CAST 0x00000001u
+#define CCF_FROMTV 0x00000002u
+#define CCF_SAME 0x00000004u
+#define CCF_IGNQUAL 0x00000008u
+
+#define CCF_ARG_SHIFT 8
+#define CCF_ARG(n) ((n) << CCF_ARG_SHIFT)
+#define CCF_GETARG(f) ((f) >> CCF_ARG_SHIFT)
+
+LJ_FUNC int lj_cconv_compatptr(CTState *cts, CType *d, CType *s, CTInfo flags);
+LJ_FUNC void lj_cconv_ct_ct(CTState *cts, CType *d, CType *s,
+ uint8_t *dp, uint8_t *sp, CTInfo flags);
+LJ_FUNC int lj_cconv_tv_ct(CTState *cts, CType *s, CTypeID sid,
+ TValue *o, uint8_t *sp);
+LJ_FUNC int lj_cconv_tv_bf(CTState *cts, CType *s, TValue *o, uint8_t *sp);
+LJ_FUNC void lj_cconv_ct_tv(CTState *cts, CType *d,
+ uint8_t *dp, TValue *o, CTInfo flags);
+LJ_FUNC void lj_cconv_bf_tv(CTState *cts, CType *d, uint8_t *dp, TValue *o);
+LJ_FUNC int lj_cconv_multi_init(CTState *cts, CType *d, TValue *o);
+LJ_FUNC void lj_cconv_ct_init(CTState *cts, CType *d, CTSize sz,
+ uint8_t *dp, TValue *o, MSize len);
+
+#endif
+
+#endif
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1f27b840/lib/luajit/src/lj_cdata.c
----------------------------------------------------------------------
diff --git a/lib/luajit/src/lj_cdata.c b/lib/luajit/src/lj_cdata.c
new file mode 100644
index 0000000..39fc13a
--- /dev/null
+++ b/lib/luajit/src/lj_cdata.c
@@ -0,0 +1,285 @@
+/*
+** C data management.
+** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
+*/
+
+#include "lj_obj.h"
+
+#if LJ_HASFFI
+
+#include "lj_gc.h"
+#include "lj_err.h"
+#include "lj_str.h"
+#include "lj_tab.h"
+#include "lj_ctype.h"
+#include "lj_cconv.h"
+#include "lj_cdata.h"
+
+/* -- C data allocation --------------------------------------------------- */
+
+/* Allocate a new C data object holding a reference to another object. */
+GCcdata *lj_cdata_newref(CTState *cts, const void *p, CTypeID id)
+{
+ CTypeID refid = lj_ctype_intern(cts, CTINFO_REF(id), CTSIZE_PTR);
+ GCcdata *cd = lj_cdata_new(cts, refid, CTSIZE_PTR);
+ *(const void **)cdataptr(cd) = p;
+ return cd;
+}
+
+/* Allocate variable-sized or specially aligned C data object. */
+GCcdata *lj_cdata_newv(CTState *cts, CTypeID id, CTSize sz, CTSize align)
+{
+ global_State *g;
+ MSize extra = sizeof(GCcdataVar) + sizeof(GCcdata) +
+ (align > CT_MEMALIGN ? (1u<<align) - (1u<<CT_MEMALIGN) : 0);
+ char *p = lj_mem_newt(cts->L, extra + sz, char);
+ uintptr_t adata = (uintptr_t)p + sizeof(GCcdataVar) + sizeof(GCcdata);
+ uintptr_t almask = (1u << align) - 1u;
+ GCcdata *cd = (GCcdata *)(((adata + almask) & ~almask) - sizeof(GCcdata));
+ lua_assert((char *)cd - p < 65536);
+ cdatav(cd)->offset = (uint16_t)((char *)cd - p);
+ cdatav(cd)->extra = extra;
+ cdatav(cd)->len = sz;
+ g = cts->g;
+ setgcrefr(cd->nextgc, g->gc.root);
+ setgcref(g->gc.root, obj2gco(cd));
+ newwhite(g, obj2gco(cd));
+ cd->marked |= 0x80;
+ cd->gct = ~LJ_TCDATA;
+ cd->ctypeid = id;
+ return cd;
+}
+
+/* Free a C data object. */
+void LJ_FASTCALL lj_cdata_free(global_State *g, GCcdata *cd)
+{
+ if (LJ_UNLIKELY(cd->marked & LJ_GC_CDATA_FIN)) {
+ GCobj *root;
+ makewhite(g, obj2gco(cd));
+ markfinalized(obj2gco(cd));
+ if ((root = gcref(g->gc.mmudata)) != NULL) {
+ setgcrefr(cd->nextgc, root->gch.nextgc);
+ setgcref(root->gch.nextgc, obj2gco(cd));
+ setgcref(g->gc.mmudata, obj2gco(cd));
+ } else {
+ setgcref(cd->nextgc, obj2gco(cd));
+ setgcref(g->gc.mmudata, obj2gco(cd));
+ }
+ } else if (LJ_LIKELY(!cdataisv(cd))) {
+ CType *ct = ctype_raw(ctype_ctsG(g), cd->ctypeid);
+ CTSize sz = ctype_hassize(ct->info) ? ct->size : CTSIZE_PTR;
+ lua_assert(ctype_hassize(ct->info) || ctype_isfunc(ct->info) ||
+ ctype_isextern(ct->info));
+ lj_mem_free(g, cd, sizeof(GCcdata) + sz);
+ } else {
+ lj_mem_free(g, memcdatav(cd), sizecdatav(cd));
+ }
+}
+
+TValue * LJ_FASTCALL lj_cdata_setfin(lua_State *L, GCcdata *cd)
+{
+ global_State *g = G(L);
+ GCtab *t = ctype_ctsG(g)->finalizer;
+ if (gcref(t->metatable)) {
+ /* Add cdata to finalizer table, if still enabled. */
+ TValue *tv, tmp;
+ setcdataV(L, &tmp, cd);
+ lj_gc_anybarriert(L, t);
+ tv = lj_tab_set(L, t, &tmp);
+ cd->marked |= LJ_GC_CDATA_FIN;
+ return tv;
+ } else {
+ /* Otherwise return dummy TValue. */
+ return &g->tmptv;
+ }
+}
+
+/* -- C data indexing ----------------------------------------------------- */
+
+/* Index C data by a TValue. Return CType and pointer. */
+CType *lj_cdata_index(CTState *cts, GCcdata *cd, cTValue *key, uint8_t **pp,
+ CTInfo *qual)
+{
+ uint8_t *p = (uint8_t *)cdataptr(cd);
+ CType *ct = ctype_get(cts, cd->ctypeid);
+ ptrdiff_t idx;
+
+ /* Resolve reference for cdata object. */
+ if (ctype_isref(ct->info)) {
+ lua_assert(ct->size == CTSIZE_PTR);
+ p = *(uint8_t **)p;
+ ct = ctype_child(cts, ct);
+ }
+
+collect_attrib:
+ /* Skip attributes and collect qualifiers. */
+ while (ctype_isattrib(ct->info)) {
+ if (ctype_attrib(ct->info) == CTA_QUAL) *qual |= ct->size;
+ ct = ctype_child(cts, ct);
+ }
+ lua_assert(!ctype_isref(ct->info)); /* Interning rejects refs to refs. */
+
+ if (tvisint(key)) {
+ idx = (ptrdiff_t)intV(key);
+ goto integer_key;
+ } else if (tvisnum(key)) { /* Numeric key. */
+ idx = LJ_64 ? (ptrdiff_t)numV(key) : (ptrdiff_t)lj_num2int(numV(key));
+ integer_key:
+ if (ctype_ispointer(ct->info)) {
+ CTSize sz = lj_ctype_size(cts, ctype_cid(ct->info)); /* Element size. */
+ if (sz == CTSIZE_INVALID)
+ lj_err_caller(cts->L, LJ_ERR_FFI_INVSIZE);
+ if (ctype_isptr(ct->info)) {
+ p = (uint8_t *)cdata_getptr(p, ct->size);
+ } else if ((ct->info & (CTF_VECTOR|CTF_COMPLEX))) {
+ if ((ct->info & CTF_COMPLEX)) idx &= 1;
+ *qual |= CTF_CONST; /* Valarray elements are constant. */
+ }
+ *pp = p + idx*(int32_t)sz;
+ return ct;
+ }
+ } else if (tviscdata(key)) { /* Integer cdata key. */
+ GCcdata *cdk = cdataV(key);
+ CType *ctk = ctype_raw(cts, cdk->ctypeid);
+ if (ctype_isenum(ctk->info)) ctk = ctype_child(cts, ctk);
+ if (ctype_isinteger(ctk->info)) {
+ lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ctk,
+ (uint8_t *)&idx, cdataptr(cdk), 0);
+ goto integer_key;
+ }
+ } else if (tvisstr(key)) { /* String key. */
+ GCstr *name = strV(key);
+ if (ctype_isstruct(ct->info)) {
+ CTSize ofs;
+ CType *fct = lj_ctype_getfieldq(cts, ct, name, &ofs, qual);
+ if (fct) {
+ *pp = p + ofs;
+ return fct;
+ }
+ } else if (ctype_iscomplex(ct->info)) {
+ if (name->len == 2) {
+ *qual |= CTF_CONST; /* Complex fields are constant. */
+ if (strdata(name)[0] == 'r' && strdata(name)[1] == 'e') {
+ *pp = p;
+ return ct;
+ } else if (strdata(name)[0] == 'i' && strdata(name)[1] == 'm') {
+ *pp = p + (ct->size >> 1);
+ return ct;
+ }
+ }
+ } else if (cd->ctypeid == CTID_CTYPEID) {
+ /* Allow indexing a (pointer to) struct constructor to get constants. */
+ CType *sct = ctype_raw(cts, *(CTypeID *)p);
+ if (ctype_isptr(sct->info))
+ sct = ctype_rawchild(cts, sct);
+ if (ctype_isstruct(sct->info)) {
+ CTSize ofs;
+ CType *fct = lj_ctype_getfield(cts, sct, name, &ofs);
+ if (fct && ctype_isconstval(fct->info))
+ return fct;
+ }
+ ct = sct; /* Allow resolving metamethods for constructors, too. */
+ }
+ }
+ if (ctype_isptr(ct->info)) { /* Automatically perform '->'. */
+ if (ctype_isstruct(ctype_rawchild(cts, ct)->info)) {
+ p = (uint8_t *)cdata_getptr(p, ct->size);
+ ct = ctype_child(cts, ct);
+ goto collect_attrib;
+ }
+ }
+ *qual |= 1; /* Lookup failed. */
+ return ct; /* But return the resolved raw type. */
+}
+
+/* -- C data getters ------------------------------------------------------ */
+
+/* Get constant value and convert to TValue. */
+static void cdata_getconst(CTState *cts, TValue *o, CType *ct)
+{
+ CType *ctt = ctype_child(cts, ct);
+ lua_assert(ctype_isinteger(ctt->info) && ctt->size <= 4);
+ /* Constants are already zero-extended/sign-extended to 32 bits. */
+ if ((ctt->info & CTF_UNSIGNED) && (int32_t)ct->size < 0)
+ setnumV(o, (lua_Number)(uint32_t)ct->size);
+ else
+ setintV(o, (int32_t)ct->size);
+}
+
+/* Get C data value and convert to TValue. */
+int lj_cdata_get(CTState *cts, CType *s, TValue *o, uint8_t *sp)
+{
+ CTypeID sid;
+
+ if (ctype_isconstval(s->info)) {
+ cdata_getconst(cts, o, s);
+ return 0; /* No GC step needed. */
+ } else if (ctype_isbitfield(s->info)) {
+ return lj_cconv_tv_bf(cts, s, o, sp);
+ }
+
+ /* Get child type of pointer/array/field. */
+ lua_assert(ctype_ispointer(s->info) || ctype_isfield(s->info));
+ sid = ctype_cid(s->info);
+ s = ctype_get(cts, sid);
+
+ /* Resolve reference for field. */
+ if (ctype_isref(s->info)) {
+ lua_assert(s->size == CTSIZE_PTR);
+ sp = *(uint8_t **)sp;
+ sid = ctype_cid(s->info);
+ s = ctype_get(cts, sid);
+ }
+
+ /* Skip attributes. */
+ while (ctype_isattrib(s->info))
+ s = ctype_child(cts, s);
+
+ return lj_cconv_tv_ct(cts, s, sid, o, sp);
+}
+
+/* -- C data setters ------------------------------------------------------ */
+
+/* Convert TValue and set C data value. */
+void lj_cdata_set(CTState *cts, CType *d, uint8_t *dp, TValue *o, CTInfo qual)
+{
+ if (ctype_isconstval(d->info)) {
+ goto err_const;
+ } else if (ctype_isbitfield(d->info)) {
+ if (((d->info|qual) & CTF_CONST)) goto err_const;
+ lj_cconv_bf_tv(cts, d, dp, o);
+ return;
+ }
+
+ /* Get child type of pointer/array/field. */
+ lua_assert(ctype_ispointer(d->info) || ctype_isfield(d->info));
+ d = ctype_child(cts, d);
+
+ /* Resolve reference for field. */
+ if (ctype_isref(d->info)) {
+ lua_assert(d->size == CTSIZE_PTR);
+ dp = *(uint8_t **)dp;
+ d = ctype_child(cts, d);
+ }
+
+ /* Skip attributes and collect qualifiers. */
+ for (;;) {
+ if (ctype_isattrib(d->info)) {
+ if (ctype_attrib(d->info) == CTA_QUAL) qual |= d->size;
+ } else {
+ break;
+ }
+ d = ctype_child(cts, d);
+ }
+
+ lua_assert(ctype_hassize(d->info) && !ctype_isvoid(d->info));
+
+ if (((d->info|qual) & CTF_CONST)) {
+ err_const:
+ lj_err_caller(cts->L, LJ_ERR_FFI_WRCONST);
+ }
+
+ lj_cconv_ct_tv(cts, d, dp, o, 0);
+}
+
+#endif
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1f27b840/lib/luajit/src/lj_cdata.h
----------------------------------------------------------------------
diff --git a/lib/luajit/src/lj_cdata.h b/lib/luajit/src/lj_cdata.h
new file mode 100644
index 0000000..3a1275e
--- /dev/null
+++ b/lib/luajit/src/lj_cdata.h
@@ -0,0 +1,75 @@
+/*
+** C data management.
+** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
+*/
+
+#ifndef _LJ_CDATA_H
+#define _LJ_CDATA_H
+
+#include "lj_obj.h"
+#include "lj_gc.h"
+#include "lj_ctype.h"
+
+#if LJ_HASFFI
+
+/* Get C data pointer. */
+static LJ_AINLINE void *cdata_getptr(void *p, CTSize sz)
+{
+ if (LJ_64 && sz == 4) { /* Support 32 bit pointers on 64 bit targets. */
+ return ((void *)(uintptr_t)*(uint32_t *)p);
+ } else {
+ lua_assert(sz == CTSIZE_PTR);
+ return *(void **)p;
+ }
+}
+
+/* Set C data pointer. */
+static LJ_AINLINE void cdata_setptr(void *p, CTSize sz, const void *v)
+{
+ if (LJ_64 && sz == 4) { /* Support 32 bit pointers on 64 bit targets. */
+ *(uint32_t *)p = (uint32_t)(uintptr_t)v;
+ } else {
+ lua_assert(sz == CTSIZE_PTR);
+ *(void **)p = (void *)v;
+ }
+}
+
+/* Allocate fixed-size C data object. */
+static LJ_AINLINE GCcdata *lj_cdata_new(CTState *cts, CTypeID id, CTSize sz)
+{
+ GCcdata *cd;
+#ifdef LUA_USE_ASSERT
+ CType *ct = ctype_raw(cts, id);
+ lua_assert((ctype_hassize(ct->info) ? ct->size : CTSIZE_PTR) == sz);
+#endif
+ cd = (GCcdata *)lj_mem_newgco(cts->L, sizeof(GCcdata) + sz);
+ cd->gct = ~LJ_TCDATA;
+ cd->ctypeid = ctype_check(cts, id);
+ return cd;
+}
+
+/* Variant which works without a valid CTState. */
+static LJ_AINLINE GCcdata *lj_cdata_new_(lua_State *L, CTypeID id, CTSize sz)
+{
+ GCcdata *cd = (GCcdata *)lj_mem_newgco(L, sizeof(GCcdata) + sz);
+ cd->gct = ~LJ_TCDATA;
+ cd->ctypeid = id;
+ return cd;
+}
+
+LJ_FUNC GCcdata *lj_cdata_newref(CTState *cts, const void *pp, CTypeID id);
+LJ_FUNC GCcdata *lj_cdata_newv(CTState *cts, CTypeID id, CTSize sz,
+ CTSize align);
+
+LJ_FUNC void LJ_FASTCALL lj_cdata_free(global_State *g, GCcdata *cd);
+LJ_FUNCA TValue * LJ_FASTCALL lj_cdata_setfin(lua_State *L, GCcdata *cd);
+
+LJ_FUNC CType *lj_cdata_index(CTState *cts, GCcdata *cd, cTValue *key,
+ uint8_t **pp, CTInfo *qual);
+LJ_FUNC int lj_cdata_get(CTState *cts, CType *s, TValue *o, uint8_t *sp);
+LJ_FUNC void lj_cdata_set(CTState *cts, CType *d, uint8_t *dp, TValue *o,
+ CTInfo qual);
+
+#endif
+
+#endif
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1f27b840/lib/luajit/src/lj_char.c
----------------------------------------------------------------------
diff --git a/lib/luajit/src/lj_char.c b/lib/luajit/src/lj_char.c
new file mode 100644
index 0000000..11f23ef
--- /dev/null
+++ b/lib/luajit/src/lj_char.c
@@ -0,0 +1,43 @@
+/*
+** Character types.
+** Donated to the public domain.
+**
+** This is intended to replace the problematic libc single-byte NLS functions.
+** These just don't make sense anymore with UTF-8 locales becoming the norm
+** on POSIX systems. It never worked too well on Windows systems since hardly
+** anyone bothered to call setlocale().
+**
+** This table is hardcoded for ASCII. Identifiers include the characters
+** 128-255, too. This allows for the use of all non-ASCII chars as identifiers
+** in the lexer. This is a broad definition, but works well in practice
+** for both UTF-8 locales and most single-byte locales (such as ISO-8859-*).
+**
+** If you really need proper character types for UTF-8 strings, please use
+** an add-on library such as slnunicode: http://luaforge.net/projects/sln/
+*/
+
+#define lj_char_c
+#define LUA_CORE
+
+#include "lj_char.h"
+
+LJ_DATADEF const uint8_t lj_char_bits[257] = {
+ 0,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+ 152,152,152,152,152,152,152,152,152,152, 4, 4, 4, 4, 4, 4,
+ 4,176,176,176,176,176,176,160,160,160,160,160,160,160,160,160,
+ 160,160,160,160,160,160,160,160,160,160,160, 4, 4, 4, 4,132,
+ 4,208,208,208,208,208,208,192,192,192,192,192,192,192,192,192,
+ 192,192,192,192,192,192,192,192,192,192,192, 4, 4, 4, 4, 1,
+ 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,
+ 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,
+ 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,
+ 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,
+ 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,
+ 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,
+ 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,
+ 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128
+};
+
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1f27b840/lib/luajit/src/lj_char.h
----------------------------------------------------------------------
diff --git a/lib/luajit/src/lj_char.h b/lib/luajit/src/lj_char.h
new file mode 100644
index 0000000..c3c86d3
--- /dev/null
+++ b/lib/luajit/src/lj_char.h
@@ -0,0 +1,42 @@
+/*
+** Character types.
+** Donated to the public domain.
+*/
+
+#ifndef _LJ_CHAR_H
+#define _LJ_CHAR_H
+
+#include "lj_def.h"
+
+#define LJ_CHAR_CNTRL 0x01
+#define LJ_CHAR_SPACE 0x02
+#define LJ_CHAR_PUNCT 0x04
+#define LJ_CHAR_DIGIT 0x08
+#define LJ_CHAR_XDIGIT 0x10
+#define LJ_CHAR_UPPER 0x20
+#define LJ_CHAR_LOWER 0x40
+#define LJ_CHAR_IDENT 0x80
+#define LJ_CHAR_ALPHA (LJ_CHAR_LOWER|LJ_CHAR_UPPER)
+#define LJ_CHAR_ALNUM (LJ_CHAR_ALPHA|LJ_CHAR_DIGIT)
+#define LJ_CHAR_GRAPH (LJ_CHAR_ALNUM|LJ_CHAR_PUNCT)
+
+/* Only pass -1 or 0..255 to these macros. Never pass a signed char! */
+#define lj_char_isa(c, t) ((lj_char_bits+1)[(c)] & t)
+#define lj_char_iscntrl(c) lj_char_isa((c), LJ_CHAR_CNTRL)
+#define lj_char_isspace(c) lj_char_isa((c), LJ_CHAR_SPACE)
+#define lj_char_ispunct(c) lj_char_isa((c), LJ_CHAR_PUNCT)
+#define lj_char_isdigit(c) lj_char_isa((c), LJ_CHAR_DIGIT)
+#define lj_char_isxdigit(c) lj_char_isa((c), LJ_CHAR_XDIGIT)
+#define lj_char_isupper(c) lj_char_isa((c), LJ_CHAR_UPPER)
+#define lj_char_islower(c) lj_char_isa((c), LJ_CHAR_LOWER)
+#define lj_char_isident(c) lj_char_isa((c), LJ_CHAR_IDENT)
+#define lj_char_isalpha(c) lj_char_isa((c), LJ_CHAR_ALPHA)
+#define lj_char_isalnum(c) lj_char_isa((c), LJ_CHAR_ALNUM)
+#define lj_char_isgraph(c) lj_char_isa((c), LJ_CHAR_GRAPH)
+
+#define lj_char_toupper(c) ((c) - (lj_char_islower(c) >> 1))
+#define lj_char_tolower(c) ((c) + lj_char_isupper(c))
+
+LJ_DATA const uint8_t lj_char_bits[257];
+
+#endif
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1f27b840/lib/luajit/src/lj_clib.c
----------------------------------------------------------------------
diff --git a/lib/luajit/src/lj_clib.c b/lib/luajit/src/lj_clib.c
new file mode 100644
index 0000000..d352609
--- /dev/null
+++ b/lib/luajit/src/lj_clib.c
@@ -0,0 +1,409 @@
+/*
+** FFI C library loader.
+** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
+*/
+
+#include "lj_obj.h"
+
+#if LJ_HASFFI
+
+#include "lj_gc.h"
+#include "lj_err.h"
+#include "lj_tab.h"
+#include "lj_str.h"
+#include "lj_udata.h"
+#include "lj_ctype.h"
+#include "lj_cconv.h"
+#include "lj_cdata.h"
+#include "lj_clib.h"
+
+/* -- OS-specific functions ----------------------------------------------- */
+
+#if LJ_TARGET_DLOPEN
+
+#include <dlfcn.h>
+#include <stdio.h>
+
+#if defined(RTLD_DEFAULT)
+#define CLIB_DEFHANDLE RTLD_DEFAULT
+#elif LJ_TARGET_OSX || LJ_TARGET_BSD
+#define CLIB_DEFHANDLE ((void *)(intptr_t)-2)
+#else
+#define CLIB_DEFHANDLE NULL
+#endif
+
+LJ_NORET LJ_NOINLINE static void clib_error_(lua_State *L)
+{
+ lj_err_callermsg(L, dlerror());
+}
+
+#define clib_error(L, fmt, name) clib_error_(L)
+
+#if defined(__CYGWIN__)
+#define CLIB_SOPREFIX "cyg"
+#else
+#define CLIB_SOPREFIX "lib"
+#endif
+
+#if LJ_TARGET_OSX
+#define CLIB_SOEXT "%s.dylib"
+#elif defined(__CYGWIN__)
+#define CLIB_SOEXT "%s.dll"
+#else
+#define CLIB_SOEXT "%s.so"
+#endif
+
+static const char *clib_extname(lua_State *L, const char *name)
+{
+ if (!strchr(name, '/')
+#ifdef __CYGWIN__
+ && !strchr(name, '\\')
+#endif
+ ) {
+ if (!strchr(name, '.')) {
+ name = lj_str_pushf(L, CLIB_SOEXT, name);
+ L->top--;
+#ifdef __CYGWIN__
+ } else {
+ return name;
+#endif
+ }
+ if (!(name[0] == CLIB_SOPREFIX[0] && name[1] == CLIB_SOPREFIX[1] &&
+ name[2] == CLIB_SOPREFIX[2])) {
+ name = lj_str_pushf(L, CLIB_SOPREFIX "%s", name);
+ L->top--;
+ }
+ }
+ return name;
+}
+
+/* Check for a recognized ld script line. */
+static const char *clib_check_lds(lua_State *L, const char *buf)
+{
+ char *p, *e;
+ if ((!strncmp(buf, "GROUP", 5) || !strncmp(buf, "INPUT", 5)) &&
+ (p = strchr(buf, '('))) {
+ while (*++p == ' ') ;
+ for (e = p; *e && *e != ' ' && *e != ')'; e++) ;
+ return strdata(lj_str_new(L, p, e-p));
+ }
+ return NULL;
+}
+
+/* Quick and dirty solution to resolve shared library name from ld script. */
+static const char *clib_resolve_lds(lua_State *L, const char *name)
+{
+ FILE *fp = fopen(name, "r");
+ const char *p = NULL;
+ if (fp) {
+ char buf[256];
+ if (fgets(buf, sizeof(buf), fp)) {
+ if (!strncmp(buf, "/* GNU ld script", 16)) { /* ld script magic? */
+ while (fgets(buf, sizeof(buf), fp)) { /* Check all lines. */
+ p = clib_check_lds(L, buf);
+ if (p) break;
+ }
+ } else { /* Otherwise check only the first line. */
+ p = clib_check_lds(L, buf);
+ }
+ }
+ fclose(fp);
+ }
+ return p;
+}
+
+static void *clib_loadlib(lua_State *L, const char *name, int global)
+{
+ void *h = dlopen(clib_extname(L, name),
+ RTLD_LAZY | (global?RTLD_GLOBAL:RTLD_LOCAL));
+ if (!h) {
+ const char *e, *err = dlerror();
+ if (*err == '/' && (e = strchr(err, ':')) &&
+ (name = clib_resolve_lds(L, strdata(lj_str_new(L, err, e-err))))) {
+ h = dlopen(name, RTLD_LAZY | (global?RTLD_GLOBAL:RTLD_LOCAL));
+ if (h) return h;
+ err = dlerror();
+ }
+ lj_err_callermsg(L, err);
+ }
+ return h;
+}
+
+static void clib_unloadlib(CLibrary *cl)
+{
+ if (cl->handle && cl->handle != CLIB_DEFHANDLE)
+ dlclose(cl->handle);
+}
+
+static void *clib_getsym(CLibrary *cl, const char *name)
+{
+ void *p = dlsym(cl->handle, name);
+ return p;
+}
+
+#elif LJ_TARGET_WINDOWS
+
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+
+#ifndef GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
+#define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 4
+#define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 2
+BOOL WINAPI GetModuleHandleExA(DWORD, LPCSTR, HMODULE*);
+#endif
+
+#define CLIB_DEFHANDLE ((void *)-1)
+
+/* Default libraries. */
+enum {
+ CLIB_HANDLE_EXE,
+ CLIB_HANDLE_DLL,
+ CLIB_HANDLE_CRT,
+ CLIB_HANDLE_KERNEL32,
+ CLIB_HANDLE_USER32,
+ CLIB_HANDLE_GDI32,
+ CLIB_HANDLE_MAX
+};
+
+static void *clib_def_handle[CLIB_HANDLE_MAX];
+
+LJ_NORET LJ_NOINLINE static void clib_error(lua_State *L, const char *fmt,
+ const char *name)
+{
+ DWORD err = GetLastError();
+ char buf[128];
+ if (!FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_FROM_SYSTEM,
+ NULL, err, 0, buf, sizeof(buf), NULL))
+ buf[0] = '\0';
+ lj_err_callermsg(L, lj_str_pushf(L, fmt, name, buf));
+}
+
+static int clib_needext(const char *s)
+{
+ while (*s) {
+ if (*s == '/' || *s == '\\' || *s == '.') return 0;
+ s++;
+ }
+ return 1;
+}
+
+static const char *clib_extname(lua_State *L, const char *name)
+{
+ if (clib_needext(name)) {
+ name = lj_str_pushf(L, "%s.dll", name);
+ L->top--;
+ }
+ return name;
+}
+
+static void *clib_loadlib(lua_State *L, const char *name, int global)
+{
+ DWORD oldwerr = GetLastError();
+ void *h = (void *)LoadLibraryA(clib_extname(L, name));
+ if (!h) clib_error(L, "cannot load module " LUA_QS ": %s", name);
+ SetLastError(oldwerr);
+ UNUSED(global);
+ return h;
+}
+
+static void clib_unloadlib(CLibrary *cl)
+{
+ if (cl->handle == CLIB_DEFHANDLE) {
+ MSize i;
+ for (i = CLIB_HANDLE_KERNEL32; i < CLIB_HANDLE_MAX; i++) {
+ void *h = clib_def_handle[i];
+ if (h) {
+ clib_def_handle[i] = NULL;
+ FreeLibrary((HINSTANCE)h);
+ }
+ }
+ } else if (cl->handle) {
+ FreeLibrary((HINSTANCE)cl->handle);
+ }
+}
+
+static void *clib_getsym(CLibrary *cl, const char *name)
+{
+ void *p = NULL;
+ if (cl->handle == CLIB_DEFHANDLE) { /* Search default libraries. */
+ MSize i;
+ for (i = 0; i < CLIB_HANDLE_MAX; i++) {
+ HINSTANCE h = (HINSTANCE)clib_def_handle[i];
+ if (!(void *)h) { /* Resolve default library handles (once). */
+ switch (i) {
+ case CLIB_HANDLE_EXE: GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, NULL, &h); break;
+ case CLIB_HANDLE_DLL:
+ GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
+ (const char *)clib_def_handle, &h);
+ break;
+ case CLIB_HANDLE_CRT:
+ GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
+ (const char *)&_fmode, &h);
+ break;
+ case CLIB_HANDLE_KERNEL32: h = LoadLibraryA("kernel32.dll"); break;
+ case CLIB_HANDLE_USER32: h = LoadLibraryA("user32.dll"); break;
+ case CLIB_HANDLE_GDI32: h = LoadLibraryA("gdi32.dll"); break;
+ }
+ if (!h) continue;
+ clib_def_handle[i] = (void *)h;
+ }
+ p = (void *)GetProcAddress(h, name);
+ if (p) break;
+ }
+ } else {
+ p = (void *)GetProcAddress((HINSTANCE)cl->handle, name);
+ }
+ return p;
+}
+
+#else
+
+#define CLIB_DEFHANDLE NULL
+
+LJ_NORET LJ_NOINLINE static void clib_error(lua_State *L, const char *fmt,
+ const char *name)
+{
+ lj_err_callermsg(L, lj_str_pushf(L, fmt, name, "no support for this OS"));
+}
+
+static void *clib_loadlib(lua_State *L, const char *name, int global)
+{
+ lj_err_callermsg(L, "no support for loading dynamic libraries for this OS");
+ UNUSED(name); UNUSED(global);
+ return NULL;
+}
+
+static void clib_unloadlib(CLibrary *cl)
+{
+ UNUSED(cl);
+}
+
+static void *clib_getsym(CLibrary *cl, const char *name)
+{
+ UNUSED(cl); UNUSED(name);
+ return NULL;
+}
+
+#endif
+
+/* -- C library indexing -------------------------------------------------- */
+
+#if LJ_TARGET_X86 && LJ_ABI_WIN
+/* Compute argument size for fastcall/stdcall functions. */
+static CTSize clib_func_argsize(CTState *cts, CType *ct)
+{
+ CTSize n = 0;
+ while (ct->sib) {
+ CType *d;
+ ct = ctype_get(cts, ct->sib);
+ if (ctype_isfield(ct->info)) {
+ d = ctype_rawchild(cts, ct);
+ n += ((d->size + 3) & ~3);
+ }
+ }
+ return n;
+}
+#endif
+
+/* Get redirected or mangled external symbol. */
+static const char *clib_extsym(CTState *cts, CType *ct, GCstr *name)
+{
+ if (ct->sib) {
+ CType *ctf = ctype_get(cts, ct->sib);
+ if (ctype_isxattrib(ctf->info, CTA_REDIR))
+ return strdata(gco2str(gcref(ctf->name)));
+ }
+ return strdata(name);
+}
+
+/* Index a C library by name. */
+TValue *lj_clib_index(lua_State *L, CLibrary *cl, GCstr *name)
+{
+ TValue *tv = lj_tab_setstr(L, cl->cache, name);
+ if (LJ_UNLIKELY(tvisnil(tv))) {
+ CTState *cts = ctype_cts(L);
+ CType *ct;
+ CTypeID id = lj_ctype_getname(cts, &ct, name, CLNS_INDEX);
+ if (!id)
+ lj_err_callerv(L, LJ_ERR_FFI_NODECL, strdata(name));
+ if (ctype_isconstval(ct->info)) {
+ CType *ctt = ctype_child(cts, ct);
+ lua_assert(ctype_isinteger(ctt->info) && ctt->size <= 4);
+ if ((ctt->info & CTF_UNSIGNED) && (int32_t)ct->size < 0)
+ setnumV(tv, (lua_Number)(uint32_t)ct->size);
+ else
+ setintV(tv, (int32_t)ct->size);
+ } else {
+ const char *sym = clib_extsym(cts, ct, name);
+#if LJ_TARGET_WINDOWS
+ DWORD oldwerr = GetLastError();
+#endif
+ void *p = clib_getsym(cl, sym);
+ GCcdata *cd;
+ lua_assert(ctype_isfunc(ct->info) || ctype_isextern(ct->info));
+#if LJ_TARGET_X86 && LJ_ABI_WIN
+ /* Retry with decorated name for fastcall/stdcall functions. */
+ if (!p && ctype_isfunc(ct->info)) {
+ CTInfo cconv = ctype_cconv(ct->info);
+ if (cconv == CTCC_FASTCALL || cconv == CTCC_STDCALL) {
+ CTSize sz = clib_func_argsize(cts, ct);
+ const char *symd = lj_str_pushf(L,
+ cconv == CTCC_FASTCALL ? "@%s@%d" : "_%s@%d",
+ sym, sz);
+ L->top--;
+ p = clib_getsym(cl, symd);
+ }
+ }
+#endif
+ if (!p)
+ clib_error(L, "cannot resolve symbol " LUA_QS ": %s", sym);
+#if LJ_TARGET_WINDOWS
+ SetLastError(oldwerr);
+#endif
+ cd = lj_cdata_new(cts, id, CTSIZE_PTR);
+ *(void **)cdataptr(cd) = p;
+ setcdataV(L, tv, cd);
+ }
+ }
+ return tv;
+}
+
+/* -- C library management ------------------------------------------------ */
+
+/* Create a new CLibrary object and push it on the stack. */
+static CLibrary *clib_new(lua_State *L, GCtab *mt)
+{
+ GCtab *t = lj_tab_new(L, 0, 0);
+ GCudata *ud = lj_udata_new(L, sizeof(CLibrary), t);
+ CLibrary *cl = (CLibrary *)uddata(ud);
+ cl->cache = t;
+ ud->udtype = UDTYPE_FFI_CLIB;
+ /* NOBARRIER: The GCudata is new (marked white). */
+ setgcref(ud->metatable, obj2gco(mt));
+ setudataV(L, L->top++, ud);
+ return cl;
+}
+
+/* Load a C library. */
+void lj_clib_load(lua_State *L, GCtab *mt, GCstr *name, int global)
+{
+ void *handle = clib_loadlib(L, strdata(name), global);
+ CLibrary *cl = clib_new(L, mt);
+ cl->handle = handle;
+}
+
+/* Unload a C library. */
+void lj_clib_unload(CLibrary *cl)
+{
+ clib_unloadlib(cl);
+ cl->handle = NULL;
+}
+
+/* Create the default C library object. */
+void lj_clib_default(lua_State *L, GCtab *mt)
+{
+ CLibrary *cl = clib_new(L, mt);
+ cl->handle = CLIB_DEFHANDLE;
+}
+
+#endif
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/1f27b840/lib/luajit/src/lj_clib.h
----------------------------------------------------------------------
diff --git a/lib/luajit/src/lj_clib.h b/lib/luajit/src/lj_clib.h
new file mode 100644
index 0000000..e5dc98e
--- /dev/null
+++ b/lib/luajit/src/lj_clib.h
@@ -0,0 +1,29 @@
+/*
+** FFI C library loader.
+** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
+*/
+
+#ifndef _LJ_CLIB_H
+#define _LJ_CLIB_H
+
+#include "lj_obj.h"
+
+#if LJ_HASFFI
+
+/* Namespace for C library indexing. */
+#define CLNS_INDEX ((1u<<CT_FUNC)|(1u<<CT_EXTERN)|(1u<<CT_CONSTVAL))
+
+/* C library namespace. */
+typedef struct CLibrary {
+ void *handle; /* Opaque handle for dynamic library loader. */
+ GCtab *cache; /* Cache for resolved symbols. Anchored in ud->env. */
+} CLibrary;
+
+LJ_FUNC TValue *lj_clib_index(lua_State *L, CLibrary *cl, GCstr *name);
+LJ_FUNC void lj_clib_load(lua_State *L, GCtab *mt, GCstr *name, int global);
+LJ_FUNC void lj_clib_unload(CLibrary *cl);
+LJ_FUNC void lj_clib_default(lua_State *L, GCtab *mt);
+
+#endif
+
+#endif