diff options
-rw-r--r-- | src/lbaselib.js | 2 | ||||
-rw-r--r-- | src/ldo.js | 4 | ||||
-rw-r--r-- | src/lfunc.js | 17 | ||||
-rw-r--r-- | src/lstate.js | 1 | ||||
-rw-r--r-- | src/lundump.js | 4 | ||||
-rw-r--r-- | src/lvm.js | 1 | ||||
-rw-r--r-- | tests/test-suite/calls.js | 449 |
7 files changed, 465 insertions, 13 deletions
diff --git a/src/lbaselib.js b/src/lbaselib.js index 5e3fff6..d1d930a 100644 --- a/src/lbaselib.js +++ b/src/lbaselib.js @@ -297,7 +297,7 @@ const luaB_load = function(L) { let status; if (s !== null) { /* loading a string? */ let chunkname = lauxlib.luaL_optstring(L, 2, s); - status = lauxlib.luaL_loadbufferx(L, s, chunkname, mode); + status = lauxlib.luaL_loadbufferx(L, s, s.length, chunkname, mode); } else { /* loading from a reader function */ let chunkname = lauxlib.luaL_optstring(L, 2, lua.to_luastring("=(load)", true)); lauxlib.luaL_checktype(L, 1, lua.LUA_TFUNCTION); @@ -574,7 +574,9 @@ const f_parser = function(L, p) { let c = p.z.getc(); /* read first character */ if (c === defs.LUA_SIGNATURE.charCodeAt(0)) { checkmode(L, p.mode, defs.to_luastring("binary", true)); - cl = new BytecodeParser(L, p.z.buffer, p.name).luaU_undump(); + let buff = p.z.buffer; + if (Array.isArray(buff)) buff = new DataView(new Uint8Array(p.z.buffer).buffer); /* Lua string holding binary (string.dump) */ + cl = new BytecodeParser(L, buff, p.name).luaU_undump(); } else { checkmode(L, p.mode, defs.to_luastring("text", true)); cl = lparser.luaY_parser(L, p.z, p.buff, p.dyd, p.name, c); diff --git a/src/lfunc.js b/src/lfunc.js index cf2cfe0..7b2c557 100644 --- a/src/lfunc.js +++ b/src/lfunc.js @@ -8,7 +8,7 @@ const CT = defs.constant_types; class Proto { - constructor(L) { + constructor() { this.k = []; // constants used by the function this.p = []; // functions defined inside the function this.code = []; // opcodes @@ -54,10 +54,10 @@ const luaF_newLclosure = function(L, n) { const findupval = function(L, level) { let pp = L.openupval; - - while(pp !== null && pp.v >= level) { - let p = pp; - + let p = pp; + while (pp !== null && pp.v >= level) { + p = pp; + assert(p.isopen()); if (p.v === level) return p; @@ -66,11 +66,14 @@ const findupval = function(L, level) { let uv = new UpVal(); - uv.open_next = pp; + if (p) { /* The openupval list is not empty */ + uv.open_next = p; /* link it to list of open upvalues */ + } + L.openupval = uv; uv.L = L; - uv.v = level; + uv.v = level; /* current value lives in the stack */ return uv; }; diff --git a/src/lstate.js b/src/lstate.js index 4fd2951..fee308c 100644 --- a/src/lstate.js +++ b/src/lstate.js @@ -168,7 +168,6 @@ const lua_newstate = function() { }; const close_state = function(L) { - let g = L.l_G; lfunc.luaF_close(L, L.stack); /* close all upvalues for this thread */ }; diff --git a/src/lundump.js b/src/lundump.js index 541adbb..1c0d4c7 100644 --- a/src/lundump.js +++ b/src/lundump.js @@ -8,11 +8,11 @@ const lfunc = require('./lfunc.js'); const lobject = require('./lobject.js'); const lopcodes = require('./lopcodes.js'); -const LUAI_MAXSHORTLEN = 40; - class BytecodeParser { constructor(L, dataView, name) { + assert(dataView instanceof DataView, "BytecodeParser only operates on a dataView") + this.L = L; this.intSize = 4; this.size_tSize = 8; @@ -516,7 +516,6 @@ const luaV_execute = function(L) { let oci = nci.previous; let nfunc = nci.func; let nfuncOff = nci.funcOff; - let ofunc = oci.func; let ofuncOff = oci.funcOff; let lim = nci.l_base + nfunc.value.p.numparams; if (cl.p.p.length > 0) lfunc.luaF_close(L, oci.l_base); diff --git a/tests/test-suite/calls.js b/tests/test-suite/calls.js new file mode 100644 index 0000000..bccad87 --- /dev/null +++ b/tests/test-suite/calls.js @@ -0,0 +1,449 @@ +"use strict"; + +const test = require('tape'); + +const lauxlib = require("../../src/lauxlib.js"); +const lua = require('../../src/lua.js'); + + +test("[test-suite] calls: test 'type'", function (t) { + let luaCode = ` + assert(type(1<2) == 'boolean') + assert(type(true) == 'boolean' and type(false) == 'boolean') + assert(type(nil) == 'nil' + and type(-3) == 'number' + and type'x' == 'string' + and type{} == 'table' + and type(type) == 'function') + + assert(type(assert) == type(print)) + function f (x) return a:x (x) end + assert(type(f) == 'function') + assert(not pcall(type)) + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lauxlib.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lua.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + +}); + + +test("[test-suite] calls: test error in 'print'", function (t) { + let luaCode = ` + do -- test error in 'print' too... + local tostring = _ENV.tostring + + _ENV.tostring = nil + local st, msg = pcall(print, 1) + assert(st == false and string.find(msg, "attempt to call a nil value")) + + _ENV.tostring = function () return {} end + local st, msg = pcall(print, 1) + assert(st == false and string.find(msg, "must return a string")) + + _ENV.tostring = tostring + end + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lauxlib.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lua.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + +}); + + +test("[test-suite] calls: testing local-function recursion", function (t) { + let luaCode = ` + fact = false + do + local res = 1 + local function fact (n) + if n==0 then return res + else return n*fact(n-1) + end + end + assert(fact(5) == 120) + end + assert(fact == false) + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lauxlib.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lua.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + +}); + + +test("[test-suite] calls: testing declarations", function (t) { + let luaCode = ` + a = {i = 10} + self = 20 + function a:x (x) return x+self.i end + function a.y (x) return x+self end + + assert(a:x(1)+10 == a.y(1)) + + a.t = {i=-100} + a["t"].x = function (self, a,b) return self.i+a+b end + + assert(a.t:x(2,3) == -95) + + do + local a = {x=0} + function a:add (x) self.x, a.y = self.x+x, 20; return self end + assert(a:add(10):add(20):add(30).x == 60 and a.y == 20) + end + + local a = {b={c={}}} + + function a.b.c.f1 (x) return x+1 end + function a.b.c:f2 (x,y) self[x] = y end + assert(a.b.c.f1(4) == 5) + a.b.c:f2('k', 12); assert(a.b.c.k == 12) + + + t = nil -- 'declare' t + function f(a,b,c) local d = 'a'; t={a,b,c,d} end + + f( -- this line change must be valid + 1,2) + assert(t[1] == 1 and t[2] == 2 and t[3] == nil and t[4] == 'a') + f(1,2, -- this one too + 3,4) + assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a') + + function fat(x) + if x <= 1 then return 1 + else return x*load("return fat(" .. x-1 .. ")", "")() + end + end + + assert(load "load 'assert(fat(6)==720)' () ")() + a = load('return fat(5), 3') + a,b = a() + assert(a == 120 and b == 3) + + function err_on_n (n) + if n==0 then error(); exit(1); + else err_on_n (n-1); exit(1); + end + end + + do + function dummy (n) + if n > 0 then + assert(not pcall(err_on_n, n)) + dummy(n-1) + end + end + end + + dummy(10) + + function deep (n) + if n>0 then deep(n-1) end + end + deep(10) + deep(200) + + -- testing tail call + function deep (n) if n>0 then return deep(n-1) else return 101 end end + assert(deep(30000) == 101) + a = {} + function a:deep (n) if n>0 then return self:deep(n-1) else return 101 end end + assert(a:deep(30000) == 101) + + + + a = nil + (function (x) a=x end)(23) + assert(a == 23 and (function (x) return x*2 end)(20) == 40) + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lauxlib.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lua.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + +}); + + +test("[test-suite] calls: testing closures", function (t) { + let luaCode = ` + -- fixed-point operator + Z = function (le) + local function a (f) + return le(function (x) return f(f)(x) end) + end + return a(a) + end + + + -- non-recursive factorial + + F = function (f) + return function (n) + if n == 0 then return 1 + else return n*f(n-1) end + end + end + + fat = Z(F) + + assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4)) + + local function g (z) + local function f (a,b,c,d) + return function (x,y) return a+b+c+d+a+x+y+z end + end + return f(z,z+1,z+2,z+3) + end + + f = g(10) + assert(f(9, 16) == 10+11+12+13+10+9+16+10) + + Z, F, f = nil + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lauxlib.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lua.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + +}); + + +test("[test-suite] calls: testing multiple returns", function (t) { + let luaCode = ` + function unlpack (t, i) + i = i or 1 + if (i <= #t) then + return t[i], unlpack(t, i+1) + end + end + + function equaltab (t1, t2) + assert(#t1 == #t2) + for i = 1, #t1 do + assert(t1[i] == t2[i]) + end + end + + local pack = function (...) return (table.pack(...)) end + + function f() return 1,2,30,4 end + function ret2 (a,b) return a,b end + + local a,b,c,d = unlpack{1,2,3} + assert(a==1 and b==2 and c==3 and d==nil) + a = {1,2,3,4,false,10,'alo',false,assert} + equaltab(pack(unlpack(a)), a) + equaltab(pack(unlpack(a), -1), {1,-1}) + a,b,c,d = ret2(f()), ret2(f()) + assert(a==1 and b==1 and c==2 and d==nil) + a,b,c,d = unlpack(pack(ret2(f()), ret2(f()))) + assert(a==1 and b==1 and c==2 and d==nil) + a,b,c,d = unlpack(pack(ret2(f()), (ret2(f())))) + assert(a==1 and b==1 and c==nil and d==nil) + + a = ret2{ unlpack{1,2,3}, unlpack{3,2,1}, unlpack{"a", "b"}} + assert(a[1] == 1 and a[2] == 3 and a[3] == "a" and a[4] == "b") + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lauxlib.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lua.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + +}); + + +test("[test-suite] calls: testing calls with 'incorrect' arguments", function (t) { + let luaCode = ` + rawget({}, "x", 1) + rawset({}, "x", 1, 2) + assert(math.sin(1,2) == math.sin(1)) + table.sort({10,9,8,4,19,23,0,0}, function (a,b) return a<b end, "extra arg") + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lauxlib.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lua.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + +}); + + +test("[test-suite] calls: test for generic load", function (t) { + let luaCode = ` + local x = "-- a comment\\0\\0\\0\\n x = 10 + \\n23; \\ + local a = function () x = 'hi' end; \\ + return '\\0'" + + function read1 (x) + local i = 0 + return function () + i=i+1 + return string.sub(x, i, i) + end + end + + function cannotload (msg, a,b) + assert(not a and string.find(b, msg)) + end + + a = assert(load(read1(x), "modname", "t", _G)) + assert(a() == "\0" and _G.x == 33) + assert(debug.getinfo(a).source == "modname") + -- cannot read text in binary mode + cannotload("attempt to load a text chunk", load(read1(x), "modname", "b", {})) + cannotload("attempt to load a text chunk", load(x, "modname", "b")) + + a = assert(load(function () return nil end)) + a() -- empty chunk + + assert(not load(function () return true end)) + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lauxlib.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lua.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + +}); + + +test("[test-suite] calls: small bug", function (t) { + let luaCode = ` + local t = {nil, "return ", "3"} + f, msg = load(function () return table.remove(t, 1) end) + assert(f() == nil) -- should read the empty chunk + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lauxlib.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lua.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + +}); |