/src/testdir/build/lua-master/source/lbaselib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ** $Id: lbaselib.c $ |
3 | | ** Basic library |
4 | | ** See Copyright Notice in lua.h |
5 | | */ |
6 | | |
7 | | #define lbaselib_c |
8 | | #define LUA_LIB |
9 | | |
10 | | #include "lprefix.h" |
11 | | |
12 | | |
13 | | #include <ctype.h> |
14 | | #include <stdio.h> |
15 | | #include <stdlib.h> |
16 | | #include <string.h> |
17 | | |
18 | | #include "lua.h" |
19 | | |
20 | | #include "lauxlib.h" |
21 | | #include "lualib.h" |
22 | | |
23 | | |
24 | 9.26k | static int luaB_print (lua_State *L) { |
25 | 9.26k | int n = lua_gettop(L); /* number of arguments */ |
26 | 9.26k | int i; |
27 | 18.5k | for (i = 1; i <= n; i++) { /* for each argument */ |
28 | 9.26k | size_t l; |
29 | 9.26k | const char *s = luaL_tolstring(L, i, &l); /* convert it to string */ |
30 | 9.26k | if (i > 1) /* not the first element? */ |
31 | 0 | lua_writestring("\t", 1); /* add a tab before it */ |
32 | 9.26k | lua_writestring(s, l); /* print it */ |
33 | 9.26k | lua_pop(L, 1); /* pop result */ |
34 | 9.26k | } |
35 | 9.26k | lua_writeline(); |
36 | 9.26k | return 0; |
37 | 9.26k | } |
38 | | |
39 | | |
40 | | /* |
41 | | ** Creates a warning with all given arguments. |
42 | | ** Check first for errors; otherwise an error may interrupt |
43 | | ** the composition of a warning, leaving it unfinished. |
44 | | */ |
45 | 77.0k | static int luaB_warn (lua_State *L) { |
46 | 77.0k | int n = lua_gettop(L); /* number of arguments */ |
47 | 77.0k | int i; |
48 | 77.0k | luaL_checkstring(L, 1); /* at least one argument */ |
49 | 77.0k | for (i = 2; i <= n; i++) |
50 | 0 | luaL_checkstring(L, i); /* make sure all arguments are strings */ |
51 | 77.0k | for (i = 1; i < n; i++) /* compose warning */ |
52 | 0 | lua_warning(L, lua_tostring(L, i), 1); |
53 | 77.0k | lua_warning(L, lua_tostring(L, n), 0); /* close warning */ |
54 | 77.0k | return 0; |
55 | 77.0k | } |
56 | | |
57 | | |
58 | 0 | #define SPACECHARS " \f\n\r\t\v" |
59 | | |
60 | 0 | static const char *b_str2int (const char *s, int base, lua_Integer *pn) { |
61 | 0 | lua_Unsigned n = 0; |
62 | 0 | int neg = 0; |
63 | 0 | s += strspn(s, SPACECHARS); /* skip initial spaces */ |
64 | 0 | if (*s == '-') { s++; neg = 1; } /* handle sign */ |
65 | 0 | else if (*s == '+') s++; |
66 | 0 | if (!isalnum((unsigned char)*s)) /* no digit? */ |
67 | 0 | return NULL; |
68 | 0 | do { |
69 | 0 | int digit = (isdigit((unsigned char)*s)) ? *s - '0' |
70 | 0 | : (toupper((unsigned char)*s) - 'A') + 10; |
71 | 0 | if (digit >= base) return NULL; /* invalid numeral */ |
72 | 0 | n = n * base + digit; |
73 | 0 | s++; |
74 | 0 | } while (isalnum((unsigned char)*s)); |
75 | 0 | s += strspn(s, SPACECHARS); /* skip trailing spaces */ |
76 | 0 | *pn = (lua_Integer)((neg) ? (0u - n) : n); |
77 | 0 | return s; |
78 | 0 | } |
79 | | |
80 | | |
81 | 1.77M | static int luaB_tonumber (lua_State *L) { |
82 | 1.77M | if (lua_isnoneornil(L, 2)) { /* standard conversion? */ |
83 | 1.77M | if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */ |
84 | 320k | lua_settop(L, 1); /* yes; return it */ |
85 | 320k | return 1; |
86 | 320k | } |
87 | 1.45M | else { |
88 | 1.45M | size_t l; |
89 | 1.45M | const char *s = lua_tolstring(L, 1, &l); |
90 | 1.45M | if (s != NULL && lua_stringtonumber(L, s) == l + 1) |
91 | 11.5k | return 1; /* successful conversion to number */ |
92 | | /* else not a number */ |
93 | 1.43M | luaL_checkany(L, 1); /* (but there must be some parameter) */ |
94 | 1.43M | } |
95 | 1.77M | } |
96 | 0 | else { |
97 | 0 | size_t l; |
98 | 0 | const char *s; |
99 | 0 | lua_Integer n = 0; /* to avoid warnings */ |
100 | 0 | lua_Integer base = luaL_checkinteger(L, 2); |
101 | 0 | luaL_checktype(L, 1, LUA_TSTRING); /* no numbers as strings */ |
102 | 0 | s = lua_tolstring(L, 1, &l); |
103 | 0 | luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); |
104 | 0 | if (b_str2int(s, (int)base, &n) == s + l) { |
105 | 0 | lua_pushinteger(L, n); |
106 | 0 | return 1; |
107 | 0 | } /* else not a number */ |
108 | 0 | } /* else not a number */ |
109 | 1.43M | luaL_pushfail(L); /* not a number */ |
110 | 1.43M | return 1; |
111 | 1.77M | } |
112 | | |
113 | | |
114 | 0 | static int luaB_error (lua_State *L) { |
115 | 0 | int level = (int)luaL_optinteger(L, 2, 1); |
116 | 0 | lua_settop(L, 1); |
117 | 0 | if (lua_type(L, 1) == LUA_TSTRING && level > 0) { |
118 | 0 | luaL_where(L, level); /* add extra information */ |
119 | 0 | lua_pushvalue(L, 1); |
120 | 0 | lua_concat(L, 2); |
121 | 0 | } |
122 | 0 | return lua_error(L); |
123 | 0 | } |
124 | | |
125 | | |
126 | 0 | static int luaB_getmetatable (lua_State *L) { |
127 | 0 | luaL_checkany(L, 1); |
128 | 0 | if (!lua_getmetatable(L, 1)) { |
129 | 0 | lua_pushnil(L); |
130 | 0 | return 1; /* no metatable */ |
131 | 0 | } |
132 | 0 | luaL_getmetafield(L, 1, "__metatable"); |
133 | 0 | return 1; /* returns either __metatable field (if present) or metatable */ |
134 | 0 | } |
135 | | |
136 | | |
137 | 1.95M | static int luaB_setmetatable (lua_State *L) { |
138 | 1.95M | int t = lua_type(L, 2); |
139 | 1.95M | luaL_checktype(L, 1, LUA_TTABLE); |
140 | 1.95M | luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table"); |
141 | 1.95M | if (l_unlikely(luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL)) |
142 | 0 | return luaL_error(L, "cannot change a protected metatable"); |
143 | 1.95M | lua_settop(L, 2); |
144 | 1.95M | lua_setmetatable(L, 1); |
145 | 1.95M | return 1; |
146 | 1.95M | } |
147 | | |
148 | | |
149 | 59.3k | static int luaB_rawequal (lua_State *L) { |
150 | 59.3k | luaL_checkany(L, 1); |
151 | 59.3k | luaL_checkany(L, 2); |
152 | 59.3k | lua_pushboolean(L, lua_rawequal(L, 1, 2)); |
153 | 59.3k | return 1; |
154 | 59.3k | } |
155 | | |
156 | | |
157 | 2.09k | static int luaB_rawlen (lua_State *L) { |
158 | 2.09k | int t = lua_type(L, 1); |
159 | 2.09k | luaL_argexpected(L, t == LUA_TTABLE || t == LUA_TSTRING, 1, |
160 | 2.09k | "table or string"); |
161 | 2.09k | lua_pushinteger(L, lua_rawlen(L, 1)); |
162 | 2.09k | return 1; |
163 | 2.09k | } |
164 | | |
165 | | |
166 | 929 | static int luaB_rawget (lua_State *L) { |
167 | 929 | luaL_checktype(L, 1, LUA_TTABLE); |
168 | 929 | luaL_checkany(L, 2); |
169 | 929 | lua_settop(L, 2); |
170 | 929 | lua_rawget(L, 1); |
171 | 929 | return 1; |
172 | 929 | } |
173 | | |
174 | 47.3k | static int luaB_rawset (lua_State *L) { |
175 | 47.3k | luaL_checktype(L, 1, LUA_TTABLE); |
176 | 47.3k | luaL_checkany(L, 2); |
177 | 47.3k | luaL_checkany(L, 3); |
178 | 47.3k | lua_settop(L, 3); |
179 | 47.3k | lua_rawset(L, 1); |
180 | 47.3k | return 1; |
181 | 47.3k | } |
182 | | |
183 | | |
184 | 85.3k | static int pushmode (lua_State *L, int oldmode) { |
185 | 85.3k | if (oldmode == -1) |
186 | 0 | luaL_pushfail(L); /* invalid call to 'lua_gc' */ |
187 | 85.3k | else |
188 | 85.3k | lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" |
189 | 85.3k | : "generational"); |
190 | 85.3k | return 1; |
191 | 85.3k | } |
192 | | |
193 | | |
194 | | /* |
195 | | ** check whether call to 'lua_gc' was valid (not inside a finalizer) |
196 | | */ |
197 | 53.3k | #define checkvalres(res) { if (res == -1) break; } |
198 | | |
199 | 139k | static int luaB_collectgarbage (lua_State *L) { |
200 | 139k | static const char *const opts[] = {"stop", "restart", "collect", |
201 | 139k | "count", "step", "isrunning", "generational", "incremental", |
202 | 139k | "param", NULL}; |
203 | 139k | static const char optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, |
204 | 139k | LUA_GCCOUNT, LUA_GCSTEP, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC, |
205 | 139k | LUA_GCPARAM}; |
206 | 139k | int o = optsnum[luaL_checkoption(L, 1, "collect", opts)]; |
207 | 139k | switch (o) { |
208 | 0 | case LUA_GCCOUNT: { |
209 | 0 | int k = lua_gc(L, o); |
210 | 0 | int b = lua_gc(L, LUA_GCCOUNTB); |
211 | 0 | checkvalres(k); |
212 | 0 | lua_pushnumber(L, (lua_Number)k + ((lua_Number)b/1024)); |
213 | 0 | return 1; |
214 | 0 | } |
215 | 15.7k | case LUA_GCSTEP: { |
216 | 15.7k | lua_Integer n = luaL_optinteger(L, 2, 0); |
217 | 15.7k | int res = lua_gc(L, o, (int)n); |
218 | 15.7k | checkvalres(res); |
219 | 15.7k | lua_pushboolean(L, res); |
220 | 15.7k | return 1; |
221 | 15.7k | } |
222 | 24 | case LUA_GCISRUNNING: { |
223 | 24 | int res = lua_gc(L, o); |
224 | 24 | checkvalres(res); |
225 | 24 | lua_pushboolean(L, res); |
226 | 24 | return 1; |
227 | 24 | } |
228 | 84.6k | case LUA_GCGEN: { |
229 | 84.6k | return pushmode(L, lua_gc(L, o)); |
230 | 24 | } |
231 | 666 | case LUA_GCINC: { |
232 | 666 | return pushmode(L, lua_gc(L, o)); |
233 | 24 | } |
234 | 0 | case LUA_GCPARAM: { |
235 | 0 | static const char *const params[] = { |
236 | 0 | "minormul", "majorminor", "minormajor", |
237 | 0 | "pause", "stepmul", "stepsize", NULL}; |
238 | 0 | static const char pnum[] = { |
239 | 0 | LUA_GCPMINORMUL, LUA_GCPMAJORMINOR, LUA_GCPMINORMAJOR, |
240 | 0 | LUA_GCPPAUSE, LUA_GCPSTEPMUL, LUA_GCPSTEPSIZE}; |
241 | 0 | int p = pnum[luaL_checkoption(L, 2, NULL, params)]; |
242 | 0 | lua_Integer value = luaL_optinteger(L, 3, -1); |
243 | 0 | lua_pushinteger(L, lua_gc(L, o, p, (int)value)); |
244 | 0 | return 1; |
245 | 24 | } |
246 | 37.6k | default: { |
247 | 37.6k | int res = lua_gc(L, o); |
248 | 37.6k | checkvalres(res); |
249 | 37.6k | lua_pushinteger(L, res); |
250 | 37.6k | return 1; |
251 | 37.6k | } |
252 | 139k | } |
253 | 0 | luaL_pushfail(L); /* invalid call (inside a finalizer) */ |
254 | 0 | return 1; |
255 | 139k | } |
256 | | |
257 | | |
258 | 1.40M | static int luaB_type (lua_State *L) { |
259 | 1.40M | int t = lua_type(L, 1); |
260 | 1.40M | luaL_argcheck(L, t != LUA_TNONE, 1, "value expected"); |
261 | 1.40M | lua_pushstring(L, lua_typename(L, t)); |
262 | 1.40M | return 1; |
263 | 1.40M | } |
264 | | |
265 | | |
266 | 74.5k | static int luaB_next (lua_State *L) { |
267 | 74.5k | luaL_checktype(L, 1, LUA_TTABLE); |
268 | 74.5k | lua_settop(L, 2); /* create a 2nd argument if there isn't one */ |
269 | 74.5k | if (lua_next(L, 1)) |
270 | 2.19k | return 2; |
271 | 72.3k | else { |
272 | 72.3k | lua_pushnil(L); |
273 | 72.3k | return 1; |
274 | 72.3k | } |
275 | 74.5k | } |
276 | | |
277 | | |
278 | 0 | static int pairscont (lua_State *L, int status, lua_KContext k) { |
279 | 0 | (void)L; (void)status; (void)k; /* unused */ |
280 | 0 | return 3; |
281 | 0 | } |
282 | | |
283 | 0 | static int luaB_pairs (lua_State *L) { |
284 | 0 | luaL_checkany(L, 1); |
285 | 0 | if (luaL_getmetafield(L, 1, "__pairs") == LUA_TNIL) { /* no metamethod? */ |
286 | 0 | lua_pushcfunction(L, luaB_next); /* will return generator, */ |
287 | 0 | lua_pushvalue(L, 1); /* state, */ |
288 | 0 | lua_pushnil(L); /* and initial value */ |
289 | 0 | } |
290 | 0 | else { |
291 | 0 | lua_pushvalue(L, 1); /* argument 'self' to metamethod */ |
292 | 0 | lua_callk(L, 1, 3, 0, pairscont); /* get 3 values from metamethod */ |
293 | 0 | } |
294 | 0 | return 3; |
295 | 0 | } |
296 | | |
297 | | |
298 | | /* |
299 | | ** Traversal function for 'ipairs' |
300 | | */ |
301 | 0 | static int ipairsaux (lua_State *L) { |
302 | 0 | lua_Integer i = luaL_checkinteger(L, 2); |
303 | 0 | i = luaL_intop(+, i, 1); |
304 | 0 | lua_pushinteger(L, i); |
305 | 0 | return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2; |
306 | 0 | } |
307 | | |
308 | | |
309 | | /* |
310 | | ** 'ipairs' function. Returns 'ipairsaux', given "table", 0. |
311 | | ** (The given "table" may not be a table.) |
312 | | */ |
313 | 0 | static int luaB_ipairs (lua_State *L) { |
314 | 0 | luaL_checkany(L, 1); |
315 | 0 | lua_pushcfunction(L, ipairsaux); /* iteration function */ |
316 | 0 | lua_pushvalue(L, 1); /* state */ |
317 | 0 | lua_pushinteger(L, 0); /* initial value */ |
318 | 0 | return 3; |
319 | 0 | } |
320 | | |
321 | | |
322 | 3.39M | static int load_aux (lua_State *L, int status, int envidx) { |
323 | 3.39M | if (l_likely(status == LUA_OK)) { |
324 | 918k | if (envidx != 0) { /* 'env' parameter? */ |
325 | 1.29k | lua_pushvalue(L, envidx); /* environment for loaded function */ |
326 | 1.29k | if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */ |
327 | 0 | lua_pop(L, 1); /* remove 'env' if not used by previous call */ |
328 | 1.29k | } |
329 | 918k | return 1; |
330 | 918k | } |
331 | 2.48M | else { /* error (message is on top of the stack) */ |
332 | 2.48M | luaL_pushfail(L); |
333 | 2.48M | lua_insert(L, -2); /* put before error message */ |
334 | 2.48M | return 2; /* return fail plus error message */ |
335 | 2.48M | } |
336 | 3.39M | } |
337 | | |
338 | | |
339 | 3.62M | static const char *getMode (lua_State *L, int idx) { |
340 | 3.62M | const char *mode = luaL_optstring(L, idx, "bt"); |
341 | 3.62M | if (strchr(mode, 'B') != NULL) /* Lua code cannot use fixed buffers */ |
342 | 0 | luaL_argerror(L, idx, "invalid mode"); |
343 | 3.62M | return mode; |
344 | 3.62M | } |
345 | | |
346 | | |
347 | 558 | static int luaB_loadfile (lua_State *L) { |
348 | 558 | const char *fname = luaL_optstring(L, 1, NULL); |
349 | 558 | const char *mode = getMode(L, 2); |
350 | 558 | int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */ |
351 | 558 | int status = luaL_loadfilex(L, fname, mode); |
352 | 558 | return load_aux(L, status, env); |
353 | 558 | } |
354 | | |
355 | | |
356 | | /* |
357 | | ** {====================================================== |
358 | | ** Generic Read function |
359 | | ** ======================================================= |
360 | | */ |
361 | | |
362 | | |
363 | | /* |
364 | | ** reserved slot, above all arguments, to hold a copy of the returned |
365 | | ** string to avoid it being collected while parsed. 'load' has four |
366 | | ** optional arguments (chunk, source name, mode, and environment). |
367 | | */ |
368 | 3.00M | #define RESERVEDSLOT 5 |
369 | | |
370 | | |
371 | | /* |
372 | | ** Reader for generic 'load' function: 'lua_load' uses the |
373 | | ** stack for internal stuff, so the reader cannot change the |
374 | | ** stack top. Instead, it keeps its resulting string in a |
375 | | ** reserved slot inside the stack. |
376 | | */ |
377 | 1.82M | static const char *generic_reader (lua_State *L, void *ud, size_t *size) { |
378 | 1.82M | (void)(ud); /* not used */ |
379 | 1.82M | luaL_checkstack(L, 2, "too many nested functions"); |
380 | 1.82M | lua_pushvalue(L, 1); /* get function */ |
381 | 1.82M | lua_call(L, 0, 1); /* call it */ |
382 | 1.82M | if (lua_isnil(L, -1)) { |
383 | 869k | lua_pop(L, 1); /* pop result */ |
384 | 869k | *size = 0; |
385 | 869k | return NULL; |
386 | 869k | } |
387 | 952k | else if (l_unlikely(!lua_isstring(L, -1))) |
388 | 0 | luaL_error(L, "reader function must return a string"); |
389 | 952k | lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */ |
390 | 952k | return lua_tolstring(L, RESERVEDSLOT, size); |
391 | 1.82M | } |
392 | | |
393 | | |
394 | 3.62M | static int luaB_load (lua_State *L) { |
395 | 3.62M | int status; |
396 | 3.62M | size_t l; |
397 | 3.62M | const char *s = lua_tolstring(L, 1, &l); |
398 | 3.62M | const char *mode = getMode(L, 3); |
399 | 3.62M | int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */ |
400 | 3.62M | if (s != NULL) { /* loading a string? */ |
401 | 1.57M | const char *chunkname = luaL_optstring(L, 2, s); |
402 | 1.57M | status = luaL_loadbufferx(L, s, l, chunkname, mode); |
403 | 1.57M | } |
404 | 2.04M | else { /* loading from a reader function */ |
405 | 2.04M | const char *chunkname = luaL_optstring(L, 2, "=(load)"); |
406 | 2.04M | luaL_checktype(L, 1, LUA_TFUNCTION); |
407 | 2.04M | lua_settop(L, RESERVEDSLOT); /* create reserved slot */ |
408 | 2.04M | status = lua_load(L, generic_reader, NULL, chunkname, mode); |
409 | 2.04M | } |
410 | 3.62M | return load_aux(L, status, env); |
411 | 3.62M | } |
412 | | |
413 | | /* }====================================================== */ |
414 | | |
415 | | |
416 | 441 | static int dofilecont (lua_State *L, int d1, lua_KContext d2) { |
417 | 441 | (void)d1; (void)d2; /* only to match 'lua_Kfunction' prototype */ |
418 | 441 | return lua_gettop(L) - 1; |
419 | 441 | } |
420 | | |
421 | | |
422 | 449 | static int luaB_dofile (lua_State *L) { |
423 | 449 | const char *fname = luaL_optstring(L, 1, NULL); |
424 | 449 | lua_settop(L, 1); |
425 | 449 | if (l_unlikely(luaL_loadfile(L, fname) != LUA_OK)) |
426 | 0 | return lua_error(L); |
427 | 449 | lua_callk(L, 0, LUA_MULTRET, 0, dofilecont); |
428 | 449 | return dofilecont(L, 0, 0); |
429 | 449 | } |
430 | | |
431 | | |
432 | 36.3k | static int luaB_assert (lua_State *L) { |
433 | 36.3k | if (l_likely(lua_toboolean(L, 1))) /* condition is true? */ |
434 | 36.3k | return lua_gettop(L); /* return all arguments */ |
435 | 0 | else { /* error */ |
436 | 0 | luaL_checkany(L, 1); /* there must be a condition */ |
437 | 0 | lua_remove(L, 1); /* remove it */ |
438 | 0 | lua_pushliteral(L, "assertion failed!"); /* default message */ |
439 | 0 | lua_settop(L, 1); /* leave only message (default if no other one) */ |
440 | 0 | return luaB_error(L); /* call 'error' */ |
441 | 0 | } |
442 | 36.3k | } |
443 | | |
444 | | |
445 | 70.2k | static int luaB_select (lua_State *L) { |
446 | 70.2k | int n = lua_gettop(L); |
447 | 70.2k | if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') { |
448 | 55.1k | lua_pushinteger(L, n-1); |
449 | 55.1k | return 1; |
450 | 55.1k | } |
451 | 15.1k | else { |
452 | 15.1k | lua_Integer i = luaL_checkinteger(L, 1); |
453 | 15.1k | if (i < 0) i = n + i; |
454 | 15.0k | else if (i > n) i = n; |
455 | 15.1k | luaL_argcheck(L, 1 <= i, 1, "index out of range"); |
456 | 15.1k | return n - (int)i; |
457 | 15.1k | } |
458 | 70.2k | } |
459 | | |
460 | | |
461 | | /* |
462 | | ** Continuation function for 'pcall' and 'xpcall'. Both functions |
463 | | ** already pushed a 'true' before doing the call, so in case of success |
464 | | ** 'finishpcall' only has to return everything in the stack minus |
465 | | ** 'extra' values (where 'extra' is exactly the number of items to be |
466 | | ** ignored). |
467 | | */ |
468 | 145 | static int finishpcall (lua_State *L, int status, lua_KContext extra) { |
469 | 145 | if (l_unlikely(status != LUA_OK && status != LUA_YIELD)) { /* error? */ |
470 | 145 | lua_pushboolean(L, 0); /* first result (false) */ |
471 | 145 | lua_pushvalue(L, -2); /* error message */ |
472 | 145 | return 2; /* return false, msg */ |
473 | 145 | } |
474 | 0 | else |
475 | 0 | return lua_gettop(L) - (int)extra; /* return all results */ |
476 | 145 | } |
477 | | |
478 | | |
479 | 145 | static int luaB_pcall (lua_State *L) { |
480 | 145 | int status; |
481 | 145 | luaL_checkany(L, 1); |
482 | 145 | lua_pushboolean(L, 1); /* first result if no errors */ |
483 | 145 | lua_insert(L, 1); /* put it in place */ |
484 | 145 | status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, finishpcall); |
485 | 145 | return finishpcall(L, status, 0); |
486 | 145 | } |
487 | | |
488 | | |
489 | | /* |
490 | | ** Do a protected call with error handling. After 'lua_rotate', the |
491 | | ** stack will have <f, err, true, f, [args...]>; so, the function passes |
492 | | ** 2 to 'finishpcall' to skip the 2 first values when returning results. |
493 | | */ |
494 | 0 | static int luaB_xpcall (lua_State *L) { |
495 | 0 | int status; |
496 | 0 | int n = lua_gettop(L); |
497 | 0 | luaL_checktype(L, 2, LUA_TFUNCTION); /* check error function */ |
498 | 0 | lua_pushboolean(L, 1); /* first result */ |
499 | 0 | lua_pushvalue(L, 1); /* function */ |
500 | 0 | lua_rotate(L, 3, 2); /* move them below function's arguments */ |
501 | 0 | status = lua_pcallk(L, n - 2, LUA_MULTRET, 2, 2, finishpcall); |
502 | 0 | return finishpcall(L, status, 2); |
503 | 0 | } |
504 | | |
505 | | |
506 | 218k | static int luaB_tostring (lua_State *L) { |
507 | 218k | luaL_checkany(L, 1); |
508 | 218k | luaL_tolstring(L, 1, NULL); |
509 | 218k | return 1; |
510 | 218k | } |
511 | | |
512 | | |
513 | | static const luaL_Reg base_funcs[] = { |
514 | | {"assert", luaB_assert}, |
515 | | {"collectgarbage", luaB_collectgarbage}, |
516 | | {"dofile", luaB_dofile}, |
517 | | {"error", luaB_error}, |
518 | | {"getmetatable", luaB_getmetatable}, |
519 | | {"ipairs", luaB_ipairs}, |
520 | | {"loadfile", luaB_loadfile}, |
521 | | {"load", luaB_load}, |
522 | | {"next", luaB_next}, |
523 | | {"pairs", luaB_pairs}, |
524 | | {"pcall", luaB_pcall}, |
525 | | {"print", luaB_print}, |
526 | | {"warn", luaB_warn}, |
527 | | {"rawequal", luaB_rawequal}, |
528 | | {"rawlen", luaB_rawlen}, |
529 | | {"rawget", luaB_rawget}, |
530 | | {"rawset", luaB_rawset}, |
531 | | {"select", luaB_select}, |
532 | | {"setmetatable", luaB_setmetatable}, |
533 | | {"tonumber", luaB_tonumber}, |
534 | | {"tostring", luaB_tostring}, |
535 | | {"type", luaB_type}, |
536 | | {"xpcall", luaB_xpcall}, |
537 | | /* placeholders */ |
538 | | {LUA_GNAME, NULL}, |
539 | | {"_VERSION", NULL}, |
540 | | {NULL, NULL} |
541 | | }; |
542 | | |
543 | | |
544 | 4.53k | LUAMOD_API int luaopen_base (lua_State *L) { |
545 | | /* open lib into global table */ |
546 | 4.53k | lua_pushglobaltable(L); |
547 | 4.53k | luaL_setfuncs(L, base_funcs, 0); |
548 | | /* set global _G */ |
549 | 4.53k | lua_pushvalue(L, -1); |
550 | 4.53k | lua_setfield(L, -2, LUA_GNAME); |
551 | | /* set global _VERSION */ |
552 | 4.53k | lua_pushliteral(L, LUA_VERSION); |
553 | 4.53k | lua_setfield(L, -2, "_VERSION"); |
554 | 4.53k | return 1; |
555 | 4.53k | } |
556 | | |