Skip to content
Open

V3.0 #39

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ CCOPT= -O2 -fomit-frame-pointer
# the binaries to a different machine you could also use: -march=native
#
CCOPT_x86= -march=i686 -msse -msse2 -mfpmath=sse
CCOPT_x64=
CCOPT_x64= -mbmi -mbmi2 -mavx -mavx2
CCOPT_arm=
CCOPT_arm64=
CCOPT_ppc=
Expand Down Expand Up @@ -511,7 +511,7 @@ LJCORE_O= lj_assert.o lj_gc.o lj_err.o lj_char.o lj_bc.o lj_obj.o lj_buf.o \
lj_mcode.o lj_snap.o lj_record.o lj_crecord.o lj_ffrecord.o \
lj_asm.o lj_trace.o lj_gdbjit.o \
lj_ctype.o lj_cdata.o lj_cconv.o lj_ccall.o lj_ccallback.o \
lj_carith.o lj_clib.o lj_cparse.o \
lj_carith.o lj_clib.o lj_cparse.o lj_arena.o \
lj_lib.o lj_alloc.o lib_aux.o \
$(LJLIB_O) lib_init.o

Expand Down
32 changes: 32 additions & 0 deletions src/lib_aux.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,35 @@ LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud)

#endif

LUA_API lua_State *luaJIT_newstate(lua_Alloc f, void *ud,
luaJIT_allocpages allocp,
luaJIT_freepages freep,
luaJIT_reallochuge realloch,
void *page_ud)
{
lua_State *L;
#ifdef LUAJIT_USE_SYSMALLOC
if (!f) {
f = mem_alloc;
ud = NULL;
}
#else
if (f == NULL) {
f = LJ_ALLOCF_INTERNAL;
ud = NULL;
}
#endif
L = lj_newstate(f, ud, allocp, freep, realloch, page_ud);

if (L) {
G(L)->panic = panic;
#ifndef LUAJIT_DISABLE_VMEVENT
luaL_findtable(L, LUA_REGISTRYINDEX, LJ_VMEVENTS_REGKEY, LJ_VMEVENTS_HSIZE);
lua_pushcfunction(L, error_finalizer);
lua_rawseti(L, -2, VMEVENT_HASH(LJ_VMEVENT_ERRFIN));
G(L)->vmevmask = VMEVENT_MASK(LJ_VMEVENT_ERRFIN);
L->top--;
#endif
}
return L;
}
2 changes: 1 addition & 1 deletion src/lib_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ LJLIB_CF(debug_upvalueid)
if ((uint32_t)n >= fn->l.nupvalues)
lj_err_arg(L, 2, LJ_ERR_IDXRNG);
lua_pushlightuserdata(L, isluafunc(fn) ? (void *)gcref(fn->l.uvptr[n]) :
(void *)&fn->c.upvalue[n]);
(void *)&fn->c.data->upvalue[n]);
return 1;
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib_ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ LJLIB_CF(ffi_new) LJLIB_REC(.)
/* Add to finalizer table, if still enabled. */
copyTV(L, lj_tab_set(L, t, o-1), tv);
lj_gc_anybarriert(L, t);
cd->marked |= LJ_GC_CDATA_FIN;
cd->gcflags |= LJ_GC_CDATA_FIN;
}
}
}
Expand Down Expand Up @@ -573,7 +573,7 @@ LJLIB_CF(ffi_typeinfo)
setintV(lj_tab_setstr(L, t, lj_str_newlit(L, "sib")), (int32_t)ct->sib);
if (gcref(ct->name)) {
GCstr *s = gco2str(gcref(ct->name));
if (isdead(G(L), obj2gco(s))) flipwhite(obj2gco(s));
maybe_resurrect_str(G(L), s);
setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "name")), s);
}
lj_gc_check(L);
Expand Down
4 changes: 2 additions & 2 deletions src/lib_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,14 @@ static int io_file_write(lua_State *L, IOFileUD *iof, int start)
static int io_file_iter(lua_State *L)
{
GCfunc *fn = curr_func(L);
IOFileUD *iof = uddata(udataV(&fn->c.upvalue[0]));
IOFileUD *iof = uddata(udataV(&fn->c.data->upvalue[0]));
int n = fn->c.nupvalues - 1;
if (iof->fp == NULL)
lj_err_caller(L, LJ_ERR_IOCLFL);
L->top = L->base;
if (n) { /* Copy upvalues with options to stack. */
lj_state_checkstack(L, (MSize)n);
memcpy(L->top, &fn->c.upvalue[1], n*sizeof(TValue));
memcpy(L->top, &fn->c.data->upvalue[1], n * sizeof(TValue));
L->top += n;
}
n = io_file_read(L, iof, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/lib_jit.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ LJLIB_CF(jit_util_funcinfo)
if (!iscfunc(fn))
setintfield(L, t, "ffid", fn->c.ffid);
setintptrV(lj_tab_setstr(L, t, lj_str_newlit(L, "addr")),
(intptr_t)(void *)fn->c.f);
(intptr_t)(void *)fn->c.data->f);
setintfield(L, t, "upvalues", fn->c.nupvalues);
}
return 1;
Expand Down
8 changes: 8 additions & 0 deletions src/lib_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ LJLIB_CF(table_insert) LJLIB_REC(.)
return 0;
}

LJLIB_CF(table_newgc)
{
GCtab *t = lj_tab_newgc(L, 0, 0);
settabV(L, L->top++, t);

return 1;
}

LJLIB_LUA(table_remove) /*
function(t, pos)
CHECK_tab(t)
Expand Down
163 changes: 153 additions & 10 deletions src/lj_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "lj_vm.h"
#include "lj_strscan.h"
#include "lj_strfmt.h"
#include "lj_clib.h"
#include "luacpp.h"

/* -- Common helper functions --------------------------------------------- */

Expand Down Expand Up @@ -56,7 +58,7 @@ static TValue *index2adr(lua_State *L, int idx)
return o;
} else {
idx = LUA_GLOBALSINDEX - idx;
return idx <= fn->c.nupvalues ? &fn->c.upvalue[idx-1] : niltv(L);
return idx <= fn->c.nupvalues ? &fn->c.data->upvalue[idx - 1] : niltv(L);
}
}
}
Expand Down Expand Up @@ -597,7 +599,7 @@ LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx)
if (tvisfunc(o)) {
BCOp op = bc_op(*mref(funcV(o)->c.pc, BCIns));
if (op == BC_FUNCC || op == BC_FUNCCW)
return funcV(o)->c.f;
return funcV(o)->c.data->f;
}
return NULL;
}
Expand All @@ -613,6 +615,22 @@ LUA_API void *lua_touserdata(lua_State *L, int idx)
return NULL;
}

LUA_API void *(lua_totypeduserdata)(lua_State *L, int idx, unsigned int type)
{
cTValue *o = index2adr(L, idx);
if (tvisudata(o)) {
GCudata *ud = udataV(o);
if (ud->udtype == UDTYPE_TYPED) {
if (ud->len == type)
return uddata(ud);
const struct lua_typeduserdatainfo *info =
(const struct lua_typeduserdatainfo *)gcrefu(ud->env);
return info->type_cast ? info->type_cast(uddata(ud), type) : NULL;
}
}
return NULL;
}

LUA_API lua_State *lua_tothread(lua_State *L, int idx)
{
cTValue *o = index2adr(L, idx);
Expand Down Expand Up @@ -692,12 +710,12 @@ LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction f, int n)
lj_gc_check(L);
lj_checkapi_slot(n);
fn = lj_func_newC(L, (MSize)n, getcurrenv(L));
fn->c.f = f;
fn->c.data->f = f;
L->top -= n;
while (n--)
copyTV(L, &fn->c.upvalue[n], L->top+n);
copyTV(L, &fn->c.data->upvalue[n], L->top + n);
setfuncV(L, L->top, fn);
lj_assertL(iswhite(obj2gco(fn)), "new GC object is not white");
lj_assertL(iswhite(G(L), obj2gco(fn)), "new GC object is not white");
incr_top(L);
}

Expand All @@ -723,6 +741,15 @@ LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
incr_top(L);
}

LUA_API void luaJIT_createtable(lua_State *L, int narray, int nrec)
{
lj_gc_check(L);
settabV(L, L->top,
lj_tab_newgc(L, (uint32_t)(narray > 0 ? narray + 1 : 0),
hsize2hbits(nrec)));
incr_top(L);
}

LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
{
GCtab *regt = tabV(registry(L));
Expand Down Expand Up @@ -768,6 +795,41 @@ LUA_API void *lua_newuserdata(lua_State *L, size_t size)
return uddata(ud);
}

LUA_API void lua_newtypeduserdata(lua_State *L, void *ptr,
const struct lua_typeduserdatainfo *info)
{
GCudata *ud;
lj_checkapi(info && ptr && info->type_id != 0, "bad typed userdata");
lj_gc_check(L);
ud = lj_udata_new(L, 0, NULL);
setudataV(L, L->top, ud);
incr_top(L);
ud->udtype = UDTYPE_TYPED;
ud->len = info->type_id;
setmref(ud->payload, ptr);
setgcrefp(ud->env, info);
}

LUA_API void(lua_releasetypeduserdata)(lua_State *L, int idx)
{
cTValue *o = index2adr(L, idx);
if (tvisudata(o)) {
GCudata *ud = udataV(o);
if (ud->udtype == UDTYPE_TYPED) {
const struct lua_typeduserdatainfo *info = (const struct lua_typeduserdatainfo *)
gcrefu(ud->env);
if (info) {
info->release(uddata(ud));

setmref(ud->payload, NULL);
setgcrefnull(ud->env);
ud->len = 0;
ud->udtype = UDTYPE_USERDATA;
}
}
}
}

LUA_API void lua_concat(lua_State *L, int n)
{
lj_checkapi_slot(n);
Expand Down Expand Up @@ -879,7 +941,10 @@ LUA_API void lua_getfenv(lua_State *L, int idx)
if (tvisfunc(o)) {
settabV(L, L->top, tabref(funcV(o)->c.env));
} else if (tvisudata(o)) {
settabV(L, L->top, tabref(udataV(o)->env));
if (udataV(o)->udtype == UDTYPE_TYPED)
setnilV(L->top);
else
settabV(L, L->top, tabref(udataV(o)->env));
} else if (tvisthread(o)) {
settabV(L, L->top, tabref(threadV(o)->env));
} else {
Expand Down Expand Up @@ -922,7 +987,7 @@ LUA_API void *lua_upvalueid(lua_State *L, int idx, int n)
n--;
lj_checkapi((uint32_t)n < fn->l.nupvalues, "bad upvalue %d", n);
return isluafunc(fn) ? (void *)gcref(fn->l.uvptr[n]) :
(void *)&fn->c.upvalue[n];
(void *)&fn->c.data->upvalue[n];
}

LUA_API void lua_upvaluejoin(lua_State *L, int idx1, int n1, int idx2, int n2)
Expand Down Expand Up @@ -1041,8 +1106,11 @@ LUA_API int lua_setmetatable(lua_State *L, int idx)
lj_gc_objbarriert(L, tabV(o), mt);
} else if (tvisudata(o)) {
setgcref(udataV(o)->metatable, obj2gco(mt));
if (mt)
if (mt) {
if (lj_meta_fastg(g, mt, MM_gc))
lj_mem_registergc_udata(L, udataV(o));
lj_gc_objbarrier(L, udataV(o), mt);
}
} else {
/* Flush cache, since traces specialize to basemt. But not during __gc. */
if (lj_trace_flushall(L))
Expand Down Expand Up @@ -1076,6 +1144,10 @@ LUA_API int lua_setfenv(lua_State *L, int idx)
if (tvisfunc(o)) {
setgcref(funcV(o)->c.env, obj2gco(t));
} else if (tvisudata(o)) {
if (udataV(o)->udtype == UDTYPE_TYPED) {
L->top--;
return 0;
}
setgcref(udataV(o)->env, obj2gco(t));
} else if (tvisthread(o)) {
setgcref(threadV(o)->env, obj2gco(t));
Expand Down Expand Up @@ -1151,7 +1223,7 @@ static TValue *cpcall(lua_State *L, lua_CFunction func, void *ud)
{
GCfunc *fn = lj_func_newC(L, 0, getcurrenv(L));
TValue *top = L->top;
fn->c.f = func;
fn->c.data->f = func;
setfuncV(L, top++, fn);
if (LJ_FR2) setnilV(top++);
#if LJ_64
Expand Down Expand Up @@ -1262,7 +1334,7 @@ LUA_API int lua_gc(lua_State *L, int what, int data)
g->gc.threshold = data == -1 ? (g->gc.total/100)*g->gc.pause : g->gc.total;
break;
case LUA_GCCOLLECT:
lj_gc_fullgc(L);
lj_gc_fullgc(L, data);
break;
case LUA_GCCOUNT:
res = (int)(g->gc.total >> 10);
Expand Down Expand Up @@ -1311,3 +1383,74 @@ LUA_API void lua_setallocf(lua_State *L, lua_Alloc f, void *ud)
g->allocf = f;
}

LUA_API size_t luaJIT_getpagesize()
{
return ARENA_SIZE;
}

LUA_API uint64_t lj_internal_getstack(lua_State *L, int index)
{
TValue *o = index2adr(L, index);
return o->u64;
}

LUA_API void lj_internal_setstack(lua_State *L, int index, uint64_t tv)
{
TValue t;
t.u64 = tv;
copy_slot(L, &t, index);
}

LUA_API void lj_internal_pushraw(lua_State *L, uint64_t tv)
{
TValue t;
t.u64 = tv;
copyTV(L, L->top, &t);
incr_top(L);
}

LUA_API void *lj_internal_mt__index(const lua_State *L, void *mt)
{
return (void *)lj_meta_fast(L, (GCtab *)mt, MM_index);
}

LUA_API void *lj_internal_getstr(const lua_State *L, const char *s, size_t n)
{
return lj_str_new((lua_State*)L, s, n);
}

LUA_API void *lj_internal_newtab(const lua_State *L)
{
return lj_tab_new((lua_State *)L, 0, 0);
}

LUA_API void lj_internal_rawset(const lua_State *cL, void *t, uint64_t k, uint64_t v)
{
lua_State *L = (lua_State *)cL;
TValue key, val;
TValue *dst;
key.u64 = k;
val.u64 = v;
dst = lj_tab_set(L, (GCtab*)t, &key);
copyTV(L, dst, &val);
lj_gc_anybarriert(L, t);
}

LUA_API void lj_internal_setmetatable(const lua_State *cL, void *rawt, void *rawmt)
{
GCtab *t = (GCtab *)rawt;
GCtab *mt = (GCtab *)rawmt;
lua_State *L = (lua_State *)cL;
setgcref(t->metatable, obj2gco(mt));
if(mt)
lj_gc_objbarriert(L, t, mt);
}

LUA_API int lj_internal_bindfunc(lua_State *L, void *clib, const char *name, size_t namelen,
const char *cdef, void *impl)
{
GCstr *namestr = name ? lj_str_new(L, name, namelen) : NULL;
if (!!name != !!impl)
return LUA_ERRERR;
return lj_clib_define_symbol(L, (CLibrary *)clib, cdef, namestr, impl);
}
Loading