Skip to content

Commit

Permalink
Rework the persistence mechanism of private C functions.
Browse files Browse the repository at this point in the history
This modification takes the number of modified Lua files from 8
down to 3 and makes the patch slightly less intrusive.
  • Loading branch information
samhocevar committed Feb 12, 2020
1 parent 1566f6c commit 7ebf861
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 145 deletions.
28 changes: 1 addition & 27 deletions src/eris.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ typedef int bool;
#endif

/* Mark us as part of the Lua core to get access to what we need. */
#define eris_c
#define LUA_CORE

/* Public Lua headers. */
Expand Down Expand Up @@ -150,33 +151,6 @@ static const lua_Unsigned kMaxComplexity = 10000;
#define eris_savestackidx(L, p) ((p) - (L)->stack)
#define eris_restorestackidx(L, n) ((L)->stack + (n))

/* Enabled if we have a patched version of Lua (for accessing internals). */
#if 1

/* Functions in Lua libraries used to access C functions we need to add to the
* permanents table to fully support yielded coroutines. */
extern void eris_permbaselib(lua_State *L, int forUnpersist);
extern void eris_permcorolib(lua_State *L, int forUnpersist);
extern void eris_permloadlib(lua_State *L, int forUnpersist);
extern void eris_permiolib(lua_State *L, int forUnpersist);
extern void eris_permstrlib(lua_State *L, int forUnpersist);

/* Utility macro for populating the perms table with internal C functions. */
#define populateperms(L, forUnpersist) {\
eris_permbaselib(L, forUnpersist);\
eris_permcorolib(L, forUnpersist);\
eris_permloadlib(L, forUnpersist);\
eris_permiolib(L, forUnpersist);\
eris_permstrlib(L, forUnpersist);\
}

#else

/* Does nothing if we don't have a patched version of Lua. */
#define populateperms(L, forUnpersist) ((void)0)

#endif

/*
** ============================================================================
** Language strings for errors.
Expand Down
36 changes: 0 additions & 36 deletions src/lbaselib.c
Original file line number Diff line number Diff line change
Expand Up @@ -456,39 +456,3 @@ LUAMOD_API int luaopen_base (lua_State *L) {
return 1;
}


void eris_permbaselib(lua_State *L, int forUnpersist) {
luaL_checktype(L, -1, LUA_TTABLE);
luaL_checkstack(L, 2, NULL);

if (forUnpersist) {
lua_pushstring(L, "__eris.baselib_pcallcont");
lua_pushcfunction(L, pcallcont);
}
else {
lua_pushcfunction(L, pcallcont);
lua_pushstring(L, "__eris.baselib_pcallcont");
}
lua_rawset(L, -3);

if (forUnpersist) {
lua_pushstring(L, "__eris.baselib_luaB_next");
lua_pushcfunction(L, luaB_next);
}
else {
lua_pushcfunction(L, luaB_next);
lua_pushstring(L, "__eris.baselib_luaB_next");
}
lua_rawset(L, -3);

if (forUnpersist) {
lua_pushstring(L, "__eris.baselib_ipairsaux");
lua_pushcfunction(L, ipairsaux);
}
else {
lua_pushcfunction(L, ipairsaux);
lua_pushstring(L, "__eris.baselib_ipairsaux");
}
lua_rawset(L, -3);
}

16 changes: 0 additions & 16 deletions src/lcorolib.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,3 @@ LUAMOD_API int luaopen_coroutine (lua_State *L) {
return 1;
}


void eris_permcorolib(lua_State *L, int forUnpersist) {
luaL_checktype(L, -1, LUA_TTABLE);
luaL_checkstack(L, 2, NULL);

if (forUnpersist) {
lua_pushstring(L, "__eris.corolib_luaB_auxwrap");
lua_pushcfunction(L, luaB_auxwrap);
}
else {
lua_pushcfunction(L, luaB_auxwrap);
lua_pushstring(L, "__eris.corolib_luaB_auxwrap");
}
lua_rawset(L, -3);
}

16 changes: 0 additions & 16 deletions src/liolib.c
Original file line number Diff line number Diff line change
Expand Up @@ -664,19 +664,3 @@ LUAMOD_API int luaopen_io (lua_State *L) {
return 1;
}


void eris_permiolib(lua_State *L, int forUnpersist) {
luaL_checktype(L, -1, LUA_TTABLE);
luaL_checkstack(L, 2, NULL);

if (forUnpersist) {
lua_pushstring(L, "__eris.iolib_io_readline");
lua_pushcfunction(L, io_readline);
}
else {
lua_pushcfunction(L, io_readline);
lua_pushstring(L, "__eris.iolib_io_readline");
}
lua_rawset(L, -3);
}

34 changes: 0 additions & 34 deletions src/loadlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -723,37 +723,3 @@ LUAMOD_API int luaopen_package (lua_State *L) {
return 1; /* return 'package' table */
}


void eris_permloadlib(lua_State *L, int forUnpersist) {
static const lua_CFunction searchers[] = {
searcher_preload,
searcher_Lua,
searcher_C,
searcher_Croot,
NULL
};
static const char *const searchernames[] = {
"__eris.loadlib_searcher_preload",
"__eris.loadlib_searcher_Lua",
"__eris.loadlib_searcher_C",
"__eris.loadlib_searcher_Croot",
NULL
};
int i;

luaL_checktype(L, -1, LUA_TTABLE);
luaL_checkstack(L, 2, NULL);

for (i = 0; searchers[i]; ++i) {
if (forUnpersist) {
lua_pushstring(L, searchernames[i]);
lua_pushcfunction(L, searchers[i]);
}
else {
lua_pushcfunction(L, searchers[i]);
lua_pushstring(L, searchernames[i]);
}
lua_rawset(L, -3);
}
}

16 changes: 0 additions & 16 deletions src/lstrlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1017,19 +1017,3 @@ LUAMOD_API int luaopen_string (lua_State *L) {
return 1;
}


void eris_permstrlib(lua_State *L, int forUnpersist) {
luaL_checktype(L, -1, LUA_TTABLE);
luaL_checkstack(L, 2, NULL);

if (forUnpersist) {
lua_pushstring(L, "__eris.strlib_gmatch_aux");
lua_pushcfunction(L, gmatch_aux);
}
else {
lua_pushcfunction(L, gmatch_aux);
lua_pushstring(L, "__eris.strlib_gmatch_aux");
}
lua_rawset(L, -3);
}

60 changes: 60 additions & 0 deletions src/lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,66 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);



/*
** =======================================================================
** Eris persistence
** =======================================================================
*/

#if defined(eris_c)
/* Utility macro for populating the perms table with internal C functions. */
# define eris_persist(lib, fn)\
extern lua_CFunction __perm_##lib##_##fn;\
if (forUnpersist) {\
lua_pushstring(L, "__eris." #lib "_" #fn);\
lua_pushcfunction(L, *__perm_##lib##_##fn);\
}\
else {\
lua_pushcfunction(L, *__perm_##lib##_##fn);\
lua_pushstring(L, "__eris." #lib "_" #fn);\
}\
lua_rawset(L, -3);
#else
/* Utility macro to export internal C functions to eris. */
# define eris_persist(lib, fn)\
static int fn (lua_State *L);\
lua_CFunction __perm_##lib##_##fn = fn;
#endif

#if defined(eris_c)
/* Functions in Lua libraries used to access C functions we need to add to the
* permanents table to fully support yielded coroutines. */
static void populateperms(lua_State *L, bool forUnpersist)
{
#endif
#if defined(eris_c) || defined(lstrlib_c)
eris_persist(strlib, gmatch_aux)
#endif
#if defined(eris_c) || defined(loadlib_c)
eris_persist(loadlib, searcher_preload)
eris_persist(loadlib, searcher_Lua)
eris_persist(loadlib, searcher_C)
eris_persist(loadlib, searcher_Croot)
#endif
#if defined(eris_c) || defined(liolib_c)
eris_persist(iolib, io_readline)
#endif
#if defined(eris_c) || defined(lbaselib_c)
eris_persist(baselib, pcallcont)
eris_persist(baselib, luaB_next)
eris_persist(baselib, ipairsaux)
#endif
#if defined(eris_c) || defined(lcorolib_c)
eris_persist(corolib, luaB_auxwrap)
#endif
#if defined(eris_c)
}
#endif

#undef eris_persist



/*
** {======================================================================
** Debug API
Expand Down

0 comments on commit 7ebf861

Please sign in to comment.