/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 | 155 | #define LEVELS1 10 /* size of the first part of the stack */ |
44 | 155 | #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 | 201 | static int findfield (lua_State *L, int objidx, int level) { |
53 | 201 | if (level == 0 || !lua_istable(L, -1)) |
54 | 201 | return 0; /* not found */ |
55 | 0 | lua_pushnil(L); /* start 'next' loop */ |
56 | 0 | while (lua_next(L, -2)) { /* for each pair in table */ |
57 | 0 | if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */ |
58 | 0 | if (lua_rawequal(L, objidx, -1)) { /* found object? */ |
59 | 0 | lua_pop(L, 1); /* remove value (but keep name) */ |
60 | 0 | return 1; |
61 | 0 | } |
62 | 0 | else if (findfield(L, objidx, level - 1)) { /* try recursively */ |
63 | | /* stack: lib_name, lib_table, field_name (top) */ |
64 | 0 | lua_pushliteral(L, "."); /* place '.' between the two names */ |
65 | 0 | lua_replace(L, -3); /* (in the slot occupied by table) */ |
66 | 0 | lua_concat(L, 3); /* lib_name.field_name */ |
67 | 0 | return 1; |
68 | 0 | } |
69 | 0 | } |
70 | 0 | lua_pop(L, 1); /* remove value */ |
71 | 0 | } |
72 | 0 | return 0; /* not found */ |
73 | 0 | } |
74 | | |
75 | | |
76 | | /* |
77 | | ** Search for a name for a function in all loaded modules |
78 | | */ |
79 | 201 | static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { |
80 | 201 | int top = lua_gettop(L); |
81 | 201 | lua_getinfo(L, "f", ar); /* push function */ |
82 | 201 | lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); |
83 | 201 | if (findfield(L, top + 1, 2)) { |
84 | 0 | const char *name = lua_tostring(L, -1); |
85 | 0 | if (strncmp(name, LUA_GNAME ".", 3) == 0) { /* name start with '_G.'? */ |
86 | 0 | lua_pushstring(L, name + 3); /* push name without prefix */ |
87 | 0 | lua_remove(L, -2); /* remove original name */ |
88 | 0 | } |
89 | 0 | lua_copy(L, -1, top + 1); /* copy name to proper place */ |
90 | 0 | lua_settop(L, top + 1); /* remove table "loaded" and name copy */ |
91 | 0 | return 1; |
92 | 0 | } |
93 | 201 | else { |
94 | 201 | lua_settop(L, top); /* remove function and global table */ |
95 | 201 | return 0; |
96 | 201 | } |
97 | 201 | } |
98 | | |
99 | | |
100 | 201 | static void pushfuncname (lua_State *L, lua_Debug *ar) { |
101 | 201 | 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 | 201 | else if (*ar->namewhat != '\0') /* is there a name from code? */ |
106 | 48 | lua_pushfstring(L, "%s '%s'", ar->namewhat, ar->name); /* use it */ |
107 | 153 | else if (*ar->what == 'm') /* main? */ |
108 | 153 | 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 | 201 | } |
114 | | |
115 | | |
116 | 153 | static int lastlevel (lua_State *L) { |
117 | 153 | lua_Debug ar; |
118 | 153 | int li = 1, le = 1; |
119 | | /* find an upper bound */ |
120 | 324 | while (lua_getstack(L, le, &ar)) { li = le; le *= 2; } |
121 | | /* do a binary search */ |
122 | 318 | while (li < le) { |
123 | 165 | int m = (li + le)/2; |
124 | 165 | if (lua_getstack(L, m, &ar)) li = m + 1; |
125 | 6 | else le = m; |
126 | 165 | } |
127 | 153 | return le - 1; |
128 | 153 | } |
129 | | |
130 | | |
131 | | LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, |
132 | 153 | const char *msg, int level) { |
133 | 153 | luaL_Buffer b; |
134 | 153 | lua_Debug ar; |
135 | 153 | int last = lastlevel(L1); |
136 | 153 | int limit2show = (last - level > LEVELS1 + LEVELS2) ? LEVELS1 : -1; |
137 | 153 | luaL_buffinit(L, &b); |
138 | 153 | if (msg) { |
139 | 153 | luaL_addstring(&b, msg); |
140 | 153 | luaL_addchar(&b, '\n'); |
141 | 153 | } |
142 | 153 | luaL_addstring(&b, "stack traceback:"); |
143 | 356 | while (lua_getstack(L1, level++, &ar)) { |
144 | 203 | if (limit2show-- == 0) { /* too many levels? */ |
145 | 2 | int n = last - level - LEVELS2 + 1; /* number of levels to skip */ |
146 | 2 | lua_pushfstring(L, "\n\t...\t(skipping %d levels)", n); |
147 | 2 | luaL_addvalue(&b); /* add warning about skip */ |
148 | 2 | level += n; /* and skip to last levels */ |
149 | 2 | } |
150 | 201 | else { |
151 | 201 | lua_getinfo(L1, "Slnt", &ar); |
152 | 201 | if (ar.currentline <= 0) |
153 | 0 | lua_pushfstring(L, "\n\t%s: in ", ar.short_src); |
154 | 201 | else |
155 | 201 | lua_pushfstring(L, "\n\t%s:%d: in ", ar.short_src, ar.currentline); |
156 | 201 | luaL_addvalue(&b); |
157 | 201 | pushfuncname(L, &ar); |
158 | 201 | luaL_addvalue(&b); |
159 | 201 | if (ar.istailcall) |
160 | 0 | luaL_addstring(&b, "\n\t(...tail calls...)"); |
161 | 201 | } |
162 | 203 | } |
163 | 153 | luaL_pushresult(&b); |
164 | 153 | } |
165 | | |
166 | | /* }====================================================== */ |
167 | | |
168 | | |
169 | | /* |
170 | | ** {====================================================== |
171 | | ** Error-report functions |
172 | | ** ======================================================= |
173 | | */ |
174 | | |
175 | 0 | LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) { |
176 | 0 | lua_Debug ar; |
177 | 0 | if (!lua_getstack(L, 0, &ar)) /* no stack frame? */ |
178 | 0 | return luaL_error(L, "bad argument #%d (%s)", arg, extramsg); |
179 | 0 | lua_getinfo(L, "n", &ar); |
180 | 0 | if (strcmp(ar.namewhat, "method") == 0) { |
181 | 0 | arg--; /* do not count 'self' */ |
182 | 0 | 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 | 0 | } |
186 | 0 | if (ar.name == NULL) |
187 | 0 | ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?"; |
188 | 0 | return luaL_error(L, "bad argument #%d to '%s' (%s)", |
189 | 0 | arg, ar.name, extramsg); |
190 | 0 | } |
191 | | |
192 | | |
193 | 0 | LUALIB_API int luaL_typeerror (lua_State *L, int arg, const char *tname) { |
194 | 0 | const char *msg; |
195 | 0 | const char *typearg; /* name for the type of the actual argument */ |
196 | 0 | if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING) |
197 | 0 | typearg = lua_tostring(L, -1); /* use the given type name */ |
198 | 0 | else if (lua_type(L, arg) == LUA_TLIGHTUSERDATA) |
199 | 0 | typearg = "light userdata"; /* special name for messages */ |
200 | 0 | else |
201 | 0 | typearg = luaL_typename(L, arg); /* standard name */ |
202 | 0 | msg = lua_pushfstring(L, "%s expected, got %s", tname, typearg); |
203 | 0 | return luaL_argerror(L, arg, msg); |
204 | 0 | } |
205 | | |
206 | | |
207 | 0 | static void tag_error (lua_State *L, int arg, int tag) { |
208 | 0 | luaL_typeerror(L, arg, lua_typename(L, tag)); |
209 | 0 | } |
210 | | |
211 | | |
212 | | /* |
213 | | ** The use of 'lua_pushfstring' ensures this function does not |
214 | | ** need reserved stack space when called. |
215 | | */ |
216 | 0 | LUALIB_API void luaL_where (lua_State *L, int level) { |
217 | 0 | lua_Debug ar; |
218 | 0 | if (lua_getstack(L, level, &ar)) { /* check function at level */ |
219 | 0 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
220 | 0 | if (ar.currentline > 0) { /* is there info? */ |
221 | 0 | lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline); |
222 | 0 | return; |
223 | 0 | } |
224 | 0 | } |
225 | 0 | lua_pushfstring(L, ""); /* else, no information available... */ |
226 | 0 | } |
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 | 0 | LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { |
235 | 0 | va_list argp; |
236 | 0 | va_start(argp, fmt); |
237 | 0 | luaL_where(L, 1); |
238 | 0 | lua_pushvfstring(L, fmt, argp); |
239 | 0 | va_end(argp); |
240 | 0 | lua_concat(L, 2); |
241 | 0 | return lua_error(L); |
242 | 0 | } |
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 | 45 | LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { |
312 | 45 | if (luaL_getmetatable(L, tname) != LUA_TNIL) /* name already in use? */ |
313 | 0 | return 0; /* leave previous value on top, but return 0 */ |
314 | 45 | lua_pop(L, 1); |
315 | 45 | lua_createtable(L, 0, 2); /* create metatable */ |
316 | 45 | lua_pushstring(L, tname); |
317 | 45 | lua_setfield(L, -2, "__name"); /* metatable.__name = tname */ |
318 | 45 | lua_pushvalue(L, -1); |
319 | 45 | lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */ |
320 | 45 | return 1; |
321 | 45 | } |
322 | | |
323 | | |
324 | 0 | LUALIB_API void luaL_setmetatable (lua_State *L, const char *tname) { |
325 | 0 | luaL_getmetatable(L, tname); |
326 | 0 | lua_setmetatable(L, -2); |
327 | 0 | } |
328 | | |
329 | | |
330 | 0 | LUALIB_API void *luaL_testudata (lua_State *L, int ud, const char *tname) { |
331 | 0 | void *p = lua_touserdata(L, ud); |
332 | 0 | if (p != NULL) { /* value is a userdata? */ |
333 | 0 | if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ |
334 | 0 | luaL_getmetatable(L, tname); /* get correct metatable */ |
335 | 0 | if (!lua_rawequal(L, -1, -2)) /* not the same? */ |
336 | 0 | p = NULL; /* value is a userdata with wrong metatable */ |
337 | 0 | lua_pop(L, 2); /* remove both metatables */ |
338 | 0 | return p; |
339 | 0 | } |
340 | 0 | } |
341 | 0 | return NULL; /* value is not a userdata with a metatable */ |
342 | 0 | } |
343 | | |
344 | | |
345 | 0 | LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { |
346 | 0 | void *p = luaL_testudata(L, ud, tname); |
347 | 0 | luaL_argexpected(L, p != NULL, ud, tname); |
348 | 0 | return p; |
349 | 0 | } |
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 | 0 | const char *const lst[]) { |
362 | 0 | const char *name = (def) ? luaL_optstring(L, arg, def) : |
363 | 0 | luaL_checkstring(L, arg); |
364 | 0 | int i; |
365 | 0 | for (i=0; lst[i]; i++) |
366 | 0 | if (strcmp(lst[i], name) == 0) |
367 | 0 | return i; |
368 | 0 | return luaL_argerror(L, arg, |
369 | 0 | lua_pushfstring(L, "invalid option '%s'", name)); |
370 | 0 | } |
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 | 45 | LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) { |
381 | 45 | if (l_unlikely(!lua_checkstack(L, space))) { |
382 | 0 | if (msg) |
383 | 0 | luaL_error(L, "stack overflow (%s)", msg); |
384 | 0 | else |
385 | 0 | luaL_error(L, "stack overflow"); |
386 | 0 | } |
387 | 45 | } |
388 | | |
389 | | |
390 | 0 | LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) { |
391 | 0 | if (l_unlikely(lua_type(L, arg) != t)) |
392 | 0 | tag_error(L, arg, t); |
393 | 0 | } |
394 | | |
395 | | |
396 | 0 | LUALIB_API void luaL_checkany (lua_State *L, int arg) { |
397 | 0 | if (l_unlikely(lua_type(L, arg) == LUA_TNONE)) |
398 | 0 | luaL_argerror(L, arg, "value expected"); |
399 | 0 | } |
400 | | |
401 | | |
402 | 0 | LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) { |
403 | 0 | const char *s = lua_tolstring(L, arg, len); |
404 | 0 | if (l_unlikely(!s)) tag_error(L, arg, LUA_TSTRING); |
405 | 0 | return s; |
406 | 0 | } |
407 | | |
408 | | |
409 | | LUALIB_API const char *luaL_optlstring (lua_State *L, int arg, |
410 | 0 | const char *def, size_t *len) { |
411 | 0 | if (lua_isnoneornil(L, arg)) { |
412 | 0 | if (len) |
413 | 0 | *len = (def ? strlen(def) : 0); |
414 | 0 | return def; |
415 | 0 | } |
416 | 0 | else return luaL_checklstring(L, arg, len); |
417 | 0 | } |
418 | | |
419 | | |
420 | 0 | LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) { |
421 | 0 | int isnum; |
422 | 0 | lua_Number d = lua_tonumberx(L, arg, &isnum); |
423 | 0 | if (l_unlikely(!isnum)) |
424 | 0 | tag_error(L, arg, LUA_TNUMBER); |
425 | 0 | return d; |
426 | 0 | } |
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 | 0 | static void interror (lua_State *L, int arg) { |
435 | 0 | if (lua_isnumber(L, arg)) |
436 | 0 | luaL_argerror(L, arg, "number has no integer representation"); |
437 | 0 | else |
438 | 0 | tag_error(L, arg, LUA_TNUMBER); |
439 | 0 | } |
440 | | |
441 | | |
442 | 0 | LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) { |
443 | 0 | int isnum; |
444 | 0 | lua_Integer d = lua_tointegerx(L, arg, &isnum); |
445 | 0 | if (l_unlikely(!isnum)) { |
446 | 0 | interror(L, arg); |
447 | 0 | } |
448 | 0 | return d; |
449 | 0 | } |
450 | | |
451 | | |
452 | | LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int arg, |
453 | 0 | lua_Integer def) { |
454 | 0 | return luaL_opt(L, luaL_checkinteger, arg, def); |
455 | 0 | } |
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 | | /* Resize the buffer used by a box. Optimize for the common case of |
474 | | ** resizing to the old size. (For instance, __gc will resize the box |
475 | | ** to 0 even after it was closed. 'pushresult' may also resize it to a |
476 | | ** final size that is equal to the one set when the buffer was created.) |
477 | | */ |
478 | 225 | static void *resizebox (lua_State *L, int idx, size_t newsize) { |
479 | 225 | UBox *box = (UBox *)lua_touserdata(L, idx); |
480 | 225 | if (box->bsize == newsize) /* not changing size? */ |
481 | 90 | return box->box; /* keep the buffer */ |
482 | 135 | else { |
483 | 135 | void *ud; |
484 | 135 | lua_Alloc allocf = lua_getallocf(L, &ud); |
485 | 135 | void *temp = allocf(ud, box->box, box->bsize, newsize); |
486 | 135 | if (l_unlikely(temp == NULL && newsize > 0)) { /* allocation error? */ |
487 | 0 | lua_pushliteral(L, "not enough memory"); |
488 | 0 | lua_error(L); /* raise a memory error */ |
489 | 0 | } |
490 | 135 | box->box = temp; |
491 | 135 | box->bsize = newsize; |
492 | 135 | return temp; |
493 | 135 | } |
494 | 225 | } |
495 | | |
496 | | |
497 | 90 | static int boxgc (lua_State *L) { |
498 | 90 | resizebox(L, 1, 0); |
499 | 90 | return 0; |
500 | 90 | } |
501 | | |
502 | | |
503 | | static const luaL_Reg boxmt[] = { /* box metamethods */ |
504 | | {"__gc", boxgc}, |
505 | | {"__close", boxgc}, |
506 | | {NULL, NULL} |
507 | | }; |
508 | | |
509 | | |
510 | 45 | static void newbox (lua_State *L) { |
511 | 45 | UBox *box = (UBox *)lua_newuserdatauv(L, sizeof(UBox), 0); |
512 | 45 | box->box = NULL; |
513 | 45 | box->bsize = 0; |
514 | 45 | if (luaL_newmetatable(L, "_UBOX*")) /* creating metatable? */ |
515 | 45 | luaL_setfuncs(L, boxmt, 0); /* set its metamethods */ |
516 | 45 | lua_setmetatable(L, -2); |
517 | 45 | } |
518 | | |
519 | | |
520 | | /* |
521 | | ** check whether buffer is using a userdata on the stack as a temporary |
522 | | ** buffer |
523 | | */ |
524 | 243 | #define buffonstack(B) ((B)->b != (B)->init.b) |
525 | | |
526 | | |
527 | | /* |
528 | | ** Whenever buffer is accessed, slot 'idx' must either be a box (which |
529 | | ** cannot be NULL) or it is a placeholder for the buffer. |
530 | | */ |
531 | | #define checkbufferlevel(B,idx) \ |
532 | 863 | lua_assert(buffonstack(B) ? lua_touserdata(B->L, idx) != NULL \ |
533 | 863 | : lua_touserdata(B->L, idx) == (void*)B) |
534 | | |
535 | | |
536 | | /* |
537 | | ** Compute new size for buffer 'B', enough to accommodate extra 'sz' |
538 | | ** bytes plus one for a terminating zero. (The test for "not big enough" |
539 | | ** also gets the case when the computation of 'newsize' overflows.) |
540 | | */ |
541 | 90 | static size_t newbuffsize (luaL_Buffer *B, size_t sz) { |
542 | 90 | size_t newsize = (B->size / 2) * 3; /* buffer size * 1.5 */ |
543 | 90 | if (l_unlikely(MAX_SIZET - sz - 1 < B->n)) /* overflow in (B->n + sz + 1)? */ |
544 | 0 | return luaL_error(B->L, "buffer too large"); |
545 | 90 | if (newsize < B->n + sz + 1) /* not big enough? */ |
546 | 45 | newsize = B->n + sz + 1; |
547 | 90 | return newsize; |
548 | 90 | } |
549 | | |
550 | | |
551 | | /* |
552 | | ** Returns a pointer to a free area with at least 'sz' bytes in buffer |
553 | | ** 'B'. 'boxidx' is the relative position in the stack where is the |
554 | | ** buffer's box or its placeholder. |
555 | | */ |
556 | 710 | static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) { |
557 | 710 | checkbufferlevel(B, boxidx); |
558 | 710 | if (B->size - B->n >= sz) /* enough space? */ |
559 | 620 | return B->b + B->n; |
560 | 90 | else { |
561 | 90 | lua_State *L = B->L; |
562 | 90 | char *newbuff; |
563 | 90 | size_t newsize = newbuffsize(B, sz); |
564 | | /* create larger buffer */ |
565 | 90 | if (buffonstack(B)) /* buffer already has a box? */ |
566 | 45 | newbuff = (char *)resizebox(L, boxidx, newsize); /* resize it */ |
567 | 45 | else { /* no box yet */ |
568 | 45 | lua_remove(L, boxidx); /* remove placeholder */ |
569 | 45 | newbox(L); /* create a new box */ |
570 | 45 | lua_insert(L, boxidx); /* move box to its intended position */ |
571 | 45 | lua_toclose(L, boxidx); |
572 | 45 | newbuff = (char *)resizebox(L, boxidx, newsize); |
573 | 45 | memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */ |
574 | 45 | } |
575 | 90 | B->b = newbuff; |
576 | 90 | B->size = newsize; |
577 | 90 | return newbuff + B->n; |
578 | 90 | } |
579 | 710 | } |
580 | | |
581 | | /* |
582 | | ** returns a pointer to a free area with at least 'sz' bytes |
583 | | */ |
584 | 0 | LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { |
585 | 0 | return prepbuffsize(B, sz, -1); |
586 | 0 | } |
587 | | |
588 | | |
589 | 306 | LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { |
590 | 306 | if (l > 0) { /* avoid 'memcpy' when 's' can be NULL */ |
591 | 306 | char *b = prepbuffsize(B, l, -1); |
592 | 306 | memcpy(b, s, l * sizeof(char)); |
593 | 306 | luaL_addsize(B, l); |
594 | 306 | } |
595 | 306 | } |
596 | | |
597 | | |
598 | 306 | LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) { |
599 | 306 | luaL_addlstring(B, s, strlen(s)); |
600 | 306 | } |
601 | | |
602 | | |
603 | 153 | LUALIB_API void luaL_pushresult (luaL_Buffer *B) { |
604 | 153 | lua_State *L = B->L; |
605 | 153 | checkbufferlevel(B, -1); |
606 | 153 | if (!buffonstack(B)) /* using static buffer? */ |
607 | 108 | lua_pushlstring(L, B->b, B->n); /* save result as regular string */ |
608 | 45 | else { /* reuse buffer already allocated */ |
609 | 45 | UBox *box = (UBox *)lua_touserdata(L, -1); |
610 | 45 | void *ud; |
611 | 45 | lua_Alloc allocf = lua_getallocf(L, &ud); /* function to free buffer */ |
612 | 45 | size_t len = B->n; /* final string length */ |
613 | 45 | char *s; |
614 | 45 | resizebox(L, -1, len + 1); /* adjust box size to content size */ |
615 | 45 | s = (char*)box->box; /* final buffer address */ |
616 | 45 | s[len] = '\0'; /* add ending zero */ |
617 | | /* clear box, as 'lua_pushextlstring' will take control over buffer */ |
618 | 45 | box->bsize = 0; box->box = NULL; |
619 | 45 | lua_pushextlstring(L, s, len, allocf, ud); |
620 | 45 | lua_closeslot(L, -2); /* close the box */ |
621 | 45 | } |
622 | 153 | lua_remove(L, -2); /* remove box or placeholder from the stack */ |
623 | 153 | } |
624 | | |
625 | | |
626 | 0 | LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) { |
627 | 0 | luaL_addsize(B, sz); |
628 | 0 | luaL_pushresult(B); |
629 | 0 | } |
630 | | |
631 | | |
632 | | /* |
633 | | ** 'luaL_addvalue' is the only function in the Buffer system where the |
634 | | ** box (if existent) is not on the top of the stack. So, instead of |
635 | | ** calling 'luaL_addlstring', it replicates the code using -2 as the |
636 | | ** last argument to 'prepbuffsize', signaling that the box is (or will |
637 | | ** be) below the string being added to the buffer. (Box creation can |
638 | | ** trigger an emergency GC, so we should not remove the string from the |
639 | | ** stack before we have the space guaranteed.) |
640 | | */ |
641 | 404 | LUALIB_API void luaL_addvalue (luaL_Buffer *B) { |
642 | 404 | lua_State *L = B->L; |
643 | 404 | size_t len; |
644 | 404 | const char *s = lua_tolstring(L, -1, &len); |
645 | 404 | char *b = prepbuffsize(B, len, -2); |
646 | 404 | memcpy(b, s, len * sizeof(char)); |
647 | 404 | luaL_addsize(B, len); |
648 | 404 | lua_pop(L, 1); /* pop string */ |
649 | 404 | } |
650 | | |
651 | | |
652 | 153 | LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { |
653 | 153 | B->L = L; |
654 | 153 | B->b = B->init.b; |
655 | 153 | B->n = 0; |
656 | 153 | B->size = LUAL_BUFFERSIZE; |
657 | 153 | lua_pushlightuserdata(L, (void*)B); /* push placeholder */ |
658 | 153 | } |
659 | | |
660 | | |
661 | 0 | LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) { |
662 | 0 | luaL_buffinit(L, B); |
663 | 0 | return prepbuffsize(B, sz, -1); |
664 | 0 | } |
665 | | |
666 | | /* }====================================================== */ |
667 | | |
668 | | |
669 | | /* |
670 | | ** {====================================================== |
671 | | ** Reference system |
672 | | ** ======================================================= |
673 | | */ |
674 | | |
675 | | /* |
676 | | ** The previously freed references form a linked list: t[1] is the index |
677 | | ** of a first free index, t[t[1]] is the index of the second element, |
678 | | ** etc. A zero signals the end of the list. |
679 | | */ |
680 | 0 | LUALIB_API int luaL_ref (lua_State *L, int t) { |
681 | 0 | int ref; |
682 | 0 | if (lua_isnil(L, -1)) { |
683 | 0 | lua_pop(L, 1); /* remove from stack */ |
684 | 0 | return LUA_REFNIL; /* 'nil' has a unique fixed reference */ |
685 | 0 | } |
686 | 0 | t = lua_absindex(L, t); |
687 | 0 | if (lua_rawgeti(L, t, 1) == LUA_TNUMBER) /* already initialized? */ |
688 | 0 | ref = (int)lua_tointeger(L, -1); /* ref = t[1] */ |
689 | 0 | else { /* first access */ |
690 | 0 | lua_assert(!lua_toboolean(L, -1)); /* must be nil or false */ |
691 | 0 | ref = 0; /* list is empty */ |
692 | 0 | lua_pushinteger(L, 0); /* initialize as an empty list */ |
693 | 0 | lua_rawseti(L, t, 1); /* ref = t[1] = 0 */ |
694 | 0 | } |
695 | 0 | lua_pop(L, 1); /* remove element from stack */ |
696 | 0 | if (ref != 0) { /* any free element? */ |
697 | 0 | lua_rawgeti(L, t, ref); /* remove it from list */ |
698 | 0 | lua_rawseti(L, t, 1); /* (t[1] = t[ref]) */ |
699 | 0 | } |
700 | 0 | else /* no free elements */ |
701 | 0 | ref = (int)lua_rawlen(L, t) + 1; /* get a new reference */ |
702 | 0 | lua_rawseti(L, t, ref); |
703 | 0 | return ref; |
704 | 0 | } |
705 | | |
706 | | |
707 | 0 | LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { |
708 | 0 | if (ref >= 0) { |
709 | 0 | t = lua_absindex(L, t); |
710 | 0 | lua_rawgeti(L, t, 1); |
711 | 0 | lua_assert(lua_isinteger(L, -1)); |
712 | 0 | lua_rawseti(L, t, ref); /* t[ref] = t[1] */ |
713 | 0 | lua_pushinteger(L, ref); |
714 | 0 | lua_rawseti(L, t, 1); /* t[1] = ref */ |
715 | 0 | } |
716 | 0 | } |
717 | | |
718 | | /* }====================================================== */ |
719 | | |
720 | | |
721 | | /* |
722 | | ** {====================================================== |
723 | | ** Load functions |
724 | | ** ======================================================= |
725 | | */ |
726 | | |
727 | | typedef struct LoadF { |
728 | | int n; /* number of pre-read characters */ |
729 | | FILE *f; /* file being read */ |
730 | | char buff[BUFSIZ]; /* area for reading file */ |
731 | | } LoadF; |
732 | | |
733 | | |
734 | 0 | static const char *getF (lua_State *L, void *ud, size_t *size) { |
735 | 0 | LoadF *lf = (LoadF *)ud; |
736 | 0 | (void)L; /* not used */ |
737 | 0 | if (lf->n > 0) { /* are there pre-read characters to be read? */ |
738 | 0 | *size = lf->n; /* return them (chars already in buffer) */ |
739 | 0 | lf->n = 0; /* no more pre-read characters */ |
740 | 0 | } |
741 | 0 | else { /* read a block from file */ |
742 | | /* 'fread' can return > 0 *and* set the EOF flag. If next call to |
743 | | 'getF' called 'fread', it might still wait for user input. |
744 | | The next check avoids this problem. */ |
745 | 0 | if (feof(lf->f)) return NULL; |
746 | 0 | *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */ |
747 | 0 | } |
748 | 0 | return lf->buff; |
749 | 0 | } |
750 | | |
751 | | |
752 | 0 | static int errfile (lua_State *L, const char *what, int fnameindex) { |
753 | 0 | const char *serr = strerror(errno); |
754 | 0 | const char *filename = lua_tostring(L, fnameindex) + 1; |
755 | 0 | lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr); |
756 | 0 | lua_remove(L, fnameindex); |
757 | 0 | return LUA_ERRFILE; |
758 | 0 | } |
759 | | |
760 | | |
761 | | /* |
762 | | ** Skip an optional BOM at the start of a stream. If there is an |
763 | | ** incomplete BOM (the first character is correct but the rest is |
764 | | ** not), returns the first character anyway to force an error |
765 | | ** (as no chunk can start with 0xEF). |
766 | | */ |
767 | 0 | static int skipBOM (FILE *f) { |
768 | 0 | int c = getc(f); /* read first character */ |
769 | 0 | if (c == 0xEF && getc(f) == 0xBB && getc(f) == 0xBF) /* correct BOM? */ |
770 | 0 | return getc(f); /* ignore BOM and return next char */ |
771 | 0 | else /* no (valid) BOM */ |
772 | 0 | return c; /* return first character */ |
773 | 0 | } |
774 | | |
775 | | |
776 | | /* |
777 | | ** reads the first character of file 'f' and skips an optional BOM mark |
778 | | ** in its beginning plus its first line if it starts with '#'. Returns |
779 | | ** true if it skipped the first line. In any case, '*cp' has the |
780 | | ** first "valid" character of the file (after the optional BOM and |
781 | | ** a first-line comment). |
782 | | */ |
783 | 0 | static int skipcomment (FILE *f, int *cp) { |
784 | 0 | int c = *cp = skipBOM(f); |
785 | 0 | if (c == '#') { /* first line is a comment (Unix exec. file)? */ |
786 | 0 | do { /* skip first line */ |
787 | 0 | c = getc(f); |
788 | 0 | } while (c != EOF && c != '\n'); |
789 | 0 | *cp = getc(f); /* next character after comment, if present */ |
790 | 0 | return 1; /* there was a comment */ |
791 | 0 | } |
792 | 0 | else return 0; /* no comment */ |
793 | 0 | } |
794 | | |
795 | | |
796 | | LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, |
797 | 0 | const char *mode) { |
798 | 0 | LoadF lf; |
799 | 0 | int status, readstatus; |
800 | 0 | int c; |
801 | 0 | int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ |
802 | 0 | if (filename == NULL) { |
803 | 0 | lua_pushliteral(L, "=stdin"); |
804 | 0 | lf.f = stdin; |
805 | 0 | } |
806 | 0 | else { |
807 | 0 | lua_pushfstring(L, "@%s", filename); |
808 | 0 | lf.f = fopen(filename, "r"); |
809 | 0 | if (lf.f == NULL) return errfile(L, "open", fnameindex); |
810 | 0 | } |
811 | 0 | lf.n = 0; |
812 | 0 | if (skipcomment(lf.f, &c)) /* read initial portion */ |
813 | 0 | lf.buff[lf.n++] = '\n'; /* add newline to correct line numbers */ |
814 | 0 | if (c == LUA_SIGNATURE[0]) { /* binary file? */ |
815 | 0 | lf.n = 0; /* remove possible newline */ |
816 | 0 | if (filename) { /* "real" file? */ |
817 | 0 | lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ |
818 | 0 | if (lf.f == NULL) return errfile(L, "reopen", fnameindex); |
819 | 0 | skipcomment(lf.f, &c); /* re-read initial portion */ |
820 | 0 | } |
821 | 0 | } |
822 | 0 | if (c != EOF) |
823 | 0 | lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ |
824 | 0 | status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); |
825 | 0 | readstatus = ferror(lf.f); |
826 | 0 | if (filename) fclose(lf.f); /* close file (even in case of errors) */ |
827 | 0 | if (readstatus) { |
828 | 0 | lua_settop(L, fnameindex); /* ignore results from 'lua_load' */ |
829 | 0 | return errfile(L, "read", fnameindex); |
830 | 0 | } |
831 | 0 | lua_remove(L, fnameindex); |
832 | 0 | return status; |
833 | 0 | } |
834 | | |
835 | | |
836 | | typedef struct LoadS { |
837 | | const char *s; |
838 | | size_t size; |
839 | | } LoadS; |
840 | | |
841 | | |
842 | 803 | static const char *getS (lua_State *L, void *ud, size_t *size) { |
843 | 803 | LoadS *ls = (LoadS *)ud; |
844 | 803 | (void)L; /* not used */ |
845 | 803 | if (ls->size == 0) return NULL; |
846 | 561 | *size = ls->size; |
847 | 561 | ls->size = 0; |
848 | 561 | return ls->s; |
849 | 803 | } |
850 | | |
851 | | |
852 | | LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size, |
853 | 561 | const char *name, const char *mode) { |
854 | 561 | LoadS ls; |
855 | 561 | ls.s = buff; |
856 | 561 | ls.size = size; |
857 | 561 | return lua_load(L, getS, &ls, name, mode); |
858 | 561 | } |
859 | | |
860 | | |
861 | 0 | LUALIB_API int luaL_loadstring (lua_State *L, const char *s) { |
862 | 0 | return luaL_loadbuffer(L, s, strlen(s), s); |
863 | 0 | } |
864 | | |
865 | | /* }====================================================== */ |
866 | | |
867 | | |
868 | | |
869 | 0 | LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) { |
870 | 0 | if (!lua_getmetatable(L, obj)) /* no metatable? */ |
871 | 0 | return LUA_TNIL; |
872 | 0 | else { |
873 | 0 | int tt; |
874 | 0 | lua_pushstring(L, event); |
875 | 0 | tt = lua_rawget(L, -2); |
876 | 0 | if (tt == LUA_TNIL) /* is metafield nil? */ |
877 | 0 | lua_pop(L, 2); /* remove metatable and metafield */ |
878 | 0 | else |
879 | 0 | lua_remove(L, -2); /* remove only metatable */ |
880 | 0 | return tt; /* return metafield type */ |
881 | 0 | } |
882 | 0 | } |
883 | | |
884 | | |
885 | 0 | LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { |
886 | 0 | obj = lua_absindex(L, obj); |
887 | 0 | if (luaL_getmetafield(L, obj, event) == LUA_TNIL) /* no metafield? */ |
888 | 0 | return 0; |
889 | 0 | lua_pushvalue(L, obj); |
890 | 0 | lua_call(L, 1, 1); |
891 | 0 | return 1; |
892 | 0 | } |
893 | | |
894 | | |
895 | 0 | LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) { |
896 | 0 | lua_Integer l; |
897 | 0 | int isnum; |
898 | 0 | lua_len(L, idx); |
899 | 0 | l = lua_tointegerx(L, -1, &isnum); |
900 | 0 | if (l_unlikely(!isnum)) |
901 | 0 | luaL_error(L, "object length is not an integer"); |
902 | 0 | lua_pop(L, 1); /* remove object */ |
903 | 0 | return l; |
904 | 0 | } |
905 | | |
906 | | |
907 | 0 | LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { |
908 | 0 | idx = lua_absindex(L,idx); |
909 | 0 | if (luaL_callmeta(L, idx, "__tostring")) { /* metafield? */ |
910 | 0 | if (!lua_isstring(L, -1)) |
911 | 0 | luaL_error(L, "'__tostring' must return a string"); |
912 | 0 | } |
913 | 0 | else { |
914 | 0 | switch (lua_type(L, idx)) { |
915 | 0 | case LUA_TNUMBER: { |
916 | 0 | if (lua_isinteger(L, idx)) |
917 | 0 | lua_pushfstring(L, "%I", (LUAI_UACINT)lua_tointeger(L, idx)); |
918 | 0 | else |
919 | 0 | lua_pushfstring(L, "%f", (LUAI_UACNUMBER)lua_tonumber(L, idx)); |
920 | 0 | break; |
921 | 0 | } |
922 | 0 | case LUA_TSTRING: |
923 | 0 | lua_pushvalue(L, idx); |
924 | 0 | break; |
925 | 0 | case LUA_TBOOLEAN: |
926 | 0 | lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false")); |
927 | 0 | break; |
928 | 0 | case LUA_TNIL: |
929 | 0 | lua_pushliteral(L, "nil"); |
930 | 0 | break; |
931 | 0 | default: { |
932 | 0 | int tt = luaL_getmetafield(L, idx, "__name"); /* try name */ |
933 | 0 | const char *kind = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : |
934 | 0 | luaL_typename(L, idx); |
935 | 0 | lua_pushfstring(L, "%s: %p", kind, lua_topointer(L, idx)); |
936 | 0 | if (tt != LUA_TNIL) |
937 | 0 | lua_remove(L, -2); /* remove '__name' */ |
938 | 0 | break; |
939 | 0 | } |
940 | 0 | } |
941 | 0 | } |
942 | 0 | return lua_tolstring(L, -1, len); |
943 | 0 | } |
944 | | |
945 | | |
946 | | /* |
947 | | ** set functions from list 'l' into table at top - 'nup'; each |
948 | | ** function gets the 'nup' elements at the top as upvalues. |
949 | | ** Returns with only the table at the stack. |
950 | | */ |
951 | 45 | LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { |
952 | 45 | luaL_checkstack(L, nup, "too many upvalues"); |
953 | 135 | for (; l->name != NULL; l++) { /* fill the table with given functions */ |
954 | 90 | if (l->func == NULL) /* place holder? */ |
955 | 0 | lua_pushboolean(L, 0); |
956 | 90 | else { |
957 | 90 | int i; |
958 | 90 | for (i = 0; i < nup; i++) /* copy upvalues to the top */ |
959 | 0 | lua_pushvalue(L, -nup); |
960 | 90 | lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ |
961 | 90 | } |
962 | 90 | lua_setfield(L, -(nup + 2), l->name); |
963 | 90 | } |
964 | 45 | lua_pop(L, nup); /* remove upvalues */ |
965 | 45 | } |
966 | | |
967 | | |
968 | | /* |
969 | | ** ensure that stack[idx][fname] has a table and push that table |
970 | | ** into the stack |
971 | | */ |
972 | 0 | LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) { |
973 | 0 | if (lua_getfield(L, idx, fname) == LUA_TTABLE) |
974 | 0 | return 1; /* table already there */ |
975 | 0 | else { |
976 | 0 | lua_pop(L, 1); /* remove previous result */ |
977 | 0 | idx = lua_absindex(L, idx); |
978 | 0 | lua_newtable(L); |
979 | 0 | lua_pushvalue(L, -1); /* copy to be left at top */ |
980 | 0 | lua_setfield(L, idx, fname); /* assign new table to field */ |
981 | 0 | return 0; /* false, because did not find table there */ |
982 | 0 | } |
983 | 0 | } |
984 | | |
985 | | |
986 | | /* |
987 | | ** Stripped-down 'require': After checking "loaded" table, calls 'openf' |
988 | | ** to open a module, registers the result in 'package.loaded' table and, |
989 | | ** if 'glb' is true, also registers the result in the global table. |
990 | | ** Leaves resulting module on the top. |
991 | | */ |
992 | | LUALIB_API void luaL_requiref (lua_State *L, const char *modname, |
993 | 0 | lua_CFunction openf, int glb) { |
994 | 0 | luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); |
995 | 0 | lua_getfield(L, -1, modname); /* LOADED[modname] */ |
996 | 0 | if (!lua_toboolean(L, -1)) { /* package not already loaded? */ |
997 | 0 | lua_pop(L, 1); /* remove field */ |
998 | 0 | lua_pushcfunction(L, openf); |
999 | 0 | lua_pushstring(L, modname); /* argument to open function */ |
1000 | 0 | lua_call(L, 1, 1); /* call 'openf' to open module */ |
1001 | 0 | lua_pushvalue(L, -1); /* make copy of module (call result) */ |
1002 | 0 | lua_setfield(L, -3, modname); /* LOADED[modname] = module */ |
1003 | 0 | } |
1004 | 0 | lua_remove(L, -2); /* remove LOADED table */ |
1005 | 0 | if (glb) { |
1006 | 0 | lua_pushvalue(L, -1); /* copy of module */ |
1007 | 0 | lua_setglobal(L, modname); /* _G[modname] = module */ |
1008 | 0 | } |
1009 | 0 | } |
1010 | | |
1011 | | |
1012 | | LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s, |
1013 | 0 | const char *p, const char *r) { |
1014 | 0 | const char *wild; |
1015 | 0 | size_t l = strlen(p); |
1016 | 0 | while ((wild = strstr(s, p)) != NULL) { |
1017 | 0 | luaL_addlstring(b, s, wild - s); /* push prefix */ |
1018 | 0 | luaL_addstring(b, r); /* push replacement in place of pattern */ |
1019 | 0 | s = wild + l; /* continue after 'p' */ |
1020 | 0 | } |
1021 | 0 | luaL_addstring(b, s); /* push last suffix */ |
1022 | 0 | } |
1023 | | |
1024 | | |
1025 | | LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, |
1026 | 0 | const char *p, const char *r) { |
1027 | 0 | luaL_Buffer b; |
1028 | 0 | luaL_buffinit(L, &b); |
1029 | 0 | luaL_addgsub(&b, s, p, r); |
1030 | 0 | luaL_pushresult(&b); |
1031 | 0 | return lua_tostring(L, -1); |
1032 | 0 | } |
1033 | | |
1034 | | |
1035 | 5.74M | static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { |
1036 | 5.74M | (void)ud; (void)osize; /* not used */ |
1037 | 5.74M | if (nsize == 0) { |
1038 | 2.86M | free(ptr); |
1039 | 2.86M | return NULL; |
1040 | 2.86M | } |
1041 | 2.88M | else |
1042 | 2.88M | return realloc(ptr, nsize); |
1043 | 5.74M | } |
1044 | | |
1045 | | |
1046 | | /* |
1047 | | ** Standard panic funcion just prints an error message. The test |
1048 | | ** with 'lua_type' avoids possible memory errors in 'lua_tostring'. |
1049 | | */ |
1050 | 0 | static int panic (lua_State *L) { |
1051 | 0 | const char *msg = (lua_type(L, -1) == LUA_TSTRING) |
1052 | 0 | ? lua_tostring(L, -1) |
1053 | 0 | : "error object is not a string"; |
1054 | 0 | lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n", |
1055 | 0 | msg); |
1056 | 0 | return 0; /* return to Lua to abort */ |
1057 | 0 | } |
1058 | | |
1059 | | |
1060 | | /* |
1061 | | ** Warning functions: |
1062 | | ** warnfoff: warning system is off |
1063 | | ** warnfon: ready to start a new message |
1064 | | ** warnfcont: previous message is to be continued |
1065 | | */ |
1066 | | static void warnfoff (void *ud, const char *message, int tocont); |
1067 | | static void warnfon (void *ud, const char *message, int tocont); |
1068 | | static void warnfcont (void *ud, const char *message, int tocont); |
1069 | | |
1070 | | |
1071 | | /* |
1072 | | ** Check whether message is a control message. If so, execute the |
1073 | | ** control or ignore it if unknown. |
1074 | | */ |
1075 | 0 | static int checkcontrol (lua_State *L, const char *message, int tocont) { |
1076 | 0 | if (tocont || *(message++) != '@') /* not a control message? */ |
1077 | 0 | return 0; |
1078 | 0 | else { |
1079 | 0 | if (strcmp(message, "off") == 0) |
1080 | 0 | lua_setwarnf(L, warnfoff, L); /* turn warnings off */ |
1081 | 0 | else if (strcmp(message, "on") == 0) |
1082 | 0 | lua_setwarnf(L, warnfon, L); /* turn warnings on */ |
1083 | 0 | return 1; /* it was a control message */ |
1084 | 0 | } |
1085 | 0 | } |
1086 | | |
1087 | | |
1088 | 0 | static void warnfoff (void *ud, const char *message, int tocont) { |
1089 | 0 | checkcontrol((lua_State *)ud, message, tocont); |
1090 | 0 | } |
1091 | | |
1092 | | |
1093 | | /* |
1094 | | ** Writes the message and handle 'tocont', finishing the message |
1095 | | ** if needed and setting the next warn function. |
1096 | | */ |
1097 | 0 | static void warnfcont (void *ud, const char *message, int tocont) { |
1098 | 0 | lua_State *L = (lua_State *)ud; |
1099 | 0 | lua_writestringerror("%s", message); /* write message */ |
1100 | 0 | if (tocont) /* not the last part? */ |
1101 | 0 | lua_setwarnf(L, warnfcont, L); /* to be continued */ |
1102 | 0 | else { /* last part */ |
1103 | 0 | lua_writestringerror("%s", "\n"); /* finish message with end-of-line */ |
1104 | 0 | lua_setwarnf(L, warnfon, L); /* next call is a new message */ |
1105 | 0 | } |
1106 | 0 | } |
1107 | | |
1108 | | |
1109 | 0 | static void warnfon (void *ud, const char *message, int tocont) { |
1110 | 0 | if (checkcontrol((lua_State *)ud, message, tocont)) /* control message? */ |
1111 | 0 | return; /* nothing else to be done */ |
1112 | 0 | lua_writestringerror("%s", "Lua warning: "); /* start a new warning */ |
1113 | 0 | warnfcont(ud, message, tocont); /* finish processing */ |
1114 | 0 | } |
1115 | | |
1116 | | |
1117 | | |
1118 | | /* |
1119 | | ** A function to compute an unsigned int with some level of |
1120 | | ** randomness. Rely on Address Space Layout Randomization (if present) |
1121 | | ** and the current time. |
1122 | | */ |
1123 | | #if !defined(luai_makeseed) |
1124 | | |
1125 | | #include <time.h> |
1126 | | |
1127 | | |
1128 | | /* Size for the buffer, in bytes */ |
1129 | 2.80k | #define BUFSEEDB (sizeof(void*) + sizeof(time_t)) |
1130 | | |
1131 | | /* Size for the buffer in int's, rounded up */ |
1132 | 2.24k | #define BUFSEED ((BUFSEEDB + sizeof(int) - 1) / sizeof(int)) |
1133 | | |
1134 | | /* |
1135 | | ** Copy the contents of variable 'v' into the buffer pointed by 'b'. |
1136 | | ** (The '&b[0]' disguises 'b' to fix an absurd warning from clang.) |
1137 | | */ |
1138 | 1.12k | #define addbuff(b,v) (memcpy(&b[0], &(v), sizeof(v)), b += sizeof(v)) |
1139 | | |
1140 | | |
1141 | 561 | static unsigned int luai_makeseed (void) { |
1142 | 561 | unsigned int buff[BUFSEED]; |
1143 | 561 | unsigned int res; |
1144 | 561 | unsigned int i; |
1145 | 561 | time_t t = time(NULL); |
1146 | 561 | char *b = (char*)buff; |
1147 | 561 | addbuff(b, b); /* local variable's address */ |
1148 | 561 | addbuff(b, t); /* time */ |
1149 | | /* fill (rare but possible) remain of the buffer with zeros */ |
1150 | 561 | memset(b, 0, sizeof(buff) - BUFSEEDB); |
1151 | 561 | res = buff[0]; |
1152 | 2.24k | for (i = 1; i < BUFSEED; i++) |
1153 | 1.68k | res ^= (res >> 3) + (res << 7) + buff[i]; |
1154 | 561 | return res; |
1155 | 561 | } |
1156 | | |
1157 | | #endif |
1158 | | |
1159 | | |
1160 | 0 | LUALIB_API unsigned int luaL_makeseed (lua_State *L) { |
1161 | 0 | (void)L; /* unused */ |
1162 | 0 | return luai_makeseed(); |
1163 | 0 | } |
1164 | | |
1165 | | |
1166 | 561 | LUALIB_API lua_State *luaL_newstate (void) { |
1167 | 561 | lua_State *L = lua_newstate(l_alloc, NULL, luai_makeseed()); |
1168 | 561 | if (l_likely(L)) { |
1169 | 561 | lua_atpanic(L, &panic); |
1170 | 561 | lua_setwarnf(L, warnfoff, L); /* default is warnings off */ |
1171 | 561 | } |
1172 | 561 | return L; |
1173 | 561 | } |
1174 | | |
1175 | | |
1176 | 0 | LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) { |
1177 | 0 | lua_Number v = lua_version(L); |
1178 | 0 | if (sz != LUAL_NUMSIZES) /* check numeric types */ |
1179 | 0 | luaL_error(L, "core and library have incompatible numeric types"); |
1180 | 0 | else if (v != ver) |
1181 | 0 | luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f", |
1182 | 0 | (LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)v); |
1183 | 0 | } |
1184 | | |