Browse Source

Abstract out post-registration handling of pre-registered modules.

Mike Pall 11 years ago
parent
commit
c378f7dbb8
3 changed files with 25 additions and 18 deletions
  1. 1 6
      src/lib_table.c
  2. 21 0
      src/lj_lib.c
  3. 3 12
      src/lj_lib.h

+ 1 - 6
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");
 }
 
 /* ------------------------------------------------------------------------ */

+ 21 - 0
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)

+ 3 - 12
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