aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoit Giannangeli <giann008@gmail.com>2017-03-24 11:58:00 +0100
committerBenoit Giannangeli <giann008@gmail.com>2017-03-24 11:58:00 +0100
commit16cdb860e9e8e5550c1178a41eabb52e461ffa46 (patch)
tree87b2aa1ad671927134696c0cd1e61f2edd7e0abd
parent58c57c57354fce407b11c0d63f8926edf5f469c0 (diff)
downloadfengari-16cdb860e9e8e5550c1178a41eabb52e461ffa46.tar.gz
fengari-16cdb860e9e8e5550c1178a41eabb52e461ffa46.tar.bz2
fengari-16cdb860e9e8e5550c1178a41eabb52e461ffa46.zip
TValue.id to print something when tostring({})
-rw-r--r--src/lapi.js2
-rw-r--r--src/lauxlib.js2
-rw-r--r--src/lobject.js3
-rw-r--r--tests/single.lua26
4 files changed, 31 insertions, 2 deletions
diff --git a/src/lapi.js b/src/lapi.js
index 5064c30..3520b15 100644
--- a/src/lapi.js
+++ b/src/lapi.js
@@ -158,7 +158,7 @@ const lua_rotate = function(L, idx, n) {
let p = index2addr(L, idx);
let pIdx = index2addr_(L, idx);
- assert(!p.ttisnil() && idx > lua.LUA_REGISTRYINDEX, "index not in the stack");
+ assert(/*!p.ttisnil() && */idx > lua.LUA_REGISTRYINDEX, "index not in the stack");
assert((n >= 0 ? n : -n) <= (L.top - idx), "invalid 'n'");
let m = n >= 0 ? L.top - 1 - n : pIdx - n - 1; /* end of prefix */
diff --git a/src/lauxlib.js b/src/lauxlib.js
index 1b306a3..e3974cb 100644
--- a/src/lauxlib.js
+++ b/src/lauxlib.js
@@ -312,7 +312,7 @@ const luaL_tolstring = function(L, idx) {
default:
let tt = luaL_getmetafield(L, idx, "__name");
let kind = tt === CT.LUA_TSTRING ? lapi.lua_tostring(L, -1) : luaL_typename(L, idx);
- lapi.lua_pushstring(L, `${kind}`); // We can't print memory address in JS
+ lapi.lua_pushstring(L, `${kind}: 0x${lapi.index2addr(L, -1).id.toString(16)}`);
if (tt !== CT.LUA_TNIL)
lapi.lua_remove(L, -2);
break;
diff --git a/src/lobject.js b/src/lobject.js
index 8d6b6a3..3cbde26 100644
--- a/src/lobject.js
+++ b/src/lobject.js
@@ -8,9 +8,12 @@ const lua = require('./lua.js');
const CT = lua.constant_types;
const UpVal = require('./lfunc.js').UpVal;
+let tvalueCount = 0;
+
class TValue {
constructor(type, value) {
+ this.id = tvalueCount++;
this.type = type;
this.value = value;
this.metatable = null;
diff --git a/tests/single.lua b/tests/single.lua
index d9b6ce9..4960b11 100644
--- a/tests/single.lua
+++ b/tests/single.lua
@@ -105,3 +105,29 @@ if string.packsize("i") == 4 then
checkerror("too large", string.rep, 'aa', (1 << 30))
checkerror("too large", string.rep, 'a', (1 << 30), ',')
end
+
+-- repetitions with separator
+assert(string.rep('teste', 0, 'xuxu') == '')
+assert(string.rep('teste', 1, 'xuxu') == 'teste')
+assert(string.rep('\1\0\1', 2, '\0\0') == '\1\0\1\0\0\1\0\1')
+assert(string.rep('', 10, '.') == string.rep('.', 9))
+assert(not pcall(string.rep, "aa", maxi // 2 + 10))
+assert(not pcall(string.rep, "", maxi // 2 + 10, "aa"))
+
+assert(string.reverse"" == "")
+assert(string.reverse"\0\1\2\3" == "\3\2\1\0")
+assert(string.reverse"\0001234" == "4321\0")
+
+for i=0,30 do assert(string.len(string.rep('a', i)) == i) end
+
+assert(type(tostring(nil)) == 'string')
+assert(type(tostring(12)) == 'string')
+assert(string.find(tostring{}, 'table:'))
+assert(string.find(tostring(print), 'function:'))
+assert(#tostring('\0') == 1)
+assert(tostring(true) == "true")
+assert(tostring(false) == "false")
+assert(tostring(-1203) == "-1203")
+assert(tostring(1203.125) == "1203.125")
+assert(tostring(-0.5) == "-0.5")
+assert(tostring(-32767) == "-32767")