2009-12-08 18:46:35 +00:00
|
|
|
/*
|
|
|
|
** Base and coroutine library.
|
2010-01-09 13:28:11 +00:00
|
|
|
** Copyright (C) 2005-2010 Mike Pall. See Copyright Notice in luajit.h
|
2009-12-08 18:46:35 +00:00
|
|
|
**
|
|
|
|
** Major portions taken verbatim or adapted from the Lua interpreter.
|
|
|
|
** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#define lib_base_c
|
|
|
|
#define LUA_LIB
|
|
|
|
|
|
|
|
#include "lua.h"
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "lualib.h"
|
|
|
|
|
|
|
|
#include "lj_obj.h"
|
|
|
|
#include "lj_gc.h"
|
|
|
|
#include "lj_err.h"
|
|
|
|
#include "lj_str.h"
|
|
|
|
#include "lj_tab.h"
|
|
|
|
#include "lj_meta.h"
|
|
|
|
#include "lj_state.h"
|
2010-02-13 03:51:56 +00:00
|
|
|
#include "lj_bc.h"
|
2009-12-08 18:46:35 +00:00
|
|
|
#include "lj_ff.h"
|
2010-02-13 03:51:56 +00:00
|
|
|
#include "lj_dispatch.h"
|
2009-12-08 18:46:35 +00:00
|
|
|
#include "lj_ctype.h"
|
|
|
|
#include "lj_lib.h"
|
|
|
|
|
|
|
|
/* -- Base library: checks ------------------------------------------------ */
|
|
|
|
|
|
|
|
#define LJLIB_MODULE_base
|
|
|
|
|
|
|
|
LJLIB_ASM(assert) LJLIB_REC(.)
|
|
|
|
{
|
|
|
|
GCstr *s;
|
|
|
|
lj_lib_checkany(L, 1);
|
|
|
|
s = lj_lib_optstr(L, 2);
|
|
|
|
if (s)
|
|
|
|
lj_err_callermsg(L, strdata(s));
|
|
|
|
else
|
|
|
|
lj_err_caller(L, LJ_ERR_ASSERT);
|
|
|
|
return FFH_UNREACHABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ORDER LJ_T */
|
|
|
|
LJLIB_PUSH("nil")
|
|
|
|
LJLIB_PUSH("boolean")
|
|
|
|
LJLIB_PUSH(top-1) /* boolean */
|
|
|
|
LJLIB_PUSH("userdata")
|
|
|
|
LJLIB_PUSH("string")
|
|
|
|
LJLIB_PUSH("upval")
|
|
|
|
LJLIB_PUSH("thread")
|
|
|
|
LJLIB_PUSH("proto")
|
|
|
|
LJLIB_PUSH("function")
|
2010-04-25 01:32:29 +00:00
|
|
|
LJLIB_PUSH("trace")
|
2009-12-08 18:46:35 +00:00
|
|
|
LJLIB_PUSH("table")
|
|
|
|
LJLIB_PUSH(top-8) /* userdata */
|
|
|
|
LJLIB_PUSH("number")
|
|
|
|
LJLIB_ASM_(type) LJLIB_REC(.)
|
|
|
|
/* Recycle the lj_lib_checkany(L, 1) from assert. */
|
|
|
|
|
|
|
|
/* -- Base library: getters and setters ----------------------------------- */
|
|
|
|
|
|
|
|
LJLIB_ASM_(getmetatable) LJLIB_REC(.)
|
|
|
|
/* Recycle the lj_lib_checkany(L, 1) from assert. */
|
|
|
|
|
|
|
|
LJLIB_ASM(setmetatable) LJLIB_REC(.)
|
|
|
|
{
|
|
|
|
GCtab *t = lj_lib_checktab(L, 1);
|
|
|
|
GCtab *mt = lj_lib_checktabornil(L, 2);
|
|
|
|
if (!tvisnil(lj_meta_lookup(L, L->base, MM_metatable)))
|
|
|
|
lj_err_caller(L, LJ_ERR_PROTMT);
|
|
|
|
setgcref(t->metatable, obj2gco(mt));
|
|
|
|
if (mt) { lj_gc_objbarriert(L, t, mt); }
|
|
|
|
settabV(L, L->base-1, t);
|
|
|
|
return FFH_RES(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
LJLIB_CF(getfenv)
|
|
|
|
{
|
|
|
|
GCfunc *fn;
|
|
|
|
cTValue *o = L->base;
|
|
|
|
if (!(o < L->top && tvisfunc(o))) {
|
|
|
|
int level = lj_lib_optint(L, 1, 1);
|
|
|
|
o = lj_err_getframe(L, level, &level);
|
|
|
|
if (o == NULL)
|
|
|
|
lj_err_arg(L, 1, LJ_ERR_INVLVL);
|
|
|
|
}
|
|
|
|
fn = &gcval(o)->fn;
|
|
|
|
settabV(L, L->top++, isluafunc(fn) ? tabref(fn->l.env) : tabref(L->env));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
LJLIB_CF(setfenv)
|
|
|
|
{
|
|
|
|
GCfunc *fn;
|
|
|
|
GCtab *t = lj_lib_checktab(L, 2);
|
|
|
|
cTValue *o = L->base;
|
|
|
|
if (!(o < L->top && tvisfunc(o))) {
|
|
|
|
int level = lj_lib_checkint(L, 1);
|
|
|
|
if (level == 0) {
|
|
|
|
/* NOBARRIER: A thread (i.e. L) is never black. */
|
|
|
|
setgcref(L->env, obj2gco(t));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
o = lj_err_getframe(L, level, &level);
|
|
|
|
if (o == NULL)
|
|
|
|
lj_err_arg(L, 1, LJ_ERR_INVLVL);
|
|
|
|
}
|
|
|
|
fn = &gcval(o)->fn;
|
|
|
|
if (!isluafunc(fn))
|
|
|
|
lj_err_caller(L, LJ_ERR_SETFENV);
|
|
|
|
setgcref(fn->l.env, obj2gco(t));
|
|
|
|
lj_gc_objbarrier(L, obj2gco(fn), t);
|
|
|
|
setfuncV(L, L->top++, fn);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
LJLIB_ASM(rawget) LJLIB_REC(.)
|
|
|
|
{
|
|
|
|
lj_lib_checktab(L, 1);
|
|
|
|
lj_lib_checkany(L, 2);
|
|
|
|
return FFH_UNREACHABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
LJLIB_CF(rawset) LJLIB_REC(.)
|
|
|
|
{
|
|
|
|
lj_lib_checktab(L, 1);
|
|
|
|
lj_lib_checkany(L, 2);
|
|
|
|
L->top = 1+lj_lib_checkany(L, 3);
|
|
|
|
lua_rawset(L, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
LJLIB_CF(rawequal) LJLIB_REC(.)
|
|
|
|
{
|
|
|
|
cTValue *o1 = lj_lib_checkany(L, 1);
|
|
|
|
cTValue *o2 = lj_lib_checkany(L, 2);
|
|
|
|
setboolV(L->top-1, lj_obj_equal(o1, o2));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
LJLIB_CF(unpack)
|
|
|
|
{
|
|
|
|
GCtab *t = lj_lib_checktab(L, 1);
|
|
|
|
int32_t n, i = lj_lib_optint(L, 2, 1);
|
|
|
|
int32_t e = (L->base+3-1 < L->top && !tvisnil(L->base+3-1)) ?
|
|
|
|
lj_lib_checkint(L, 3) : (int32_t)lj_tab_len(t);
|
|
|
|
if (i > e) return 0;
|
|
|
|
n = e - i + 1;
|
|
|
|
if (n <= 0 || !lua_checkstack(L, n))
|
|
|
|
lj_err_caller(L, LJ_ERR_UNPACK);
|
|
|
|
do {
|
|
|
|
cTValue *tv = lj_tab_getint(t, i);
|
|
|
|
if (tv) {
|
|
|
|
copyTV(L, L->top++, tv);
|
|
|
|
} else {
|
|
|
|
setnilV(L->top++);
|
|
|
|
}
|
|
|
|
} while (i++ < e);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2010-09-12 23:21:44 +00:00
|
|
|
LJLIB_CF(select) LJLIB_REC(.)
|
2009-12-08 18:46:35 +00:00
|
|
|
{
|
|
|
|
int32_t n = (int32_t)(L->top - L->base);
|
|
|
|
if (n >= 1 && tvisstr(L->base) && *strVdata(L->base) == '#') {
|
|
|
|
setintV(L->top-1, n-1);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
int32_t i = lj_lib_checkint(L, 1);
|
|
|
|
if (i < 0) i = n + i; else if (i > n) i = n;
|
|
|
|
if (i < 1)
|
|
|
|
lj_err_arg(L, 1, LJ_ERR_IDXRNG);
|
|
|
|
return n - i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -- Base library: conversions ------------------------------------------- */
|
|
|
|
|
|
|
|
LJLIB_ASM(tonumber) LJLIB_REC(.)
|
|
|
|
{
|
|
|
|
int32_t base = lj_lib_optint(L, 2, 10);
|
|
|
|
if (base == 10) {
|
|
|
|
TValue *o = lj_lib_checkany(L, 1);
|
2009-12-08 19:35:29 +00:00
|
|
|
if (tvisnum(o) || (tvisstr(o) && lj_str_tonum(strV(o), o))) {
|
2009-12-08 18:46:35 +00:00
|
|
|
setnumV(L->base-1, numV(o));
|
|
|
|
return FFH_RES(1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const char *p = strdata(lj_lib_checkstr(L, 1));
|
|
|
|
char *ep;
|
|
|
|
unsigned long ul;
|
|
|
|
if (base < 2 || base > 36)
|
|
|
|
lj_err_arg(L, 2, LJ_ERR_BASERNG);
|
|
|
|
ul = strtoul(p, &ep, base);
|
|
|
|
if (p != ep) {
|
|
|
|
while (lj_ctype_isspace((unsigned char)(*ep))) ep++;
|
|
|
|
if (*ep == '\0') {
|
|
|
|
setnumV(L->base-1, cast_num(ul));
|
|
|
|
return FFH_RES(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setnilV(L->base-1);
|
|
|
|
return FFH_RES(1);
|
|
|
|
}
|
|
|
|
|
2009-12-08 19:35:29 +00:00
|
|
|
LJLIB_PUSH("nil")
|
|
|
|
LJLIB_PUSH("false")
|
|
|
|
LJLIB_PUSH("true")
|
2009-12-08 18:46:35 +00:00
|
|
|
LJLIB_ASM(tostring) LJLIB_REC(.)
|
|
|
|
{
|
|
|
|
TValue *o = lj_lib_checkany(L, 1);
|
|
|
|
cTValue *mo;
|
|
|
|
L->top = o+1; /* Only keep one argument. */
|
|
|
|
if (!tvisnil(mo = lj_meta_lookup(L, o, MM_tostring))) {
|
|
|
|
copyTV(L, L->base-1, mo); /* Replace callable. */
|
2010-09-02 15:16:56 +00:00
|
|
|
return FFH_TAILCALL;
|
2009-12-08 18:46:35 +00:00
|
|
|
} else {
|
|
|
|
GCstr *s;
|
|
|
|
if (tvisnum(o)) {
|
|
|
|
s = lj_str_fromnum(L, &o->n);
|
2009-12-08 19:35:29 +00:00
|
|
|
} else if (tvispri(o)) {
|
2010-04-25 21:21:15 +00:00
|
|
|
s = strV(lj_lib_upvalue(L, -(int32_t)itype(o)));
|
2009-12-08 18:46:35 +00:00
|
|
|
} else {
|
|
|
|
if (tvisfunc(o) && isffunc(funcV(o)))
|
|
|
|
lua_pushfstring(L, "function: fast#%d", funcV(o)->c.ffid);
|
|
|
|
else
|
|
|
|
lua_pushfstring(L, "%s: %p", typename(o), lua_topointer(L, 1));
|
|
|
|
/* Note: lua_pushfstring calls the GC which may invalidate o. */
|
|
|
|
s = strV(L->top-1);
|
|
|
|
}
|
|
|
|
setstrV(L, L->base-1, s);
|
|
|
|
return FFH_RES(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -- Base library: iterators --------------------------------------------- */
|
|
|
|
|
|
|
|
LJLIB_ASM(next)
|
|
|
|
{
|
|
|
|
lj_lib_checktab(L, 1);
|
|
|
|
lj_lib_checknum(L, 2); /* For ipairs_aux. */
|
|
|
|
return FFH_UNREACHABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
LJLIB_PUSH(lastcl)
|
|
|
|
LJLIB_ASM_(pairs)
|
|
|
|
|
|
|
|
LJLIB_NOREGUV LJLIB_ASM_(ipairs_aux) LJLIB_REC(.)
|
|
|
|
|
|
|
|
LJLIB_PUSH(lastcl)
|
|
|
|
LJLIB_ASM_(ipairs) LJLIB_REC(.)
|
|
|
|
|
|
|
|
/* -- Base library: throw and catch errors -------------------------------- */
|
|
|
|
|
|
|
|
LJLIB_CF(error)
|
|
|
|
{
|
|
|
|
int32_t level = lj_lib_optint(L, 2, 1);
|
|
|
|
lua_settop(L, 1);
|
|
|
|
if (lua_isstring(L, 1) && level > 0) {
|
|
|
|
luaL_where(L, level);
|
|
|
|
lua_pushvalue(L, 1);
|
|
|
|
lua_concat(L, 2);
|
|
|
|
}
|
|
|
|
return lua_error(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
LJLIB_ASM(pcall) LJLIB_REC(.)
|
|
|
|
{
|
|
|
|
lj_lib_checkany(L, 1);
|
|
|
|
lj_lib_checkfunc(L, 2); /* For xpcall only. */
|
|
|
|
return FFH_UNREACHABLE;
|
|
|
|
}
|
|
|
|
LJLIB_ASM_(xpcall) LJLIB_REC(.)
|
|
|
|
|
|
|
|
/* -- Base library: load Lua code ----------------------------------------- */
|
|
|
|
|
|
|
|
static int load_aux(lua_State *L, int status)
|
|
|
|
{
|
|
|
|
if (status == 0)
|
|
|
|
return 1;
|
|
|
|
copyTV(L, L->top, L->top-1);
|
|
|
|
setnilV(L->top-1);
|
|
|
|
L->top++;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
LJLIB_CF(loadstring)
|
|
|
|
{
|
|
|
|
GCstr *s = lj_lib_checkstr(L, 1);
|
|
|
|
GCstr *name = lj_lib_optstr(L, 2);
|
|
|
|
return load_aux(L,
|
|
|
|
luaL_loadbuffer(L, strdata(s), s->len, strdata(name ? name : s)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LJLIB_CF(loadfile)
|
|
|
|
{
|
|
|
|
GCstr *fname = lj_lib_optstr(L, 1);
|
|
|
|
return load_aux(L, luaL_loadfile(L, fname ? strdata(fname) : NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *reader_func(lua_State *L, void *ud, size_t *size)
|
|
|
|
{
|
|
|
|
UNUSED(ud);
|
|
|
|
luaL_checkstack(L, 2, "too many nested functions");
|
|
|
|
copyTV(L, L->top++, L->base);
|
|
|
|
lua_call(L, 0, 1); /* Call user-supplied function. */
|
|
|
|
L->top--;
|
|
|
|
if (tvisnil(L->top)) {
|
|
|
|
*size = 0;
|
|
|
|
return NULL;
|
|
|
|
} else if (tvisstr(L->top) || tvisnum(L->top)) {
|
|
|
|
copyTV(L, L->base+2, L->top); /* Anchor string in reserved stack slot. */
|
|
|
|
return lua_tolstring(L, 3, size);
|
|
|
|
} else {
|
|
|
|
lj_err_caller(L, LJ_ERR_RDRSTR);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LJLIB_CF(load)
|
|
|
|
{
|
|
|
|
GCstr *name = lj_lib_optstr(L, 2);
|
|
|
|
lj_lib_checkfunc(L, 1);
|
|
|
|
lua_settop(L, 3); /* Reserve a slot for the string from the reader. */
|
|
|
|
return load_aux(L,
|
|
|
|
lua_load(L, reader_func, NULL, name ? strdata(name) : "=(load)"));
|
|
|
|
}
|
|
|
|
|
|
|
|
LJLIB_CF(dofile)
|
|
|
|
{
|
|
|
|
GCstr *fname = lj_lib_optstr(L, 1);
|
|
|
|
setnilV(L->top);
|
|
|
|
L->top = L->base+1;
|
|
|
|
if (luaL_loadfile(L, fname ? strdata(fname) : NULL) != 0)
|
|
|
|
lua_error(L);
|
|
|
|
lua_call(L, 0, LUA_MULTRET);
|
2009-12-29 19:19:54 +00:00
|
|
|
return cast_int(L->top - L->base) - 1;
|
2009-12-08 18:46:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -- Base library: GC control -------------------------------------------- */
|
|
|
|
|
|
|
|
LJLIB_CF(gcinfo)
|
|
|
|
{
|
|
|
|
setintV(L->top++, (G(L)->gc.total >> 10));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
LJLIB_CF(collectgarbage)
|
|
|
|
{
|
|
|
|
int opt = lj_lib_checkopt(L, 1, LUA_GCCOLLECT, /* ORDER LUA_GC* */
|
|
|
|
"\4stop\7restart\7collect\5count\1\377\4step\10setpause\12setstepmul");
|
|
|
|
int32_t data = lj_lib_optint(L, 2, 0);
|
|
|
|
if (opt == LUA_GCCOUNT) {
|
2009-12-29 01:34:15 +00:00
|
|
|
setnumV(L->top, cast_num((int32_t)G(L)->gc.total)/1024.0);
|
2009-12-08 18:46:35 +00:00
|
|
|
} else {
|
|
|
|
int res = lua_gc(L, opt, data);
|
|
|
|
if (opt == LUA_GCSTEP)
|
2009-12-29 01:34:15 +00:00
|
|
|
setboolV(L->top, res);
|
2009-12-08 18:46:35 +00:00
|
|
|
else
|
2009-12-29 01:34:15 +00:00
|
|
|
setintV(L->top, res);
|
2009-12-08 18:46:35 +00:00
|
|
|
}
|
2009-12-29 01:34:15 +00:00
|
|
|
L->top++;
|
2009-12-08 18:46:35 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -- Base library: miscellaneous functions ------------------------------- */
|
|
|
|
|
|
|
|
LJLIB_PUSH(top-2) /* Upvalue holds weak table. */
|
|
|
|
LJLIB_CF(newproxy)
|
|
|
|
{
|
|
|
|
lua_settop(L, 1);
|
|
|
|
lua_newuserdata(L, 0);
|
|
|
|
if (lua_toboolean(L, 1) == 0) { /* newproxy(): without metatable. */
|
|
|
|
return 1;
|
|
|
|
} else if (lua_isboolean(L, 1)) { /* newproxy(true): with metatable. */
|
|
|
|
lua_newtable(L);
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
lua_pushboolean(L, 1);
|
|
|
|
lua_rawset(L, lua_upvalueindex(1)); /* Remember mt in weak table. */
|
|
|
|
} else { /* newproxy(proxy): inherit metatable. */
|
|
|
|
int validproxy = 0;
|
|
|
|
if (lua_getmetatable(L, 1)) {
|
|
|
|
lua_rawget(L, lua_upvalueindex(1));
|
|
|
|
validproxy = lua_toboolean(L, -1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
if (!validproxy)
|
|
|
|
lj_err_arg(L, 1, LJ_ERR_NOPROXY);
|
|
|
|
lua_getmetatable(L, 1);
|
|
|
|
}
|
|
|
|
lua_setmetatable(L, 2);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
LJLIB_PUSH("tostring")
|
|
|
|
LJLIB_CF(print)
|
|
|
|
{
|
|
|
|
ptrdiff_t i, nargs = L->top - L->base;
|
|
|
|
cTValue *tv = lj_tab_getstr(tabref(L->env), strV(lj_lib_upvalue(L, 1)));
|
2010-06-16 17:43:07 +00:00
|
|
|
int shortcut;
|
|
|
|
if (tv && !tvisnil(tv)) {
|
|
|
|
copyTV(L, L->top++, tv);
|
|
|
|
} else {
|
|
|
|
setstrV(L, L->top++, strV(lj_lib_upvalue(L, 1)));
|
|
|
|
lua_gettable(L, LUA_GLOBALSINDEX);
|
|
|
|
tv = L->top-1;
|
|
|
|
}
|
|
|
|
shortcut = (tvisfunc(tv) && funcV(tv)->c.ffid == FF_tostring);
|
2009-12-08 18:46:35 +00:00
|
|
|
for (i = 0; i < nargs; i++) {
|
|
|
|
const char *str;
|
|
|
|
size_t size;
|
|
|
|
cTValue *o = &L->base[i];
|
|
|
|
if (shortcut && tvisstr(o)) {
|
|
|
|
str = strVdata(o);
|
|
|
|
size = strV(o)->len;
|
|
|
|
} else if (shortcut && tvisnum(o)) {
|
|
|
|
char buf[LUAI_MAXNUMBER2STR];
|
2010-05-19 22:40:51 +00:00
|
|
|
size = lj_str_bufnum(buf, o);
|
2009-12-08 18:46:35 +00:00
|
|
|
str = buf;
|
|
|
|
} else {
|
|
|
|
copyTV(L, L->top+1, o);
|
|
|
|
copyTV(L, L->top, L->top-1);
|
|
|
|
L->top += 2;
|
|
|
|
lua_call(L, 1, 1);
|
|
|
|
str = lua_tolstring(L, -1, &size);
|
|
|
|
if (!str)
|
|
|
|
lj_err_caller(L, LJ_ERR_PRTOSTR);
|
|
|
|
L->top--;
|
|
|
|
}
|
|
|
|
if (i)
|
|
|
|
putchar('\t');
|
|
|
|
fwrite(str, 1, size, stdout);
|
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
LJLIB_PUSH(top-3)
|
|
|
|
LJLIB_SET(_VERSION)
|
|
|
|
|
|
|
|
#include "lj_libdef.h"
|
|
|
|
|
|
|
|
/* -- Coroutine library --------------------------------------------------- */
|
|
|
|
|
|
|
|
#define LJLIB_MODULE_coroutine
|
|
|
|
|
|
|
|
LJLIB_CF(coroutine_status)
|
|
|
|
{
|
|
|
|
const char *s;
|
|
|
|
lua_State *co;
|
|
|
|
if (!(L->top > L->base && tvisthread(L->base)))
|
|
|
|
lj_err_arg(L, 1, LJ_ERR_NOCORO);
|
|
|
|
co = threadV(L->base);
|
|
|
|
if (co == L) s = "running";
|
|
|
|
else if (co->status == LUA_YIELD) s = "suspended";
|
|
|
|
else if (co->status != 0) s = "dead";
|
2010-09-09 10:28:17 +00:00
|
|
|
else if (co->base > tvref(co->stack)+1) s = "normal";
|
2009-12-08 18:46:35 +00:00
|
|
|
else if (co->top == co->base) s = "dead";
|
|
|
|
else s = "suspended";
|
|
|
|
lua_pushstring(L, s);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
LJLIB_CF(coroutine_running)
|
|
|
|
{
|
|
|
|
if (lua_pushthread(L))
|
|
|
|
setnilV(L->top++);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
LJLIB_CF(coroutine_create)
|
|
|
|
{
|
|
|
|
lua_State *L1 = lua_newthread(L);
|
|
|
|
if (!(L->top > L->base && tvisfunc(L->base) && isluafunc(funcV(L->base))))
|
|
|
|
lj_err_arg(L, 1, LJ_ERR_NOLFUNC);
|
|
|
|
setfuncV(L, L1->top++, funcV(L->base));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
LJLIB_ASM(coroutine_yield)
|
|
|
|
{
|
|
|
|
lj_err_caller(L, LJ_ERR_CYIELD);
|
|
|
|
return FFH_UNREACHABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ffh_resume(lua_State *L, lua_State *co, int wrap)
|
|
|
|
{
|
|
|
|
if (co->cframe != NULL || co->status > LUA_YIELD ||
|
|
|
|
(co->status == 0 && co->top == co->base)) {
|
|
|
|
ErrMsg em = co->cframe ? LJ_ERR_CORUN : LJ_ERR_CODEAD;
|
|
|
|
if (wrap) lj_err_caller(L, em);
|
|
|
|
setboolV(L->base-1, 0);
|
|
|
|
setstrV(L, L->base, lj_err_str(L, em));
|
|
|
|
return FFH_RES(2);
|
|
|
|
}
|
|
|
|
lj_state_growstack(co, (MSize)(L->top - L->base - 1));
|
|
|
|
return FFH_RETRY;
|
|
|
|
}
|
|
|
|
|
|
|
|
LJLIB_ASM(coroutine_resume)
|
|
|
|
{
|
|
|
|
if (!(L->top > L->base && tvisthread(L->base)))
|
|
|
|
lj_err_arg(L, 1, LJ_ERR_NOCORO);
|
|
|
|
return ffh_resume(L, threadV(L->base), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
LJLIB_NOREG LJLIB_ASM(coroutine_wrap_aux)
|
|
|
|
{
|
|
|
|
return ffh_resume(L, threadV(lj_lib_upvalue(L, 1)), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Inline declarations. */
|
|
|
|
LJ_ASMF void lj_ff_coroutine_wrap_aux(void);
|
2009-12-27 16:42:41 +00:00
|
|
|
LJ_FUNCA_NORET void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L,
|
|
|
|
lua_State *co);
|
2009-12-08 18:46:35 +00:00
|
|
|
|
|
|
|
/* Error handler, called from assembler VM. */
|
2009-12-27 16:42:41 +00:00
|
|
|
void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L, lua_State *co)
|
2009-12-08 18:46:35 +00:00
|
|
|
{
|
|
|
|
co->top--; copyTV(L, L->top, co->top); L->top++;
|
|
|
|
if (tvisstr(L->top-1))
|
|
|
|
lj_err_callermsg(L, strVdata(L->top-1));
|
|
|
|
else
|
|
|
|
lj_err_run(L);
|
|
|
|
}
|
|
|
|
|
2010-02-13 03:51:56 +00:00
|
|
|
/* Forward declaration. */
|
|
|
|
static void setpc_wrap_aux(lua_State *L, GCfunc *fn);
|
|
|
|
|
2009-12-08 18:46:35 +00:00
|
|
|
LJLIB_CF(coroutine_wrap)
|
|
|
|
{
|
|
|
|
lj_cf_coroutine_create(L);
|
2010-02-13 03:51:56 +00:00
|
|
|
lj_lib_pushcc(L, lj_ffh_coroutine_wrap_aux, FF_coroutine_wrap_aux, 1);
|
|
|
|
setpc_wrap_aux(L, funcV(L->top-1));
|
2009-12-08 18:46:35 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "lj_libdef.h"
|
|
|
|
|
2010-02-13 03:51:56 +00:00
|
|
|
/* Fix the PC of wrap_aux. Really ugly workaround. */
|
|
|
|
static void setpc_wrap_aux(lua_State *L, GCfunc *fn)
|
|
|
|
{
|
|
|
|
setmref(fn->c.pc, &L2GG(L)->bcff[lj_lib_init_coroutine[1]+2]);
|
|
|
|
}
|
|
|
|
|
2009-12-08 18:46:35 +00:00
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
static void newproxy_weaktable(lua_State *L)
|
|
|
|
{
|
|
|
|
/* NOBARRIER: The table is new (marked white). */
|
|
|
|
GCtab *t = lj_tab_new(L, 0, 1);
|
|
|
|
settabV(L, L->top++, t);
|
|
|
|
setgcref(t->metatable, obj2gco(t));
|
|
|
|
setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "__mode")),
|
|
|
|
lj_str_newlit(L, "kv"));
|
|
|
|
t->nomm = cast_byte(~(1u<<MM_mode));
|
|
|
|
}
|
|
|
|
|
|
|
|
LUALIB_API int luaopen_base(lua_State *L)
|
|
|
|
{
|
|
|
|
/* NOBARRIER: Table and value are the same. */
|
|
|
|
GCtab *env = tabref(L->env);
|
|
|
|
settabV(L, lj_tab_setstr(L, env, lj_str_newlit(L, "_G")), env);
|
|
|
|
lua_pushliteral(L, LUA_VERSION); /* top-3. */
|
|
|
|
newproxy_weaktable(L); /* top-2. */
|
|
|
|
LJ_LIB_REG_(L, "_G", base);
|
|
|
|
LJ_LIB_REG(L, coroutine);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|