Some usefull feature from lua 5.2

  • From: Denis F <deniska.nm@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Wed, 8 Jul 2015 08:48:25 +0300

Hello, All :)

LUA 5.2 have very usefull feature. It helps to associete some user context
with lua_state.
Short description from lua docs.
http://www.lua.org/manual/5.3/manual.html#lua_getextraspace
http://www.lua.org/source/5.3/luaconf.h.html#LUA_EXTRASPACE

I think this is not so difficult and doesn't influence luajit internal
functionality.

Mike, can you add this feature to luajit?
I wrote little patch for luajit 2.0.4 from lua 5.2 source code.

Sorry for my poor english.
Thanks for usefull project :)

-=-=-=-=-=-= PATCH START =-=-=-=-=-=-=-
--- src/lj_dispatch.h Thu May 14 21:30:00 2015
+++ src/lj_dispatch.h Mon Jul 06 15:41:36 2015
@@ -66,9 +66,20 @@
#define GG_LEN_SDISP BC_FUNCF
#define GG_LEN_DISP (GG_LEN_DDISP + GG_LEN_SDISP)

+/*
+** thread state + extra space
+*/
+typedef struct LX {
+ char extra_[LUA_EXTRASPACE];
+ lua_State l;
+} LX;
+
+#define fromstate(L) ((LX*)(((char *)(L)) - offsetof(LX, l)))
+#define tostate(L) (&((L)->l))
+
/* Global state, main thread and extra fields are allocated together. */
typedef struct GG_State {
- lua_State L; /* Main thread. */
+ LX L; /* Main thread. */
global_State g; /* Global state. */
#if LJ_TARGET_MIPS
ASMFunction got[LJ_GOT__MAX]; /* Global offset table. */
--- src/lj_state.c Thu May 14 21:30:00 2015
+++ src/lj_state.c Mon Jul 06 15:45:44 2015
@@ -182,7 +182,7 @@
#endif
{
GG_State *GG = (GG_State *)f(ud, NULL, 0, sizeof(GG_State));
- lua_State *L = &GG->L;
+ lua_State *L = tostate(&GG->L);
global_State *g = &GG->g;
if (GG == NULL || !checkptr32(GG)) return NULL;
memset(GG, 0, sizeof(GG_State));
@@ -210,7 +210,7 @@
g->gc.total = sizeof(GG_State);
g->gc.pause = LUAI_GCPAUSE;
g->gc.stepmul = LUAI_GCMUL;
- lj_dispatch_init((GG_State *)L);
+ lj_dispatch_init((GG_State *)fromstate(L));
L->status = LUA_ERRERR+1; /* Avoid touching the stack upon memory
error. */
if (lj_vm_cpcall(L, NULL, NULL, cpluaopen) != 0) {
/* Memory allocation error: free partial state. */
@@ -260,7 +260,7 @@

lua_State *lj_state_new(lua_State *L)
{
- lua_State *L1 = lj_mem_newobj(L, lua_State);
+ lua_State *L1 = tostate(lj_mem_newobj(L, lua_State));
L1->gct = ~LJ_TTHREAD;
L1->dummy_ffid = FF_C;
L1->status = 0;
@@ -272,6 +272,7 @@
setmrefr(L1->glref, L->glref);
setgcrefr(L1->env, L->env);
stack_init(L1, L); /* init stack */
+ memcpy(lua_getextraspace(L1), lua_getextraspace(mainthread(G(L))),
LUA_EXTRASPACE);
lua_assert(iswhite(obj2gco(L1)));
return L1;
}
@@ -282,6 +283,6 @@
lj_func_closeuv(L, tvref(L->stack));
lua_assert(gcref(L->openupval) == NULL);
lj_mem_freevec(g, tvref(L->stack), L->stacksize, TValue);
- lj_mem_freet(g, L);
+ lj_mem_freet(g, fromstate(L));
}

--- src/lua.h Thu May 14 21:30:00 2015
+++ src/lua.h Mon Jul 06 15:49:20 2015
@@ -278,7 +278,13 @@

#define lua_tostring(L,i) lua_tolstring(L, (i), NULL)

-
+/*
+** LUA_EXTRASPACE defines the size of a raw memory area associated with
+** a Lua state with very fast access.
+** CHANGE it if you need a different size.
+*/
+#define LUA_EXTRASPACE (sizeof(void *))
+#define lua_getextraspace(L) ((void *)((char *)(L) - LUA_EXTRASPACE))

/*
** compatibility macros and functions
-=-=-=-=-=-= PATCH END =-=-=-=-=-=-=-

Other related posts: