From 036bb181d59364d13207225bac25b419485a3df7 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Fri, 15 Dec 2017 15:19:45 +1100 Subject: src/: Add defs.from_userstring function to take string from api --- src/defs.js | 6 ++++++ src/lapi.js | 25 ++++++++++--------------- src/ldebug.js | 1 + src/lobject.js | 1 + 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/defs.js b/src/defs.js index 381114b..a2c5c75 100644 --- a/src/defs.js +++ b/src/defs.js @@ -247,6 +247,11 @@ const to_luastring = function(str, cache) { return outU8Array; }; +const from_userstring = function(str) { + assert(is_luastring(str), "expects an array of bytes"); + return str; +}; + /* ** Event codes */ @@ -429,3 +434,4 @@ module.exports.string_of = string_of; module.exports.to_jsstring = to_jsstring; module.exports.to_luastring = to_luastring; module.exports.to_uristring = to_uristring; +module.exports.from_userstring = from_userstring; diff --git a/src/lapi.js b/src/lapi.js index 111735f..b42ad92 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -236,7 +236,8 @@ const lua_pushlstring = function(L, s, len) { if (len === 0) { s = defs.to_luastring("", true); } else { - assert(defs.is_luastring(s) && s.length >= len, "lua_pushlstring expects array of byte"); + s = defs.from_userstring(s); + assert(s.length >= len, "invalid length to lua_pushlstring"); s = s.slice(0, len); } ts = lstring.luaS_bless(L, s); @@ -247,13 +248,11 @@ const lua_pushlstring = function(L, s, len) { }; const lua_pushstring = function (L, s) { - assert(defs.is_luastring(s) || s === undefined || s === null, "lua_pushstring expects array of byte"); - if (s === undefined || s === null) { L.stack[L.top] = new TValue(CT.LUA_TNIL, null); L.top++; } else { - let ts = lstring.luaS_new(L, s); + let ts = lstring.luaS_new(L, defs.from_userstring(s)); lobject.pushsvalue2s(L, ts); s = ts.getstr(); /* internal copy */ } @@ -263,23 +262,22 @@ const lua_pushstring = function (L, s) { }; const lua_pushvfstring = function (L, fmt, argp) { - assert(defs.is_luastring(fmt)); + fmt = defs.from_userstring(fmt); return lobject.luaO_pushvfstring(L, fmt, argp); }; const lua_pushfstring = function (L, fmt, ...argp) { - assert(defs.is_luastring(fmt)); + fmt = defs.from_userstring(fmt); return lobject.luaO_pushvfstring(L, fmt, argp); }; /* Similar to lua_pushstring, but takes a JS string */ const lua_pushliteral = function (L, s) { - assert(typeof s === "string" || s === undefined || s === null, "lua_pushliteral expects a JS string"); - if (s === undefined || s === null) { L.stack[L.top] = new TValue(CT.LUA_TNIL, null); L.top++; } else { + assert(typeof s === "string", "lua_pushliteral expects a JS string"); let ts = lstring.luaS_newliteral(L, s); lobject.pushsvalue2s(L, ts); s = ts.getstr(); /* internal copy */ @@ -351,9 +349,7 @@ const lua_pushglobaltable = function(L) { ** t[k] = value at the top of the stack (where 'k' is a string) */ const auxsetstr = function(L, t, k) { - assert(defs.is_luastring(k), "key must be an array of bytes"); - - let str = lstring.luaS_new(L, k); + let str = lstring.luaS_new(L, defs.from_userstring(k)); assert(1 < L.top - L.ci.funcOff, "not enough elements in the stack"); lobject.pushsvalue2s(L, str); /* push 'str' (to make it a TValue) */ assert(L.top <= L.ci.top, "stack overflow"); @@ -465,8 +461,7 @@ const lua_rawsetp = function(L, idx, p) { */ const auxgetstr = function(L, t, k) { - assert(defs.is_luastring(k), "key must be an array of bytes"); - let str = lstring.luaS_new(L, k); + let str = lstring.luaS_new(L, defs.from_userstring(k)); lobject.pushsvalue2s(L, str); assert(L.top <= L.ci.top, "stack overflow"); lvm.luaV_gettable(L, t, L.stack[L.top - 1], L.top - 1); @@ -904,8 +899,8 @@ const lua_arith = function(L, op) { const default_chunkname = defs.to_luastring("?"); const lua_load = function(L, reader, data, chunkname, mode) { if (!chunkname) chunkname = default_chunkname; - else assert(defs.is_luastring(chunkname), "lua_load expect an array of byte as chunkname"); - assert(mode ? defs.is_luastring(mode) : true, "lua_load expect an array of byte as mode"); + else chunkname = defs.from_userstring(chunkname); + if (mode !== null) mode = defs.from_userstring(mode); let z = new lzio.ZIO(L, reader, data); let status = ldo.luaD_protectedparser(L, z, chunkname, mode); if (status === TS.LUA_OK) { /* no errors? */ diff --git a/src/ldebug.js b/src/ldebug.js index c93df91..1bb7a8a 100644 --- a/src/ldebug.js +++ b/src/ldebug.js @@ -266,6 +266,7 @@ const auxgetinfo = function(L, what, ar, f, ci) { }; const lua_getinfo = function(L, what, ar) { + what = defs.from_userstring(what); let status, cl, ci, func; swapextra(L); if (what[0] === '>'.charCodeAt(0)) { diff --git a/src/lobject.js b/src/lobject.js index c02bc91..1b490f3 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -547,6 +547,7 @@ const luaO_pushvfstring = function(L, fmt, argp) { let s = argp[a++]; if (s === null) s = defs.to_luastring("(null)", true); else { + s = defs.from_userstring(s); /* respect null terminator */ let i = s.indexOf(0); if (i !== -1) -- cgit v1.2.3-54-g00ecf