From 2ccf4ee80dbbf3e9af8ff006fcc18798e668a413 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Fri, 24 Feb 2017 11:14:25 +0100 Subject: table.remove --- README.md | 4 ++-- fengari.sublime-project | 11 +++++++++-- src/ltablib.js | 16 ++++++++++++++++ tests/ltablib.js | 36 +++++++++++++++++++++++++++++++++++- 4 files changed, 62 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a3f9278..00ceffa 100644 --- a/README.md +++ b/README.md @@ -207,11 +207,11 @@ - [x] Coroutine - [ ] Table - [x] table.concat + - [x] table.insert - [x] table.pack + - [x] table.remove - [x] table.unpack - - [x] table.insert - [ ] table.move - - [ ] table.remove - [ ] table.sort - [ ] Run [Lua test suite](https://github.com/lua/tests) - [ ] DOM API binding diff --git a/fengari.sublime-project b/fengari.sublime-project index 545a6d4..173bb4f 100644 --- a/fengari.sublime-project +++ b/fengari.sublime-project @@ -2,11 +2,18 @@ "folders": [ { - "path": ".", - "folder_exclude_patterns": ["*.dSYM", "*.out"] + "folder_exclude_patterns": + [ + "*.dSYM", + "*.out" + ], + "path": "." }, { "path": "/Users/giann/lua-5.3.4" + }, + { + "path": "/Users/giann/lua-tests" } ] } diff --git a/src/ltablib.js b/src/ltablib.js index 8ea6f08..6255761 100644 --- a/src/ltablib.js +++ b/src/ltablib.js @@ -83,6 +83,21 @@ const tinsert = function(L) { return 0; }; +const tremove = function(L) { + let size = aux_getn(L, 1, TAB_RW); + let pos = lauxlib.luaL_optinteger(L, 2, size); + if (pos !== size) /* validate 'pos' if given */ + lauxlib.luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); + lapi.lua_geti(L, 1, pos); /* result = t[pos] */ + for (; pos < size; pos++) { + lapi.lua_geti(L, 1, pos + 1); + lapi.lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */ + } + lapi.lua_pushnil(L); + lapi.lua_seti(L, 1, pos); /* t[pos] = nil */ + return 1; +}; + const tconcat = function(L) { let last = aux_getn(L, 1, TAB_R); let sep = lauxlib.luaL_optlstring(L, 2, ""); @@ -133,6 +148,7 @@ const tab_funcs = { "concat": tconcat, "insert": tinsert, "pack": pack, + "remove": tremove, "unpack": unpack }; diff --git a/tests/ltablib.js b/tests/ltablib.js index 1a04e4c..9a18ff6 100644 --- a/tests/ltablib.js +++ b/tests/ltablib.js @@ -145,9 +145,43 @@ test('table.insert', function (t) { t.deepEqual( [...lapi.lua_topointer(L, -1).entries()] - .filter(e => typeof e[0] === 'number') // Filter out the 'n' field + .filter(e => typeof e[0] === 'number') .map(e => e[1].value).sort(), [1, 2, 3, 4, 5], "Correct element(s) on the stack" ); +}); + + +test('table.remove', function (t) { + let luaCode = ` + local t = {1, 2, 3, 3, 4, 4} + table.remove(t) + table.remove(t, 3) + return t + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + let bc = toByteCode(luaCode).dataView; + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lapi.lua_load(L, bc, "test-table.insert"); + + lapi.lua_call(L, 0, -1); + + }, "JS Lua program ran without error"); + + t.deepEqual( + [...lapi.lua_topointer(L, -1).entries()] + .filter(e => typeof e[0] === 'number') + .map(e => e[1].value).sort(), + [1, 2, 3, 4, null, null], + "Correct element(s) on the stack" + ); }); \ No newline at end of file -- cgit v1.2.3-54-g00ecf