From aea4af6bafafd0aa166e41ead5ce90b530e3ac0a Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Fri, 17 Mar 2017 15:08:28 +0100 Subject: string.packsize --- tests/lstrlib.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'tests/lstrlib.js') diff --git a/tests/lstrlib.js b/tests/lstrlib.js index 23cd786..4185f04 100644 --- a/tests/lstrlib.js +++ b/tests/lstrlib.js @@ -459,4 +459,38 @@ test('string.dump', function (t) { "hello1212.5", "Correct element(s) on the stack" ); +}); + + +test('string.pack/unpack/packsize', function (t) { + let luaCode = ` + local s1, n, s2 = "hello", 2, "you" + local packed = string.pack("zjz", s1, n, s2) + local us1, un, us2 = string.unpack("zjz", packed) + return s1 == us1 and n == un and s2 == us2 + `, L; + + t.plan(3); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, luaCode); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lapi.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + + t.strictEqual( + lapi.lua_tostring(L, -1), + "FFFFFFF", + "Correct element(s) on the stack" + ); }); \ No newline at end of file -- cgit v1.2.3-54-g00ecf From 6d684333cbbf92ce940b0f10b126197bb2e04b93 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Fri, 17 Mar 2017 16:06:10 +0100 Subject: string.pack/unpack/packsize test --- sizes | Bin 8656 -> 0 bytes sizes.c | 9 -------- src/lstrlib.js | 64 ++++++++++++++++++++++++++++++++++++------------------- tests/lstrlib.js | 17 +++++++++------ 4 files changed, 53 insertions(+), 37 deletions(-) delete mode 100755 sizes delete mode 100644 sizes.c (limited to 'tests/lstrlib.js') diff --git a/sizes b/sizes deleted file mode 100755 index eabd657..0000000 Binary files a/sizes and /dev/null differ diff --git a/sizes.c b/sizes.c deleted file mode 100644 index 647c169..0000000 --- a/sizes.c +++ /dev/null @@ -1,9 +0,0 @@ -#include - -int main(void) { - - printf("sizeof(char): %lu\nsizeof(short): %lu\nsizeof(long): %lu\nsizeof(size_t): %lu\nsizeof(float): %lu\nsizeof(double): %lu\nsizeof(int): %lu\n", - sizeof(char), sizeof(short), sizeof(long), sizeof(size_t), sizeof(float), sizeof(double), sizeof(int)); - - return 0; -} \ No newline at end of file diff --git a/src/lstrlib.js b/src/lstrlib.js index 030e063..56fcc11 100644 --- a/src/lstrlib.js +++ b/src/lstrlib.js @@ -397,17 +397,18 @@ const KOption = { }; const digit = function(c) { - return '0'.charCodeAt(0) <= c && c <= '9'.charCodeAt(0); + return '0'.charCodeAt(0) <= c.charCodeAt(0) && c.charCodeAt(0) <= '9'.charCodeAt(0); }; const getnum = function(fmt, df) { - if (!digit(fmt)) /* no number? */ + if (!digit(fmt.s[0])) /* no number? */ return df; /* return default value */ else { let a = 0; do { - a = a * 10 + ((fmt = fmt.slice(1))[0] - '0'.charCodeAt(0)); - } while (digit(fmt[0]) && a <= (MAXSIZE - 9)/10); + a = a * 10 + (fmt.s[0].charCodeAt(0) - '0'.charCodeAt(0)); + fmt.s = fmt.s.slice(1); + } while (digit(fmt.s[0]) && a <= (MAXSIZE - 9)/10); return a; } }; @@ -432,7 +433,8 @@ const getoption = function(h, fmt) { size: NaN }; - r.opt = (fmt = fmt.slice(1))[0]; + r.opt = fmt.s[0]; + fmt.s = fmt.s.slice(1); r.size = 0; /* default */ switch (r.opt) { case 'b': r.size = 1; r.opt = KOption.Kint; return r; // sizeof(char): 1 @@ -492,8 +494,8 @@ const getdetails = function(h, totalsize, fmt) { r.size = opt.size; r.opt = opt.opt; let align = r.size; /* usually, alignment follows size */ - if (opt === KOption.Kpaddalign) { /* 'X' gets alignment from following option */ - if (fmt[0] === 0) + if (r.opt === KOption.Kpaddalign) { /* 'X' gets alignment from following option */ + if (fmt.s[0] === 0) lauxlib.luaL_argerror(h.L, 1, "invalid next option for option 'X'"); else { let o = getoption(h, fmt); @@ -503,7 +505,7 @@ const getdetails = function(h, totalsize, fmt) { lauxlib.luaL_argerror(h.L, 1, "invalid next option for option 'X'"); } } - if (align <= 1 || opt === KOption.Kchar) /* need no alignment? */ + if (align <= 1 || r.opt === KOption.Kchar) /* need no alignment? */ r.ntoalign = 0; else { if (align > h.maxalign) /* enforce maximum alignment */ @@ -533,7 +535,7 @@ const packint = function(b, n, islittle, size, neg) { for (let i = SZINT; i < size; i++) /* correct extra bytes */ buff[islittle ? i : size - 1 - i] = MC; } - b.concat(buff); /* add result to buffer */ + b.push(...buff); /* add result to buffer */ }; const packnum = function(b, n, islittle, size) { @@ -546,12 +548,17 @@ const packnum = function(b, n, islittle, size) { const str_pack = function(L) { let b = []; - let h = new Header(); + let h = new Header(L); let fmt = lauxlib.luaL_checkstring(L, 1).split(''); /* format string */ + fmt.push('\0'); // Add \0 to avoid overflow + fmt = { + s: fmt, + off: 0 + }; let arg = 1; /* current argument to pack */ let totalsize = 0; /* accumulate total size of result */ lapi.lua_pushnil(L); /* mark to separate arguments from string buffer */ - while (fmt.length > 0) { + while (fmt.s.length - 1 > 0) { let details = getdetails(h, totalsize, fmt); let opt = details.opt; let size = details.size; @@ -584,10 +591,10 @@ const str_pack = function(L) { } case KOption.Kchar: { /* fixed-size string */ let s = lauxlib.luaL_checkstring(L, arg); - s = L.stack[lapi.index2addr_(L, arg)].value; + s = L.stack[lapi.index2addr_(L, arg)]; let len = s.value.length; lauxlib.luaL_argcheck(L, len <= size, arg, "string long than given size"); - b.concat(s.value); /* add string */ + b.push(...s.value); /* add string */ while (len++ < size) /* pad extra space */ b.push(LUAL_PACKPADBYTE); break; @@ -598,7 +605,7 @@ const str_pack = function(L) { let len = s.value.length; lauxlib.luaL_argcheck(L, size >= NB || len < (1 << size * NB), arg, "string length does not fit in given size"); packint(b, len, h.islittle, size, 0); /* pack length */ - b.concat(s.value); + b.push(...s.value); totalsize += len; break; } @@ -607,7 +614,7 @@ const str_pack = function(L) { s = L.stack[lapi.index2addr_(L, arg)].value; let len = s.value.length; lauxlib.luaL_argcheck(L, s.value.length === String.fromCharCode(...s.value).length, arg, "strings contains zeros"); - b.concat(s.value); + b.push(...s.value); b.push(0); /* add zero at the end */ totalsize += len + 1; break; @@ -669,8 +676,13 @@ const str_byte = function(L) { const str_packsize = function(L) { let h = new Header(L); let fmt = lauxlib.luaL_checkstring(L, 1).split(''); + fmt.push('\0'); // Add \0 to avoid overflow + fmt = { + s: fmt, + off: 0 + }; let totalsize = 0; /* accumulate total size of result */ - while (fmt.length > 0) { + while (fmt.s.length - 1 > 0) { let details = getdetails(h, totalsize, fmt); let opt = details.opt; let size = details.size; @@ -731,18 +743,23 @@ const unpacknum = function(L, b, islittle, size) { const str_unpack = function(L) { let h = new Header(L); let fmt = lauxlib.luaL_checkstring(L, 1).split(''); + fmt.push('\0'); // Add \0 to avoid overflow + fmt = { + s: fmt, + off: 0 + }; let data = lauxlib.luaL_checkstring(L, 2); - data = L.stack[lapi.index2addr_(L, 2)]; + data = L.stack[lapi.index2addr_(L, 2)].value; let ld = data.length; let pos = posrelat(lauxlib.luaL_optinteger(L, 3, 1), ld) - 1; let n = 0; /* number of results */ lauxlib.luaL_argcheck(L, pos <= ld, 3, "initial position out of string"); - while (fmt.length > 0) { + while (fmt.s.length - 1 > 0) { let details = getdetails(h, pos, fmt); let opt = details.opt; let size = details.size; let ntoalign = details.ntoalign; - if (ntoalign + size > ~pos || pos + ntoalign + size > ld) + if (/*ntoalign + size > ~pos ||*/ pos + ntoalign + size > ld) lauxlib.luaL_argerror(L, 2, "data string too short"); pos += ntoalign; /* skip alignment */ /* stack space for item + next position */ @@ -761,19 +778,22 @@ const str_unpack = function(L) { break; } case KOption.Kchar: { - lapi.lua_pushstring(L, data.slice(pos, pos + size)); + // lapi.lua_pushstring(L, data.slice(pos, pos + size)); + L.stack[L.top++] = new lobject.TValue(CT.LUA_TLNGSTR, data.slice(pos, pos + size)); break; } case KOption.Kstring: { let len = unpackint(L, data.slice(pos), h.islittle, size, 0); lauxlib.luaL_argcheck(L, pos + len + size <= ld, 2, "data string too short"); - lapi.lua_pushstring(L, data.slice(pos + size, pos + size + len)); + // lapi.lua_pushstring(L, data.slice(pos + size, pos + size + len)); + L.stack[L.top++] = new lobject.TValue(CT.LUA_TLNGSTR, data.slice(pos + size, pos + size + len)); pos += len; /* skip string */ break; } case KOption.Kzstr: { let len = data.slice(pos).indexOf(0); - lapi.lua_pushstring(L, data.slice(pos, pos + len)); + // lapi.lua_pushstring(L, data.slice(pos, pos + len)); + L.stack[L.top++] = new lobject.TValue(CT.LUA_TLNGSTR, data.slice(pos, pos + len)); pos += len + 1; /* skip string plus final '\0' */ break; } diff --git a/tests/lstrlib.js b/tests/lstrlib.js index 4185f04..6364c7f 100644 --- a/tests/lstrlib.js +++ b/tests/lstrlib.js @@ -465,12 +465,12 @@ test('string.dump', function (t) { test('string.pack/unpack/packsize', function (t) { let luaCode = ` local s1, n, s2 = "hello", 2, "you" - local packed = string.pack("zjz", s1, n, s2) - local us1, un, us2 = string.unpack("zjz", packed) - return s1 == us1 and n == un and s2 == us2 + local packed = string.pack("c5jc3", s1, n, s2) + local us1, un, us2 = string.unpack("c5jc3", packed) + return string.packsize("c5jc3"), s1 == us1 and n == un and s2 == us2 `, L; - t.plan(3); + t.plan(4); t.doesNotThrow(function () { @@ -489,8 +489,13 @@ test('string.pack/unpack/packsize', function (t) { }, "Lua program ran without error"); t.strictEqual( - lapi.lua_tostring(L, -1), - "FFFFFFF", + lapi.lua_tointeger(L, -2), + 16, + "Correct element(s) on the stack" + ); + + t.ok( + lapi.lua_toboolean(L, -1), "Correct element(s) on the stack" ); }); \ No newline at end of file -- cgit v1.2.3-54-g00ecf From 645437b8e0846635f886520bb679a79ec1849c09 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Mon, 20 Mar 2017 14:21:21 +0100 Subject: string.match/find test --- src/lstrlib.js | 52 +++++++++++++++-------- tests/lstrlib.js | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+), 17 deletions(-) (limited to 'tests/lstrlib.js') diff --git a/src/lstrlib.js b/src/lstrlib.js index 64870d6..cedb403 100644 --- a/src/lstrlib.js +++ b/src/lstrlib.js @@ -10,7 +10,8 @@ const lua = require('./lua.js'); const luaconf = require('./luaconf.js'); const CT = lua.constant_types; -const L_ESC = '%'.charCodeAt(0); +const sL_ESC = '%'; +const L_ESC = sL_ESC.charCodeAt(0); // (sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t)(INT_MAX)) const MAXSIZE = Number.MAX_SAFE_INTEGER; @@ -836,13 +837,27 @@ class MatchState { this.L = L; this.matchdepth = NaN; /* control for recursive depth */ this.level = NaN; /* total number of captures (finished or unfinished) */ - this.captures = []; + this.capture = []; } } +const check_capture = function(ms, l) { + l = String.fromCharCode(l - '1'.charCodeAt(0)); + if (l < 0 || l >= ms.level || ms.capture[l].len === CAP_UNFINISHED) + return lauxlib.luaL_error(ms.L, `invalid capture index %${l + 1}`); + return l; +}; + +const capture_to_close = function(ms) { + let level = ms.level; + for (level--; level >= 0; level--) + if (ms.capture[level].len === CAP_UNFINISHED) return level; + return lauxlib.luaL_error(ms.L, "invalid pattern capture"); +}; + const classend = function(ms, p) { switch(ms.p.charAt(p++)) { - case L_ESC: { + case sL_ESC: { if (p === ms.p_end) lauxlib.luaL_error(ms.L, "malformed pattern (ends with '%')"); return p + 1; @@ -863,12 +878,12 @@ const classend = function(ms, p) { } }; -const match_class = function(ms, c, cl) { +const match_class = function(c, cl) { let res; switch (cl.toLowerCase()) { case 'a' : res = isalpha(c); break; case 'c' : res = iscntrl(c); break; - case 'd' : res = isdigit(c); break; + case 'd' : res = isdigit(c.charCodeAt(0)); break; case 'g' : res = isgraph(c); break; case 'l' : res = islower(c); break; case 'p' : res = ispunct(c); break; @@ -909,7 +924,7 @@ const singlematch = function(ms, s, p, ep) { let c = ms.src.charAt(s); switch (ms.p.charAt(p)) { case '.': return true; /* matches any char */ - case L_ESC: return match_class(c, ms.p.charAt(p + 1)); + case sL_ESC: return match_class(c, ms.p.charAt(p + 1)); case '[': return matchbracketclass(ms, c, p, ep - 1); default: return ms.p.charAt(p) === c; } @@ -962,6 +977,7 @@ const min_expand = function(ms, s, p, ep) { const start_capture = function(ms, s, p, what) { let level = ms.level; if (level >= LUA_MAXCAPTURES) lauxlib.luaL_error(ms.L, "too many captures"); + ms.capture[level] = ms.capture[level] ? ms.capture[level] : {}; ms.capture[level].init = s; ms.capture[level].len = what; ms.level = level + 1; @@ -1001,13 +1017,13 @@ const match = function(ms, s, p) { switch (gotodefault ? 'x' : ms.p.charAt(p)) { case '(': { /* start capture */ if (ms.p.charAt(p + 1) === ')') /* position capture? */ - s = start_capture(ms, s, 2, CAP_POSITION); + s = start_capture(ms, s, p + 2, CAP_POSITION); else - s = start_capture(ms, s, 1, CAP_UNFINISHED); + s = start_capture(ms, s, p + 1, CAP_UNFINISHED); break; } case ')': { /* end capture */ - s = end_capture(ms, s, 1); + s = end_capture(ms, s, p + 1); break; } case '$': { @@ -1018,7 +1034,7 @@ const match = function(ms, s, p) { s = ms.src.slice(s).length === 0 ? s : null; /* check end of string */ break; } - case L_ESC: { /* escaped sequences not in the format class[*+?-]? */ + case sL_ESC: { /* escaped sequences not in the format class[*+?-]? */ switch (ms.p.charAt(p + 1)) { case 'b': { /* balanced string? */ s = matchbalance(ms, s, p + 2); @@ -1058,7 +1074,7 @@ const match = function(ms, s, p) { let ep = classend(ms, p); /* points to optional suffix */ /* does not match at least once? */ if (!singlematch(ms, s, p, ep)) { - if (ep.charAt(0) === '*' || ep.charAt(0) === '?' || ep.charAt(0) === '-') { /* accept empty? */ + if (ms.p.charAt(ep) === '*' || ms.p.charAt(ep) === '?' || ms.p.charAt(ep) === '-') { /* accept empty? */ p = ep + 1; gotoinit = true; break; } else /* '+' or no suffix */ s = null; /* fail */ @@ -1094,7 +1110,7 @@ const match = function(ms, s, p) { return s; }; -const push_onecatpure = function(ms, i, s, e) { +const push_onecapture = function(ms, i, s, e) { if (i >= ms.level) { if (i === 0) lapi.lua_pushlstring(ms.L, s, e); /* add whole match */ @@ -1106,7 +1122,7 @@ const push_onecatpure = function(ms, i, s, e) { if (l === CAP_POSITION) lapi.lua_pushinteger(ms.L, ms.src_init + 1); else - lapi.lua_pushlstring(ms.L, ms.capture[i].init, l); + lapi.lua_pushlstring(ms.L, ms.src.slice(ms.capture[i].init), l); } }; @@ -1114,7 +1130,7 @@ const push_captures = function(ms, s, e) { let nlevels = ms.level === 0 && s ? 1 : ms.level; lauxlib.luaL_checkstack(ms.L, nlevels, "too many catpures"); for (let i = 0; i < nlevels; i++) - push_onecatpure(ms, i, s, e); + push_onecapture(ms, i, s, e); return nlevels; /* number of strings pushed */ }; @@ -1168,7 +1184,7 @@ const str_find_aux = function(L, find) { /* do a plain search */ let f = s.indexOf(p); if (f > -1) { - lapi.lua_pushinteger(L, f); + lapi.lua_pushinteger(L, f + 1); lapi.lua_pushinteger(L, f + lp); return 2; } @@ -1185,7 +1201,7 @@ const str_find_aux = function(L, find) { reprepstate(ms); if ((res = match(ms, s1, 0)) !== null) { if (find) { - lapi.lua_pushinteger(L, s1); /* start */ + lapi.lua_pushinteger(L, s1 + 1); /* start */ lapi.lua_pushinteger(L, res); /* end */ return push_captures(ms, null, 0) + 2; } else @@ -1193,6 +1209,8 @@ const str_find_aux = function(L, find) { } } while (s1++ < ms.src_end && !anchor); } + lapi.lua_pushnil(L); /* not found */ + return 1; }; const str_find = function(L) { @@ -1200,7 +1218,7 @@ const str_find = function(L) { }; const str_match = function(L) { - return str_find_aux(L, 1); + return str_find_aux(L, 0); }; const strlib = { diff --git a/tests/lstrlib.js b/tests/lstrlib.js index 6364c7f..aac3851 100644 --- a/tests/lstrlib.js +++ b/tests/lstrlib.js @@ -498,4 +498,127 @@ test('string.pack/unpack/packsize', function (t) { lapi.lua_toboolean(L, -1), "Correct element(s) on the stack" ); +}); + + +test('string.find without pattern', function (t) { + let luaCode = ` + return string.find("hello to you", " to ") + `, L; + + t.plan(4); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, luaCode); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lapi.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + + t.strictEqual( + lapi.lua_tointeger(L, -2), + 6, + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tointeger(L, -1), + 9, + "Correct element(s) on the stack" + ); +}); + + +test('string.match', function (t) { + let luaCode = ` + return string.match("foo: 123 bar: 456", "(%a+):%s*(%d+)") + `, L; + + t.plan(4); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, luaCode); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lapi.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + + t.strictEqual( + lapi.lua_tostring(L, -2), + "foo", + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tostring(L, -1), + "123", + "Correct element(s) on the stack" + ); +}); + + +test('string.find', function (t) { + let luaCode = ` + return string.find("foo: 123 bar: 456", "(%a+):%s*(%d+)") + `, L; + + t.plan(6); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, luaCode); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lapi.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + + t.strictEqual( + lapi.lua_tointeger(L, -4), + 1, + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tointeger(L, -3), + 8, + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tostring(L, -2), + "foo", + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tostring(L, -1), + "123", + "Correct element(s) on the stack" + ); }); \ No newline at end of file -- cgit v1.2.3-54-g00ecf From 5f5ba74ace8a956ae48a9f0b182a157052c2546e Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Mon, 20 Mar 2017 16:15:36 +0100 Subject: string.gsub --- README.md | 14 ++++++-------- src/lapi.js | 20 ++++++++++++++++++++ src/lstrlib.js | 40 ++++++++++++++++++++++++++++++++++++++++ tests/lstrlib.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 8 deletions(-) (limited to 'tests/lstrlib.js') diff --git a/README.md b/README.md index 6de8cab..2d1e35a 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,6 @@ - [ ] lua_islightuserdata - [ ] lua_isthread - [ ] lua_isuserdata - - [ ] lua_newuserdata - [ ] lua_pcallk - [ ] lua_pushfstring - [ ] lua_pushlightuserdata @@ -57,7 +56,6 @@ - [ ] lua_setlocal - [ ] lua_setuservalue - [ ] lua_tocfunction - - [ ] lua_touserdata - [ ] lua_upvalueid - [ ] lua_upvaluejoin - [ ] Auxiliary library @@ -99,20 +97,20 @@ - [x] string.byte - [x] string.char - [x] string.dump + - [x] string.find - [x] string.format + - [x] string.gsub - [x] string.len - [x] string.lower + - [x] string.match + - [x] string.pack + - [x] string.packsize - [x] string.rep - [x] string.reverse - [x] string.sub + - [x] string.unpack - [x] string.upper - - [ ] string.find - [ ] string.gmatch - - [ ] string.gsub - - [ ] string.match - - [ ] string.pack - - [ ] string.packsize - - [ ] string.unpack - [ ] Package - [ ] os - [ ] io diff --git a/src/lapi.js b/src/lapi.js index a884a97..6840ef9 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -428,6 +428,14 @@ const lua_createtable = function(L, narray, nrec) { assert(L.top <= L.ci.top, "stack overflow"); }; +const lua_newuserdata = function(L, u) { + L.stack[L.top++] = new lobject.TValue(CT.LUA_TUSERDATA, u); + + assert(L.top <= L.ci.top, "stack overflow"); + + return L.stack[L.top - 1].value; +}; + const aux_upvalue = function(fi, n) { switch(fi.ttype()) { case CT.LUAT_TCCL: { /* C closure */ @@ -578,6 +586,16 @@ const lua_tonumber = function(L, idx) { return lvm.tonumber(index2addr(L, idx)); }; +const lua_touserdata = function(L, idx) { + let o = index2addr(L, idx); + switch (o.ttnov()) { + case CT.LUA_TUSERDATA: + case CT.LUA_TLIGHTUSERDATA: + return o.value; + default: return null; + } +}; + const lua_tothread = function(L, idx) { let o = index2addr(L, idx); return o.ttisthread() ? o.value : null; @@ -879,6 +897,7 @@ module.exports.lua_istable = lua_istable; module.exports.lua_len = lua_len; module.exports.lua_load = lua_load; module.exports.lua_newtable = lua_newtable; +module.exports.lua_newuserdata = lua_newuserdata; module.exports.lua_next = lua_next; module.exports.lua_pcall = lua_pcall; module.exports.lua_pcallk = lua_pcallk; @@ -924,6 +943,7 @@ module.exports.lua_tonumber = lua_tonumber; module.exports.lua_topointer = lua_topointer; module.exports.lua_tostring = lua_tostring; module.exports.lua_tothread = lua_tothread; +module.exports.lua_touserdata = lua_touserdata; module.exports.lua_type = lua_type; module.exports.lua_typename = lua_typename; module.exports.lua_version = lua_version; diff --git a/src/lstrlib.js b/src/lstrlib.js index cedb403..9caa3cc 100644 --- a/src/lstrlib.js +++ b/src/lstrlib.js @@ -1221,12 +1221,52 @@ const str_match = function(L) { return str_find_aux(L, 0); }; +/* state for 'gmatch' */ +class GMatchState { + constructor() { + this.src = NaN; /* current position */ + this.p = NaN; /* pattern */ + this.lastmatch = NaN; /* end of last match */ + this.ms = new MatchState(); /* match state */ + } +} + +const gmatch_aux = function(L) { + let gm = lapi.lua_touserdata(L, lua.lua_upvalueindex(3)); + gm.ms.L = L; + for (let src = 0; src < gm.ms.src_end; src++) { + reprepstate(gm.ms); + let e; + if ((e = match(gm.ms, src, gm.p)) !== null && e !== gm.lastmatch) { + gm.src = gm.lastmatch = e; + return push_captures(gm.ms, src, e); + } + } + return 0; /* not found */ +}; + +const str_gmatch = function(L) { + let s = lauxlib.luaL_checkstring(L, 1); + let p = lauxlib.luaL_checkstring(L, 2); + let ls = s.length; + let lp = p.length; + lapi.lua_settop(L, 2); /* keep them on closure to avoid being collected */ + let gm = lapi.lua_newuserdata(L, new GMatchState()); + prepstate(gm.ms, L, s, ls, p, lp); + gm.src = s; + gm.p = p; + gm.lastmatch = null; + lapi.lua_pushcclosure(L, gmatch_aux, 3); + return 1; +}; + const strlib = { "byte": str_byte, "char": str_char, "dump": str_dump, "find": str_find, "format": str_format, + "gmatch": str_gmatch, "len": str_len, "lower": str_lower, "match": str_match, diff --git a/tests/lstrlib.js b/tests/lstrlib.js index aac3851..ce77bdc 100644 --- a/tests/lstrlib.js +++ b/tests/lstrlib.js @@ -621,4 +621,60 @@ test('string.find', function (t) { "123", "Correct element(s) on the stack" ); +}); + + +test('string.gmatch', function (t) { + let luaCode = ` + local s = "hello world from Lua" + local t = {} + + for w in string.gmatch(s, "%a+") do + table.insert(t, w) + end + + return table.unpack(t) + `, L; + + t.plan(6); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, luaCode); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lapi.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + + t.strictEqual( + lapi.lua_tostring(L, -4), + "hello", + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tostring(L, -3), + "world", + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tostring(L, -2), + "from", + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tostring(L, -1), + "Lua", + "Correct element(s) on the stack" + ); }); \ No newline at end of file -- cgit v1.2.3-54-g00ecf From 66292a6a85cf22cae31520ea4d08c76f544e338a Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Tue, 21 Mar 2017 08:38:41 +0100 Subject: string.gsub tests --- src/lapi.js | 2 +- src/lauxlib.js | 4 +- src/ljstype.js | 10 +-- src/lstrlib.js | 6 +- tests/lstrlib.js | 189 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 201 insertions(+), 10 deletions(-) (limited to 'tests/lstrlib.js') diff --git a/src/lapi.js b/src/lapi.js index 6840ef9..5872545 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -542,7 +542,7 @@ const lua_toboolean = function(L, idx) { const lua_tolstring = function(L, idx) { let o = index2addr(L, idx); - if (!o.ttisstring() && !o.ttisnumber()) + if ((!o.ttisstring() && !o.ttisnumber())) return null; return o.ttisstring() ? o.jsstring() : `${o.value}`; diff --git a/src/lauxlib.js b/src/lauxlib.js index 879d081..ac7ab00 100644 --- a/src/lauxlib.js +++ b/src/lauxlib.js @@ -299,10 +299,12 @@ const luaL_tolstring = function(L, idx) { } else { switch(lapi.lua_type(L, idx)) { case CT.LUA_TNUMBER: - case CT.LUA_TSTRING: case CT.LUA_TBOOLEAN: lapi.lua_pushstring(L, `${lapi.index2addr(L, idx).value}`); break; + case CT.LUA_TSTRING: + lapi.lua_pushstring(L, lapi.index2addr(L, idx).jsstring()); + break; case CT.LUA_TNIL: lapi.lua_pushstring(L, `nil`); break; diff --git a/src/ljstype.js b/src/ljstype.js index ef4be1d..7de9d9a 100644 --- a/src/ljstype.js +++ b/src/ljstype.js @@ -3,23 +3,23 @@ const assert = require('assert'); const lisdigit = function(c) { - return /^\d$/.test(c.charAt(0)); + return typeof c === 'string' && /^\d$/.test(c.charAt(0)); }; const lisxdigit = function(c) { - return /^[0-9a-fA-F]$/.test(c.charAt(0)); + return typeof c === 'string' && /^[0-9a-fA-F]$/.test(c.charAt(0)); }; const lisspace = function(c) { - return /^\s$/.test(c.charAt(0)); + return typeof c === 'string' && /^\s$/.test(c.charAt(0)); }; const lislalpha = function(c) { - return /^[_a-zA-Z]$/.test(c.charAt(0)); + return typeof c === 'string' && /^[_a-zA-Z]$/.test(c.charAt(0)); }; const lislalnum = function(c) { - return /^[_a-zA-Z0-9]$/.test(c.charAt(0)); + return typeof c === 'string' && /^[_a-zA-Z0-9]$/.test(c.charAt(0)); }; module.exports.lisdigit = lisdigit; diff --git a/src/lstrlib.js b/src/lstrlib.js index dc3845b..3872cb2 100644 --- a/src/lstrlib.js +++ b/src/lstrlib.js @@ -176,7 +176,7 @@ const MAX_FORMAT = 32; const isalpha = e => /^[a-zA-Z]$/.test(e.charAt(0)); const isdigit = e => "0".charCodeAt(0) <= e && e <= "9".charCodeAt(0); const iscntrl = e => (0x00 <= e && e <= 0x1f) || e === 0x7f; -const isgraph = e => e.charCodeAt(0) > 32 && e.charCodeAt < 127; // TODO: Will only work for ASCII +const isgraph = e => e.charCodeAt(0) > 32 && e.charCodeAt(0) < 127; // TODO: Will only work for ASCII const islower = e => /^(?![A-Z]).*$/.test(e.charAt(0)); const isupper = e => /^(?![a-z]).*$/.test(e.charAt(0)); const isalnum = e => /^[a-zA-Z0-9]$/.test(e.charAt(0)); @@ -1269,7 +1269,7 @@ const add_s = function(ms, b, s, e) { lauxlib.luaL_addchar(b, news.charAt(i)); else { i++; /* skip ESC */ - if (!isdigit(news.charAt(i))) { + if (!isdigit(news.charCodeAt(i))) { if (news.charAt(i) !== sL_ESC) lauxlib.luaL_error(L, `invalid use of '${sL_ESC}' in replacement string`); lauxlib.luaL_addchar(b, news.charAt(i)); @@ -1277,7 +1277,7 @@ const add_s = function(ms, b, s, e) { lauxlib.luaL_addlstring(b, ms.src.slice(s), e - s); else { push_onecapture(ms, news.charCodeAt(i) - '1'.charCodeAt(0), s, e); - lauxlib.luaL_tostring(L, -1); + lauxlib.luaL_tolstring(L, -1); lapi.lua_remove(L, -2); /* remove original value */ lauxlib.luaL_addvalue(b); /* add capture to accumulated result */ } diff --git a/tests/lstrlib.js b/tests/lstrlib.js index ce77bdc..12df173 100644 --- a/tests/lstrlib.js +++ b/tests/lstrlib.js @@ -677,4 +677,193 @@ test('string.gmatch', function (t) { "Lua", "Correct element(s) on the stack" ); +}); + + +test('string.gsub', function (t) { + let luaCode = ` + return string.gsub("hello world", "(%w+)", "%1 %1") + `, L; + + t.plan(4); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, luaCode); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lapi.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + + t.strictEqual( + lapi.lua_tostring(L, -2), + "hello hello world world", + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tointeger(L, -1), + 2, + "Correct element(s) on the stack" + ); +}); + + +test('string.gsub (number)', function (t) { + let luaCode = ` + return string.gsub("hello world", "%w+", "%0 %0", 1) + `, L; + + t.plan(4); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, luaCode); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lapi.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + + t.strictEqual( + lapi.lua_tostring(L, -2), + "hello hello world", + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tointeger(L, -1), + 1, + "Correct element(s) on the stack" + ); +}); + + +test('string.gsub (pattern)', function (t) { + let luaCode = ` + return string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1") + `, L; + + t.plan(4); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, luaCode); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lapi.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + + t.strictEqual( + lapi.lua_tostring(L, -2), + "world hello Lua from", + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tointeger(L, -1), + 2, + "Correct element(s) on the stack" + ); +}); + + +test('string.gsub (function)', function (t) { + let luaCode = ` + return string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s) + return load(s)() + end) + `, L; + + t.plan(4); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, luaCode); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lapi.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + + t.strictEqual( + lapi.lua_tostring(L, -2), + "4+5 = 9", + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tointeger(L, -1), + 1, + "Correct element(s) on the stack" + ); +}); + + + +test('string.gsub (table)', function (t) { + let luaCode = ` + local t = {name="lua", version="5.3"} + return string.gsub("$name-$version.tar.gz", "%$(%w+)", t) + `, L; + + t.plan(4); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, luaCode); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lapi.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + + t.strictEqual( + lapi.lua_tostring(L, -2), + "lua-5.3.tar.gz", + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tointeger(L, -1), + 2, + "Correct element(s) on the stack" + ); }); \ No newline at end of file -- cgit v1.2.3-54-g00ecf