aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md4
-rw-r--r--src/lapi.js18
-rw-r--r--src/lvm.js4
-rw-r--r--tests/C/Makefile3
-rw-r--r--tests/C/lua_setgettable.c31
-rw-r--r--tests/lapi.js28
6 files changed, 83 insertions, 5 deletions
diff --git a/README.md b/README.md
index b86a4b4..e2a9864 100644
--- a/README.md
+++ b/README.md
@@ -50,6 +50,8 @@
- [x] lua_upvalueindex
- [x] lua_createtable
- [x] lua_newtable
+ - [x] lua_gettable
+ - [x] lua_settable
- [ ] lua_absindex
- [ ] lua_arith
- [ ] lua_checkstack
@@ -72,7 +74,6 @@
- [ ] lua_getlocal
- [ ] lua_getmetatable
- [ ] lua_getstack
- - [ ] lua_gettable
- [ ] lua_getupvalue
- [ ] lua_getuservalue
- [ ] lua_insert
@@ -122,7 +123,6 @@
- [ ] lua_seti
- [ ] lua_setlocal
- [ ] lua_setmetatable
- - [ ] lua_settable
- [ ] lua_settop
- [ ] lua_setupvalue
- [ ] lua_setuservalue
diff --git a/src/lapi.js b/src/lapi.js
index 089f138..705fec4 100644
--- a/src/lapi.js
+++ b/src/lapi.js
@@ -210,6 +210,14 @@ const lua_setglobal = function(L, name) {
auxsetstr(L, L.l_G.l_registry.value.array[lua.LUA_RIDX_GLOBALS], name);
};
+const lua_settable = function(L, idx) {
+ assert(2 < L.top - L.ci.funcOff, "not enough elements in the stack");
+
+ let t = index2addr(L, idx);
+ lvm.settable(L, t, L.stack[L.top - 2], L.stack[L.top - 1]);
+ L.top -= 2;
+};
+
/*
** get functions (Lua -> stack)
@@ -230,6 +238,12 @@ const lua_newtable = function(L) {
lua_createtable(L, 0, 0);
};
+const lua_gettable = function(L, idx) {
+ let t = index2addr(L, idx);
+ lvm.gettable(L, t, L.stack[L.top - 1], L.top - 1);
+ return L.stack[L.top - 1].ttnov();
+};
+
/*
** access functions (stack -> JS)
@@ -401,4 +415,6 @@ module.exports.lua_pop = lua_pop;
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
+module.exports.lua_newtable = lua_newtable;
+module.exports.lua_settable = lua_settable;
+module.exports.lua_gettable = lua_gettable; \ No newline at end of file
diff --git a/src/lvm.js b/src/lvm.js
index 4f52678..a75e305 100644
--- a/src/lvm.js
+++ b/src/lvm.js
@@ -1034,4 +1034,6 @@ module.exports.LEintfloat = LEintfloat;
module.exports.LTintfloat = LTintfloat;
module.exports.l_strcmp = l_strcmp;
module.exports.luaV_objlen = luaV_objlen;
-module.exports.luaV_finishset = luaV_finishset; \ No newline at end of file
+module.exports.luaV_finishset = luaV_finishset;
+module.exports.gettable = gettable;
+module.exports.settable = settable; \ No newline at end of file
diff --git a/tests/C/Makefile b/tests/C/Makefile
index 52475ec..2b753be 100644
--- a/tests/C/Makefile
+++ b/tests/C/Makefile
@@ -13,4 +13,5 @@ all:
$(CC) $(CFLAGS) $(LIBS) lua_pushcclosure-light.c -o lua_pushcclosure-light.out
$(CC) $(CFLAGS) $(LIBS) lua_call.c -o lua_call.out
$(CC) $(CFLAGS) $(LIBS) lua_call-jsclosure.c -o lua_call-jsclosure.out
- $(CC) $(CFLAGS) $(LIBS) lua_pop.c -o lua_pop.out \ No newline at end of file
+ $(CC) $(CFLAGS) $(LIBS) lua_pop.c -o lua_pop.out
+ $(CC) $(CFLAGS) $(LIBS) lua_setgettable.c -o lua_setgettable.out \ No newline at end of file
diff --git a/tests/C/lua_setgettable.c b/tests/C/lua_setgettable.c
new file mode 100644
index 0000000..af3d9f8
--- /dev/null
+++ b/tests/C/lua_setgettable.c
@@ -0,0 +1,31 @@
+#include <lua.h>
+#include <lualib.h>
+#include <lauxlib.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+int main(void) {
+
+ lua_State *L = luaL_newstate();
+
+ luaL_openlibs(L);
+
+ lua_newtable(L);
+
+ lua_pushstring(L, "key");
+ lua_pushstring(L, "value");
+
+ lua_settable(L, -3);
+
+ lua_pushstring(L, "key");
+ lua_gettable(L, -2);
+
+
+ printf("L->top(%d): %s\n", lua_gettop(L), luaL_typename(L, -1));
+ printf("Table has value: %s\n", lua_tostring(L, -1));
+
+ lua_close(L);
+
+ return 0;
+
+} \ No newline at end of file
diff --git a/tests/lapi.js b/tests/lapi.js
index 561841f..298328c 100644
--- a/tests/lapi.js
+++ b/tests/lapi.js
@@ -476,4 +476,32 @@ test('lua_newtable', function (t) {
lapi.lua_istable(L, -1),
"Correct element(s) on the stack"
);
+});
+
+
+test('lua_settable, lua_gettable', function (t) {
+ let L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+ L = lauxlib.luaL_newstate();
+
+ lapi.lua_newtable(L);
+
+ lapi.lua_pushstring(L, "key");
+ lapi.lua_pushstring(L, "value");
+
+ lapi.lua_settable(L, -3);
+
+ lapi.lua_pushstring(L, "key");
+ lapi.lua_gettable(L, -2);
+
+ }, "JS Lua program ran without error");
+
+ t.strictEqual(
+ lapi.lua_tostring(L, -1),
+ "value",
+ "Correct element(s) on the stack"
+ );
}); \ No newline at end of file