aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-02-17 13:47:33 +0100
committerBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-02-17 13:47:33 +0100
commitea562b1094672f641707c0f809827a0d3a982f23 (patch)
tree2cc9fe28a7553a58037b9541a25b2f5d2437c0f2
parentcdf8cf1806ca793c47095b69382b2dd733899af7 (diff)
downloadfengari-ea562b1094672f641707c0f809827a0d3a982f23.tar.gz
fengari-ea562b1094672f641707c0f809827a0d3a982f23.tar.bz2
fengari-ea562b1094672f641707c0f809827a0d3a982f23.zip
lua_newtable, lua_createtable
-rw-r--r--README.md4
-rw-r--r--src/lapi.js46
-rw-r--r--tests/lapi.js38
3 files changed, 73 insertions, 15 deletions
diff --git a/README.md b/README.md
index 6bade18..b86a4b4 100644
--- a/README.md
+++ b/README.md
@@ -48,6 +48,8 @@
- [x] lua_callk
- [x] lua_setglobal
- [x] lua_upvalueindex
+ - [x] lua_createtable
+ - [x] lua_newtable
- [ ] lua_absindex
- [ ] lua_arith
- [ ] lua_checkstack
@@ -55,7 +57,6 @@
- [ ] lua_compare
- [ ] lua_concat
- [ ] lua_copy
- - [ ] lua_createtable
- [ ] lua_dump
- [ ] lua_error
- [x] lua_gc (unvailable)
@@ -90,7 +91,6 @@
- [ ] lua_isuserdata
- [ ] lua_isyieldable
- [ ] lua_len
- - [ ] lua_newtable
- [ ] lua_newthread
- [ ] lua_newuserdata
- [ ] lua_next
diff --git a/src/lapi.js b/src/lapi.js
index 6a88f6f..089f138 100644
--- a/src/lapi.js
+++ b/src/lapi.js
@@ -88,27 +88,24 @@ const lua_pop = function(L, n) {
*/
const lua_pushnil = function(L) {
- L.stack[L.top] = ldo.nil;
+ L.stack[L.top++] = ldo.nil;
- L.top++;
assert(L.top <= L.ci.top, "stack overflow");
};
const lua_pushnumber = function(L, n) {
assert(typeof n === "number");
- L.stack[L.top] = new TValue(CT.LUA_TNUMFLT, n);
+ L.stack[L.top++] = new TValue(CT.LUA_TNUMFLT, n);
- L.top++;
assert(L.top <= L.ci.top, "stack overflow");
};
const lua_pushinteger = function(L, n) {
assert(typeof n === "number");
- L.stack[L.top] = new TValue(CT.LUA_TNUMINT, n|0);
+ L.stack[L.top++] = new TValue(CT.LUA_TNUMINT, n|0);
- L.top++;
assert(L.top <= L.ci.top, "stack overflow");
};
@@ -117,9 +114,8 @@ const lua_pushlstring = function(L, s, len) { // TODO: embedded \0
assert(typeof n === "number");
let ts = len === 0 ? new TValue(CT.LUA_TLNGSTR, "") : new TValue(CT.LUA_TLNGSTR, s.substr(0, len));
- L.stack[L.top] = ts;
+ L.stack[L.top++] = ts;
- L.top++;
assert(L.top <= L.ci.top, "stack overflow");
return ts.value;
@@ -174,18 +170,16 @@ const lua_pushcfunction = function(L, fn) {
const lua_pushjsfunction = lua_pushcfunction;
const lua_pushboolean = function(L, b) {
- L.stack[L.top] = new TValue(CT.LUA_TBOOLEAN, b ? true : false);
+ L.stack[L.top++] = new TValue(CT.LUA_TBOOLEAN, b ? true : false);
- L.top++;
assert(L.top <= L.ci.top, "stack overflow");
};
const lua_pushlightuserdata = function(L, p) {
assert(typeof p === "object");
- L.stack[L.top] = new TValue(CT.LUA_TLIGHTUSERDATA, p);
+ L.stack[L.top++] = new TValue(CT.LUA_TLIGHTUSERDATA, p);
- L.top++;
assert(L.top <= L.ci.top, "stack overflow");
};
@@ -218,6 +212,26 @@ const lua_setglobal = function(L, name) {
/*
+** get functions (Lua -> stack)
+*/
+
+// narray and nrec are mostly useless for this implementation
+const lua_createtable = function(L, narray, nrec) {
+ let t = new lobject.Table();
+ L.stack[L.top++] = t;
+
+ assert(L.top <= L.ci.top, "stack overflow");
+
+ if (narray > 0)
+ t.value.array = new Array(narray);
+};
+
+const lua_newtable = function(L) {
+ lua_createtable(L, 0, 0);
+};
+
+
+/*
** access functions (stack -> JS)
*/
@@ -261,6 +275,9 @@ const lua_typename = function(L, t) {
return ltm.ttypename(t);
};
+const lua_istable = function(L, idx) {
+ return index2addr(L, idx).ttistable();
+};
/*
** 'load' and 'call' functions (run Lua code)
@@ -381,4 +398,7 @@ module.exports.lua_load = lua_load;
module.exports.lua_callk = lua_callk;
module.exports.lua_call = lua_call;
module.exports.lua_pop = lua_pop;
-module.exports.lua_setglobal = lua_setglobal; \ No newline at end of file
+module.exports.lua_setglobal = lua_setglobal;
+module.exports.lua_istable = lua_istable;
+module.exports.lua_createtable = lua_createtable;
+module.exports.lua_newtable = lua_newtable; \ No newline at end of file
diff --git a/tests/lapi.js b/tests/lapi.js
index 419e5c4..561841f 100644
--- a/tests/lapi.js
+++ b/tests/lapi.js
@@ -438,4 +438,42 @@ test('lua script reads js upvalues', function (t) {
"hello world",
"Correct element(s) on the stack"
);
+});
+
+
+test('lua_createtable', function (t) {
+ let L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+ L = lauxlib.luaL_newstate();
+
+ lapi.lua_createtable(L, 3, 3);
+
+ }, "JS Lua program ran without error");
+
+ t.ok(
+ lapi.lua_istable(L, -1),
+ "Correct element(s) on the stack"
+ );
+});
+
+
+test('lua_newtable', function (t) {
+ let L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+ L = lauxlib.luaL_newstate();
+
+ lapi.lua_newtable(L);
+
+ }, "JS Lua program ran without error");
+
+ t.ok(
+ lapi.lua_istable(L, -1),
+ "Correct element(s) on the stack"
+ );
}); \ No newline at end of file