1 /*
2 ** $Id: lbaselib.c,v 1.191a 2006/06/02 15:34:00 roberto Exp $
3 ** Basic library
4 ** See Copyright Notice in lua.h
5 */
6
7 #include <common/Logger.h>
8
9 #include <ctype.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #define lbaselib_c
15 #define LUA_LIB
16
17 #include "lua.h"
18
19 #include "lauxlib.h"
20 #include "lualib.h"
21
22
23
24
25 /*
26 ** If your system does not support `stdout', you can just remove this function.
27 ** If you need, you can define your own `print' function, following this
28 ** model but changing `fputs' to put the strings at a proper place
29 ** (a console window or a log file, for instance).
30 */
luaB_print(lua_State * L)31 static int luaB_print (lua_State *L) {
32
33 std::string result;
34
35 int n = lua_gettop(L); /* number of arguments */
36 int i;
37 lua_getglobal(L, "tostring");
38 for (i=1; i<=n; i++) {
39 const char *s;
40 lua_pushvalue(L, -1); /* function to be called */
41 lua_pushvalue(L, i); /* value to print */
42 lua_call(L, 1, 1);
43 s = lua_tostring(L, -1); /* get result */
44 if (s == NULL)
45 return luaL_error(L, LUA_QL("tostring") " must return a string to "
46 LUA_QL("print"));
47 if (i>1) result.append("\t");
48 result.append(s);
49 lua_pop(L, 1); /* pop result */
50 }
51 result.append("\n");
52 Logger::log(result.c_str());
53 return 0;
54 }
55
56
luaB_tonumber(lua_State * L)57 static int luaB_tonumber (lua_State *L) {
58 int base = luaL_optint(L, 2, 10);
59 if (base == 10) { /* standard conversion */
60 luaL_checkany(L, 1);
61 if (lua_isnumber(L, 1)) {
62 lua_pushnumber(L, lua_tonumber(L, 1));
63 return 1;
64 }
65 }
66 else {
67 const char *s1 = luaL_checkstring(L, 1);
68 char *s2;
69 unsigned long n;
70 luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
71 n = strtoul(s1, &s2, base);
72 if (s1 != s2) { /* at least one valid digit? */
73 while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
74 if (*s2 == '\0') { /* no invalid trailing characters? */
75 lua_pushnumber(L, (lua_Number)n);
76 return 1;
77 }
78 }
79 }
80 lua_pushnil(L); /* else not a number */
81 return 1;
82 }
83
84
luaB_error(lua_State * L)85 static int luaB_error (lua_State *L) {
86 int level = luaL_optint(L, 2, 1);
87 lua_settop(L, 1);
88 if (lua_isstring(L, 1) && level > 0) { /* add extra information? */
89 luaL_where(L, level);
90 lua_pushvalue(L, 1);
91 lua_concat(L, 2);
92 }
93 return lua_error(L);
94 }
95
96
luaB_getmetatable(lua_State * L)97 static int luaB_getmetatable (lua_State *L) {
98 luaL_checkany(L, 1);
99 if (!lua_getmetatable(L, 1)) {
100 lua_pushnil(L);
101 return 1; /* no metatable */
102 }
103 luaL_getmetafield(L, 1, "__metatable");
104 return 1; /* returns either __metatable field (if present) or metatable */
105 }
106
107
luaB_setmetatable(lua_State * L)108 static int luaB_setmetatable (lua_State *L) {
109 int t = lua_type(L, 2);
110 luaL_checktype(L, 1, LUA_TTABLE);
111 luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
112 "nil or table expected");
113 if (luaL_getmetafield(L, 1, "__metatable"))
114 luaL_error(L, "cannot change a protected metatable");
115 lua_settop(L, 2);
116 lua_setmetatable(L, 1);
117 return 1;
118 }
119
120
getfunc(lua_State * L,int opt)121 static void getfunc (lua_State *L, int opt) {
122 if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);
123 else {
124 lua_Debug ar;
125 int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1);
126 luaL_argcheck(L, level >= 0, 1, "level must be non-negative");
127 if (lua_getstack(L, level, &ar) == 0)
128 luaL_argerror(L, 1, "invalid level");
129 lua_getinfo(L, "f", &ar);
130 if (lua_isnil(L, -1))
131 luaL_error(L, "no function environment for tail call at level %d",
132 level);
133 }
134 }
135
136
luaB_getfenv(lua_State * L)137 static int luaB_getfenv (lua_State *L) {
138 getfunc(L, 1);
139 if (lua_iscfunction(L, -1)) /* is a C function? */
140 lua_pushvalue(L, LUA_GLOBALSINDEX); /* return the thread's global env. */
141 else
142 lua_getfenv(L, -1);
143 return 1;
144 }
145
146
luaB_setfenv(lua_State * L)147 static int luaB_setfenv (lua_State *L) {
148 luaL_checktype(L, 2, LUA_TTABLE);
149 getfunc(L, 0);
150 lua_pushvalue(L, 2);
151 if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) {
152 /* change environment of current thread */
153 lua_pushthread(L);
154 lua_insert(L, -2);
155 lua_setfenv(L, -2);
156 return 0;
157 }
158 else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0)
159 luaL_error(L,
160 LUA_QL("setfenv") " cannot change environment of given object");
161 return 1;
162 }
163
164
luaB_rawequal(lua_State * L)165 static int luaB_rawequal (lua_State *L) {
166 luaL_checkany(L, 1);
167 luaL_checkany(L, 2);
168 lua_pushboolean(L, lua_rawequal(L, 1, 2));
169 return 1;
170 }
171
172
luaB_rawget(lua_State * L)173 static int luaB_rawget (lua_State *L) {
174 luaL_checktype(L, 1, LUA_TTABLE);
175 luaL_checkany(L, 2);
176 lua_settop(L, 2);
177 lua_rawget(L, 1);
178 return 1;
179 }
180
luaB_rawset(lua_State * L)181 static int luaB_rawset (lua_State *L) {
182 luaL_checktype(L, 1, LUA_TTABLE);
183 luaL_checkany(L, 2);
184 luaL_checkany(L, 3);
185 lua_settop(L, 3);
186 lua_rawset(L, 1);
187 return 1;
188 }
189
190
luaB_gcinfo(lua_State * L)191 static int luaB_gcinfo (lua_State *L) {
192 lua_pushinteger(L, lua_getgccount(L));
193 return 1;
194 }
195
196
luaB_collectgarbage(lua_State * L)197 static int luaB_collectgarbage (lua_State *L) {
198 static const char *const opts[] = {"stop", "restart", "collect",
199 "count", "step", "setpause", "setstepmul", NULL};
200 static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
201 LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL};
202 int o = luaL_checkoption(L, 1, "collect", opts);
203 int ex = luaL_optint(L, 2, 0);
204 int res = lua_gc(L, optsnum[o], ex);
205 switch (optsnum[o]) {
206 case LUA_GCCOUNT: {
207 int b = lua_gc(L, LUA_GCCOUNTB, 0);
208 lua_pushnumber(L, res + ((lua_Number)b/1024));
209 return 1;
210 }
211 case LUA_GCSTEP: {
212 lua_pushboolean(L, res);
213 return 1;
214 }
215 default: {
216 lua_pushnumber(L, res);
217 return 1;
218 }
219 }
220 }
221
222
luaB_type(lua_State * L)223 static int luaB_type (lua_State *L) {
224 luaL_checkany(L, 1);
225 lua_pushstring(L, luaL_typename(L, 1));
226 return 1;
227 }
228
229
luaB_next(lua_State * L)230 static int luaB_next (lua_State *L) {
231 luaL_checktype(L, 1, LUA_TTABLE);
232 lua_settop(L, 2); /* create a 2nd argument if there isn't one */
233 if (lua_next(L, 1))
234 return 2;
235 else {
236 lua_pushnil(L);
237 return 1;
238 }
239 }
240
241
luaB_pairs(lua_State * L)242 static int luaB_pairs (lua_State *L) {
243 luaL_checktype(L, 1, LUA_TTABLE);
244 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
245 lua_pushvalue(L, 1); /* state, */
246 lua_pushnil(L); /* and initial value */
247 return 3;
248 }
249
250
ipairsaux(lua_State * L)251 static int ipairsaux (lua_State *L) {
252 int i = luaL_checkint(L, 2);
253 luaL_checktype(L, 1, LUA_TTABLE);
254 i++; /* next value */
255 lua_pushinteger(L, i);
256 lua_rawgeti(L, 1, i);
257 return (lua_isnil(L, -1)) ? 0 : 2;
258 }
259
260
luaB_ipairs(lua_State * L)261 static int luaB_ipairs (lua_State *L) {
262 luaL_checktype(L, 1, LUA_TTABLE);
263 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
264 lua_pushvalue(L, 1); /* state, */
265 lua_pushinteger(L, 0); /* and initial value */
266 return 3;
267 }
268
269
load_aux(lua_State * L,int status)270 static int load_aux (lua_State *L, int status) {
271 if (status == 0) /* OK? */
272 return 1;
273 else {
274 lua_pushnil(L);
275 lua_insert(L, -2); /* put before error message */
276 return 2; /* return nil plus error message */
277 }
278 }
279
280
luaB_loadstring(lua_State * L)281 static int luaB_loadstring (lua_State *L) {
282 size_t l;
283 const char *s = luaL_checklstring(L, 1, &l);
284 const char *chunkname = luaL_optstring(L, 2, s);
285 return load_aux(L, luaL_loadbuffer(L, s, l, chunkname));
286 }
287
288
luaB_loadfile(lua_State * L)289 static int luaB_loadfile (lua_State *L) {
290 const char *fname = luaL_optstring(L, 1, NULL);
291 return load_aux(L, luaL_loadfile(L, fname));
292 }
293
294
295 /*
296 ** Reader for generic `load' function: `lua_load' uses the
297 ** stack for internal stuff, so the reader cannot change the
298 ** stack top. Instead, it keeps its resulting string in a
299 ** reserved slot inside the stack.
300 */
generic_reader(lua_State * L,void * ud,size_t * size)301 static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
302 (void)ud; /* to avoid warnings */
303 luaL_checkstack(L, 2, "too many nested functions");
304 lua_pushvalue(L, 1); /* get function */
305 lua_call(L, 0, 1); /* call it */
306 if (lua_isnil(L, -1)) {
307 *size = 0;
308 return NULL;
309 }
310 else if (lua_isstring(L, -1)) {
311 lua_replace(L, 3); /* save string in a reserved stack slot */
312 return lua_tolstring(L, 3, size);
313 }
314 else luaL_error(L, "reader function must return a string");
315 return NULL; /* to avoid warnings */
316 }
317
318
luaB_load(lua_State * L)319 static int luaB_load (lua_State *L) {
320 int status;
321 const char *cname = luaL_optstring(L, 2, "=(load)");
322 luaL_checktype(L, 1, LUA_TFUNCTION);
323 lua_settop(L, 3); /* function, eventual name, plus one reserved slot */
324 status = lua_load(L, generic_reader, NULL, cname);
325 return load_aux(L, status);
326 }
327
328
luaB_dofile(lua_State * L)329 static int luaB_dofile (lua_State *L) {
330 const char *fname = luaL_optstring(L, 1, NULL);
331 int n = lua_gettop(L);
332 if (luaL_loadfile(L, fname) != 0) lua_error(L);
333 lua_call(L, 0, LUA_MULTRET);
334 return lua_gettop(L) - n;
335 }
336
337
luaB_assert(lua_State * L)338 static int luaB_assert (lua_State *L) {
339 luaL_checkany(L, 1);
340 if (!lua_toboolean(L, 1))
341 return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
342 return lua_gettop(L);
343 }
344
345
luaB_unpack(lua_State * L)346 static int luaB_unpack (lua_State *L) {
347 int i, e, n;
348 luaL_checktype(L, 1, LUA_TTABLE);
349 i = luaL_optint(L, 2, 1);
350 e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1));
351 n = e - i + 1; /* number of elements */
352 if (n <= 0) return 0; /* empty range */
353 luaL_checkstack(L, n, "table too big to unpack");
354 for (; i<=e; i++) /* push arg[i...e] */
355 lua_rawgeti(L, 1, i);
356 return n;
357 }
358
359
luaB_select(lua_State * L)360 static int luaB_select (lua_State *L) {
361 int n = lua_gettop(L);
362 if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {
363 lua_pushinteger(L, n-1);
364 return 1;
365 }
366 else {
367 int i = luaL_checkint(L, 1);
368 if (i < 0) i = n + i;
369 else if (i > n) i = n;
370 luaL_argcheck(L, 1 <= i, 1, "index out of range");
371 return n - i;
372 }
373 }
374
375
luaB_pcall(lua_State * L)376 static int luaB_pcall (lua_State *L) {
377 int status;
378 luaL_checkany(L, 1);
379 status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0);
380 lua_pushboolean(L, (status == 0));
381 lua_insert(L, 1);
382 return lua_gettop(L); /* return status + all results */
383 }
384
385
luaB_xpcall(lua_State * L)386 static int luaB_xpcall (lua_State *L) {
387 int status;
388 luaL_checkany(L, 2);
389 lua_settop(L, 2);
390 lua_insert(L, 1); /* put error function under function to be called */
391 status = lua_pcall(L, 0, LUA_MULTRET, 1);
392 lua_pushboolean(L, (status == 0));
393 lua_replace(L, 1);
394 return lua_gettop(L); /* return status + all results */
395 }
396
397
luaB_tostring(lua_State * L)398 static int luaB_tostring (lua_State *L) {
399 luaL_checkany(L, 1);
400 if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
401 return 1; /* use its value */
402 switch (lua_type(L, 1)) {
403 case LUA_TNUMBER:
404 lua_pushstring(L, lua_tostring(L, 1));
405 break;
406 case LUA_TSTRING:
407 lua_pushvalue(L, 1);
408 break;
409 case LUA_TBOOLEAN:
410 lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
411 break;
412 case LUA_TNIL:
413 lua_pushliteral(L, "nil");
414 break;
415 default:
416 lua_pushfstring(L, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1));
417 break;
418 }
419 return 1;
420 }
421
422
luaB_newproxy(lua_State * L)423 static int luaB_newproxy (lua_State *L) {
424 lua_settop(L, 1);
425 lua_newuserdata(L, 0); /* create proxy */
426 if (lua_toboolean(L, 1) == 0)
427 return 1; /* no metatable */
428 else if (lua_isboolean(L, 1)) {
429 lua_newtable(L); /* create a new metatable `m' ... */
430 lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */
431 lua_pushboolean(L, 1);
432 lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */
433 }
434 else {
435 int validproxy = 0; /* to check if weaktable[metatable(u)] == true */
436 if (lua_getmetatable(L, 1)) {
437 lua_rawget(L, lua_upvalueindex(1));
438 validproxy = lua_toboolean(L, -1);
439 lua_pop(L, 1); /* remove value */
440 }
441 luaL_argcheck(L, validproxy, 1, "boolean or proxy expected");
442 lua_getmetatable(L, 1); /* metatable is valid; get it */
443 }
444 lua_setmetatable(L, 2);
445 return 1;
446 }
447
448
449 static const luaL_Reg base_funcs[] = {
450 {"assert", luaB_assert},
451 {"collectgarbage", luaB_collectgarbage},
452 {"dofile", luaB_dofile},
453 {"error", luaB_error},
454 {"gcinfo", luaB_gcinfo},
455 {"getfenv", luaB_getfenv},
456 {"getmetatable", luaB_getmetatable},
457 {"loadfile", luaB_loadfile},
458 {"load", luaB_load},
459 {"loadstring", luaB_loadstring},
460 {"next", luaB_next},
461 {"pcall", luaB_pcall},
462 {"print", luaB_print},
463 {"log", luaB_print},
464 {"rawequal", luaB_rawequal},
465 {"rawget", luaB_rawget},
466 {"rawset", luaB_rawset},
467 {"select", luaB_select},
468 {"setfenv", luaB_setfenv},
469 {"setmetatable", luaB_setmetatable},
470 {"tonumber", luaB_tonumber},
471 {"tostring", luaB_tostring},
472 {"type", luaB_type},
473 {"unpack", luaB_unpack},
474 {"xpcall", luaB_xpcall},
475 {NULL, NULL}
476 };
477
478
479 /*
480 ** {======================================================
481 ** Coroutine library
482 ** =======================================================
483 */
484
auxresume(lua_State * L,lua_State * co,int narg)485 static int auxresume (lua_State *L, lua_State *co, int narg) {
486 int status;
487 if (!lua_checkstack(co, narg))
488 luaL_error(L, "too many arguments to resume");
489 if (lua_status(co) == 0 && lua_gettop(co) == 0) {
490 lua_pushliteral(L, "cannot resume dead coroutine");
491 return -1; /* error flag */
492 }
493 lua_xmove(L, co, narg);
494 status = lua_resume(co, narg);
495 if (status == 0 || status == LUA_YIELD) {
496 int nres = lua_gettop(co);
497 if (!lua_checkstack(L, nres))
498 luaL_error(L, "too many results to resume");
499 lua_xmove(co, L, nres); /* move yielded values */
500 return nres;
501 }
502 else {
503 lua_xmove(co, L, 1); /* move error message */
504 return -1; /* error flag */
505 }
506 }
507
508
luaB_coresume(lua_State * L)509 static int luaB_coresume (lua_State *L) {
510 lua_State *co = lua_tothread(L, 1);
511 int r;
512 luaL_argcheck(L, co, 1, "coroutine expected");
513 r = auxresume(L, co, lua_gettop(L) - 1);
514 if (r < 0) {
515 lua_pushboolean(L, 0);
516 lua_insert(L, -2);
517 return 2; /* return false + error message */
518 }
519 else {
520 lua_pushboolean(L, 1);
521 lua_insert(L, -(r + 1));
522 return r + 1; /* return true + `resume' returns */
523 }
524 }
525
526
luaB_auxwrap(lua_State * L)527 static int luaB_auxwrap (lua_State *L) {
528 lua_State *co = lua_tothread(L, lua_upvalueindex(1));
529 int r = auxresume(L, co, lua_gettop(L));
530 if (r < 0) {
531 if (lua_isstring(L, -1)) { /* error object is a string? */
532 luaL_where(L, 1); /* add extra info */
533 lua_insert(L, -2);
534 lua_concat(L, 2);
535 }
536 lua_error(L); /* propagate error */
537 }
538 return r;
539 }
540
541
luaB_cocreate(lua_State * L)542 static int luaB_cocreate (lua_State *L) {
543 lua_State *NL = lua_newthread(L);
544 luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
545 "Lua function expected");
546 lua_pushvalue(L, 1); /* move function to top */
547 lua_xmove(L, NL, 1); /* move function from L to NL */
548 return 1;
549 }
550
551
luaB_cowrap(lua_State * L)552 static int luaB_cowrap (lua_State *L) {
553 luaB_cocreate(L);
554 lua_pushcclosure(L, luaB_auxwrap, 1);
555 return 1;
556 }
557
558
luaB_yield(lua_State * L)559 static int luaB_yield (lua_State *L) {
560 return lua_yield(L, lua_gettop(L));
561 }
562
563
luaB_costatus(lua_State * L)564 static int luaB_costatus (lua_State *L) {
565 lua_State *co = lua_tothread(L, 1);
566 luaL_argcheck(L, co, 1, "coroutine expected");
567 if (L == co) lua_pushliteral(L, "running");
568 else {
569 switch (lua_status(co)) {
570 case LUA_YIELD:
571 lua_pushliteral(L, "suspended");
572 break;
573 case 0: {
574 lua_Debug ar;
575 if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
576 lua_pushliteral(L, "normal"); /* it is running */
577 else if (lua_gettop(co) == 0)
578 lua_pushliteral(L, "dead");
579 else
580 lua_pushliteral(L, "suspended"); /* initial state */
581 break;
582 }
583 default: /* some error occured */
584 lua_pushliteral(L, "dead");
585 break;
586 }
587 }
588 return 1;
589 }
590
591
luaB_corunning(lua_State * L)592 static int luaB_corunning (lua_State *L) {
593 if (lua_pushthread(L))
594 return 0; /* main thread is not a coroutine */
595 else
596 return 1;
597 }
598
599
600 static const luaL_Reg co_funcs[] = {
601 {"create", luaB_cocreate},
602 {"resume", luaB_coresume},
603 {"running", luaB_corunning},
604 {"status", luaB_costatus},
605 {"wrap", luaB_cowrap},
606 {"yield", luaB_yield},
607 {NULL, NULL}
608 };
609
610 /* }====================================================== */
611
612
auxopen(lua_State * L,const char * name,lua_CFunction f,lua_CFunction u)613 static void auxopen (lua_State *L, const char *name,
614 lua_CFunction f, lua_CFunction u) {
615 lua_pushcfunction(L, u);
616 lua_pushcclosure(L, f, 1);
617 lua_setfield(L, -2, name);
618 }
619
620
base_open(lua_State * L)621 static void base_open (lua_State *L) {
622 /* set global _G */
623 lua_pushvalue(L, LUA_GLOBALSINDEX);
624 lua_setglobal(L, "_G");
625 /* open lib into global table */
626 luaL_register(L, "_G", base_funcs);
627 lua_pushliteral(L, LUA_VERSION);
628 lua_setglobal(L, "_VERSION"); /* set global _VERSION */
629 /* `ipairs' and `pairs' need auxliliary functions as upvalues */
630 auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
631 auxopen(L, "pairs", luaB_pairs, luaB_next);
632 /* `newproxy' needs a weaktable as upvalue */
633 lua_createtable(L, 0, 1); /* new table `w' */
634 lua_pushvalue(L, -1); /* `w' will be its own metatable */
635 lua_setmetatable(L, -2);
636 lua_pushliteral(L, "kv");
637 lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */
638 lua_pushcclosure(L, luaB_newproxy, 1);
639 lua_setglobal(L, "newproxy"); /* set global `newproxy' */
640 }
641
642
luaopen_base(lua_State * L)643 LUALIB_API int luaopen_base (lua_State *L) {
644 base_open(L);
645 luaL_register(L, LUA_COLIBNAME, co_funcs);
646 return 2;
647 }
648
649