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"
34 #include "lj_strscan.h"
37 /* -- Base library: checks ------------------------------------------------ */
39 #define LJLIB_MODULE_base
41 LJLIB_ASM(assert) LJLIB_REC(.)
44 lj_lib_checkany(L
, 1);
45 s
= lj_lib_optstr(L
, 2);
47 lj_err_callermsg(L
, strdata(s
));
49 lj_err_caller(L
, LJ_ERR_ASSERT
);
50 return FFH_UNREACHABLE
;
56 LJLIB_PUSH(top
-1) /* boolean */
57 LJLIB_PUSH("userdata")
62 LJLIB_PUSH("function")
66 LJLIB_PUSH(top
-9) /* userdata */
68 LJLIB_ASM_(type
) LJLIB_REC(.)
69 /* Recycle the lj_lib_checkany(L, 1) from assert. */
71 /* -- Base library: iterators --------------------------------------------- */
73 /* This solves a circular dependency problem -- change FF_next_N as needed. */
74 LJ_STATIC_ASSERT((int)FF_next
== FF_next_N
);
78 lj_lib_checktab(L
, 1);
79 return FFH_UNREACHABLE
;
82 #if LJ_52 || LJ_HASFFI
83 static int ffh_pairs(lua_State
*L
, MMS mm
)
85 TValue
*o
= lj_lib_checkany(L
, 1);
86 cTValue
*mo
= lj_meta_lookup(L
, o
, mm
);
87 if ((LJ_52
|| tviscdata(o
)) && !tvisnil(mo
)) {
88 L
->top
= o
+1; /* Only keep one argument. */
89 copyTV(L
, L
->base
-1, mo
); /* Replace callable. */
92 if (!tvistab(o
)) lj_err_argt(L
, 1, LUA_TTABLE
);
93 setfuncV(L
, o
-1, funcV(lj_lib_upvalue(L
, 1)));
94 if (mm
== MM_pairs
) setnilV(o
+1); else setintV(o
+1, 0);
99 #define ffh_pairs(L, mm) (lj_lib_checktab(L, 1), FFH_UNREACHABLE)
105 return ffh_pairs(L
, MM_pairs
);
108 LJLIB_NOREGUV
LJLIB_ASM(ipairs_aux
) LJLIB_REC(.)
110 lj_lib_checktab(L
, 1);
111 lj_lib_checkint(L
, 2);
112 return FFH_UNREACHABLE
;
116 LJLIB_ASM(ipairs
) LJLIB_REC(.)
118 return ffh_pairs(L
, MM_ipairs
);
121 /* -- Base library: getters and setters ----------------------------------- */
123 LJLIB_ASM_(getmetatable
) LJLIB_REC(.)
124 /* Recycle the lj_lib_checkany(L, 1) from assert. */
126 LJLIB_ASM(setmetatable
) LJLIB_REC(.)
128 GCtab
*t
= lj_lib_checktab(L
, 1);
129 GCtab
*mt
= lj_lib_checktabornil(L
, 2);
130 if (!tvisnil(lj_meta_lookup(L
, L
->base
, MM_metatable
)))
131 lj_err_caller(L
, LJ_ERR_PROTMT
);
132 setgcref(t
->metatable
, obj2gco(mt
));
133 if (mt
) { lj_gc_objbarriert(L
, t
, mt
); }
134 settabV(L
, L
->base
-1, t
);
141 cTValue
*o
= L
->base
;
142 if (!(o
< L
->top
&& tvisfunc(o
))) {
143 int level
= lj_lib_optint(L
, 1, 1);
144 o
= lj_debug_frame(L
, level
, &level
);
146 lj_err_arg(L
, 1, LJ_ERR_INVLVL
);
149 settabV(L
, L
->top
++, isluafunc(fn
) ? tabref(fn
->l
.env
) : tabref(L
->env
));
156 GCtab
*t
= lj_lib_checktab(L
, 2);
157 cTValue
*o
= L
->base
;
158 if (!(o
< L
->top
&& tvisfunc(o
))) {
159 int level
= lj_lib_checkint(L
, 1);
161 /* NOBARRIER: A thread (i.e. L) is never black. */
162 setgcref(L
->env
, obj2gco(t
));
165 o
= lj_debug_frame(L
, level
, &level
);
167 lj_err_arg(L
, 1, LJ_ERR_INVLVL
);
171 lj_err_caller(L
, LJ_ERR_SETFENV
);
172 setgcref(fn
->l
.env
, obj2gco(t
));
173 lj_gc_objbarrier(L
, obj2gco(fn
), t
);
174 setfuncV(L
, L
->top
++, fn
);
178 LJLIB_ASM(rawget
) LJLIB_REC(.)
180 lj_lib_checktab(L
, 1);
181 lj_lib_checkany(L
, 2);
182 return FFH_UNREACHABLE
;
185 LJLIB_CF(rawset
) LJLIB_REC(.)
187 lj_lib_checktab(L
, 1);
188 lj_lib_checkany(L
, 2);
189 L
->top
= 1+lj_lib_checkany(L
, 3);
194 LJLIB_CF(rawequal
) LJLIB_REC(.)
196 cTValue
*o1
= lj_lib_checkany(L
, 1);
197 cTValue
*o2
= lj_lib_checkany(L
, 2);
198 setboolV(L
->top
-1, lj_obj_equal(o1
, o2
));
203 LJLIB_CF(rawlen
) LJLIB_REC(.)
205 cTValue
*o
= L
->base
;
207 if (L
->top
> o
&& tvisstr(o
))
208 len
= (int32_t)strV(o
)->len
;
210 len
= (int32_t)lj_tab_len(lj_lib_checktab(L
, 1));
211 setintV(L
->top
-1, len
);
218 GCtab
*t
= lj_lib_checktab(L
, 1);
219 int32_t n
, i
= lj_lib_optint(L
, 2, 1);
220 int32_t e
= (L
->base
+3-1 < L
->top
&& !tvisnil(L
->base
+3-1)) ?
221 lj_lib_checkint(L
, 3) : (int32_t)lj_tab_len(t
);
224 if (n
<= 0 || !lua_checkstack(L
, n
))
225 lj_err_caller(L
, LJ_ERR_UNPACK
);
227 cTValue
*tv
= lj_tab_getint(t
, i
);
229 copyTV(L
, L
->top
++, tv
);
237 LJLIB_CF(select
) LJLIB_REC(.)
239 int32_t n
= (int32_t)(L
->top
- L
->base
);
240 if (n
>= 1 && tvisstr(L
->base
) && *strVdata(L
->base
) == '#') {
241 setintV(L
->top
-1, n
-1);
244 int32_t i
= lj_lib_checkint(L
, 1);
245 if (i
< 0) i
= n
+ i
; else if (i
> n
) i
= n
;
247 lj_err_arg(L
, 1, LJ_ERR_IDXRNG
);
252 /* -- Base library: conversions ------------------------------------------- */
254 LJLIB_ASM(tonumber
) LJLIB_REC(.)
256 int32_t base
= lj_lib_optint(L
, 2, 10);
258 TValue
*o
= lj_lib_checkany(L
, 1);
259 if (lj_strscan_numberobj(o
)) {
260 copyTV(L
, L
->base
-1, o
);
265 CTState
*cts
= ctype_cts(L
);
266 CType
*ct
= lj_ctype_rawref(cts
, cdataV(o
)->ctypeid
);
267 if (ctype_isenum(ct
->info
)) ct
= ctype_child(cts
, ct
);
268 if (ctype_isnum(ct
->info
) || ctype_iscomplex(ct
->info
)) {
269 if (LJ_DUALNUM
&& ctype_isinteger_or_bool(ct
->info
) &&
270 ct
->size
<= 4 && !(ct
->size
== 4 && (ct
->info
& CTF_UNSIGNED
))) {
272 lj_cconv_ct_tv(cts
, ctype_get(cts
, CTID_INT32
), (uint8_t *)&i
, o
, 0);
273 setintV(L
->base
-1, i
);
276 lj_cconv_ct_tv(cts
, ctype_get(cts
, CTID_DOUBLE
),
277 (uint8_t *)&(L
->base
-1)->n
, o
, 0);
283 const char *p
= strdata(lj_lib_checkstr(L
, 1));
286 if (base
< 2 || base
> 36)
287 lj_err_arg(L
, 2, LJ_ERR_BASERNG
);
288 ul
= strtoul(p
, &ep
, base
);
290 while (lj_char_isspace((unsigned char)(*ep
))) ep
++;
292 if (LJ_DUALNUM
&& LJ_LIKELY(ul
< 0x80000000u
))
293 setintV(L
->base
-1, (int32_t)ul
);
295 setnumV(L
->base
-1, (lua_Number
)ul
);
307 LJLIB_ASM(tostring
) LJLIB_REC(.)
309 TValue
*o
= lj_lib_checkany(L
, 1);
311 L
->top
= o
+1; /* Only keep one argument. */
312 if (!tvisnil(mo
= lj_meta_lookup(L
, o
, MM_tostring
))) {
313 copyTV(L
, L
->base
-1, mo
); /* Replace callable. */
318 s
= lj_str_fromnumber(L
, o
);
319 } else if (tvispri(o
)) {
320 s
= strV(lj_lib_upvalue(L
, -(int32_t)itype(o
)));
322 if (tvisfunc(o
) && isffunc(funcV(o
)))
323 lua_pushfstring(L
, "function: builtin#%d", funcV(o
)->c
.ffid
);
325 lua_pushfstring(L
, "%s: %p", lj_typename(o
), lua_topointer(L
, 1));
326 /* Note: lua_pushfstring calls the GC which may invalidate o. */
329 setstrV(L
, L
->base
-1, s
);
334 /* -- Base library: throw and catch errors -------------------------------- */
338 int32_t level
= lj_lib_optint(L
, 2, 1);
340 if (lua_isstring(L
, 1) && level
> 0) {
341 luaL_where(L
, level
);
348 LJLIB_ASM(pcall
) LJLIB_REC(.)
350 lj_lib_checkany(L
, 1);
351 lj_lib_checkfunc(L
, 2); /* For xpcall only. */
352 return FFH_UNREACHABLE
;
354 LJLIB_ASM_(xpcall
) LJLIB_REC(.)
356 /* -- Base library: load Lua code ----------------------------------------- */
358 static int load_aux(lua_State
*L
, int status
, int envarg
)
361 if (tvistab(L
->base
+envarg
-1)) {
362 GCfunc
*fn
= funcV(L
->top
-1);
363 GCtab
*t
= tabV(L
->base
+envarg
-1);
364 setgcref(fn
->c
.env
, obj2gco(t
));
365 lj_gc_objbarrier(L
, fn
, t
);
376 GCstr
*fname
= lj_lib_optstr(L
, 1);
377 GCstr
*mode
= lj_lib_optstr(L
, 2);
379 lua_settop(L
, 3); /* Ensure env arg exists. */
380 status
= luaL_loadfilex(L
, fname
? strdata(fname
) : NULL
,
381 mode
? strdata(mode
) : NULL
);
382 return load_aux(L
, status
, 3);
385 static const char *reader_func(lua_State
*L
, void *ud
, size_t *size
)
388 luaL_checkstack(L
, 2, "too many nested functions");
389 copyTV(L
, L
->top
++, L
->base
);
390 lua_call(L
, 0, 1); /* Call user-supplied function. */
392 if (tvisnil(L
->top
)) {
395 } else if (tvisstr(L
->top
) || tvisnumber(L
->top
)) {
396 copyTV(L
, L
->base
+4, L
->top
); /* Anchor string in reserved stack slot. */
397 return lua_tolstring(L
, 5, size
);
399 lj_err_caller(L
, LJ_ERR_RDRSTR
);
406 GCstr
*name
= lj_lib_optstr(L
, 2);
407 GCstr
*mode
= lj_lib_optstr(L
, 3);
409 if (L
->base
< L
->top
&& (tvisstr(L
->base
) || tvisnumber(L
->base
))) {
410 GCstr
*s
= lj_lib_checkstr(L
, 1);
411 lua_settop(L
, 4); /* Ensure env arg exists. */
412 status
= luaL_loadbufferx(L
, strdata(s
), s
->len
, strdata(name
? name
: s
),
413 mode
? strdata(mode
) : NULL
);
415 lj_lib_checkfunc(L
, 1);
416 lua_settop(L
, 5); /* Reserve a slot for the string from the reader. */
417 status
= lua_loadx(L
, reader_func
, NULL
, name
? strdata(name
) : "=(load)",
418 mode
? strdata(mode
) : NULL
);
420 return load_aux(L
, status
, 4);
425 return lj_cf_load(L
);
430 GCstr
*fname
= lj_lib_optstr(L
, 1);
433 if (luaL_loadfile(L
, fname
? strdata(fname
) : NULL
) != 0)
435 lua_call(L
, 0, LUA_MULTRET
);
436 return (int)(L
->top
- L
->base
) - 1;
439 /* -- Base library: GC control -------------------------------------------- */
443 setintV(L
->top
++, (G(L
)->gc
.total
>> 10));
447 LJLIB_CF(collectgarbage
)
449 int opt
= lj_lib_checkopt(L
, 1, LUA_GCCOLLECT
, /* ORDER LUA_GC* */
450 "\4stop\7restart\7collect\5count\1\377\4step\10setpause\12setstepmul");
451 int32_t data
= lj_lib_optint(L
, 2, 0);
452 if (opt
== LUA_GCCOUNT
) {
453 setnumV(L
->top
, (lua_Number
)G(L
)->gc
.total
/1024.0);
455 int res
= lua_gc(L
, opt
, data
);
456 if (opt
== LUA_GCSTEP
)
457 setboolV(L
->top
, res
);
459 setintV(L
->top
, res
);
465 /* -- Base library: miscellaneous functions ------------------------------- */
467 LJLIB_PUSH(top
-2) /* Upvalue holds weak table. */
471 lua_newuserdata(L
, 0);
472 if (lua_toboolean(L
, 1) == 0) { /* newproxy(): without metatable. */
474 } else if (lua_isboolean(L
, 1)) { /* newproxy(true): with metatable. */
476 lua_pushvalue(L
, -1);
477 lua_pushboolean(L
, 1);
478 lua_rawset(L
, lua_upvalueindex(1)); /* Remember mt in weak table. */
479 } else { /* newproxy(proxy): inherit metatable. */
481 if (lua_getmetatable(L
, 1)) {
482 lua_rawget(L
, lua_upvalueindex(1));
483 validproxy
= lua_toboolean(L
, -1);
487 lj_err_arg(L
, 1, LJ_ERR_NOPROXY
);
488 lua_getmetatable(L
, 1);
490 lua_setmetatable(L
, 2);
494 LJLIB_PUSH("tostring")
497 ptrdiff_t i
, nargs
= L
->top
- L
->base
;
498 cTValue
*tv
= lj_tab_getstr(tabref(L
->env
), strV(lj_lib_upvalue(L
, 1)));
500 if (tv
&& !tvisnil(tv
)) {
501 copyTV(L
, L
->top
++, tv
);
503 setstrV(L
, L
->top
++, strV(lj_lib_upvalue(L
, 1)));
504 lua_gettable(L
, LUA_GLOBALSINDEX
);
507 shortcut
= (tvisfunc(tv
) && funcV(tv
)->c
.ffid
== FF_tostring
);
508 for (i
= 0; i
< nargs
; i
++) {
511 cTValue
*o
= &L
->base
[i
];
512 if (shortcut
&& tvisstr(o
)) {
515 } else if (shortcut
&& tvisint(o
)) {
516 char buf
[LJ_STR_INTBUF
];
517 char *p
= lj_str_bufint(buf
, intV(o
));
518 size
= (size_t)(buf
+LJ_STR_INTBUF
-p
);
520 } else if (shortcut
&& tvisnum(o
)) {
521 char buf
[LJ_STR_NUMBUF
];
522 size
= lj_str_bufnum(buf
, o
);
525 copyTV(L
, L
->top
+1, o
);
526 copyTV(L
, L
->top
, L
->top
-1);
529 str
= lua_tolstring(L
, -1, &size
);
531 lj_err_caller(L
, LJ_ERR_PRTOSTR
);
536 fwrite(str
, 1, size
, stdout
);
545 #include "lj_libdef.h"
547 /* -- Coroutine library --------------------------------------------------- */
549 #define LJLIB_MODULE_coroutine
551 LJLIB_CF(coroutine_status
)
555 if (!(L
->top
> L
->base
&& tvisthread(L
->base
)))
556 lj_err_arg(L
, 1, LJ_ERR_NOCORO
);
557 co
= threadV(L
->base
);
558 if (co
== L
) s
= "running";
559 else if (co
->status
== LUA_YIELD
) s
= "suspended";
560 else if (co
->status
!= 0) s
= "dead";
561 else if (co
->base
> tvref(co
->stack
)+1) s
= "normal";
562 else if (co
->top
== co
->base
) s
= "dead";
563 else s
= "suspended";
564 lua_pushstring(L
, s
);
568 LJLIB_CF(coroutine_running
)
571 int ismain
= lua_pushthread(L
);
572 setboolV(L
->top
++, ismain
);
575 if (lua_pushthread(L
))
581 LJLIB_CF(coroutine_create
)
584 if (!(L
->base
< L
->top
&& tvisfunc(L
->base
)))
585 lj_err_argt(L
, 1, LUA_TFUNCTION
);
586 L1
= lua_newthread(L
);
587 setfuncV(L
, L1
->top
++, funcV(L
->base
));
591 LJLIB_ASM(coroutine_yield
)
593 lj_err_caller(L
, LJ_ERR_CYIELD
);
594 return FFH_UNREACHABLE
;
597 static int ffh_resume(lua_State
*L
, lua_State
*co
, int wrap
)
599 if (co
->cframe
!= NULL
|| co
->status
> LUA_YIELD
||
600 (co
->status
== 0 && co
->top
== co
->base
)) {
601 ErrMsg em
= co
->cframe
? LJ_ERR_CORUN
: LJ_ERR_CODEAD
;
602 if (wrap
) lj_err_caller(L
, em
);
603 setboolV(L
->base
-1, 0);
604 setstrV(L
, L
->base
, lj_err_str(L
, em
));
607 lj_state_growstack(co
, (MSize
)(L
->top
- L
->base
));
611 LJLIB_ASM(coroutine_resume
)
613 if (!(L
->top
> L
->base
&& tvisthread(L
->base
)))
614 lj_err_arg(L
, 1, LJ_ERR_NOCORO
);
615 return ffh_resume(L
, threadV(L
->base
), 0);
618 LJLIB_NOREG
LJLIB_ASM(coroutine_wrap_aux
)
620 return ffh_resume(L
, threadV(lj_lib_upvalue(L
, 1)), 1);
623 /* Inline declarations. */
624 LJ_ASMF
void lj_ff_coroutine_wrap_aux(void);
625 #if !(LJ_TARGET_MIPS && defined(ljamalg_c))
626 LJ_FUNCA_NORET
void LJ_FASTCALL
lj_ffh_coroutine_wrap_err(lua_State
*L
,
630 /* Error handler, called from assembler VM. */
631 void LJ_FASTCALL
lj_ffh_coroutine_wrap_err(lua_State
*L
, lua_State
*co
)
633 co
->top
--; copyTV(L
, L
->top
, co
->top
); L
->top
++;
634 if (tvisstr(L
->top
-1))
635 lj_err_callermsg(L
, strVdata(L
->top
-1));
640 /* Forward declaration. */
641 static void setpc_wrap_aux(lua_State
*L
, GCfunc
*fn
);
643 LJLIB_CF(coroutine_wrap
)
645 lj_cf_coroutine_create(L
);
646 lj_lib_pushcc(L
, lj_ffh_coroutine_wrap_aux
, FF_coroutine_wrap_aux
, 1);
647 setpc_wrap_aux(L
, funcV(L
->top
-1));
651 #include "lj_libdef.h"
653 /* Fix the PC of wrap_aux. Really ugly workaround. */
654 static void setpc_wrap_aux(lua_State
*L
, GCfunc
*fn
)
656 setmref(fn
->c
.pc
, &L2GG(L
)->bcff
[lj_lib_init_coroutine
[1]+2]);
659 /* ------------------------------------------------------------------------ */
661 static void newproxy_weaktable(lua_State
*L
)
663 /* NOBARRIER: The table is new (marked white). */
664 GCtab
*t
= lj_tab_new(L
, 0, 1);
665 settabV(L
, L
->top
++, t
);
666 setgcref(t
->metatable
, obj2gco(t
));
667 setstrV(L
, lj_tab_setstr(L
, t
, lj_str_newlit(L
, "__mode")),
668 lj_str_newlit(L
, "kv"));
669 t
->nomm
= (uint8_t)(~(1u<<MM_mode
));
672 LUALIB_API
int luaopen_base(lua_State
*L
)
674 /* NOBARRIER: Table and value are the same. */
675 GCtab
*env
= tabref(L
->env
);
676 settabV(L
, lj_tab_setstr(L
, env
, lj_str_newlit(L
, "_G")), env
);
677 lua_pushliteral(L
, LUA_VERSION
); /* top-3. */
678 newproxy_weaktable(L
); /* top-2. */
679 LJ_LIB_REG(L
, "_G", base
);
680 LJ_LIB_REG(L
, LUA_COLIBNAME
, coroutine
);