📄 ltests.cpp
字号:
static int doonnewstack (lua_State *L) {
lua_State *L1 = lua_newthread(L);
size_t l;
const char *s = luaL_checklstring(L, 1, &l);
int status = luaL_loadbuffer(L1, s, l, s);
if (status == 0)
status = lua_pcall(L1, 0, 0, 0);
lua_pushintegral(L, status);
return 1;
}
static int s2d (lua_State *L) {
lua_pushnumber(L, *cast(const double *, luaL_checkstring(L, 1)));
return 1;
}
static int d2s (lua_State *L) {
double d = luaL_checknumber(L, 1);
lua_pushlstring(L, cast(char *, &d), sizeof(d));
return 1;
}
static int newstate (lua_State *L) {
lua_State *L1 = lua_open();
if (L1) {
lua_userstateopen(L1); /* init lock */
lua_pushintegral(L, (unsigned long)L1);
}
else
lua_pushnil(L);
return 1;
}
static int loadlib (lua_State *L) {
static const luaL_reg libs[] = {
{"mathlibopen", luaopen_math},
{"strlibopen", luaopen_string},
{"iolibopen", luaopen_io},
{"tablibopen", luaopen_table},
{"dblibopen", luaopen_debug},
{"baselibopen", luaopen_base},
{0, 0}
};
lua_State *L1 = cast(lua_State *,
cast(unsigned long, luaL_checknumber(L, 1)));
lua_pushvalue(L1, LUA_GLOBALSINDEX);
luaL_openlib(L1, 0, libs, 0);
return 0;
}
static int closestate (lua_State *L) {
lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_checknumber(L, 1)));
lua_close(L1);
lua_unlock(L); /* close cannot unlock that */
return 0;
}
static int doremote (lua_State *L) {
lua_State *L1 = cast(lua_State *,cast(unsigned long,luaL_checknumber(L, 1)));
size_t lcode;
const char *code = luaL_checklstring(L, 2, &lcode);
int status;
lua_settop(L1, 0);
status = luaL_loadbuffer(L1, code, lcode, code);
if (status == 0)
status = lua_pcall(L1, 0, LUA_MULTRET, 0);
if (status != 0) {
lua_pushnil(L);
lua_pushintegral(L, status);
lua_pushstring(L, lua_tostring(L1, -1));
return 3;
}
else {
int i = 0;
while (!lua_isnone(L1, ++i))
lua_pushstring(L, lua_tostring(L1, i));
lua_pop(L1, i-1);
return i-1;
}
}
static int log2_aux (lua_State *L) {
lua_pushintegral(L, luaO_log2(luaL_checkint(L, 1)));
return 1;
}
static int int2fb_aux (lua_State *L) {
int b = luaO_int2fb(luaL_checkint(L, 1));
lua_pushintegral(L, b);
lua_pushintegral(L, fb2int(b));
return 2;
}
static int test_do (lua_State *L) {
const char *p = luaL_checkstring(L, 1);
if (*p == '@')
lua_dofile(L, p+1);
else
lua_dostring(L, p);
return lua_gettop(L);
}
/*
** {======================================================
** function to test the API with C. It interprets a kind of assembler
** language with calls to the API, so the test can be driven by Lua code
** =======================================================
*/
static const char *const delimits = " \t\n,;";
static void skip (const char **pc) {
while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
}
static int getnum_aux (lua_State *L, const char **pc) {
int res = 0;
int sig = 1;
skip(pc);
if (**pc == '.') {
res = cast(int, lua_tonumber(L, -1));
lua_pop(L, 1);
(*pc)++;
return res;
}
else if (**pc == '-') {
sig = -1;
(*pc)++;
}
while (isdigit(cast(int, **pc))) res = res*10 + (*(*pc)++) - '0';
return sig*res;
}
static const char *getname_aux (char *buff, const char **pc) {
int i = 0;
skip(pc);
while (**pc != '\0' && !strchr(delimits, **pc))
buff[i++] = *(*pc)++;
buff[i] = '\0';
return buff;
}
#define EQ(s1) (strcmp(s1, inst) == 0)
#define getnum (getnum_aux(L, &pc))
#define getname (getname_aux(buff, &pc))
static int testC (lua_State *L) {
char buff[30];
const char *pc = luaL_checkstring(L, 1);
for (;;) {
const char *inst = getname;
if EQ("") return 0;
else if EQ("isnumber") {
lua_pushintegral(L, lua_isnumber(L, getnum));
}
else if EQ("isstring") {
lua_pushintegral(L, lua_isstring(L, getnum));
}
else if EQ("istable") {
lua_pushintegral(L, lua_istable(L, getnum));
}
else if EQ("iscfunction") {
lua_pushintegral(L, lua_iscfunction(L, getnum));
}
else if EQ("isfunction") {
lua_pushintegral(L, lua_isfunction(L, getnum));
}
else if EQ("isuserdata") {
lua_pushintegral(L, lua_isuserdata(L, getnum));
}
else if EQ("isudataval") {
lua_pushintegral(L, lua_islightuserdata(L, getnum));
}
else if EQ("isnil") {
lua_pushintegral(L, lua_isnil(L, getnum));
}
else if EQ("isnull") {
lua_pushintegral(L, lua_isnone(L, getnum));
}
else if EQ("tonumber") {
lua_pushnumber(L, lua_tonumber(L, getnum));
}
else if EQ("tostring") {
const char *s = lua_tostring(L, getnum);
lua_pushstring(L, s);
}
else if EQ("strlen") {
lua_pushintegral(L, lua_strlen(L, getnum));
}
else if EQ("tocfunction") {
lua_pushcfunction(L, lua_tocfunction(L, getnum));
}
else if EQ("return") {
return getnum;
}
else if EQ("gettop") {
lua_pushintegral(L, lua_gettop(L));
}
else if EQ("settop") {
lua_settop(L, getnum);
}
else if EQ("pop") {
lua_pop(L, getnum);
}
else if EQ("pushnum") {
lua_pushintegral(L, getnum);
}
else if EQ("pushnil") {
lua_pushnil(L);
}
else if EQ("pushbool") {
lua_pushboolean(L, getnum);
}
else if EQ("tobool") {
lua_pushintegral(L, lua_toboolean(L, getnum));
}
else if EQ("pushvalue") {
lua_pushvalue(L, getnum);
}
else if EQ("pushcclosure") {
lua_pushcclosure(L, testC, getnum);
}
else if EQ("pushupvalues") {
lua_pushupvalues(L);
}
else if EQ("remove") {
lua_remove(L, getnum);
}
else if EQ("insert") {
lua_insert(L, getnum);
}
else if EQ("replace") {
lua_replace(L, getnum);
}
else if EQ("gettable") {
lua_gettable(L, getnum);
}
else if EQ("settable") {
lua_settable(L, getnum);
}
else if EQ("next") {
lua_next(L, -2);
}
else if EQ("concat") {
lua_concat(L, getnum);
}
else if EQ("lessthan") {
int a = getnum;
lua_pushboolean(L, lua_lessthan(L, a, getnum));
}
else if EQ("equal") {
int a = getnum;
lua_pushboolean(L, lua_equal(L, a, getnum));
}
else if EQ("rawcall") {
int narg = getnum;
int nres = getnum;
lua_call(L, narg, nres);
}
else if EQ("call") {
int narg = getnum;
int nres = getnum;
lua_pcall(L, narg, nres, 0);
}
else if EQ("loadstring") {
size_t sl;
const char *s = luaL_checklstring(L, getnum, &sl);
luaL_loadbuffer(L, s, sl, s);
}
else if EQ("loadfile") {
luaL_loadfile(L, luaL_checkstring(L, getnum));
}
else if EQ("setmetatable") {
lua_setmetatable(L, getnum);
}
else if EQ("getmetatable") {
if (lua_getmetatable(L, getnum) == 0)
lua_pushnil(L);
}
else if EQ("type") {
lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
}
else if EQ("getn") {
int i = getnum;
lua_pushintegral(L, luaL_getn(L, i));
}
else if EQ("setn") {
int i = getnum;
int n = cast(int, lua_tonumber(L, -1));
luaL_setn(L, i, n);
lua_pop(L, 1);
}
else luaL_error(L, "unknown instruction %s", buff);
}
return 0;
}
/* }====================================================== */
/*
** {======================================================
** tests for yield inside hooks
** =======================================================
*/
static void yieldf (lua_State *L, lua_Debug *ar) {
lua_yield(L, 0);
}
static int setyhook (lua_State *L) {
if (lua_isnoneornil(L, 1))
lua_sethook(L, 0, 0, 0); /* turn off hooks */
else {
const char *smask = luaL_checkstring(L, 1);
int count = luaL_optint(L, 2, 0);
int mask = 0;
if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
if (count > 0) mask |= LUA_MASKCOUNT;
lua_sethook(L, yieldf, mask, count);
}
return 0;
}
static int coresume (lua_State *L) {
int status;
lua_State *co = lua_tothread(L, 1);
luaL_argcheck(L, co, 1, "coroutine expected");
status = lua_resume(co, 0);
if (status != 0) {
lua_pushboolean(L, 0);
lua_insert(L, -2);
return 2; /* return false + error message */
}
else {
lua_pushboolean(L, 1);
return 1;
}
}
/* }====================================================== */
static const struct luaL_reg tests_funcs[] = {
{"hash", hash_query},
{"limits", get_limits},
{"listcode", listcode},
{"listk", listk},
{"listlocals", listlocals},
{"loadlib", loadlib},
{"stacklevel", stacklevel},
{"querystr", string_query},
{"querytab", table_query},
{"doit", test_do},
{"testC", testC},
{"ref", tref},
{"getref", getref},
{"unref", unref},
{"d2s", d2s},
{"s2d", s2d},
{"metatable", metatable},
{"upvalue", upvalue},
{"newuserdata", newuserdata},
{"pushuserdata", pushuserdata},
{"udataval", udataval},
{"doonnewstack", doonnewstack},
{"newstate", newstate},
{"closestate", closestate},
{"doremote", doremote},
{"log2", log2_aux},
{"int2fb", int2fb_aux},
{"totalmem", mem_query},
{"resume", coresume},
{"setyhook", setyhook},
{0, 0}
};
static void fim (void) {
if (!islocked)
lua_close(lua_state);
lua_assert(memdebug_numblocks == 0);
lua_assert(memdebug_total == 0);
}
static int l_panic (lua_State *L) {
UNUSED(L);
fprintf(stderr, "unable to recover; exiting\n");
return 0;
}
int luaB_opentests (lua_State *L) {
lua_atpanic(L, l_panic);
lua_userstateopen(L); /* init lock */
lua_state = L; /* keep first state to be opened */
luaL_openlib(L, "T", tests_funcs, 0);
atexit(fim);
return 0;
}
#undef main
int main (int argc, char *argv[]) {
char *limit = getenv("MEMLIMIT");
if (limit)
memdebug_memlimit = strtoul(limit, 0, 10);
l_main(argc, argv);
return 0;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -