interplane.Get Omega
Read in your language

lua

[Ported from github.com/lua/lua] Cast in 'rl_readline_name'

Brought by drakestapleton

branch master Branches Tags History

lua / lbaselib.c

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