summaryrefslogtreecommitdiff
path: root/tests/test-suite/ltests.js
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test-suite/ltests.js')
-rw-r--r--tests/test-suite/ltests.js65
1 files changed, 36 insertions, 29 deletions
diff --git a/tests/test-suite/ltests.js b/tests/test-suite/ltests.js
index fa6d697..6fc086c 100644
--- a/tests/test-suite/ltests.js
+++ b/tests/test-suite/ltests.js
@@ -4,8 +4,15 @@ const assert = require("assert");
const lua = require('../../src/lua.js');
const lauxlib = require('../../src/lauxlib.js');
+const {
+ luastring_eq,
+ luastring_indexOf,
+ to_jsstring,
+ to_luastring
+} = require("../../src/fengaricore.js");
const ljstype = require('../../src/ljstype.js');
const lopcodes = require('../../src/lopcodes.js');
+const { pushobj2s } = require('../../src/lobject.js');
const sprintf = require('sprintf-js').sprintf;
const delimits = [" ", "\t", "\n", ",", ";"].map(e => e.charCodeAt(0));
@@ -40,7 +47,7 @@ const getnum = function(L, L1, pc) {
pc.offset++;
}
if (!ljstype.lisdigit(pc.script[pc.offset]))
- lauxlib.luaL_error(L, lua.to_luastring("number expected (%s)"), pc.script);
+ lauxlib.luaL_error(L, to_luastring("number expected (%s)"), pc.script);
while (ljstype.lisdigit(pc.script[pc.offset])) res = res*10 + pc.script[pc.offset++] - '0'.charCodeAt(0);
return sig*res;
};
@@ -52,7 +59,7 @@ const getstring = function(L, buff, pc) {
let quote = pc.script[pc.offset++];
while (pc.script[pc.offset] !== quote) {
if (pc.script[pc.offset] === 0 || pc.offset >= pc.script.length)
- lauxlib.luaL_error(L, lua.to_luastring("unfinished string in JS script", true));
+ lauxlib.luaL_error(L, to_luastring("unfinished string in JS script", true));
buff[i++] = pc.script[pc.offset++];
}
pc.offset++;
@@ -67,13 +74,13 @@ const getindex = function(L, L1, pc) {
skip(pc);
switch (pc.script[pc.offset++]) {
case 'R'.charCodeAt(0): return lua.LUA_REGISTRYINDEX;
- case 'G'.charCodeAt(0): return lauxlib.luaL_error(L, lua.to_luastring("deprecated index 'G'", true));
+ case 'G'.charCodeAt(0): return lauxlib.luaL_error(L, to_luastring("deprecated index 'G'", true));
case 'U'.charCodeAt(0): return lua.lua_upvalueindex(getnum(L, L1, pc));
default: pc.offset--; return getnum(L, L1, pc);
}
};
-const codes = ["OK", "YIELD", "ERRRUN", "ERRSYNTAX", "ERRMEM", "ERRGCMM", "ERRERR"].map(e => lua.to_luastring(e));
+const codes = ["OK", "YIELD", "ERRRUN", "ERRSYNTAX", "ERRMEM", "ERRGCMM", "ERRERR"].map(e => to_luastring(e));
const pushcode = function(L, code) {
lua.lua_pushstring(L, codes[code]);
@@ -82,7 +89,7 @@ const pushcode = function(L, code) {
const printstack = function(L) {
let n = lua.lua_gettop(L);
for (let i = 1; i <= n; i++) {
- console.log("${i}: %{lua.to_jsstring(lauxlib.luaL_tolstring(L, i, null))}\n");
+ console.log("${i}: %{to_jsstring(lauxlib.luaL_tolstring(L, i, null))}\n");
lua.lua_pop(L, 1);
}
console.log("");
@@ -101,9 +108,9 @@ const ops = "+-*%^/\\&|~<>_!".split('').map(e => e.charCodeAt(0));
const runJS = function(L, L1, pc) {
let buff = new Uint8Array(300);
let status = 0;
- if (!pc || !pc.script) return lauxlib.luaL_error(L, lua.to_luastring("attempt to runJS null script"));
+ if (!pc || !pc.script) return lauxlib.luaL_error(L, to_luastring("attempt to runJS null script"));
for (;;) {
- let inst = lua.to_jsstring(getstring(L, buff, pc));
+ let inst = to_jsstring(getstring(L, buff, pc));
if (inst.length === 0) return 0;
switch (inst) {
case "absindex": {
@@ -449,7 +456,7 @@ const runJS = function(L, L1, pc) {
case "tostring": {
let s = lua.lua_tostring(L1, getindex(L, L1, pc));
let s1 = lua.lua_pushstring(L1, s);
- assert((s === null && s1 === null) || s.join('|') === s1.join('|'));
+ assert(luastring_eq(s, s1));
break;
}
case "type": {
@@ -475,7 +482,7 @@ const runJS = function(L, L1, pc) {
return lua.lua_yieldk(L1, nres, i, Cfunck);
}
default:
- lauxlib.luaL_error(L, lua.to_luastring("unknown instruction %s"), buff);
+ lauxlib.luaL_error(L, to_luastring("unknown instruction %s"), buff);
}
}
};
@@ -578,16 +585,16 @@ const loadlib = function(L) {
"table": luaopen_table
};
let L1 = getstate(L);
- lauxlib.luaL_requiref(L1, lua.to_luastring("package", true), luaopen_package, 0);
+ lauxlib.luaL_requiref(L1, to_luastring("package", true), luaopen_package, 0);
assert(lua.lua_type(L1, -1) == lua.LUA_TTABLE);
/* 'requiref' should not reload module already loaded... */
- lauxlib.luaL_requiref(L1, lua.to_luastring("package", true), null, 1); /* seg. fault if it reloads */
+ lauxlib.luaL_requiref(L1, to_luastring("package", true), null, 1); /* seg. fault if it reloads */
/* ...but should return the same module */
assert(lua.lua_compare(L1, -1, -2, lua.LUA_OPEQ));
lauxlib.luaL_getsubtable(L1, lua.LUA_REGISTRYINDEX, lauxlib.LUA_PRELOAD_TABLE);
for (let name in libs) {
lua.lua_pushcfunction(L1, libs[name]);
- lua.lua_setfield(L1, -2, lua.to_luastring(name, true));
+ lua.lua_setfield(L1, -2, to_luastring(name, true));
}
return 0;
};
@@ -637,8 +644,8 @@ const newuserdata = function(L) {
*/
const Chook = function(L, ar) {
let scpt;
- let events = ["call", "ret", "line", "count", "tailcall"].map(e => lua.to_luastring(e));
- lua.lua_getfield(L, lua.LUA_REGISTRYINDEX, lua.to_luastring("JS_HOOK", true));
+ let events = ["call", "ret", "line", "count", "tailcall"].map(e => to_luastring(e));
+ lua.lua_getfield(L, lua.LUA_REGISTRYINDEX, to_luastring("JS_HOOK", true));
lua.lua_pushlightuserdata(L, L);
lua.lua_gettable(L, -2); /* get C_HOOK[L] (script saved by sethookaux) */
scpt = lua.lua_tostring(L, -1); /* not very religious (string will be popped) */
@@ -661,7 +668,7 @@ class Aux {
const panicback = function(L) {
let b = new Aux();
lua.lua_checkstack(L, 1); /* open space for 'Aux' struct */
- lua.lua_getfield(L, lua.LUA_REGISTRYINDEX, lua.to_luastring("_jmpbuf", true)); /* get 'Aux' struct */
+ lua.lua_getfield(L, lua.LUA_REGISTRYINDEX, to_luastring("_jmpbuf", true)); /* get 'Aux' struct */
b = lua.lua_touserdata(L, -1);
lua.lua_pop(L, 1); /* remove 'Aux' struct */
runJS(b.L, L, { script: b.paniccode, offset: 0 }); /* run optional panic code */
@@ -671,7 +678,7 @@ const panicback = function(L) {
const checkpanic = function(L) {
let b = new Aux();
let code = lauxlib.luaL_checkstring(L, 1);
- b.paniccode = lauxlib.luaL_optstring(L, 2, lua.to_luastring("", true));
+ b.paniccode = lauxlib.luaL_optstring(L, 2, "");
b.L = L;
let L1 = lua.lua_newstate(); /* create new state */
if (L1 === null) { /* error? */
@@ -680,7 +687,7 @@ const checkpanic = function(L) {
}
lua.lua_atpanic(L1, panicback); /* set its panic function */
lua.lua_pushlightuserdata(L1, b);
- lua.lua_setfield(L1, lua.LUA_REGISTRYINDEX, lua.to_luastring("_jmpbuf", true)); /* store 'Aux' struct */
+ lua.lua_setfield(L1, lua.LUA_REGISTRYINDEX, to_luastring("_jmpbuf", true)); /* store 'Aux' struct */
try { /* set jump buffer */
runJS(L, L1, { script: code, offset: 0 }); /* run code unprotected */
lua.lua_pushliteral(L, "no errors");
@@ -700,12 +707,12 @@ const sethookaux = function(L, mask, count, scpt) {
lua.lua_sethook(L, null, 0, 0); /* turn off hooks */
return;
}
- lua.lua_getfield(L, lua.LUA_REGISTRYINDEX, lua.to_luastring("JS_HOOK", true)); /* get C_HOOK table */
+ lua.lua_getfield(L, lua.LUA_REGISTRYINDEX, to_luastring("JS_HOOK", true)); /* get C_HOOK table */
if (!lua.lua_istable(L, -1)) { /* no hook table? */
lua.lua_pop(L, 1); /* remove previous value */
lua.lua_newtable(L); /* create new C_HOOK table */
lua.lua_pushvalue(L, -1);
- lua.lua_setfield(L, lua.LUA_REGISTRYINDEX, lua.to_luastring("JS_HOOK", true)); /* register it */
+ lua.lua_setfield(L, lua.LUA_REGISTRYINDEX, to_luastring("JS_HOOK", true)); /* register it */
}
lua.lua_pushlightuserdata(L, L);
lua.lua_pushstring(L, scpt);
@@ -721,9 +728,9 @@ const sethook = function(L) {
const smask = lauxlib.luaL_checkstring(L, 2);
let count = lauxlib.luaL_optinteger(L, 3, 0);
let mask = 0;
- if (smask.indexOf('c'.charCodeAt(0)) >= 0) mask |= lua.LUA_MASKCALL;
- if (smask.indexOf('r'.charCodeAt(0)) >= 0) mask |= lua.LUA_MASKRET;
- if (smask.indexOf('l'.charCodeAt(0)) >= 0) mask |= lua.LUA_MASKLINE;
+ if (luastring_indexOf(smask, 'c'.charCodeAt(0)) >= 0) mask |= lua.LUA_MASKCALL;
+ if (luastring_indexOf(smask, 'r'.charCodeAt(0)) >= 0) mask |= lua.LUA_MASKRET;
+ if (luastring_indexOf(smask, 'l'.charCodeAt(0)) >= 0) mask |= lua.LUA_MASKLINE;
if (count > 0) mask |= lua.LUA_MASKCOUNT;
sethookaux(L, mask, count, scpt);
}
@@ -736,9 +743,9 @@ const Cfunc = function(L) {
const Cfunck = function(L, status, ctx) {
pushcode(L, status);
- lua.lua_setglobal(L, lua.to_luastring("status", true));
+ lua.lua_setglobal(L, to_luastring("status", true));
lua.lua_pushinteger(L, ctx);
- lua.lua_setglobal(L, lua.to_luastring("ctx", true));
+ lua.lua_setglobal(L, to_luastring("ctx", true));
return runJS(L, L, { script: lua.lua_tostring(L, ctx), offset: 0 });
};
@@ -775,7 +782,7 @@ const setnameval = function(L, name, val) {
};
const pushobject = function(L, o){
- L.stack[L.top++] = o;
+ pushobj2s(L, o);
assert(L.top <= L.ci.top, "stack overflow");
};
@@ -800,7 +807,7 @@ const buildop = function(p, pc) {
break;
}
- return lua.to_luastring(result);
+ return to_luastring(result);
};
const listcode = function(L) {
@@ -808,8 +815,8 @@ const listcode = function(L) {
1, "Lua function expected");
let p = obj_at(L, 1);
lua.lua_newtable(L);
- setnameval(L, lua.to_luastring("maxstack", true), p.maxstacksize);
- setnameval(L, lua.to_luastring("numparams", true), p.numparams);
+ setnameval(L, to_luastring("maxstack", true), p.maxstacksize);
+ setnameval(L, to_luastring("numparams", true), p.numparams);
for (let pc = 0; pc < p.code.length; pc++) {
lua.lua_pushinteger(L, pc+1);
lua.lua_pushstring(L, buildop(p, pc));
@@ -859,7 +866,7 @@ const luaB_opentests = function(L) {
};
const luaopen_tests = function(L) {
- lauxlib.luaL_requiref(L, lua.to_luastring("T"), luaB_opentests, 1);
+ lauxlib.luaL_requiref(L, to_luastring("T"), luaB_opentests, 1);
lua.lua_pop(L, 1); /* remove lib */
};