From c378f7dbb882068d1ae06edc8e7049fb1907db2d Mon Sep 17 00:00:00 2001
From: Mike Pall <mike>
Date: Mon, 25 Nov 2013 15:17:44 +0100
Subject: Abstract out post-registration handling of pre-registered modules.

---
 src/lib_table.c |  7 +------
 src/lj_lib.c    | 21 +++++++++++++++++++++
 src/lj_lib.h    | 15 +++------------
 3 files changed, 25 insertions(+), 18 deletions(-)

(limited to 'src')

diff --git a/src/lib_table.c b/src/lib_table.c
index 85ca660e..91c022bb 100644
--- a/src/lib_table.c
+++ b/src/lib_table.c
@@ -275,12 +275,7 @@ LJLIB_NOREG LJLIB_CF(table_new)		LJLIB_REC(.)
 
 static int luaopen_table_new(lua_State *L)
 {
-  GCfunc *fn = lj_lib_pushcc(L, lj_cf_table_new, FF_table_new, 0);
-  GCtab *t = tabref(curr_func(L)->c.env);  /* Reference to "table". */
-  setfuncV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "new")), fn);
-  lj_gc_anybarriert(L, t);
-  setfuncV(L, L->top++, fn);
-  return 1;
+  return lj_lib_postreg(L, lj_cf_table_new, FF_table_new, "new");
 }
 
 /* ------------------------------------------------------------------------ */
diff --git a/src/lj_lib.c b/src/lj_lib.c
index df66ed51..aea96120 100644
--- a/src/lj_lib.c
+++ b/src/lj_lib.c
@@ -148,6 +148,17 @@ void lj_lib_register(lua_State *L, const char *libname,
   }
 }
 
+/* Push internal function on the stack. */
+GCfunc *lj_lib_pushcc(lua_State *L, lua_CFunction f, int id, int n)
+{
+  GCfunc *fn;
+  lua_pushcclosure(L, f, n);
+  fn = funcV(L->top-1);
+  fn->c.ffid = (uint8_t)id;
+  setmref(fn->c.pc, &G(L)->bc_cfunc_int);
+  return fn;
+}
+
 void lj_lib_prereg(lua_State *L, const char *name, lua_CFunction f, GCtab *env)
 {
   luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", 4);
@@ -158,6 +169,16 @@ void lj_lib_prereg(lua_State *L, const char *name, lua_CFunction f, GCtab *env)
   L->top--;
 }
 
+int lj_lib_postreg(lua_State *L, lua_CFunction cf, int id, const char *name)
+{
+  GCfunc *fn = lj_lib_pushcf(L, cf, id);
+  GCtab *t = tabref(curr_func(L)->c.env);  /* Reference to parent table. */
+  setfuncV(L, lj_tab_setstr(L, t, lj_str_newz(L, name)), fn);
+  lj_gc_anybarriert(L, t);
+  setfuncV(L, L->top++, fn);
+  return 1;
+}
+
 /* -- Type checks --------------------------------------------------------- */
 
 TValue *lj_lib_checkany(lua_State *L, int narg)
diff --git a/src/lj_lib.h b/src/lj_lib.h
index f9377bad..c77c5229 100644
--- a/src/lj_lib.h
+++ b/src/lj_lib.h
@@ -59,18 +59,7 @@ LJ_FUNC int lj_lib_checkopt(lua_State *L, int narg, int def, const char *lst);
 #define lj_lib_checkfpu(L)	UNUSED(L)
 #endif
 
-/* Push internal function on the stack. */
-static LJ_AINLINE GCfunc *lj_lib_pushcc(lua_State *L, lua_CFunction f,
-					int id, int n)
-{
-  GCfunc *fn;
-  lua_pushcclosure(L, f, n);
-  fn = funcV(L->top-1);
-  fn->c.ffid = (uint8_t)id;
-  setmref(fn->c.pc, &G(L)->bc_cfunc_int);
-  return fn;
-}
-
+LJ_FUNC GCfunc *lj_lib_pushcc(lua_State *L, lua_CFunction f, int id, int n);
 #define lj_lib_pushcf(L, fn, id)	(lj_lib_pushcc(L, (fn), (id), 0))
 
 /* Library function declarations. Scanned by buildvm. */
@@ -91,6 +80,8 @@ LJ_FUNC void lj_lib_register(lua_State *L, const char *libname,
 			     const uint8_t *init, const lua_CFunction *cf);
 LJ_FUNC void lj_lib_prereg(lua_State *L, const char *name, lua_CFunction f,
 			   GCtab *env);
+LJ_FUNC int lj_lib_postreg(lua_State *L, lua_CFunction cf, int id,
+			   const char *name);
 
 /* Library init data tags. */
 #define LIBINIT_LENMASK	0x3f
-- 
cgit v1.2.3-55-g6feb