2 ** Base and coroutine library.
3 ** Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h
5 ** Major portions taken verbatim or adapted from the Lua interpreter.
6 ** Copyright (C) 1994-2011 Lua.org, PUC-Rio. See Copyright Notice in lua.h
32 #include "lj_dispatch.h"
36 /* -- Base library: checks ------------------------------------------------ */
38 #define LJLIB_MODULE_base
40 LJLIB_ASM(assert) LJLIB_REC(.)
43 lj_lib_checkany(L
, 1);
44 s
= lj_lib_optstr(L
, 2);
46 lj_err_callermsg(L
, strdata(s
));
48 lj_err_caller(L
, LJ_ERR_ASSERT
);
49 return FFH_UNREACHABLE
;
55 LJLIB_PUSH(top
-1) /* boolean */
56 LJLIB_PUSH("userdata")
61 LJLIB_PUSH("function")
65 LJLIB_PUSH(top
-9) /* userdata */
67 LJLIB_ASM_(type
) LJLIB_REC(.)
68 /* Recycle the lj_lib_checkany(L, 1) from assert. */
70 /* -- Base library: getters and setters ----------------------------------- */
72 LJLIB_ASM_(getmetatable
) LJLIB_REC(.)
73 /* Recycle the lj_lib_checkany(L, 1) from assert. */
75 LJLIB_ASM(setmetatable
) LJLIB_REC(.)
77 GCtab
*t
= lj_lib_checktab(L
, 1);
78 GCtab
*mt
= lj_lib_checktabornil(L
, 2);
79 if (!tvisnil(lj_meta_lookup(L
, L
->base
, MM_metatable
)))
80 lj_err_caller(L
, LJ_ERR_PROTMT
);
81 setgcref(t
->metatable
, obj2gco(mt
));
82 if (mt
) { lj_gc_objbarriert(L
, t
, mt
); }
83 settabV(L
, L
->base
-1, t
);
91 if (!(o
< L
->top
&& tvisfunc(o
))) {
92 int level
= lj_lib_optint(L
, 1, 1);
93 o
= lj_debug_frame(L
, level
, &level
);
95 lj_err_arg(L
, 1, LJ_ERR_INVLVL
);
98 settabV(L
, L
->top
++, isluafunc(fn
) ? tabref(fn
->l
.env
) : tabref(L
->env
));
105 GCtab
*t
= lj_lib_checktab(L
, 2);
106 cTValue
*o
= L
->base
;
107 if (!(o
< L
->top
&& tvisfunc(o
))) {
108 int level
= lj_lib_checkint(L
, 1);
110 /* NOBARRIER: A thread (i.e. L) is never black. */
111 setgcref(L
->env
, obj2gco(t
));
114 o
= lj_debug_frame(L
, level
, &level
);
116 lj_err_arg(L
, 1, LJ_ERR_INVLVL
);
120 lj_err_caller(L
, LJ_ERR_SETFENV
);
121 setgcref(fn
->l
.env
, obj2gco(t
));
122 lj_gc_objbarrier(L
, obj2gco(fn
), t
);
123 setfuncV(L
, L
->top
++, fn
);
127 LJLIB_ASM(rawget
) LJLIB_REC(.)
129 lj_lib_checktab(L
, 1);
130 lj_lib_checkany(L
, 2);
131 return FFH_UNREACHABLE
;
134 LJLIB_CF(rawset
) LJLIB_REC(.)
136 lj_lib_checktab(L
, 1);
137 lj_lib_checkany(L
, 2);
138 L
->top
= 1+lj_lib_checkany(L
, 3);
143 LJLIB_CF(rawequal
) LJLIB_REC(.)
145 cTValue
*o1
= lj_lib_checkany(L
, 1);
146 cTValue
*o2
= lj_lib_checkany(L
, 2);
147 setboolV(L
->top
-1, lj_obj_equal(o1
, o2
));
153 GCtab
*t
= lj_lib_checktab(L
, 1);
154 int32_t n
, i
= lj_lib_optint(L
, 2, 1);
155 int32_t e
= (L
->base
+3-1 < L
->top
&& !tvisnil(L
->base
+3-1)) ?
156 lj_lib_checkint(L
, 3) : (int32_t)lj_tab_len(t
);
159 if (n
<= 0 || !lua_checkstack(L
, n
))
160 lj_err_caller(L
, LJ_ERR_UNPACK
);
162 cTValue
*tv
= lj_tab_getint(t
, i
);
164 copyTV(L
, L
->top
++, tv
);
172 LJLIB_CF(select
) LJLIB_REC(.)
174 int32_t n
= (int32_t)(L
->top
- L
->base
);
175 if (n
>= 1 && tvisstr(L
->base
) && *strVdata(L
->base
) == '#') {
176 setintV(L
->top
-1, n
-1);
179 int32_t i
= lj_lib_checkint(L
, 1);
180 if (i
< 0) i
= n
+ i
; else if (i
> n
) i
= n
;
182 lj_err_arg(L
, 1, LJ_ERR_IDXRNG
);
187 /* -- Base library: conversions ------------------------------------------- */
189 LJLIB_ASM(tonumber
) LJLIB_REC(.)
191 int32_t base
= lj_lib_optint(L
, 2, 10);
193 TValue
*o
= lj_lib_checkany(L
, 1);
194 if (tvisnumber(o
) || (tvisstr(o
) && lj_str_tonumber(strV(o
), o
))) {
195 copyTV(L
, L
->base
-1, o
);
200 CTState
*cts
= ctype_cts(L
);
201 CType
*ct
= lj_ctype_rawref(cts
, cdataV(o
)->ctypeid
);
202 if (ctype_isenum(ct
->info
)) ct
= ctype_child(cts
, ct
);
203 if (ctype_isnum(ct
->info
) || ctype_iscomplex(ct
->info
)) {
204 if (LJ_DUALNUM
&& ctype_isinteger_or_bool(ct
->info
) &&
205 ct
->size
<= 4 && !(ct
->size
== 4 && (ct
->info
& CTF_UNSIGNED
))) {
207 lj_cconv_ct_tv(cts
, ctype_get(cts
, CTID_INT32
), (uint8_t *)&i
, o
, 0);
208 setintV(L
->base
-1, i
);
211 lj_cconv_ct_tv(cts
, ctype_get(cts
, CTID_DOUBLE
),
212 (uint8_t *)&(L
->base
-1)->n
, o
, 0);
218 const char *p
= strdata(lj_lib_checkstr(L
, 1));
221 if (base
< 2 || base
> 36)
222 lj_err_arg(L
, 2, LJ_ERR_BASERNG
);
223 ul
= strtoul(p
, &ep
, base
);
225 while (lj_char_isspace((unsigned char)(*ep
))) ep
++;
227 if (LJ_DUALNUM
&& LJ_LIKELY(ul
< 0x80000000u
))
228 setintV(L
->base
-1, (int32_t)ul
);
230 setnumV(L
->base
-1, (lua_Number
)ul
);
242 LJLIB_ASM(tostring
) LJLIB_REC(.)
244 TValue
*o
= lj_lib_checkany(L
, 1);
246 L
->top
= o
+1; /* Only keep one argument. */
247 if (!tvisnil(mo
= lj_meta_lookup(L
, o
, MM_tostring
))) {
248 copyTV(L
, L
->base
-1, mo
); /* Replace callable. */
253 s
= lj_str_fromnumber(L
, o
);
254 } else if (tvispri(o
)) {
255 s
= strV(lj_lib_upvalue(L
, -(int32_t)itype(o
)));
257 if (tvisfunc(o
) && isffunc(funcV(o
)))
258 lua_pushfstring(L
, "function: fast#%d", funcV(o
)->c
.ffid
);
260 lua_pushfstring(L
, "%s: %p", lj_typename(o
), lua_topointer(L
, 1));
261 /* Note: lua_pushfstring calls the GC which may invalidate o. */
264 setstrV(L
, L
->base
-1, s
);
269 /* -- Base library: iterators --------------------------------------------- */
271 /* This solves a circular dependency problem -- change FF_next_N as needed. */
272 LJ_STATIC_ASSERT((int)FF_next
== FF_next_N
);
276 lj_lib_checktab(L
, 1);
277 return FFH_UNREACHABLE
;
280 #ifdef LUAJIT_ENABLE_LUA52COMPAT
281 static int ffh_pairs(lua_State
*L
, MMS mm
)
283 TValue
*o
= lj_lib_checkany(L
, 1);
284 cTValue
*mo
= lj_meta_lookup(L
, o
, mm
);
286 L
->top
= o
+1; /* Only keep one argument. */
287 copyTV(L
, L
->base
-1, mo
); /* Replace callable. */
290 if (!tvistab(o
)) lj_err_argt(L
, 1, LUA_TTABLE
);
291 setfuncV(L
, o
-1, funcV(lj_lib_upvalue(L
, 1)));
292 if (mm
== MM_pairs
) setnilV(o
+1); else setintV(o
+1, 0);
297 #define ffh_pairs(L, mm) (lj_lib_checktab(L, 1), FFH_UNREACHABLE)
303 return ffh_pairs(L
, MM_pairs
);
306 LJLIB_NOREGUV
LJLIB_ASM(ipairs_aux
) LJLIB_REC(.)
308 lj_lib_checktab(L
, 1);
309 lj_lib_checkint(L
, 2);
310 return FFH_UNREACHABLE
;
314 LJLIB_ASM(ipairs
) LJLIB_REC(.)
316 return ffh_pairs(L
, MM_ipairs
);
319 /* -- Base library: throw and catch errors -------------------------------- */
323 int32_t level
= lj_lib_optint(L
, 2, 1);
325 if (lua_isstring(L
, 1) && level
> 0) {
326 luaL_where(L
, level
);
333 LJLIB_ASM(pcall
) LJLIB_REC(.)
335 lj_lib_checkany(L
, 1);
336 lj_lib_checkfunc(L
, 2); /* For xpcall only. */
337 return FFH_UNREACHABLE
;
339 LJLIB_ASM_(xpcall
) LJLIB_REC(.)
341 /* -- Base library: load Lua code ----------------------------------------- */
343 static int load_aux(lua_State
*L
, int status
)
347 copyTV(L
, L
->top
, L
->top
-1);
355 GCstr
*s
= lj_lib_checkstr(L
, 1);
356 GCstr
*name
= lj_lib_optstr(L
, 2);
358 luaL_loadbuffer(L
, strdata(s
), s
->len
, strdata(name
? name
: s
)));
363 GCstr
*fname
= lj_lib_optstr(L
, 1);
364 return load_aux(L
, luaL_loadfile(L
, fname
? strdata(fname
) : NULL
));
367 static const char *reader_func(lua_State
*L
, void *ud
, size_t *size
)
370 luaL_checkstack(L
, 2, "too many nested functions");
371 copyTV(L
, L
->top
++, L
->base
);
372 lua_call(L
, 0, 1); /* Call user-supplied function. */
374 if (tvisnil(L
->top
)) {
377 } else if (tvisstr(L
->top
) || tvisnumber(L
->top
)) {
378 copyTV(L
, L
->base
+2, L
->top
); /* Anchor string in reserved stack slot. */
379 return lua_tolstring(L
, 3, size
);
381 lj_err_caller(L
, LJ_ERR_RDRSTR
);
389 if (L
->base
< L
->top
&& (tvisstr(L
->base
) || tvisnumber(L
->base
)))
390 return lj_cf_loadstring(L
);
391 lj_lib_checkfunc(L
, 1);
392 name
= lj_lib_optstr(L
, 2);
393 lua_settop(L
, 3); /* Reserve a slot for the string from the reader. */
395 lua_load(L
, reader_func
, NULL
, name
? strdata(name
) : "=(load)"));
400 GCstr
*fname
= lj_lib_optstr(L
, 1);
403 if (luaL_loadfile(L
, fname
? strdata(fname
) : NULL
) != 0)
405 lua_call(L
, 0, LUA_MULTRET
);
406 return (int)(L
->top
- L
->base
) - 1;
409 /* -- Base library: GC control -------------------------------------------- */
413 setintV(L
->top
++, (G(L
)->gc
.total
>> 10));
417 LJLIB_CF(collectgarbage
)
419 int opt
= lj_lib_checkopt(L
, 1, LUA_GCCOLLECT
, /* ORDER LUA_GC* */
420 "\4stop\7restart\7collect\5count\1\377\4step\10setpause\12setstepmul");
421 int32_t data
= lj_lib_optint(L
, 2, 0);
422 if (opt
== LUA_GCCOUNT
) {
423 setnumV(L
->top
, (lua_Number
)G(L
)->gc
.total
/1024.0);
425 int res
= lua_gc(L
, opt
, data
);
426 if (opt
== LUA_GCSTEP
)
427 setboolV(L
->top
, res
);
429 setintV(L
->top
, res
);
435 /* -- Base library: miscellaneous functions ------------------------------- */
437 LJLIB_PUSH(top
-2) /* Upvalue holds weak table. */
441 lua_newuserdata(L
, 0);
442 if (lua_toboolean(L
, 1) == 0) { /* newproxy(): without metatable. */
444 } else if (lua_isboolean(L
, 1)) { /* newproxy(true): with metatable. */
446 lua_pushvalue(L
, -1);
447 lua_pushboolean(L
, 1);
448 lua_rawset(L
, lua_upvalueindex(1)); /* Remember mt in weak table. */
449 } else { /* newproxy(proxy): inherit metatable. */
451 if (lua_getmetatable(L
, 1)) {
452 lua_rawget(L
, lua_upvalueindex(1));
453 validproxy
= lua_toboolean(L
, -1);
457 lj_err_arg(L
, 1, LJ_ERR_NOPROXY
);
458 lua_getmetatable(L
, 1);
460 lua_setmetatable(L
, 2);
464 LJLIB_PUSH("tostring")
467 ptrdiff_t i
, nargs
= L
->top
- L
->base
;
468 cTValue
*tv
= lj_tab_getstr(tabref(L
->env
), strV(lj_lib_upvalue(L
, 1)));
470 if (tv
&& !tvisnil(tv
)) {
471 copyTV(L
, L
->top
++, tv
);
473 setstrV(L
, L
->top
++, strV(lj_lib_upvalue(L
, 1)));
474 lua_gettable(L
, LUA_GLOBALSINDEX
);
477 shortcut
= (tvisfunc(tv
) && funcV(tv
)->c
.ffid
== FF_tostring
);
478 for (i
= 0; i
< nargs
; i
++) {
481 cTValue
*o
= &L
->base
[i
];
482 if (shortcut
&& tvisstr(o
)) {
485 } else if (shortcut
&& tvisint(o
)) {
486 char buf
[LJ_STR_INTBUF
];
487 char *p
= lj_str_bufint(buf
, intV(o
));
488 size
= (size_t)(buf
+LJ_STR_INTBUF
-p
);
490 } else if (shortcut
&& tvisnum(o
)) {
491 char buf
[LJ_STR_NUMBUF
];
492 size
= lj_str_bufnum(buf
, o
);
495 copyTV(L
, L
->top
+1, o
);
496 copyTV(L
, L
->top
, L
->top
-1);
499 str
= lua_tolstring(L
, -1, &size
);
501 lj_err_caller(L
, LJ_ERR_PRTOSTR
);
506 fwrite(str
, 1, size
, stdout
);
515 #include "lj_libdef.h"
517 /* -- Coroutine library --------------------------------------------------- */
519 #define LJLIB_MODULE_coroutine
521 LJLIB_CF(coroutine_status
)
525 if (!(L
->top
> L
->base
&& tvisthread(L
->base
)))
526 lj_err_arg(L
, 1, LJ_ERR_NOCORO
);
527 co
= threadV(L
->base
);
528 if (co
== L
) s
= "running";
529 else if (co
->status
== LUA_YIELD
) s
= "suspended";
530 else if (co
->status
!= 0) s
= "dead";
531 else if (co
->base
> tvref(co
->stack
)+1) s
= "normal";
532 else if (co
->top
== co
->base
) s
= "dead";
533 else s
= "suspended";
534 lua_pushstring(L
, s
);
538 LJLIB_CF(coroutine_running
)
540 #ifdef LUAJIT_ENABLE_LUA52COMPAT
541 int ismain
= lua_pushthread(L
);
542 setboolV(L
->top
++, ismain
);
545 if (lua_pushthread(L
))
551 LJLIB_CF(coroutine_create
)
554 if (!(L
->base
< L
->top
&& tvisfunc(L
->base
)))
555 lj_err_argt(L
, 1, LUA_TFUNCTION
);
556 L1
= lua_newthread(L
);
557 setfuncV(L
, L1
->top
++, funcV(L
->base
));
561 LJLIB_ASM(coroutine_yield
)
563 lj_err_caller(L
, LJ_ERR_CYIELD
);
564 return FFH_UNREACHABLE
;
567 static int ffh_resume(lua_State
*L
, lua_State
*co
, int wrap
)
569 if (co
->cframe
!= NULL
|| co
->status
> LUA_YIELD
||
570 (co
->status
== 0 && co
->top
== co
->base
)) {
571 ErrMsg em
= co
->cframe
? LJ_ERR_CORUN
: LJ_ERR_CODEAD
;
572 if (wrap
) lj_err_caller(L
, em
);
573 setboolV(L
->base
-1, 0);
574 setstrV(L
, L
->base
, lj_err_str(L
, em
));
577 lj_state_growstack(co
, (MSize
)(L
->top
- L
->base
));
581 LJLIB_ASM(coroutine_resume
)
583 if (!(L
->top
> L
->base
&& tvisthread(L
->base
)))
584 lj_err_arg(L
, 1, LJ_ERR_NOCORO
);
585 return ffh_resume(L
, threadV(L
->base
), 0);
588 LJLIB_NOREG
LJLIB_ASM(coroutine_wrap_aux
)
590 return ffh_resume(L
, threadV(lj_lib_upvalue(L
, 1)), 1);
593 /* Inline declarations. */
594 LJ_ASMF
void lj_ff_coroutine_wrap_aux(void);
595 #if !(LJ_TARGET_MIPS && defined(ljamalg_c))
596 LJ_FUNCA_NORET
void LJ_FASTCALL
lj_ffh_coroutine_wrap_err(lua_State
*L
,
600 /* Error handler, called from assembler VM. */
601 void LJ_FASTCALL
lj_ffh_coroutine_wrap_err(lua_State
*L
, lua_State
*co
)
603 co
->top
--; copyTV(L
, L
->top
, co
->top
); L
->top
++;
604 if (tvisstr(L
->top
-1))
605 lj_err_callermsg(L
, strVdata(L
->top
-1));
610 /* Forward declaration. */
611 static void setpc_wrap_aux(lua_State
*L
, GCfunc
*fn
);
613 LJLIB_CF(coroutine_wrap
)
615 lj_cf_coroutine_create(L
);
616 lj_lib_pushcc(L
, lj_ffh_coroutine_wrap_aux
, FF_coroutine_wrap_aux
, 1);
617 setpc_wrap_aux(L
, funcV(L
->top
-1));
621 #include "lj_libdef.h"
623 /* Fix the PC of wrap_aux. Really ugly workaround. */
624 static void setpc_wrap_aux(lua_State
*L
, GCfunc
*fn
)
626 setmref(fn
->c
.pc
, &L2GG(L
)->bcff
[lj_lib_init_coroutine
[1]+2]);
629 /* ------------------------------------------------------------------------ */
631 static void newproxy_weaktable(lua_State
*L
)
633 /* NOBARRIER: The table is new (marked white). */
634 GCtab
*t
= lj_tab_new(L
, 0, 1);
635 settabV(L
, L
->top
++, t
);
636 setgcref(t
->metatable
, obj2gco(t
));
637 setstrV(L
, lj_tab_setstr(L
, t
, lj_str_newlit(L
, "__mode")),
638 lj_str_newlit(L
, "kv"));
639 t
->nomm
= (uint8_t)(~(1u<<MM_mode
));
642 LUALIB_API
int luaopen_base(lua_State
*L
)
644 /* NOBARRIER: Table and value are the same. */
645 GCtab
*env
= tabref(L
->env
);
646 settabV(L
, lj_tab_setstr(L
, env
, lj_str_newlit(L
, "_G")), env
);
647 lua_pushliteral(L
, LUA_VERSION
); /* top-3. */
648 newproxy_weaktable(L
); /* top-2. */
649 LJ_LIB_REG(L
, "_G", base
);
650 LJ_LIB_REG(L
, LUA_COLIBNAME
, coroutine
);