2 ** Base and coroutine library.
3 ** Copyright (C) 2005-2009 Mike Pall. See Copyright Notice in luajit.h
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
29 /* -- Base library: checks ------------------------------------------------ */
31 #define LJLIB_MODULE_base
33 LJLIB_ASM(assert) LJLIB_REC(.)
36 lj_lib_checkany(L
, 1);
37 s
= lj_lib_optstr(L
, 2);
39 lj_err_callermsg(L
, strdata(s
));
41 lj_err_caller(L
, LJ_ERR_ASSERT
);
42 return FFH_UNREACHABLE
;
48 LJLIB_PUSH(top
-1) /* boolean */
49 LJLIB_PUSH("userdata")
54 LJLIB_PUSH("function")
57 LJLIB_PUSH(top
-8) /* userdata */
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
);
83 if (!(o
< L
->top
&& tvisfunc(o
))) {
84 int level
= lj_lib_optint(L
, 1, 1);
85 o
= lj_err_getframe(L
, level
, &level
);
87 lj_err_arg(L
, 1, LJ_ERR_INVLVL
);
90 settabV(L
, L
->top
++, isluafunc(fn
) ? tabref(fn
->l
.env
) : tabref(L
->env
));
97 GCtab
*t
= lj_lib_checktab(L
, 2);
99 if (!(o
< L
->top
&& tvisfunc(o
))) {
100 int level
= lj_lib_checkint(L
, 1);
102 /* NOBARRIER: A thread (i.e. L) is never black. */
103 setgcref(L
->env
, obj2gco(t
));
106 o
= lj_err_getframe(L
, level
, &level
);
108 lj_err_arg(L
, 1, LJ_ERR_INVLVL
);
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
);
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);
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
));
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
);
151 if (n
<= 0 || !lua_checkstack(L
, n
))
152 lj_err_caller(L
, LJ_ERR_UNPACK
);
154 cTValue
*tv
= lj_tab_getint(t
, i
);
156 copyTV(L
, L
->top
++, tv
);
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);
171 int32_t i
= lj_lib_checkint(L
, 1);
172 if (i
< 0) i
= n
+ i
; else if (i
> n
) i
= n
;
174 lj_err_arg(L
, 1, LJ_ERR_IDXRNG
);
179 /* -- Base library: conversions ------------------------------------------- */
181 LJLIB_ASM(tonumber
) LJLIB_REC(.)
183 int32_t base
= lj_lib_optint(L
, 2, 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
));
191 const char *p
= strdata(lj_lib_checkstr(L
, 1));
194 if (base
< 2 || base
> 36)
195 lj_err_arg(L
, 2, LJ_ERR_BASERNG
);
196 ul
= strtoul(p
, &ep
, base
);
198 while (lj_ctype_isspace((unsigned char)(*ep
))) ep
++;
200 setnumV(L
->base
-1, cast_num(ul
));
212 LJLIB_ASM(tostring
) LJLIB_REC(.)
214 TValue
*o
= lj_lib_checkany(L
, 1);
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. */
223 s
= lj_str_fromnum(L
, &o
->n
);
224 } else if (tvispri(o
)) {
225 s
= strV(lj_lib_upvalue(L
, -itype(o
)));
227 if (tvisfunc(o
) && isffunc(funcV(o
)))
228 lua_pushfstring(L
, "function: fast#%d", funcV(o
)->c
.ffid
);
230 lua_pushfstring(L
, "%s: %p", typename(o
), lua_topointer(L
, 1));
231 /* Note: lua_pushfstring calls the GC which may invalidate o. */
234 setstrV(L
, L
->base
-1, s
);
239 /* -- Base library: iterators --------------------------------------------- */
243 lj_lib_checktab(L
, 1);
244 lj_lib_checknum(L
, 2); /* For ipairs_aux. */
245 return FFH_UNREACHABLE
;
251 LJLIB_NOREGUV
LJLIB_ASM_(ipairs_aux
) LJLIB_REC(.)
254 LJLIB_ASM_(ipairs
) LJLIB_REC(.)
256 /* -- Base library: throw and catch errors -------------------------------- */
260 int32_t level
= lj_lib_optint(L
, 2, 1);
262 if (lua_isstring(L
, 1) && level
> 0) {
263 luaL_where(L
, level
);
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
)
284 copyTV(L
, L
->top
, L
->top
-1);
292 GCstr
*s
= lj_lib_checkstr(L
, 1);
293 GCstr
*name
= lj_lib_optstr(L
, 2);
295 luaL_loadbuffer(L
, strdata(s
), s
->len
, strdata(name
? name
: s
)));
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
)
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. */
311 if (tvisnil(L
->top
)) {
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
);
318 lj_err_caller(L
, LJ_ERR_RDRSTR
);
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. */
329 lua_load(L
, reader_func
, NULL
, name
? strdata(name
) : "=(load)"));
334 GCstr
*fname
= lj_lib_optstr(L
, 1);
337 if (luaL_loadfile(L
, fname
? strdata(fname
) : NULL
) != 0)
339 lua_call(L
, 0, LUA_MULTRET
);
340 return (L
->top
- L
->base
) - 1;
343 /* -- Base library: GC control -------------------------------------------- */
347 setintV(L
->top
++, (G(L
)->gc
.total
>> 10));
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);
359 int res
= lua_gc(L
, opt
, data
);
360 if (opt
== LUA_GCSTEP
)
361 setboolV(L
->top
, res
);
363 setintV(L
->top
, res
);
369 /* -- Base library: miscellaneous functions ------------------------------- */
371 LJLIB_PUSH(top
-2) /* Upvalue holds weak table. */
375 lua_newuserdata(L
, 0);
376 if (lua_toboolean(L
, 1) == 0) { /* newproxy(): without metatable. */
378 } else if (lua_isboolean(L
, 1)) { /* newproxy(true): with metatable. */
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. */
385 if (lua_getmetatable(L
, 1)) {
386 lua_rawget(L
, lua_upvalueindex(1));
387 validproxy
= lua_toboolean(L
, -1);
391 lj_err_arg(L
, 1, LJ_ERR_NOPROXY
);
392 lua_getmetatable(L
, 1);
394 lua_setmetatable(L
, 2);
398 LJLIB_PUSH("tostring")
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
++) {
408 cTValue
*o
= &L
->base
[i
];
409 if (shortcut
&& tvisstr(o
)) {
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
);
418 copyTV(L
, L
->top
+1, o
);
419 copyTV(L
, L
->top
, L
->top
-1);
422 str
= lua_tolstring(L
, -1, &size
);
424 lj_err_caller(L
, LJ_ERR_PRTOSTR
);
429 fwrite(str
, 1, size
, stdout
);
438 #include "lj_libdef.h"
440 /* -- Coroutine library --------------------------------------------------- */
442 #define LJLIB_MODULE_coroutine
444 LJLIB_CF(coroutine_status
)
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
);
461 LJLIB_CF(coroutine_running
)
463 if (lua_pushthread(L
))
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
));
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
));
493 lj_state_growstack(co
, (MSize
)(L
->top
- L
->base
- 1));
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
,
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));
524 LJLIB_CF(coroutine_wrap
)
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
;
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
);