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