diff options
| author | Mike Pall <mike> | 2013-11-25 15:17:44 +0100 |
|---|---|---|
| committer | Mike Pall <mike> | 2013-11-25 15:17:44 +0100 |
| commit | c378f7dbb882068d1ae06edc8e7049fb1907db2d (patch) | |
| tree | 2a99541a907ac84a3be740ef67dfadb640832dce /src | |
| parent | a8c3862d63ad60acf07252304bbfd74bce35371c (diff) | |
| download | luajit-c378f7dbb882068d1ae06edc8e7049fb1907db2d.tar.gz luajit-c378f7dbb882068d1ae06edc8e7049fb1907db2d.tar.bz2 luajit-c378f7dbb882068d1ae06edc8e7049fb1907db2d.zip | |
Abstract out post-registration handling of pre-registered modules.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib_table.c | 7 | ||||
| -rw-r--r-- | src/lj_lib.c | 21 | ||||
| -rw-r--r-- | src/lj_lib.h | 15 |
3 files changed, 25 insertions, 18 deletions
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(.) | |||
| 275 | 275 | ||
| 276 | static int luaopen_table_new(lua_State *L) | 276 | static int luaopen_table_new(lua_State *L) |
| 277 | { | 277 | { |
| 278 | GCfunc *fn = lj_lib_pushcc(L, lj_cf_table_new, FF_table_new, 0); | 278 | return lj_lib_postreg(L, lj_cf_table_new, FF_table_new, "new"); |
| 279 | GCtab *t = tabref(curr_func(L)->c.env); /* Reference to "table". */ | ||
| 280 | setfuncV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "new")), fn); | ||
| 281 | lj_gc_anybarriert(L, t); | ||
| 282 | setfuncV(L, L->top++, fn); | ||
| 283 | return 1; | ||
| 284 | } | 279 | } |
| 285 | 280 | ||
| 286 | /* ------------------------------------------------------------------------ */ | 281 | /* ------------------------------------------------------------------------ */ |
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, | |||
| 148 | } | 148 | } |
| 149 | } | 149 | } |
| 150 | 150 | ||
| 151 | /* Push internal function on the stack. */ | ||
| 152 | GCfunc *lj_lib_pushcc(lua_State *L, lua_CFunction f, int id, int n) | ||
| 153 | { | ||
| 154 | GCfunc *fn; | ||
| 155 | lua_pushcclosure(L, f, n); | ||
| 156 | fn = funcV(L->top-1); | ||
| 157 | fn->c.ffid = (uint8_t)id; | ||
| 158 | setmref(fn->c.pc, &G(L)->bc_cfunc_int); | ||
| 159 | return fn; | ||
| 160 | } | ||
| 161 | |||
| 151 | void lj_lib_prereg(lua_State *L, const char *name, lua_CFunction f, GCtab *env) | 162 | void lj_lib_prereg(lua_State *L, const char *name, lua_CFunction f, GCtab *env) |
| 152 | { | 163 | { |
| 153 | luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", 4); | 164 | 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) | |||
| 158 | L->top--; | 169 | L->top--; |
| 159 | } | 170 | } |
| 160 | 171 | ||
| 172 | int lj_lib_postreg(lua_State *L, lua_CFunction cf, int id, const char *name) | ||
| 173 | { | ||
| 174 | GCfunc *fn = lj_lib_pushcf(L, cf, id); | ||
| 175 | GCtab *t = tabref(curr_func(L)->c.env); /* Reference to parent table. */ | ||
| 176 | setfuncV(L, lj_tab_setstr(L, t, lj_str_newz(L, name)), fn); | ||
| 177 | lj_gc_anybarriert(L, t); | ||
| 178 | setfuncV(L, L->top++, fn); | ||
| 179 | return 1; | ||
| 180 | } | ||
| 181 | |||
| 161 | /* -- Type checks --------------------------------------------------------- */ | 182 | /* -- Type checks --------------------------------------------------------- */ |
| 162 | 183 | ||
| 163 | TValue *lj_lib_checkany(lua_State *L, int narg) | 184 | 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); | |||
| 59 | #define lj_lib_checkfpu(L) UNUSED(L) | 59 | #define lj_lib_checkfpu(L) UNUSED(L) |
| 60 | #endif | 60 | #endif |
| 61 | 61 | ||
| 62 | /* Push internal function on the stack. */ | 62 | LJ_FUNC GCfunc *lj_lib_pushcc(lua_State *L, lua_CFunction f, int id, int n); |
| 63 | static LJ_AINLINE GCfunc *lj_lib_pushcc(lua_State *L, lua_CFunction f, | ||
| 64 | int id, int n) | ||
| 65 | { | ||
| 66 | GCfunc *fn; | ||
| 67 | lua_pushcclosure(L, f, n); | ||
| 68 | fn = funcV(L->top-1); | ||
| 69 | fn->c.ffid = (uint8_t)id; | ||
| 70 | setmref(fn->c.pc, &G(L)->bc_cfunc_int); | ||
| 71 | return fn; | ||
| 72 | } | ||
| 73 | |||
| 74 | #define lj_lib_pushcf(L, fn, id) (lj_lib_pushcc(L, (fn), (id), 0)) | 63 | #define lj_lib_pushcf(L, fn, id) (lj_lib_pushcc(L, (fn), (id), 0)) |
| 75 | 64 | ||
| 76 | /* Library function declarations. Scanned by buildvm. */ | 65 | /* Library function declarations. Scanned by buildvm. */ |
| @@ -91,6 +80,8 @@ LJ_FUNC void lj_lib_register(lua_State *L, const char *libname, | |||
| 91 | const uint8_t *init, const lua_CFunction *cf); | 80 | const uint8_t *init, const lua_CFunction *cf); |
| 92 | LJ_FUNC void lj_lib_prereg(lua_State *L, const char *name, lua_CFunction f, | 81 | LJ_FUNC void lj_lib_prereg(lua_State *L, const char *name, lua_CFunction f, |
| 93 | GCtab *env); | 82 | GCtab *env); |
| 83 | LJ_FUNC int lj_lib_postreg(lua_State *L, lua_CFunction cf, int id, | ||
| 84 | const char *name); | ||
| 94 | 85 | ||
| 95 | /* Library init data tags. */ | 86 | /* Library init data tags. */ |
| 96 | #define LIBINIT_LENMASK 0x3f | 87 | #define LIBINIT_LENMASK 0x3f |
