/src/testdir/build/lua-master/source/lauxlib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ** $Id: lauxlib.c $ |
3 | | ** Auxiliary functions for building Lua libraries |
4 | | ** See Copyright Notice in lua.h |
5 | | */ |
6 | | |
7 | | #define lauxlib_c |
8 | | #define LUA_LIB |
9 | | |
10 | | #include "lprefix.h" |
11 | | |
12 | | |
13 | | #include <errno.h> |
14 | | #include <stdarg.h> |
15 | | #include <stdio.h> |
16 | | #include <stdlib.h> |
17 | | #include <string.h> |
18 | | |
19 | | |
20 | | /* |
21 | | ** This file uses only the official API of Lua. |
22 | | ** Any function declared here could be written as an application function. |
23 | | */ |
24 | | |
25 | | #include "lua.h" |
26 | | |
27 | | #include "lauxlib.h" |
28 | | |
29 | | |
30 | | #if !defined(MAX_SIZET) |
31 | | /* maximum value for size_t */ |
32 | | #define MAX_SIZET ((size_t)(~(size_t)0)) |
33 | | #endif |
34 | | |
35 | | |
36 | | /* |
37 | | ** {====================================================== |
38 | | ** Traceback |
39 | | ** ======================================================= |
40 | | */ |
41 | | |
42 | | |
43 | 402 | #define LEVELS1 10 /* size of the first part of the stack */ |
44 | 402 | #define LEVELS2 11 /* size of the second part of the stack */ |
45 | | |
46 | | |
47 | | |
48 | | /* |
49 | | ** Search for 'objidx' in table at index -1. ('objidx' must be an |
50 | | ** absolute index.) Return 1 + string at top if it found a good name. |
51 | | */ |
52 | 643k | static int findfield (lua_State *L, int objidx, int level) { |
53 | 643k | if (level == 0 || !lua_istable(L, -1)) |
54 | 598k | return 0; /* not found */ |
55 | 45.6k | lua_pushnil(L); /* start 'next' loop */ |
56 | 675k | while (lua_next(L, -2)) { /* for each pair in table */ |
57 | 647k | if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */ |
58 | 643k | if (lua_rawequal(L, objidx, -1)) { /* found object? */ |
59 | 9.23k | lua_pop(L, 1); /* remove value (but keep name) */ |
60 | 9.23k | return 1; |
61 | 9.23k | } |
62 | 634k | else if (findfield(L, objidx, level - 1)) { /* try recursively */ |
63 | | /* stack: lib_name, lib_table, field_name (top) */ |
64 | 9.23k | lua_pushliteral(L, "."); /* place '.' between the two names */ |
65 | 9.23k | lua_replace(L, -3); /* (in the slot occupied by table) */ |
66 | 9.23k | lua_concat(L, 3); /* lib_name.field_name */ |
67 | 9.23k | return 1; |
68 | 9.23k | } |
69 | 643k | } |
70 | 629k | lua_pop(L, 1); /* remove value */ |
71 | 629k | } |
72 | 27.1k | return 0; /* not found */ |
73 | 45.6k | } |
74 | | |
75 | | |
76 | | /* |
77 | | ** Search for a name for a function in all loaded modules |
78 | | */ |
79 | 9.30k | static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { |
80 | 9.30k | int top = lua_gettop(L); |
81 | 9.30k | lua_getinfo(L, "f", ar); /* push function */ |
82 | 9.30k | lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); |
83 | 9.30k | if (findfield(L, top + 1, 2)) { |
84 | 9.23k | const char *name = lua_tostring(L, -1); |
85 | 9.23k | if (strncmp(name, LUA_GNAME ".", 3) == 0) { /* name start with '_G.'? */ |
86 | 9.23k | lua_pushstring(L, name + 3); /* push name without prefix */ |
87 | 9.23k | lua_remove(L, -2); /* remove original name */ |
88 | 9.23k | } |
89 | 9.23k | lua_copy(L, -1, top + 1); /* copy name to proper place */ |
90 | 9.23k | lua_settop(L, top + 1); /* remove table "loaded" and name copy */ |
91 | 9.23k | return 1; |
92 | 9.23k | } |
93 | 69 | else { |
94 | 69 | lua_settop(L, top); /* remove function and global table */ |
95 | 69 | return 0; |
96 | 69 | } |
97 | 9.30k | } |
98 | | |
99 | | |
100 | 56 | static void pushfuncname (lua_State *L, lua_Debug *ar) { |
101 | 56 | if (pushglobalfuncname(L, ar)) { /* try first a global name */ |
102 | 0 | lua_pushfstring(L, "function '%s'", lua_tostring(L, -1)); |
103 | 0 | lua_remove(L, -2); /* remove name */ |
104 | 0 | } |
105 | 56 | else if (*ar->namewhat != '\0') /* is there a name from code? */ |
106 | 0 | lua_pushfstring(L, "%s '%s'", ar->namewhat, ar->name); /* use it */ |
107 | 56 | else if (*ar->what == 'm') /* main? */ |
108 | 56 | lua_pushliteral(L, "main chunk"); |
109 | 0 | else if (*ar->what != 'C') /* for Lua functions, use <file:line> */ |
110 | 0 | lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined); |
111 | 0 | else /* nothing left... */ |
112 | 0 | lua_pushliteral(L, "?"); |
113 | 56 | } |
114 | | |
115 | | |
116 | 402 | static int lastlevel (lua_State *L) { |
117 | 402 | lua_Debug ar; |
118 | 402 | int li = 1, le = 1; |
119 | | /* find an upper bound */ |
120 | 458 | while (lua_getstack(L, le, &ar)) { li = le; le *= 2; } |
121 | | /* do a binary search */ |
122 | 458 | while (li < le) { |
123 | 56 | int m = (li + le)/2; |
124 | 56 | if (lua_getstack(L, m, &ar)) li = m + 1; |
125 | 0 | else le = m; |
126 | 56 | } |
127 | 402 | return le - 1; |
128 | 402 | } |
129 | | |
130 | | |
131 | | LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, |
132 | 402 | const char *msg, int level) { |
133 | 402 | luaL_Buffer b; |
134 | 402 | lua_Debug ar; |
135 | 402 | int last = lastlevel(L1); |
136 | 402 | int limit2show = (last - level > LEVELS1 + LEVELS2) ? LEVELS1 : -1; |
137 | 402 | luaL_buffinit(L, &b); |
138 | 402 | if (msg) { |
139 | 402 | luaL_addstring(&b, msg); |
140 | 402 | luaL_addchar(&b, '\n'); |
141 | 402 | } |
142 | 402 | luaL_addstring(&b, "stack traceback:"); |
143 | 458 | while (lua_getstack(L1, level++, &ar)) { |
144 | 56 | if (limit2show-- == 0) { /* too many levels? */ |
145 | 0 | int n = last - level - LEVELS2 + 1; /* number of levels to skip */ |
146 | 0 | lua_pushfstring(L, "\n\t...\t(skipping %d levels)", n); |
147 | 0 | luaL_addvalue(&b); /* add warning about skip */ |
148 | 0 | level += n; /* and skip to last levels */ |
149 | 0 | } |
150 | 56 | else { |
151 | 56 | lua_getinfo(L1, "Slnt", &ar); |
152 | 56 | if (ar.currentline <= 0) |
153 | 0 | lua_pushfstring(L, "\n\t%s: in ", ar.short_src); |
154 | 56 | else |
155 | 56 | lua_pushfstring(L, "\n\t%s:%d: in ", ar.short_src, ar.currentline); |
156 | 56 | luaL_addvalue(&b); |
157 | 56 | pushfuncname(L, &ar); |
158 | 56 | luaL_addvalue(&b); |
159 | 56 | if (ar.istailcall) |
160 | 0 | luaL_addstring(&b, "\n\t(...tail calls...)"); |
161 | 56 | } |
162 | 56 | } |
163 | 402 | luaL_pushresult(&b); |
164 | 402 | } |
165 | | |
166 | | /* }====================================================== */ |
167 | | |
168 | | |
169 | | /* |
170 | | ** {====================================================== |
171 | | ** Error-report functions |
172 | | ** ======================================================= |
173 | | */ |
174 | | |
175 | 9.34k | LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) { |
176 | 9.34k | lua_Debug ar; |
177 | 9.34k | if (!lua_getstack(L, 0, &ar)) /* no stack frame? */ |
178 | 0 | return luaL_error(L, "bad argument #%d (%s)", arg, extramsg); |
179 | 9.34k | lua_getinfo(L, "n", &ar); |
180 | 9.34k | if (strcmp(ar.namewhat, "method") == 0) { |
181 | 29 | arg--; /* do not count 'self' */ |
182 | 29 | if (arg == 0) /* error is in the self argument itself? */ |
183 | 0 | return luaL_error(L, "calling '%s' on bad self (%s)", |
184 | 0 | ar.name, extramsg); |
185 | 29 | } |
186 | 9.34k | if (ar.name == NULL) |
187 | 9.25k | ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?"; |
188 | 9.34k | return luaL_error(L, "bad argument #%d to '%s' (%s)", |
189 | 9.34k | arg, ar.name, extramsg); |
190 | 9.34k | } |
191 | | |
192 | | |
193 | 6.66k | LUALIB_API int luaL_typeerror (lua_State *L, int arg, const char *tname) { |
194 | 6.66k | const char *msg; |
195 | 6.66k | const char *typearg; /* name for the type of the actual argument */ |
196 | 6.66k | if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING) |
197 | 0 | typearg = lua_tostring(L, -1); /* use the given type name */ |
198 | 6.66k | else if (lua_type(L, arg) == LUA_TLIGHTUSERDATA) |
199 | 0 | typearg = "light userdata"; /* special name for messages */ |
200 | 6.66k | else |
201 | 6.66k | typearg = luaL_typename(L, arg); /* standard name */ |
202 | 6.66k | msg = lua_pushfstring(L, "%s expected, got %s", tname, typearg); |
203 | 6.66k | return luaL_argerror(L, arg, msg); |
204 | 6.66k | } |
205 | | |
206 | | |
207 | 6.66k | static void tag_error (lua_State *L, int arg, int tag) { |
208 | 6.66k | luaL_typeerror(L, arg, lua_typename(L, tag)); |
209 | 6.66k | } |
210 | | |
211 | | |
212 | | /* |
213 | | ** The use of 'lua_pushfstring' ensures this function does not |
214 | | ** need reserved stack space when called. |
215 | | */ |
216 | 9.56k | LUALIB_API void luaL_where (lua_State *L, int level) { |
217 | 9.56k | lua_Debug ar; |
218 | 9.56k | if (lua_getstack(L, level, &ar)) { /* check function at level */ |
219 | 9.56k | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
220 | 9.56k | if (ar.currentline > 0) { /* is there info? */ |
221 | 330 | lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline); |
222 | 330 | return; |
223 | 330 | } |
224 | 9.56k | } |
225 | 9.23k | lua_pushfstring(L, ""); /* else, no information available... */ |
226 | 9.23k | } |
227 | | |
228 | | |
229 | | /* |
230 | | ** Again, the use of 'lua_pushvfstring' ensures this function does |
231 | | ** not need reserved stack space when called. (At worst, it generates |
232 | | ** an error with "stack overflow" instead of the given message.) |
233 | | */ |
234 | 9.56k | LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { |
235 | 9.56k | va_list argp; |
236 | 9.56k | va_start(argp, fmt); |
237 | 9.56k | luaL_where(L, 1); |
238 | 9.56k | lua_pushvfstring(L, fmt, argp); |
239 | 9.56k | va_end(argp); |
240 | 9.56k | lua_concat(L, 2); |
241 | 9.56k | return lua_error(L); |
242 | 9.56k | } |
243 | | |
244 | | |
245 | 0 | LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { |
246 | 0 | int en = errno; /* calls to Lua API may change this value */ |
247 | 0 | if (stat) { |
248 | 0 | lua_pushboolean(L, 1); |
249 | 0 | return 1; |
250 | 0 | } |
251 | 0 | else { |
252 | 0 | luaL_pushfail(L); |
253 | 0 | if (fname) |
254 | 0 | lua_pushfstring(L, "%s: %s", fname, strerror(en)); |
255 | 0 | else |
256 | 0 | lua_pushstring(L, strerror(en)); |
257 | 0 | lua_pushinteger(L, en); |
258 | 0 | return 3; |
259 | 0 | } |
260 | 0 | } |
261 | | |
262 | | |
263 | | #if !defined(l_inspectstat) /* { */ |
264 | | |
265 | | #if defined(LUA_USE_POSIX) |
266 | | |
267 | | #include <sys/wait.h> |
268 | | |
269 | | /* |
270 | | ** use appropriate macros to interpret 'pclose' return status |
271 | | */ |
272 | | #define l_inspectstat(stat,what) \ |
273 | | if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \ |
274 | | else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; } |
275 | | |
276 | | #else |
277 | | |
278 | | #define l_inspectstat(stat,what) /* no op */ |
279 | | |
280 | | #endif |
281 | | |
282 | | #endif /* } */ |
283 | | |
284 | | |
285 | 0 | LUALIB_API int luaL_execresult (lua_State *L, int stat) { |
286 | 0 | if (stat != 0 && errno != 0) /* error with an 'errno'? */ |
287 | 0 | return luaL_fileresult(L, 0, NULL); |
288 | 0 | else { |
289 | 0 | const char *what = "exit"; /* type of termination */ |
290 | 0 | l_inspectstat(stat, what); /* interpret result */ |
291 | 0 | if (*what == 'e' && stat == 0) /* successful termination? */ |
292 | 0 | lua_pushboolean(L, 1); |
293 | 0 | else |
294 | 0 | luaL_pushfail(L); |
295 | 0 | lua_pushstring(L, what); |
296 | 0 | lua_pushinteger(L, stat); |
297 | 0 | return 3; /* return true/fail,what,code */ |
298 | 0 | } |
299 | 0 | } |
300 | | |
301 | | /* }====================================================== */ |
302 | | |
303 | | |
304 | | |
305 | | /* |
306 | | ** {====================================================== |
307 | | ** Userdata's metatable manipulation |
308 | | ** ======================================================= |
309 | | */ |
310 | | |
311 | 53.7k | LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { |
312 | 53.7k | if (luaL_getmetatable(L, tname) != LUA_TNIL) /* name already in use? */ |
313 | 49.1k | return 0; /* leave previous value on top, but return 0 */ |
314 | 4.60k | lua_pop(L, 1); |
315 | 4.60k | lua_createtable(L, 0, 2); /* create metatable */ |
316 | 4.60k | lua_pushstring(L, tname); |
317 | 4.60k | lua_setfield(L, -2, "__name"); /* metatable.__name = tname */ |
318 | 4.60k | lua_pushvalue(L, -1); |
319 | 4.60k | lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */ |
320 | 4.60k | return 1; |
321 | 53.7k | } |
322 | | |
323 | | |
324 | 9.61k | LUALIB_API void luaL_setmetatable (lua_State *L, const char *tname) { |
325 | 9.61k | luaL_getmetatable(L, tname); |
326 | 9.61k | lua_setmetatable(L, -2); |
327 | 9.61k | } |
328 | | |
329 | | |
330 | 28.8k | LUALIB_API void *luaL_testudata (lua_State *L, int ud, const char *tname) { |
331 | 28.8k | void *p = lua_touserdata(L, ud); |
332 | 28.8k | if (p != NULL) { /* value is a userdata? */ |
333 | 28.8k | if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ |
334 | 28.8k | luaL_getmetatable(L, tname); /* get correct metatable */ |
335 | 28.8k | if (!lua_rawequal(L, -1, -2)) /* not the same? */ |
336 | 0 | p = NULL; /* value is a userdata with wrong metatable */ |
337 | 28.8k | lua_pop(L, 2); /* remove both metatables */ |
338 | 28.8k | return p; |
339 | 28.8k | } |
340 | 28.8k | } |
341 | 0 | return NULL; /* value is not a userdata with a metatable */ |
342 | 28.8k | } |
343 | | |
344 | | |
345 | 28.8k | LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { |
346 | 28.8k | void *p = luaL_testudata(L, ud, tname); |
347 | 28.8k | luaL_argexpected(L, p != NULL, ud, tname); |
348 | 28.8k | return p; |
349 | 28.8k | } |
350 | | |
351 | | /* }====================================================== */ |
352 | | |
353 | | |
354 | | /* |
355 | | ** {====================================================== |
356 | | ** Argument check functions |
357 | | ** ======================================================= |
358 | | */ |
359 | | |
360 | | LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def, |
361 | 8.24k | const char *const lst[]) { |
362 | 8.24k | const char *name = (def) ? luaL_optstring(L, arg, def) : |
363 | 8.24k | luaL_checkstring(L, arg); |
364 | 8.24k | int i; |
365 | 42.7k | for (i=0; lst[i]; i++) |
366 | 42.7k | if (strcmp(lst[i], name) == 0) |
367 | 8.23k | return i; |
368 | 9 | return luaL_argerror(L, arg, |
369 | 9 | lua_pushfstring(L, "invalid option '%s'", name)); |
370 | 8.24k | } |
371 | | |
372 | | |
373 | | /* |
374 | | ** Ensures the stack has at least 'space' extra slots, raising an error |
375 | | ** if it cannot fulfill the request. (The error handling needs a few |
376 | | ** extra slots to format the error message. In case of an error without |
377 | | ** this extra space, Lua will generate the same 'stack overflow' error, |
378 | | ** but without 'msg'.) |
379 | | */ |
380 | 73.6k | LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) { |
381 | 73.6k | if (l_unlikely(!lua_checkstack(L, space))) { |
382 | 1 | if (msg) |
383 | 1 | luaL_error(L, "stack overflow (%s)", msg); |
384 | 0 | else |
385 | 0 | luaL_error(L, "stack overflow"); |
386 | 1 | } |
387 | 73.6k | } |
388 | | |
389 | | |
390 | 18.4k | LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) { |
391 | 18.4k | if (l_unlikely(lua_type(L, arg) != t)) |
392 | 6.58k | tag_error(L, arg, t); |
393 | 18.4k | } |
394 | | |
395 | | |
396 | 17.9k | LUALIB_API void luaL_checkany (lua_State *L, int arg) { |
397 | 17.9k | if (l_unlikely(lua_type(L, arg) == LUA_TNONE)) |
398 | 0 | luaL_argerror(L, arg, "value expected"); |
399 | 17.9k | } |
400 | | |
401 | | |
402 | 171k | LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) { |
403 | 171k | const char *s = lua_tolstring(L, arg, len); |
404 | 171k | if (l_unlikely(!s)) tag_error(L, arg, LUA_TSTRING); |
405 | 171k | return s; |
406 | 171k | } |
407 | | |
408 | | |
409 | | LUALIB_API const char *luaL_optlstring (lua_State *L, int arg, |
410 | 427k | const char *def, size_t *len) { |
411 | 427k | if (lua_isnoneornil(L, arg)) { |
412 | 400k | if (len) |
413 | 617 | *len = (def ? strlen(def) : 0); |
414 | 400k | return def; |
415 | 400k | } |
416 | 27.0k | else return luaL_checklstring(L, arg, len); |
417 | 427k | } |
418 | | |
419 | | |
420 | 85 | LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) { |
421 | 85 | int isnum; |
422 | 85 | lua_Number d = lua_tonumberx(L, arg, &isnum); |
423 | 85 | if (l_unlikely(!isnum)) |
424 | 6 | tag_error(L, arg, LUA_TNUMBER); |
425 | 85 | return d; |
426 | 85 | } |
427 | | |
428 | | |
429 | 0 | LUALIB_API lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number def) { |
430 | 0 | return luaL_opt(L, luaL_checknumber, arg, def); |
431 | 0 | } |
432 | | |
433 | | |
434 | 53 | static void interror (lua_State *L, int arg) { |
435 | 53 | if (lua_isnumber(L, arg)) |
436 | 1 | luaL_argerror(L, arg, "number has no integer representation"); |
437 | 52 | else |
438 | 52 | tag_error(L, arg, LUA_TNUMBER); |
439 | 53 | } |
440 | | |
441 | | |
442 | 32.9k | LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) { |
443 | 32.9k | int isnum; |
444 | 32.9k | lua_Integer d = lua_tointegerx(L, arg, &isnum); |
445 | 32.9k | if (l_unlikely(!isnum)) { |
446 | 53 | interror(L, arg); |
447 | 53 | } |
448 | 32.9k | return d; |
449 | 32.9k | } |
450 | | |
451 | | |
452 | | LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int arg, |
453 | 38.5k | lua_Integer def) { |
454 | 38.5k | return luaL_opt(L, luaL_checkinteger, arg, def); |
455 | 38.5k | } |
456 | | |
457 | | /* }====================================================== */ |
458 | | |
459 | | |
460 | | /* |
461 | | ** {====================================================== |
462 | | ** Generic Buffer manipulation |
463 | | ** ======================================================= |
464 | | */ |
465 | | |
466 | | /* userdata to box arbitrary data */ |
467 | | typedef struct UBox { |
468 | | void *box; |
469 | | size_t bsize; |
470 | | } UBox; |
471 | | |
472 | | |
473 | 383k | static void *resizebox (lua_State *L, int idx, size_t newsize) { |
474 | 383k | void *ud; |
475 | 383k | lua_Alloc allocf = lua_getallocf(L, &ud); |
476 | 383k | UBox *box = (UBox *)lua_touserdata(L, idx); |
477 | 383k | void *temp = allocf(ud, box->box, box->bsize, newsize); |
478 | 383k | if (l_unlikely(temp == NULL && newsize > 0)) { /* allocation error? */ |
479 | 0 | lua_pushliteral(L, "not enough memory"); |
480 | 0 | lua_error(L); /* raise a memory error */ |
481 | 0 | } |
482 | 383k | box->box = temp; |
483 | 383k | box->bsize = newsize; |
484 | 383k | return temp; |
485 | 383k | } |
486 | | |
487 | | |
488 | 101k | static int boxgc (lua_State *L) { |
489 | 101k | resizebox(L, 1, 0); |
490 | 101k | return 0; |
491 | 101k | } |
492 | | |
493 | | |
494 | | static const luaL_Reg boxmt[] = { /* box metamethods */ |
495 | | {"__gc", boxgc}, |
496 | | {"__close", boxgc}, |
497 | | {NULL, NULL} |
498 | | }; |
499 | | |
500 | | |
501 | 50.5k | static void newbox (lua_State *L) { |
502 | 50.5k | UBox *box = (UBox *)lua_newuserdatauv(L, sizeof(UBox), 0); |
503 | 50.5k | box->box = NULL; |
504 | 50.5k | box->bsize = 0; |
505 | 50.5k | if (luaL_newmetatable(L, "_UBOX*")) /* creating metatable? */ |
506 | 1.39k | luaL_setfuncs(L, boxmt, 0); /* set its metamethods */ |
507 | 50.5k | lua_setmetatable(L, -2); |
508 | 50.5k | } |
509 | | |
510 | | |
511 | | /* |
512 | | ** check whether buffer is using a userdata on the stack as a temporary |
513 | | ** buffer |
514 | | */ |
515 | 345k | #define buffonstack(B) ((B)->b != (B)->init.b) |
516 | | |
517 | | |
518 | | /* |
519 | | ** Whenever buffer is accessed, slot 'idx' must either be a box (which |
520 | | ** cannot be NULL) or it is a placeholder for the buffer. |
521 | | */ |
522 | | #define checkbufferlevel(B,idx) \ |
523 | 3.05M | lua_assert(buffonstack(B) ? lua_touserdata(B->L, idx) != NULL \ |
524 | 3.05M | : lua_touserdata(B->L, idx) == (void*)B) |
525 | | |
526 | | |
527 | | /* |
528 | | ** Compute new size for buffer 'B', enough to accommodate extra 'sz' |
529 | | ** bytes. (The test for "not big enough" also gets the case when the |
530 | | ** computation of 'newsize' overflows.) |
531 | | */ |
532 | 282k | static size_t newbuffsize (luaL_Buffer *B, size_t sz) { |
533 | 282k | size_t newsize = (B->size / 2) * 3; /* buffer size * 1.5 */ |
534 | 282k | if (l_unlikely(MAX_SIZET - sz < B->n)) /* overflow in (B->n + sz)? */ |
535 | 0 | return luaL_error(B->L, "buffer too large"); |
536 | 282k | if (newsize < B->n + sz) /* not big enough? */ |
537 | 20.5k | newsize = B->n + sz; |
538 | 282k | return newsize; |
539 | 282k | } |
540 | | |
541 | | |
542 | | /* |
543 | | ** Returns a pointer to a free area with at least 'sz' bytes in buffer |
544 | | ** 'B'. 'boxidx' is the relative position in the stack where is the |
545 | | ** buffer's box or its placeholder. |
546 | | */ |
547 | 2.99M | static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) { |
548 | 2.99M | checkbufferlevel(B, boxidx); |
549 | 2.99M | if (B->size - B->n >= sz) /* enough space? */ |
550 | 2.71M | return B->b + B->n; |
551 | 282k | else { |
552 | 282k | lua_State *L = B->L; |
553 | 282k | char *newbuff; |
554 | 282k | size_t newsize = newbuffsize(B, sz); |
555 | | /* create larger buffer */ |
556 | 282k | if (buffonstack(B)) /* buffer already has a box? */ |
557 | 231k | newbuff = (char *)resizebox(L, boxidx, newsize); /* resize it */ |
558 | 50.5k | else { /* no box yet */ |
559 | 50.5k | lua_remove(L, boxidx); /* remove placeholder */ |
560 | 50.5k | newbox(L); /* create a new box */ |
561 | 50.5k | lua_insert(L, boxidx); /* move box to its intended position */ |
562 | 50.5k | lua_toclose(L, boxidx); |
563 | 50.5k | newbuff = (char *)resizebox(L, boxidx, newsize); |
564 | 50.5k | memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */ |
565 | 50.5k | } |
566 | 282k | B->b = newbuff; |
567 | 282k | B->size = newsize; |
568 | 282k | return newbuff + B->n; |
569 | 282k | } |
570 | 2.99M | } |
571 | | |
572 | | /* |
573 | | ** returns a pointer to a free area with at least 'sz' bytes |
574 | | */ |
575 | 257k | LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { |
576 | 257k | return prepbuffsize(B, sz, -1); |
577 | 257k | } |
578 | | |
579 | | |
580 | 3.46M | LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { |
581 | 3.46M | if (l > 0) { /* avoid 'memcpy' when 's' can be NULL */ |
582 | 2.71M | char *b = prepbuffsize(B, l, -1); |
583 | 2.71M | memcpy(b, s, l * sizeof(char)); |
584 | 2.71M | luaL_addsize(B, l); |
585 | 2.71M | } |
586 | 3.46M | } |
587 | | |
588 | | |
589 | 790k | LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) { |
590 | 790k | luaL_addlstring(B, s, strlen(s)); |
591 | 790k | } |
592 | | |
593 | | |
594 | 63.0k | LUALIB_API void luaL_pushresult (luaL_Buffer *B) { |
595 | 63.0k | lua_State *L = B->L; |
596 | 63.0k | checkbufferlevel(B, -1); |
597 | 63.0k | lua_pushlstring(L, B->b, B->n); |
598 | 63.0k | if (buffonstack(B)) |
599 | 50.4k | lua_closeslot(L, -2); /* close the box */ |
600 | 63.0k | lua_remove(L, -2); /* remove box or placeholder from the stack */ |
601 | 63.0k | } |
602 | | |
603 | | |
604 | 23.6k | LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) { |
605 | 23.6k | luaL_addsize(B, sz); |
606 | 23.6k | luaL_pushresult(B); |
607 | 23.6k | } |
608 | | |
609 | | |
610 | | /* |
611 | | ** 'luaL_addvalue' is the only function in the Buffer system where the |
612 | | ** box (if existent) is not on the top of the stack. So, instead of |
613 | | ** calling 'luaL_addlstring', it replicates the code using -2 as the |
614 | | ** last argument to 'prepbuffsize', signaling that the box is (or will |
615 | | ** be) below the string being added to the buffer. (Box creation can |
616 | | ** trigger an emergency GC, so we should not remove the string from the |
617 | | ** stack before we have the space guaranteed.) |
618 | | */ |
619 | 486 | LUALIB_API void luaL_addvalue (luaL_Buffer *B) { |
620 | 486 | lua_State *L = B->L; |
621 | 486 | size_t len; |
622 | 486 | const char *s = lua_tolstring(L, -1, &len); |
623 | 486 | char *b = prepbuffsize(B, len, -2); |
624 | 486 | memcpy(b, s, len * sizeof(char)); |
625 | 486 | luaL_addsize(B, len); |
626 | 486 | lua_pop(L, 1); /* pop string */ |
627 | 486 | } |
628 | | |
629 | | |
630 | 63.2k | LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { |
631 | 63.2k | B->L = L; |
632 | 63.2k | B->b = B->init.b; |
633 | 63.2k | B->n = 0; |
634 | 63.2k | B->size = LUAL_BUFFERSIZE; |
635 | 63.2k | lua_pushlightuserdata(L, (void*)B); /* push placeholder */ |
636 | 63.2k | } |
637 | | |
638 | | |
639 | 23.6k | LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) { |
640 | 23.6k | luaL_buffinit(L, B); |
641 | 23.6k | return prepbuffsize(B, sz, -1); |
642 | 23.6k | } |
643 | | |
644 | | /* }====================================================== */ |
645 | | |
646 | | |
647 | | /* |
648 | | ** {====================================================== |
649 | | ** Reference system |
650 | | ** ======================================================= |
651 | | */ |
652 | | |
653 | | /* index of free-list header (after the predefined values) */ |
654 | 0 | #define freelist (LUA_RIDX_LAST + 1) |
655 | | |
656 | | /* |
657 | | ** The previously freed references form a linked list: |
658 | | ** t[freelist] is the index of a first free index, or zero if list is |
659 | | ** empty; t[t[freelist]] is the index of the second element; etc. |
660 | | */ |
661 | 0 | LUALIB_API int luaL_ref (lua_State *L, int t) { |
662 | 0 | int ref; |
663 | 0 | if (lua_isnil(L, -1)) { |
664 | 0 | lua_pop(L, 1); /* remove from stack */ |
665 | 0 | return LUA_REFNIL; /* 'nil' has a unique fixed reference */ |
666 | 0 | } |
667 | 0 | t = lua_absindex(L, t); |
668 | 0 | if (lua_rawgeti(L, t, freelist) == LUA_TNIL) { /* first access? */ |
669 | 0 | ref = 0; /* list is empty */ |
670 | 0 | lua_pushinteger(L, 0); /* initialize as an empty list */ |
671 | 0 | lua_rawseti(L, t, freelist); /* ref = t[freelist] = 0 */ |
672 | 0 | } |
673 | 0 | else { /* already initialized */ |
674 | 0 | lua_assert(lua_isinteger(L, -1)); |
675 | 0 | ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */ |
676 | 0 | } |
677 | 0 | lua_pop(L, 1); /* remove element from stack */ |
678 | 0 | if (ref != 0) { /* any free element? */ |
679 | 0 | lua_rawgeti(L, t, ref); /* remove it from list */ |
680 | 0 | lua_rawseti(L, t, freelist); /* (t[freelist] = t[ref]) */ |
681 | 0 | } |
682 | 0 | else /* no free elements */ |
683 | 0 | ref = (int)lua_rawlen(L, t) + 1; /* get a new reference */ |
684 | 0 | lua_rawseti(L, t, ref); |
685 | 0 | return ref; |
686 | 0 | } |
687 | | |
688 | | |
689 | 0 | LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { |
690 | 0 | if (ref >= 0) { |
691 | 0 | t = lua_absindex(L, t); |
692 | 0 | lua_rawgeti(L, t, freelist); |
693 | 0 | lua_assert(lua_isinteger(L, -1)); |
694 | 0 | lua_rawseti(L, t, ref); /* t[ref] = t[freelist] */ |
695 | 0 | lua_pushinteger(L, ref); |
696 | 0 | lua_rawseti(L, t, freelist); /* t[freelist] = ref */ |
697 | 0 | } |
698 | 0 | } |
699 | | |
700 | | /* }====================================================== */ |
701 | | |
702 | | |
703 | | /* |
704 | | ** {====================================================== |
705 | | ** Load functions |
706 | | ** ======================================================= |
707 | | */ |
708 | | |
709 | | typedef struct LoadF { |
710 | | int n; /* number of pre-read characters */ |
711 | | FILE *f; /* file being read */ |
712 | | char buff[BUFSIZ]; /* area for reading file */ |
713 | | } LoadF; |
714 | | |
715 | | |
716 | 31 | static const char *getF (lua_State *L, void *ud, size_t *size) { |
717 | 31 | LoadF *lf = (LoadF *)ud; |
718 | 31 | (void)L; /* not used */ |
719 | 31 | if (lf->n > 0) { /* are there pre-read characters to be read? */ |
720 | 0 | *size = lf->n; /* return them (chars already in buffer) */ |
721 | 0 | lf->n = 0; /* no more pre-read characters */ |
722 | 0 | } |
723 | 31 | else { /* read a block from file */ |
724 | | /* 'fread' can return > 0 *and* set the EOF flag. If next call to |
725 | | 'getF' called 'fread', it might still wait for user input. |
726 | | The next check avoids this problem. */ |
727 | 31 | if (feof(lf->f)) return NULL; |
728 | 3 | *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */ |
729 | 3 | } |
730 | 3 | return lf->buff; |
731 | 31 | } |
732 | | |
733 | | |
734 | 2.25k | static int errfile (lua_State *L, const char *what, int fnameindex) { |
735 | 2.25k | const char *serr = strerror(errno); |
736 | 2.25k | const char *filename = lua_tostring(L, fnameindex) + 1; |
737 | 2.25k | lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr); |
738 | 2.25k | lua_remove(L, fnameindex); |
739 | 2.25k | return LUA_ERRFILE; |
740 | 2.25k | } |
741 | | |
742 | | |
743 | | /* |
744 | | ** Skip an optional BOM at the start of a stream. If there is an |
745 | | ** incomplete BOM (the first character is correct but the rest is |
746 | | ** not), returns the first character anyway to force an error |
747 | | ** (as no chunk can start with 0xEF). |
748 | | */ |
749 | 31 | static int skipBOM (FILE *f) { |
750 | 31 | int c = getc(f); /* read first character */ |
751 | 31 | if (c == 0xEF && getc(f) == 0xBB && getc(f) == 0xBF) /* correct BOM? */ |
752 | 0 | return getc(f); /* ignore BOM and return next char */ |
753 | 31 | else /* no (valid) BOM */ |
754 | 31 | return c; /* return first character */ |
755 | 31 | } |
756 | | |
757 | | |
758 | | /* |
759 | | ** reads the first character of file 'f' and skips an optional BOM mark |
760 | | ** in its beginning plus its first line if it starts with '#'. Returns |
761 | | ** true if it skipped the first line. In any case, '*cp' has the |
762 | | ** first "valid" character of the file (after the optional BOM and |
763 | | ** a first-line comment). |
764 | | */ |
765 | 31 | static int skipcomment (FILE *f, int *cp) { |
766 | 31 | int c = *cp = skipBOM(f); |
767 | 31 | if (c == '#') { /* first line is a comment (Unix exec. file)? */ |
768 | 0 | do { /* skip first line */ |
769 | 0 | c = getc(f); |
770 | 0 | } while (c != EOF && c != '\n'); |
771 | 0 | *cp = getc(f); /* next character after comment, if present */ |
772 | 0 | return 1; /* there was a comment */ |
773 | 0 | } |
774 | 31 | else return 0; /* no comment */ |
775 | 31 | } |
776 | | |
777 | | |
778 | | LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, |
779 | 2.28k | const char *mode) { |
780 | 2.28k | LoadF lf; |
781 | 2.28k | int status, readstatus; |
782 | 2.28k | int c; |
783 | 2.28k | int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ |
784 | 2.28k | if (filename == NULL) { |
785 | 28 | lua_pushliteral(L, "=stdin"); |
786 | 28 | lf.f = stdin; |
787 | 28 | } |
788 | 2.25k | else { |
789 | 2.25k | lua_pushfstring(L, "@%s", filename); |
790 | 2.25k | lf.f = fopen(filename, "r"); |
791 | 2.25k | if (lf.f == NULL) return errfile(L, "open", fnameindex); |
792 | 2.25k | } |
793 | 31 | lf.n = 0; |
794 | 31 | if (skipcomment(lf.f, &c)) /* read initial portion */ |
795 | 0 | lf.buff[lf.n++] = '\n'; /* add newline to correct line numbers */ |
796 | 31 | if (c == LUA_SIGNATURE[0]) { /* binary file? */ |
797 | 0 | lf.n = 0; /* remove possible newline */ |
798 | 0 | if (filename) { /* "real" file? */ |
799 | 0 | lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ |
800 | 0 | if (lf.f == NULL) return errfile(L, "reopen", fnameindex); |
801 | 0 | skipcomment(lf.f, &c); /* re-read initial portion */ |
802 | 0 | } |
803 | 0 | } |
804 | 31 | if (c != EOF) |
805 | 0 | lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ |
806 | 31 | status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); |
807 | 31 | readstatus = ferror(lf.f); |
808 | 31 | if (filename) fclose(lf.f); /* close file (even in case of errors) */ |
809 | 31 | if (readstatus) { |
810 | 3 | lua_settop(L, fnameindex); /* ignore results from 'lua_load' */ |
811 | 3 | return errfile(L, "read", fnameindex); |
812 | 3 | } |
813 | 28 | lua_remove(L, fnameindex); |
814 | 28 | return status; |
815 | 31 | } |
816 | | |
817 | | |
818 | | typedef struct LoadS { |
819 | | const char *s; |
820 | | size_t size; |
821 | | } LoadS; |
822 | | |
823 | | |
824 | 243k | static const char *getS (lua_State *L, void *ud, size_t *size) { |
825 | 243k | LoadS *ls = (LoadS *)ud; |
826 | 243k | (void)L; /* not used */ |
827 | 243k | if (ls->size == 0) return NULL; |
828 | 178k | *size = ls->size; |
829 | 178k | ls->size = 0; |
830 | 178k | return ls->s; |
831 | 243k | } |
832 | | |
833 | | |
834 | | LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size, |
835 | 193k | const char *name, const char *mode) { |
836 | 193k | LoadS ls; |
837 | 193k | ls.s = buff; |
838 | 193k | ls.size = size; |
839 | 193k | return lua_load(L, getS, &ls, name, mode); |
840 | 193k | } |
841 | | |
842 | | |
843 | 0 | LUALIB_API int luaL_loadstring (lua_State *L, const char *s) { |
844 | 0 | return luaL_loadbuffer(L, s, strlen(s), s); |
845 | 0 | } |
846 | | |
847 | | /* }====================================================== */ |
848 | | |
849 | | |
850 | | |
851 | 27.8k | LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) { |
852 | 27.8k | if (!lua_getmetatable(L, obj)) /* no metatable? */ |
853 | 23.6k | return LUA_TNIL; |
854 | 4.14k | else { |
855 | 4.14k | int tt; |
856 | 4.14k | lua_pushstring(L, event); |
857 | 4.14k | tt = lua_rawget(L, -2); |
858 | 4.14k | if (tt == LUA_TNIL) /* is metafield nil? */ |
859 | 4.14k | lua_pop(L, 2); /* remove metatable and metafield */ |
860 | 0 | else |
861 | 0 | lua_remove(L, -2); /* remove only metatable */ |
862 | 4.14k | return tt; /* return metafield type */ |
863 | 4.14k | } |
864 | 27.8k | } |
865 | | |
866 | | |
867 | 12.6k | LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { |
868 | 12.6k | obj = lua_absindex(L, obj); |
869 | 12.6k | if (luaL_getmetafield(L, obj, event) == LUA_TNIL) /* no metafield? */ |
870 | 12.6k | return 0; |
871 | 0 | lua_pushvalue(L, obj); |
872 | 0 | lua_call(L, 1, 1); |
873 | 0 | return 1; |
874 | 12.6k | } |
875 | | |
876 | | |
877 | 3.20k | LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) { |
878 | 3.20k | lua_Integer l; |
879 | 3.20k | int isnum; |
880 | 3.20k | lua_len(L, idx); |
881 | 3.20k | l = lua_tointegerx(L, -1, &isnum); |
882 | 3.20k | if (l_unlikely(!isnum)) |
883 | 0 | luaL_error(L, "object length is not an integer"); |
884 | 3.20k | lua_pop(L, 1); /* remove object */ |
885 | 3.20k | return l; |
886 | 3.20k | } |
887 | | |
888 | | |
889 | 12.6k | LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { |
890 | 12.6k | idx = lua_absindex(L,idx); |
891 | 12.6k | if (luaL_callmeta(L, idx, "__tostring")) { /* metafield? */ |
892 | 0 | if (!lua_isstring(L, -1)) |
893 | 0 | luaL_error(L, "'__tostring' must return a string"); |
894 | 0 | } |
895 | 12.6k | else { |
896 | 12.6k | switch (lua_type(L, idx)) { |
897 | 148 | case LUA_TNUMBER: { |
898 | 148 | if (lua_isinteger(L, idx)) |
899 | 49 | lua_pushfstring(L, "%I", (LUAI_UACINT)lua_tointeger(L, idx)); |
900 | 99 | else |
901 | 99 | lua_pushfstring(L, "%f", (LUAI_UACNUMBER)lua_tonumber(L, idx)); |
902 | 148 | break; |
903 | 0 | } |
904 | 3.59k | case LUA_TSTRING: |
905 | 3.59k | lua_pushvalue(L, idx); |
906 | 3.59k | break; |
907 | 3 | case LUA_TBOOLEAN: |
908 | 3 | lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false")); |
909 | 3 | break; |
910 | 1.02k | case LUA_TNIL: |
911 | 1.02k | lua_pushliteral(L, "nil"); |
912 | 1.02k | break; |
913 | 7.92k | default: { |
914 | 7.92k | int tt = luaL_getmetafield(L, idx, "__name"); /* try name */ |
915 | 7.92k | const char *kind = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : |
916 | 7.92k | luaL_typename(L, idx); |
917 | 7.92k | lua_pushfstring(L, "%s: %p", kind, lua_topointer(L, idx)); |
918 | 7.92k | if (tt != LUA_TNIL) |
919 | 0 | lua_remove(L, -2); /* remove '__name' */ |
920 | 7.92k | break; |
921 | 0 | } |
922 | 12.6k | } |
923 | 12.6k | } |
924 | 12.6k | return lua_tolstring(L, -1, len); |
925 | 12.6k | } |
926 | | |
927 | | |
928 | | /* |
929 | | ** set functions from list 'l' into table at top - 'nup'; each |
930 | | ** function gets the 'nup' elements at the top as upvalues. |
931 | | ** Returns with only the table at the stack. |
932 | | */ |
933 | 49.4k | LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { |
934 | 49.4k | luaL_checkstack(L, nup, "too many upvalues"); |
935 | 561k | for (; l->name != NULL; l++) { /* fill the table with given functions */ |
936 | 512k | if (l->func == NULL) /* place holder? */ |
937 | 51.2k | lua_pushboolean(L, 0); |
938 | 460k | else { |
939 | 460k | int i; |
940 | 470k | for (i = 0; i < nup; i++) /* copy upvalues to the top */ |
941 | 9.61k | lua_pushvalue(L, -nup); |
942 | 460k | lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ |
943 | 460k | } |
944 | 512k | lua_setfield(L, -(nup + 2), l->name); |
945 | 512k | } |
946 | 49.4k | lua_pop(L, nup); /* remove upvalues */ |
947 | 49.4k | } |
948 | | |
949 | | |
950 | | /* |
951 | | ** ensure that stack[idx][fname] has a table and push that table |
952 | | ** into the stack |
953 | | */ |
954 | 41.6k | LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) { |
955 | 41.6k | if (lua_getfield(L, idx, fname) == LUA_TTABLE) |
956 | 32.0k | return 1; /* table already there */ |
957 | 9.61k | else { |
958 | 9.61k | lua_pop(L, 1); /* remove previous result */ |
959 | 9.61k | idx = lua_absindex(L, idx); |
960 | 9.61k | lua_newtable(L); |
961 | 9.61k | lua_pushvalue(L, -1); /* copy to be left at top */ |
962 | 9.61k | lua_setfield(L, idx, fname); /* assign new table to field */ |
963 | 9.61k | return 0; /* false, because did not find table there */ |
964 | 9.61k | } |
965 | 41.6k | } |
966 | | |
967 | | |
968 | | /* |
969 | | ** Stripped-down 'require': After checking "loaded" table, calls 'openf' |
970 | | ** to open a module, registers the result in 'package.loaded' table and, |
971 | | ** if 'glb' is true, also registers the result in the global table. |
972 | | ** Leaves resulting module on the top. |
973 | | */ |
974 | | LUALIB_API void luaL_requiref (lua_State *L, const char *modname, |
975 | 32.0k | lua_CFunction openf, int glb) { |
976 | 32.0k | luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); |
977 | 32.0k | lua_getfield(L, -1, modname); /* LOADED[modname] */ |
978 | 32.0k | if (!lua_toboolean(L, -1)) { /* package not already loaded? */ |
979 | 32.0k | lua_pop(L, 1); /* remove field */ |
980 | 32.0k | lua_pushcfunction(L, openf); |
981 | 32.0k | lua_pushstring(L, modname); /* argument to open function */ |
982 | 32.0k | lua_call(L, 1, 1); /* call 'openf' to open module */ |
983 | 32.0k | lua_pushvalue(L, -1); /* make copy of module (call result) */ |
984 | 32.0k | lua_setfield(L, -3, modname); /* LOADED[modname] = module */ |
985 | 32.0k | } |
986 | 32.0k | lua_remove(L, -2); /* remove LOADED table */ |
987 | 32.0k | if (glb) { |
988 | 32.0k | lua_pushvalue(L, -1); /* copy of module */ |
989 | 32.0k | lua_setglobal(L, modname); /* _G[modname] = module */ |
990 | 32.0k | } |
991 | 32.0k | } |
992 | | |
993 | | |
994 | | LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s, |
995 | 766 | const char *p, const char *r) { |
996 | 766 | const char *wild; |
997 | 766 | size_t l = strlen(p); |
998 | 788k | while ((wild = strstr(s, p)) != NULL) { |
999 | 787k | luaL_addlstring(b, s, wild - s); /* push prefix */ |
1000 | 787k | luaL_addstring(b, r); /* push replacement in place of pattern */ |
1001 | 787k | s = wild + l; /* continue after 'p' */ |
1002 | 787k | } |
1003 | 766 | luaL_addstring(b, s); /* push last suffix */ |
1004 | 766 | } |
1005 | | |
1006 | | |
1007 | | LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, |
1008 | 184 | const char *p, const char *r) { |
1009 | 184 | luaL_Buffer b; |
1010 | 184 | luaL_buffinit(L, &b); |
1011 | 184 | luaL_addgsub(&b, s, p, r); |
1012 | 184 | luaL_pushresult(&b); |
1013 | 184 | return lua_tostring(L, -1); |
1014 | 184 | } |
1015 | | |
1016 | | |
1017 | 17.3M | static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { |
1018 | 17.3M | (void)ud; (void)osize; /* not used */ |
1019 | 17.3M | if (nsize == 0) { |
1020 | 9.27M | free(ptr); |
1021 | 9.27M | return NULL; |
1022 | 9.27M | } |
1023 | 8.02M | else |
1024 | 8.02M | return realloc(ptr, nsize); |
1025 | 17.3M | } |
1026 | | |
1027 | | |
1028 | 0 | static int panic (lua_State *L) { |
1029 | 0 | const char *msg = lua_tostring(L, -1); |
1030 | 0 | if (msg == NULL) msg = "error object is not a string"; |
1031 | 0 | lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n", |
1032 | 0 | msg); |
1033 | 0 | return 0; /* return to Lua to abort */ |
1034 | 0 | } |
1035 | | |
1036 | | |
1037 | | /* |
1038 | | ** Warning functions: |
1039 | | ** warnfoff: warning system is off |
1040 | | ** warnfon: ready to start a new message |
1041 | | ** warnfcont: previous message is to be continued |
1042 | | */ |
1043 | | static void warnfoff (void *ud, const char *message, int tocont); |
1044 | | static void warnfon (void *ud, const char *message, int tocont); |
1045 | | static void warnfcont (void *ud, const char *message, int tocont); |
1046 | | |
1047 | | |
1048 | | /* |
1049 | | ** Check whether message is a control message. If so, execute the |
1050 | | ** control or ignore it if unknown. |
1051 | | */ |
1052 | 8.01k | static int checkcontrol (lua_State *L, const char *message, int tocont) { |
1053 | 8.01k | if (tocont || *(message++) != '@') /* not a control message? */ |
1054 | 3.14k | return 0; |
1055 | 4.87k | else { |
1056 | 4.87k | if (strcmp(message, "off") == 0) |
1057 | 730 | lua_setwarnf(L, warnfoff, L); /* turn warnings off */ |
1058 | 4.14k | else if (strcmp(message, "on") == 0) |
1059 | 931 | lua_setwarnf(L, warnfon, L); /* turn warnings on */ |
1060 | 4.87k | return 1; /* it was a control message */ |
1061 | 4.87k | } |
1062 | 8.01k | } |
1063 | | |
1064 | | |
1065 | 4.42k | static void warnfoff (void *ud, const char *message, int tocont) { |
1066 | 4.42k | checkcontrol((lua_State *)ud, message, tocont); |
1067 | 4.42k | } |
1068 | | |
1069 | | |
1070 | | /* |
1071 | | ** Writes the message and handle 'tocont', finishing the message |
1072 | | ** if needed and setting the next warn function. |
1073 | | */ |
1074 | 1.06k | static void warnfcont (void *ud, const char *message, int tocont) { |
1075 | 1.06k | lua_State *L = (lua_State *)ud; |
1076 | 1.06k | lua_writestringerror("%s", message); /* write message */ |
1077 | 1.06k | if (tocont) /* not the last part? */ |
1078 | 0 | lua_setwarnf(L, warnfcont, L); /* to be continued */ |
1079 | 1.06k | else { /* last part */ |
1080 | 1.06k | lua_writestringerror("%s", "\n"); /* finish message with end-of-line */ |
1081 | 1.06k | lua_setwarnf(L, warnfon, L); /* next call is a new message */ |
1082 | 1.06k | } |
1083 | 1.06k | } |
1084 | | |
1085 | | |
1086 | 3.59k | static void warnfon (void *ud, const char *message, int tocont) { |
1087 | 3.59k | if (checkcontrol((lua_State *)ud, message, tocont)) /* control message? */ |
1088 | 2.53k | return; /* nothing else to be done */ |
1089 | 1.06k | lua_writestringerror("%s", "Lua warning: "); /* start a new warning */ |
1090 | 1.06k | warnfcont(ud, message, tocont); /* finish processing */ |
1091 | 1.06k | } |
1092 | | |
1093 | | |
1094 | 15.8k | LUALIB_API lua_State *luaL_newstate (void) { |
1095 | 15.8k | lua_State *L = lua_newstate(l_alloc, NULL); |
1096 | 15.8k | if (l_likely(L)) { |
1097 | 15.8k | lua_atpanic(L, &panic); |
1098 | 15.8k | lua_setwarnf(L, warnfoff, L); /* default is warnings off */ |
1099 | 15.8k | } |
1100 | 15.8k | return L; |
1101 | 15.8k | } |
1102 | | |
1103 | | |
1104 | 28.8k | LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) { |
1105 | 28.8k | lua_Number v = lua_version(L); |
1106 | 28.8k | if (sz != LUAL_NUMSIZES) /* check numeric types */ |
1107 | 0 | luaL_error(L, "core and library have incompatible numeric types"); |
1108 | 28.8k | else if (v != ver) |
1109 | 0 | luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f", |
1110 | 0 | (LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)v); |
1111 | 28.8k | } |
1112 | | |