aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-02-17 12:25:28 +0100
committerBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-02-17 13:25:56 +0100
commitcdf8cf1806ca793c47095b69382b2dd733899af7 (patch)
treed101b270e2d07488efa1b3c33f9bf70f70c5ed5d /src
parenta8e82fc01f2558550289f76f55c917039296ec11 (diff)
downloadfengari-cdf8cf1806ca793c47095b69382b2dd733899af7.tar.gz
fengari-cdf8cf1806ca793c47095b69382b2dd733899af7.tar.bz2
fengari-cdf8cf1806ca793c47095b69382b2dd733899af7.zip
lua can read globals set by js
Diffstat (limited to 'src')
-rw-r--r--src/lapi.js33
-rw-r--r--src/lvm.js3
2 files changed, 32 insertions, 4 deletions
diff --git a/src/lapi.js b/src/lapi.js
index 6cc95b2..6a88f6f 100644
--- a/src/lapi.js
+++ b/src/lapi.js
@@ -50,7 +50,6 @@ const index2addr = function(L, idx) {
return idx <= ci.func.nupvalues ? ci.func.upvalue[idx - 1] : ldo.nil;
}
}
-
};
/*
@@ -190,6 +189,33 @@ const lua_pushlightuserdata = function(L, p) {
assert(L.top <= L.ci.top, "stack overflow");
};
+/*
+** set functions (stack -> Lua)
+*/
+
+/*
+** t[k] = value at the top of the stack (where 'k' is a string)
+*/
+const auxsetstr = function(L, t, k) {
+ let str = new TValue(CT.LUA_TLNGSTR, k);
+
+ assert(1 < L.top - L.ci.funcOff, "not enough elements in the stack");
+
+ if (t.ttistable() && !t.__index(t, k).ttisnil()) {
+ t.__newindex(t, k, L.stack[L.top - 1]);
+ L.top--; /* pop value */
+ } else {
+ L.stack[L.top] = str;
+ L.top++;
+ lvm.luaV_finishset(L, t, L.stack[L.top - 1], L.stack[L.top - 2], t.__index(t, k), 0);
+ L.top -= 2; /* pop value and key */
+ }
+};
+
+const lua_setglobal = function(L, name) {
+ auxsetstr(L, L.l_G.l_registry.value.array[lua.LUA_RIDX_GLOBALS], name);
+};
+
/*
** access functions (stack -> JS)
@@ -251,7 +277,7 @@ const lua_load = function(L, data, chunckname) {
let reg = L.l_G.l_registry;
let gt = reg.value.array[lua.LUA_RIDX_GLOBALS];
/* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
- f.upvals[0].v = gt; // TODO: is gt on the stack ? is that upvalue opened or closed ?
+ f.upvals[0].u.value = gt;
}
}
@@ -354,4 +380,5 @@ module.exports.lua_tostring = lua_tostring;
module.exports.lua_load = lua_load;
module.exports.lua_callk = lua_callk;
module.exports.lua_call = lua_call;
-module.exports.lua_pop = lua_pop; \ No newline at end of file
+module.exports.lua_pop = lua_pop;
+module.exports.lua_setglobal = lua_setglobal; \ No newline at end of file
diff --git a/src/lvm.js b/src/lvm.js
index 931b7a5..4f52678 100644
--- a/src/lvm.js
+++ b/src/lvm.js
@@ -1033,4 +1033,5 @@ module.exports.LEnum = LEnum;
module.exports.LEintfloat = LEintfloat;
module.exports.LTintfloat = LTintfloat;
module.exports.l_strcmp = l_strcmp;
-module.exports.luaV_objlen = luaV_objlen; \ No newline at end of file
+module.exports.luaV_objlen = luaV_objlen;
+module.exports.luaV_finishset = luaV_finishset; \ No newline at end of file