Minor fixes for x64 interpreter.
[luajit-2.0/celess22.git] / src / lib_base.c
blob34282cfc069b091788ceed4f1d59f63275a8e5d0
1 /*
2 ** Base and coroutine library.
3 ** Copyright (C) 2005-2009 Mike Pall. See Copyright Notice in luajit.h
4 **
5 ** Major portions taken verbatim or adapted from the Lua interpreter.
6 ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
7 */
9 #include <stdio.h>
11 #define lib_base_c
12 #define LUA_LIB
14 #include "lua.h"
15 #include "lauxlib.h"
16 #include "lualib.h"
18 #include "lj_obj.h"
19 #include "lj_gc.h"
20 #include "lj_err.h"
21 #include "lj_str.h"
22 #include "lj_tab.h"
23 #include "lj_meta.h"
24 #include "lj_state.h"
25 #include "lj_ff.h"
26 #include "lj_ctype.h"
27 #include "lj_lib.h"
29 /* -- Base library: checks ------------------------------------------------ */
31 #define LJLIB_MODULE_base
33 LJLIB_ASM(assert) LJLIB_REC(.)
35 GCstr *s;
36 lj_lib_checkany(L, 1);
37 s = lj_lib_optstr(L, 2);
38 if (s)
39 lj_err_callermsg(L, strdata(s));
40 else
41 lj_err_caller(L, LJ_ERR_ASSERT);
42 return FFH_UNREACHABLE;
45 /* ORDER LJ_T */
46 LJLIB_PUSH("nil")
47 LJLIB_PUSH("boolean")
48 LJLIB_PUSH(top-1) /* boolean */
49 LJLIB_PUSH("userdata")
50 LJLIB_PUSH("string")
51 LJLIB_PUSH("upval")
52 LJLIB_PUSH("thread")
53 LJLIB_PUSH("proto")
54 LJLIB_PUSH("function")
55 LJLIB_PUSH("deadkey")
56 LJLIB_PUSH("table")
57 LJLIB_PUSH(top-8) /* userdata */
58 LJLIB_PUSH("number")
59 LJLIB_ASM_(type) LJLIB_REC(.)
60 /* Recycle the lj_lib_checkany(L, 1) from assert. */
62 /* -- Base library: getters and setters ----------------------------------- */
64 LJLIB_ASM_(getmetatable) LJLIB_REC(.)
65 /* Recycle the lj_lib_checkany(L, 1) from assert. */
67 LJLIB_ASM(setmetatable) LJLIB_REC(.)
69 GCtab *t = lj_lib_checktab(L, 1);
70 GCtab *mt = lj_lib_checktabornil(L, 2);
71 if (!tvisnil(lj_meta_lookup(L, L->base, MM_metatable)))
72 lj_err_caller(L, LJ_ERR_PROTMT);
73 setgcref(t->metatable, obj2gco(mt));
74 if (mt) { lj_gc_objbarriert(L, t, mt); }
75 settabV(L, L->base-1, t);
76 return FFH_RES(1);
79 LJLIB_CF(getfenv)
81 GCfunc *fn;
82 cTValue *o = L->base;
83 if (!(o < L->top && tvisfunc(o))) {
84 int level = lj_lib_optint(L, 1, 1);
85 o = lj_err_getframe(L, level, &level);
86 if (o == NULL)
87 lj_err_arg(L, 1, LJ_ERR_INVLVL);
89 fn = &gcval(o)->fn;
90 settabV(L, L->top++, isluafunc(fn) ? tabref(fn->l.env) : tabref(L->env));
91 return 1;
94 LJLIB_CF(setfenv)
96 GCfunc *fn;
97 GCtab *t = lj_lib_checktab(L, 2);
98 cTValue *o = L->base;
99 if (!(o < L->top && tvisfunc(o))) {
100 int level = lj_lib_checkint(L, 1);
101 if (level == 0) {
102 /* NOBARRIER: A thread (i.e. L) is never black. */
103 setgcref(L->env, obj2gco(t));
104 return 0;
106 o = lj_err_getframe(L, level, &level);
107 if (o == NULL)
108 lj_err_arg(L, 1, LJ_ERR_INVLVL);
110 fn = &gcval(o)->fn;
111 if (!isluafunc(fn))
112 lj_err_caller(L, LJ_ERR_SETFENV);
113 setgcref(fn->l.env, obj2gco(t));
114 lj_gc_objbarrier(L, obj2gco(fn), t);
115 setfuncV(L, L->top++, fn);
116 return 1;
119 LJLIB_ASM(rawget) LJLIB_REC(.)
121 lj_lib_checktab(L, 1);
122 lj_lib_checkany(L, 2);
123 return FFH_UNREACHABLE;
126 LJLIB_CF(rawset) LJLIB_REC(.)
128 lj_lib_checktab(L, 1);
129 lj_lib_checkany(L, 2);
130 L->top = 1+lj_lib_checkany(L, 3);
131 lua_rawset(L, 1);
132 return 1;
135 LJLIB_CF(rawequal) LJLIB_REC(.)
137 cTValue *o1 = lj_lib_checkany(L, 1);
138 cTValue *o2 = lj_lib_checkany(L, 2);
139 setboolV(L->top-1, lj_obj_equal(o1, o2));
140 return 1;
143 LJLIB_CF(unpack)
145 GCtab *t = lj_lib_checktab(L, 1);
146 int32_t n, i = lj_lib_optint(L, 2, 1);
147 int32_t e = (L->base+3-1 < L->top && !tvisnil(L->base+3-1)) ?
148 lj_lib_checkint(L, 3) : (int32_t)lj_tab_len(t);
149 if (i > e) return 0;
150 n = e - i + 1;
151 if (n <= 0 || !lua_checkstack(L, n))
152 lj_err_caller(L, LJ_ERR_UNPACK);
153 do {
154 cTValue *tv = lj_tab_getint(t, i);
155 if (tv) {
156 copyTV(L, L->top++, tv);
157 } else {
158 setnilV(L->top++);
160 } while (i++ < e);
161 return n;
164 LJLIB_CF(select)
166 int32_t n = (int32_t)(L->top - L->base);
167 if (n >= 1 && tvisstr(L->base) && *strVdata(L->base) == '#') {
168 setintV(L->top-1, n-1);
169 return 1;
170 } else {
171 int32_t i = lj_lib_checkint(L, 1);
172 if (i < 0) i = n + i; else if (i > n) i = n;
173 if (i < 1)
174 lj_err_arg(L, 1, LJ_ERR_IDXRNG);
175 return n - i;
179 /* -- Base library: conversions ------------------------------------------- */
181 LJLIB_ASM(tonumber) LJLIB_REC(.)
183 int32_t base = lj_lib_optint(L, 2, 10);
184 if (base == 10) {
185 TValue *o = lj_lib_checkany(L, 1);
186 if (tvisnum(o) || (tvisstr(o) && lj_str_tonum(strV(o), o))) {
187 setnumV(L->base-1, numV(o));
188 return FFH_RES(1);
190 } else {
191 const char *p = strdata(lj_lib_checkstr(L, 1));
192 char *ep;
193 unsigned long ul;
194 if (base < 2 || base > 36)
195 lj_err_arg(L, 2, LJ_ERR_BASERNG);
196 ul = strtoul(p, &ep, base);
197 if (p != ep) {
198 while (lj_ctype_isspace((unsigned char)(*ep))) ep++;
199 if (*ep == '\0') {
200 setnumV(L->base-1, cast_num(ul));
201 return FFH_RES(1);
205 setnilV(L->base-1);
206 return FFH_RES(1);
209 LJLIB_PUSH("nil")
210 LJLIB_PUSH("false")
211 LJLIB_PUSH("true")
212 LJLIB_ASM(tostring) LJLIB_REC(.)
214 TValue *o = lj_lib_checkany(L, 1);
215 cTValue *mo;
216 L->top = o+1; /* Only keep one argument. */
217 if (!tvisnil(mo = lj_meta_lookup(L, o, MM_tostring))) {
218 copyTV(L, L->base-1, mo); /* Replace callable. */
219 return FFH_RETRY;
220 } else {
221 GCstr *s;
222 if (tvisnum(o)) {
223 s = lj_str_fromnum(L, &o->n);
224 } else if (tvispri(o)) {
225 s = strV(lj_lib_upvalue(L, -itype(o)));
226 } else {
227 if (tvisfunc(o) && isffunc(funcV(o)))
228 lua_pushfstring(L, "function: fast#%d", funcV(o)->c.ffid);
229 else
230 lua_pushfstring(L, "%s: %p", typename(o), lua_topointer(L, 1));
231 /* Note: lua_pushfstring calls the GC which may invalidate o. */
232 s = strV(L->top-1);
234 setstrV(L, L->base-1, s);
235 return FFH_RES(1);
239 /* -- Base library: iterators --------------------------------------------- */
241 LJLIB_ASM(next)
243 lj_lib_checktab(L, 1);
244 lj_lib_checknum(L, 2); /* For ipairs_aux. */
245 return FFH_UNREACHABLE;
248 LJLIB_PUSH(lastcl)
249 LJLIB_ASM_(pairs)
251 LJLIB_NOREGUV LJLIB_ASM_(ipairs_aux) LJLIB_REC(.)
253 LJLIB_PUSH(lastcl)
254 LJLIB_ASM_(ipairs) LJLIB_REC(.)
256 /* -- Base library: throw and catch errors -------------------------------- */
258 LJLIB_CF(error)
260 int32_t level = lj_lib_optint(L, 2, 1);
261 lua_settop(L, 1);
262 if (lua_isstring(L, 1) && level > 0) {
263 luaL_where(L, level);
264 lua_pushvalue(L, 1);
265 lua_concat(L, 2);
267 return lua_error(L);
270 LJLIB_ASM(pcall) LJLIB_REC(.)
272 lj_lib_checkany(L, 1);
273 lj_lib_checkfunc(L, 2); /* For xpcall only. */
274 return FFH_UNREACHABLE;
276 LJLIB_ASM_(xpcall) LJLIB_REC(.)
278 /* -- Base library: load Lua code ----------------------------------------- */
280 static int load_aux(lua_State *L, int status)
282 if (status == 0)
283 return 1;
284 copyTV(L, L->top, L->top-1);
285 setnilV(L->top-1);
286 L->top++;
287 return 2;
290 LJLIB_CF(loadstring)
292 GCstr *s = lj_lib_checkstr(L, 1);
293 GCstr *name = lj_lib_optstr(L, 2);
294 return load_aux(L,
295 luaL_loadbuffer(L, strdata(s), s->len, strdata(name ? name : s)));
298 LJLIB_CF(loadfile)
300 GCstr *fname = lj_lib_optstr(L, 1);
301 return load_aux(L, luaL_loadfile(L, fname ? strdata(fname) : NULL));
304 static const char *reader_func(lua_State *L, void *ud, size_t *size)
306 UNUSED(ud);
307 luaL_checkstack(L, 2, "too many nested functions");
308 copyTV(L, L->top++, L->base);
309 lua_call(L, 0, 1); /* Call user-supplied function. */
310 L->top--;
311 if (tvisnil(L->top)) {
312 *size = 0;
313 return NULL;
314 } else if (tvisstr(L->top) || tvisnum(L->top)) {
315 copyTV(L, L->base+2, L->top); /* Anchor string in reserved stack slot. */
316 return lua_tolstring(L, 3, size);
317 } else {
318 lj_err_caller(L, LJ_ERR_RDRSTR);
319 return NULL;
323 LJLIB_CF(load)
325 GCstr *name = lj_lib_optstr(L, 2);
326 lj_lib_checkfunc(L, 1);
327 lua_settop(L, 3); /* Reserve a slot for the string from the reader. */
328 return load_aux(L,
329 lua_load(L, reader_func, NULL, name ? strdata(name) : "=(load)"));
332 LJLIB_CF(dofile)
334 GCstr *fname = lj_lib_optstr(L, 1);
335 setnilV(L->top);
336 L->top = L->base+1;
337 if (luaL_loadfile(L, fname ? strdata(fname) : NULL) != 0)
338 lua_error(L);
339 lua_call(L, 0, LUA_MULTRET);
340 return (L->top - L->base) - 1;
343 /* -- Base library: GC control -------------------------------------------- */
345 LJLIB_CF(gcinfo)
347 setintV(L->top++, (G(L)->gc.total >> 10));
348 return 1;
351 LJLIB_CF(collectgarbage)
353 int opt = lj_lib_checkopt(L, 1, LUA_GCCOLLECT, /* ORDER LUA_GC* */
354 "\4stop\7restart\7collect\5count\1\377\4step\10setpause\12setstepmul");
355 int32_t data = lj_lib_optint(L, 2, 0);
356 if (opt == LUA_GCCOUNT) {
357 setnumV(L->top, cast_num((int32_t)G(L)->gc.total)/1024.0);
358 } else {
359 int res = lua_gc(L, opt, data);
360 if (opt == LUA_GCSTEP)
361 setboolV(L->top, res);
362 else
363 setintV(L->top, res);
365 L->top++;
366 return 1;
369 /* -- Base library: miscellaneous functions ------------------------------- */
371 LJLIB_PUSH(top-2) /* Upvalue holds weak table. */
372 LJLIB_CF(newproxy)
374 lua_settop(L, 1);
375 lua_newuserdata(L, 0);
376 if (lua_toboolean(L, 1) == 0) { /* newproxy(): without metatable. */
377 return 1;
378 } else if (lua_isboolean(L, 1)) { /* newproxy(true): with metatable. */
379 lua_newtable(L);
380 lua_pushvalue(L, -1);
381 lua_pushboolean(L, 1);
382 lua_rawset(L, lua_upvalueindex(1)); /* Remember mt in weak table. */
383 } else { /* newproxy(proxy): inherit metatable. */
384 int validproxy = 0;
385 if (lua_getmetatable(L, 1)) {
386 lua_rawget(L, lua_upvalueindex(1));
387 validproxy = lua_toboolean(L, -1);
388 lua_pop(L, 1);
390 if (!validproxy)
391 lj_err_arg(L, 1, LJ_ERR_NOPROXY);
392 lua_getmetatable(L, 1);
394 lua_setmetatable(L, 2);
395 return 1;
398 LJLIB_PUSH("tostring")
399 LJLIB_CF(print)
401 ptrdiff_t i, nargs = L->top - L->base;
402 cTValue *tv = lj_tab_getstr(tabref(L->env), strV(lj_lib_upvalue(L, 1)));
403 int shortcut = (tv && tvisfunc(tv) && funcV(tv)->c.ffid == FF_tostring);
404 copyTV(L, L->top++, tv ? tv : niltv(L));
405 for (i = 0; i < nargs; i++) {
406 const char *str;
407 size_t size;
408 cTValue *o = &L->base[i];
409 if (shortcut && tvisstr(o)) {
410 str = strVdata(o);
411 size = strV(o)->len;
412 } else if (shortcut && tvisnum(o)) {
413 char buf[LUAI_MAXNUMBER2STR];
414 lua_Number n = numV(o);
415 size = (size_t)lua_number2str(buf, n);
416 str = buf;
417 } else {
418 copyTV(L, L->top+1, o);
419 copyTV(L, L->top, L->top-1);
420 L->top += 2;
421 lua_call(L, 1, 1);
422 str = lua_tolstring(L, -1, &size);
423 if (!str)
424 lj_err_caller(L, LJ_ERR_PRTOSTR);
425 L->top--;
427 if (i)
428 putchar('\t');
429 fwrite(str, 1, size, stdout);
431 putchar('\n');
432 return 0;
435 LJLIB_PUSH(top-3)
436 LJLIB_SET(_VERSION)
438 #include "lj_libdef.h"
440 /* -- Coroutine library --------------------------------------------------- */
442 #define LJLIB_MODULE_coroutine
444 LJLIB_CF(coroutine_status)
446 const char *s;
447 lua_State *co;
448 if (!(L->top > L->base && tvisthread(L->base)))
449 lj_err_arg(L, 1, LJ_ERR_NOCORO);
450 co = threadV(L->base);
451 if (co == L) s = "running";
452 else if (co->status == LUA_YIELD) s = "suspended";
453 else if (co->status != 0) s = "dead";
454 else if (co->base > co->stack+1) s = "normal";
455 else if (co->top == co->base) s = "dead";
456 else s = "suspended";
457 lua_pushstring(L, s);
458 return 1;
461 LJLIB_CF(coroutine_running)
463 if (lua_pushthread(L))
464 setnilV(L->top++);
465 return 1;
468 LJLIB_CF(coroutine_create)
470 lua_State *L1 = lua_newthread(L);
471 if (!(L->top > L->base && tvisfunc(L->base) && isluafunc(funcV(L->base))))
472 lj_err_arg(L, 1, LJ_ERR_NOLFUNC);
473 setfuncV(L, L1->top++, funcV(L->base));
474 return 1;
477 LJLIB_ASM(coroutine_yield)
479 lj_err_caller(L, LJ_ERR_CYIELD);
480 return FFH_UNREACHABLE;
483 static int ffh_resume(lua_State *L, lua_State *co, int wrap)
485 if (co->cframe != NULL || co->status > LUA_YIELD ||
486 (co->status == 0 && co->top == co->base)) {
487 ErrMsg em = co->cframe ? LJ_ERR_CORUN : LJ_ERR_CODEAD;
488 if (wrap) lj_err_caller(L, em);
489 setboolV(L->base-1, 0);
490 setstrV(L, L->base, lj_err_str(L, em));
491 return FFH_RES(2);
493 lj_state_growstack(co, (MSize)(L->top - L->base - 1));
494 return FFH_RETRY;
497 LJLIB_ASM(coroutine_resume)
499 if (!(L->top > L->base && tvisthread(L->base)))
500 lj_err_arg(L, 1, LJ_ERR_NOCORO);
501 return ffh_resume(L, threadV(L->base), 0);
504 LJLIB_NOREG LJLIB_ASM(coroutine_wrap_aux)
506 return ffh_resume(L, threadV(lj_lib_upvalue(L, 1)), 1);
509 /* Inline declarations. */
510 LJ_ASMF void lj_ff_coroutine_wrap_aux(void);
511 LJ_FUNCA_NORET void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L,
512 lua_State *co);
514 /* Error handler, called from assembler VM. */
515 void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L, lua_State *co)
517 co->top--; copyTV(L, L->top, co->top); L->top++;
518 if (tvisstr(L->top-1))
519 lj_err_callermsg(L, strVdata(L->top-1));
520 else
521 lj_err_run(L);
524 LJLIB_CF(coroutine_wrap)
526 GCfunc *fn;
527 lj_cf_coroutine_create(L);
528 lua_pushcclosure(L, lj_ffh_coroutine_wrap_aux, 1);
529 fn = funcV(L->top-1);
530 fn->c.gate = lj_ff_coroutine_wrap_aux;
531 fn->c.ffid = FF_coroutine_wrap_aux;
532 return 1;
535 #include "lj_libdef.h"
537 /* ------------------------------------------------------------------------ */
539 static void newproxy_weaktable(lua_State *L)
541 /* NOBARRIER: The table is new (marked white). */
542 GCtab *t = lj_tab_new(L, 0, 1);
543 settabV(L, L->top++, t);
544 setgcref(t->metatable, obj2gco(t));
545 setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "__mode")),
546 lj_str_newlit(L, "kv"));
547 t->nomm = cast_byte(~(1u<<MM_mode));
550 LUALIB_API int luaopen_base(lua_State *L)
552 /* NOBARRIER: Table and value are the same. */
553 GCtab *env = tabref(L->env);
554 settabV(L, lj_tab_setstr(L, env, lj_str_newlit(L, "_G")), env);
555 lua_pushliteral(L, LUA_VERSION); /* top-3. */
556 newproxy_weaktable(L); /* top-2. */
557 LJ_LIB_REG_(L, "_G", base);
558 LJ_LIB_REG(L, coroutine);
559 return 2;