aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ldebug.js10
-rw-r--r--tests/test-suite/inprogress/db.js248
2 files changed, 254 insertions, 4 deletions
diff --git a/src/ldebug.js b/src/ldebug.js
index 4f8d385..532fd9c 100644
--- a/src/ldebug.js
+++ b/src/ldebug.js
@@ -151,13 +151,15 @@ const lua_getlocal = function(L, ar, n) {
};
const lua_setlocal = function(L, ar, n) {
+ let name;
swapextra(L);
let local = findlocal(L, ar.i_ci, n);
- let name = local.name;
- let pos = local.pos;
- if (name) {
- lobject.setobjs2s(L, pos, L.top - 1);
+ if (local) {
+ name = local.name;
+ lobject.setobjs2s(L, local.pos, L.top - 1);
delete L.stack[--L.top]; /* pop value */
+ } else {
+ name = null;
}
swapextra(L);
return name;
diff --git a/tests/test-suite/inprogress/db.js b/tests/test-suite/inprogress/db.js
index 5ff307c..af1d042 100644
--- a/tests/test-suite/inprogress/db.js
+++ b/tests/test-suite/inprogress/db.js
@@ -96,3 +96,251 @@ test("[test-suite] db: getinfo, ...line...", function (t) {
}, "Lua program ran without error");
});
+
+
+test("[test-suite] db: test file ad string names truncation", function (t) {
+ let luaCode = `
+ a = "function f () end"
+ local function dostring (s, x) return load(s, x)() end
+ dostring(a)
+ assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a))
+ dostring(a..string.format("; %s\\n=1", string.rep('p', 400)))
+ assert(string.find(debug.getinfo(f).short_src, '^%[string [^\\n]*%.%.%."%]$'))
+ dostring(a..string.format("; %s=1", string.rep('p', 400)))
+ assert(string.find(debug.getinfo(f).short_src, '^%[string [^\\n]*%.%.%."%]$'))
+ dostring("\\n"..a)
+ assert(debug.getinfo(f).short_src == '[string "..."]')
+ dostring(a, "")
+ assert(debug.getinfo(f).short_src == '[string ""]')
+ dostring(a, "@xuxu")
+ assert(debug.getinfo(f).short_src == "xuxu")
+ dostring(a, "@"..string.rep('p', 1000)..'t')
+ assert(string.find(debug.getinfo(f).short_src, "^%.%.%.p*t$"))
+ dostring(a, "=xuxu")
+ assert(debug.getinfo(f).short_src == "xuxu")
+ dostring(a, string.format("=%s", string.rep('x', 500)))
+ assert(string.find(debug.getinfo(f).short_src, "^x*$"))
+ dostring(a, "=")
+ assert(debug.getinfo(f).short_src == "")
+ a = nil; f = nil;
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+test("[test-suite] db: local", function (t) {
+ let luaCode = `
+ repeat
+ local g = {x = function ()
+ local a = debug.getinfo(2)
+ assert(a.name == 'f' and a.namewhat == 'local')
+ a = debug.getinfo(1)
+ assert(a.name == 'x' and a.namewhat == 'field')
+ return 'xixi'
+ end}
+ local f = function () return 1+1 and (not 1 or g.x()) end
+ assert(f() == 'xixi')
+ g = debug.getinfo(f)
+ assert(g.what == "Lua" and g.func == f and g.namewhat == "" and not g.name)
+
+ function f (x, name) -- local!
+ name = name or 'f'
+ local a = debug.getinfo(1)
+ assert(a.name == name and a.namewhat == 'local')
+ return x
+ end
+
+ -- breaks in different conditions
+ if 3>4 then break end; f()
+ if 3<4 then a=1 else break end; f()
+ while 1 do local x=10; break end; f()
+ local b = 1
+ if 3>4 then return math.sin(1) end; f()
+ a = 3<4; f()
+ a = 3<4 or 1; f()
+ repeat local x=20; if 4>3 then f() else break end; f() until 1
+ g = {}
+ f(g).x = f(2) and f(10)+f(9)
+ assert(g.x == f(19))
+ function g(x) if not x then return 3 end return (x('a', 'x')) end
+ assert(g(f) == 'a')
+ until 1
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] db: line hook", function (t) {
+ let luaCode = `
+ test([[if
+ math.sin(1)
+ then
+ a=1
+ else
+ a=2
+ end
+ ]], {2,3,4,7})
+
+ test([[--
+ if nil then
+ a=1
+ else
+ a=2
+ end
+ ]], {2,5,6})
+
+ test([[a=1
+ repeat
+ a=a+1
+ until a==3
+ ]], {1,3,4,3,4})
+
+ test([[ do
+ return
+ end
+ ]], {2})
+
+ test([[local a
+ a=1
+ while a<=3 do
+ a=a+1
+ end
+ ]], {1,2,3,4,3,4,3,4,3,5})
+
+ test([[while math.sin(1) do
+ if math.sin(1)
+ then break
+ end
+ end
+ a=1]], {1,2,3,6})
+
+ test([[for i=1,3 do
+ a=i
+ end
+ ]], {1,2,1,2,1,2,1,3})
+
+ test([[for i,v in pairs{'a','b'} do
+ a=tostring(i) .. v
+ end
+ ]], {1,2,1,2,1,3})
+
+ test([[for i=1,4 do a=1 end]], {1,1,1,1,1})
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] db: invalid levels in [gs]etlocal", function (t) {
+ let luaCode = `
+ assert(not pcall(debug.getlocal, 20, 1))
+ assert(not pcall(debug.setlocal, -1, 1, 10))
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] db: parameter names", function (t) {
+ let luaCode = `
+ local function foo (a,b,...) local d, e end
+ local co = coroutine.create(foo)
+
+ assert(debug.getlocal(foo, 1) == 'a')
+ assert(debug.getlocal(foo, 2) == 'b')
+ assert(not debug.getlocal(foo, 3))
+ assert(debug.getlocal(co, foo, 1) == 'a')
+ assert(debug.getlocal(co, foo, 2) == 'b')
+ assert(not debug.getlocal(co, foo, 3))
+
+ assert(not debug.getlocal(print, 1))
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});