diff options
Diffstat (limited to 'src/lib_aux.c')
-rw-r--r-- | src/lib_aux.c | 74 |
1 files changed, 46 insertions, 28 deletions
diff --git a/src/lib_aux.c b/src/lib_aux.c index 7af1cd43..2682a38a 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 | } | 135 | } |
134 | for (; l->name; l++) { | 136 | if (l) |
135 | int i; | 137 | luaL_setfuncs(L, l, nup); |
136 | for (i = 0; i < nup; i++) /* copy upvalues to the top */ | 138 | else |
137 | lua_pushvalue(L, -nup); | 139 | lua_pop(L, nup); /* Remove upvalues. */ |
138 | lua_pushcclosure(L, l->func, nup); | ||
139 | lua_setfield(L, -(nup+2), l->name); | ||
140 | } | ||
141 | 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 | { |
@@ -207,8 +218,15 @@ LUALIB_API char *luaL_prepbuffer(luaL_Buffer *B) | |||
207 | 218 | ||
208 | LUALIB_API void luaL_addlstring(luaL_Buffer *B, const char *s, size_t l) | 219 | LUALIB_API void luaL_addlstring(luaL_Buffer *B, const char *s, size_t l) |
209 | { | 220 | { |
210 | while (l--) | 221 | if (l <= bufffree(B)) { |
211 | luaL_addchar(B, *s++); | 222 | memcpy(B->p, s, l); |
223 | B->p += l; | ||
224 | } else { | ||
225 | emptybuffer(B); | ||
226 | lua_pushlstring(B->L, s, l); | ||
227 | B->lvl++; | ||
228 | adjuststack(B); | ||
229 | } | ||
212 | } | 230 | } |
213 | 231 | ||
214 | LUALIB_API void luaL_addstring(luaL_Buffer *B, const char *s) | 232 | LUALIB_API void luaL_addstring(luaL_Buffer *B, const char *s) |
@@ -302,7 +320,7 @@ static int panic(lua_State *L) | |||
302 | 320 | ||
303 | #ifdef LUAJIT_USE_SYSMALLOC | 321 | #ifdef LUAJIT_USE_SYSMALLOC |
304 | 322 | ||
305 | #if LJ_64 && !defined(LUAJIT_USE_VALGRIND) | 323 | #if LJ_64 && !LJ_GC64 && !defined(LUAJIT_USE_VALGRIND) |
306 | #error "Must use builtin allocator for 64 bit target" | 324 | #error "Must use builtin allocator for 64 bit target" |
307 | #endif | 325 | #endif |
308 | 326 | ||
@@ -334,7 +352,7 @@ LUALIB_API lua_State *luaL_newstate(void) | |||
334 | lua_State *L; | 352 | lua_State *L; |
335 | void *ud = lj_alloc_create(); | 353 | void *ud = lj_alloc_create(); |
336 | if (ud == NULL) return NULL; | 354 | if (ud == NULL) return NULL; |
337 | #if LJ_64 | 355 | #if LJ_64 && !LJ_GC64 |
338 | L = lj_state_newstate(lj_alloc_f, ud); | 356 | L = lj_state_newstate(lj_alloc_f, ud); |
339 | #else | 357 | #else |
340 | L = lua_newstate(lj_alloc_f, ud); | 358 | L = lua_newstate(lj_alloc_f, ud); |
@@ -343,7 +361,7 @@ LUALIB_API lua_State *luaL_newstate(void) | |||
343 | return L; | 361 | return L; |
344 | } | 362 | } |
345 | 363 | ||
346 | #if LJ_64 | 364 | #if LJ_64 && !LJ_GC64 |
347 | LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud) | 365 | LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud) |
348 | { | 366 | { |
349 | UNUSED(f); UNUSED(ud); | 367 | UNUSED(f); UNUSED(ud); |