diff options
Diffstat (limited to 'src/lib_aux.c')
-rw-r--r-- | src/lib_aux.c | 63 |
1 files changed, 37 insertions, 26 deletions
diff --git a/src/lib_aux.c b/src/lib_aux.c index 7af1cd43..c40565c3 100644 --- a/src/lib_aux.c +++ b/src/lib_aux.c | |||
@@ -107,38 +107,36 @@ LUALIB_API const char *luaL_findtable(lua_State *L, int idx, | |||
107 | static int libsize(const luaL_Reg *l) | 107 | static int libsize(const luaL_Reg *l) |
108 | { | 108 | { |
109 | int size = 0; | 109 | int size = 0; |
110 | for (; l->name; l++) size++; | 110 | for (; l && l->name; l++) size++; |
111 | return size; | 111 | return size; |
112 | } | 112 | } |
113 | 113 | ||
114 | LUALIB_API void luaL_pushmodule(lua_State *L, const char *modname, int sizehint) | ||
115 | { | ||
116 | luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16); | ||
117 | lua_getfield(L, -1, modname); | ||
118 | if (!lua_istable(L, -1)) { | ||
119 | lua_pop(L, 1); | ||
120 | if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, sizehint) != NULL) | ||
121 | lj_err_callerv(L, LJ_ERR_BADMODN, modname); | ||
122 | lua_pushvalue(L, -1); | ||
123 | lua_setfield(L, -3, modname); /* _LOADED[modname] = new table. */ | ||
124 | } | ||
125 | lua_remove(L, -2); /* Remove _LOADED table. */ | ||
126 | } | ||
127 | |||
114 | LUALIB_API void luaL_openlib(lua_State *L, const char *libname, | 128 | LUALIB_API void luaL_openlib(lua_State *L, const char *libname, |
115 | const luaL_Reg *l, int nup) | 129 | const luaL_Reg *l, int nup) |
116 | { | 130 | { |
117 | lj_lib_checkfpu(L); | 131 | lj_lib_checkfpu(L); |
118 | if (libname) { | 132 | if (libname) { |
119 | int size = libsize(l); | 133 | luaL_pushmodule(L, libname, libsize(l)); |
120 | /* check whether lib already exists */ | 134 | lua_insert(L, -(nup + 1)); /* Move module table below upvalues. */ |
121 | luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16); | ||
122 | lua_getfield(L, -1, libname); /* get _LOADED[libname] */ | ||
123 | if (!lua_istable(L, -1)) { /* not found? */ | ||
124 | lua_pop(L, 1); /* remove previous result */ | ||
125 | /* try global variable (and create one if it does not exist) */ | ||
126 | if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL) | ||
127 | lj_err_callerv(L, LJ_ERR_BADMODN, libname); | ||
128 | lua_pushvalue(L, -1); | ||
129 | lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ | ||
130 | } | ||
131 | lua_remove(L, -2); /* remove _LOADED table */ | ||
132 | lua_insert(L, -(nup+1)); /* move library table to below upvalues */ | ||
133 | } | ||
134 | for (; l->name; l++) { | ||
135 | int i; | ||
136 | for (i = 0; i < nup; i++) /* copy upvalues to the top */ | ||
137 | lua_pushvalue(L, -nup); | ||
138 | lua_pushcclosure(L, l->func, nup); | ||
139 | lua_setfield(L, -(nup+2), l->name); | ||
140 | } | 135 | } |
141 | lua_pop(L, nup); /* remove upvalues */ | 136 | if (l) |
137 | luaL_setfuncs(L, l, nup); | ||
138 | else | ||
139 | lua_pop(L, nup); /* Remove upvalues. */ | ||
142 | } | 140 | } |
143 | 141 | ||
144 | LUALIB_API void luaL_register(lua_State *L, const char *libname, | 142 | LUALIB_API void luaL_register(lua_State *L, const char *libname, |
@@ -147,6 +145,19 @@ LUALIB_API void luaL_register(lua_State *L, const char *libname, | |||
147 | luaL_openlib(L, libname, l, 0); | 145 | luaL_openlib(L, libname, l, 0); |
148 | } | 146 | } |
149 | 147 | ||
148 | LUALIB_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup) | ||
149 | { | ||
150 | luaL_checkstack(L, nup, "too many upvalues"); | ||
151 | for (; l->name; l++) { | ||
152 | int i; | ||
153 | for (i = 0; i < nup; i++) /* Copy upvalues to the top. */ | ||
154 | lua_pushvalue(L, -nup); | ||
155 | lua_pushcclosure(L, l->func, nup); | ||
156 | lua_setfield(L, -(nup + 2), l->name); | ||
157 | } | ||
158 | lua_pop(L, nup); /* Remove upvalues. */ | ||
159 | } | ||
160 | |||
150 | LUALIB_API const char *luaL_gsub(lua_State *L, const char *s, | 161 | LUALIB_API const char *luaL_gsub(lua_State *L, const char *s, |
151 | const char *p, const char *r) | 162 | const char *p, const char *r) |
152 | { | 163 | { |
@@ -302,7 +313,7 @@ static int panic(lua_State *L) | |||
302 | 313 | ||
303 | #ifdef LUAJIT_USE_SYSMALLOC | 314 | #ifdef LUAJIT_USE_SYSMALLOC |
304 | 315 | ||
305 | #if LJ_64 && !defined(LUAJIT_USE_VALGRIND) | 316 | #if LJ_64 && !LJ_GC64 && !defined(LUAJIT_USE_VALGRIND) |
306 | #error "Must use builtin allocator for 64 bit target" | 317 | #error "Must use builtin allocator for 64 bit target" |
307 | #endif | 318 | #endif |
308 | 319 | ||
@@ -334,7 +345,7 @@ LUALIB_API lua_State *luaL_newstate(void) | |||
334 | lua_State *L; | 345 | lua_State *L; |
335 | void *ud = lj_alloc_create(); | 346 | void *ud = lj_alloc_create(); |
336 | if (ud == NULL) return NULL; | 347 | if (ud == NULL) return NULL; |
337 | #if LJ_64 | 348 | #if LJ_64 && !LJ_GC64 |
338 | L = lj_state_newstate(lj_alloc_f, ud); | 349 | L = lj_state_newstate(lj_alloc_f, ud); |
339 | #else | 350 | #else |
340 | L = lua_newstate(lj_alloc_f, ud); | 351 | L = lua_newstate(lj_alloc_f, ud); |
@@ -343,7 +354,7 @@ LUALIB_API lua_State *luaL_newstate(void) | |||
343 | return L; | 354 | return L; |
344 | } | 355 | } |
345 | 356 | ||
346 | #if LJ_64 | 357 | #if LJ_64 && !LJ_GC64 |
347 | LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud) | 358 | LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud) |
348 | { | 359 | { |
349 | UNUSED(f); UNUSED(ud); | 360 | UNUSED(f); UNUSED(ud); |