From 0e7ae6679bc7950b93799500a7749fbe61690ffa Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Fri, 17 Feb 2017 07:28:36 +0100 Subject: LUA_REGISTRYINDEX --- src/lapi.js | 7 ++++--- src/lua.js | 15 ++++++++++++--- src/luaconf.js | 12 ++++++++++++ 3 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 src/luaconf.js diff --git a/src/lapi.js b/src/lapi.js index 41c0fbb..dd74552 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -35,12 +35,13 @@ const index2addr = function(L, idx) { assert(idx <= ci.top - (ci.funcOff + 1), "unacceptable index"); if (o >= L.top) return ldo.nil; else return L.stack[o]; - } else if (idx < 0) { // TODO: pseudo-indices relative to LUA_REGISTRYINDEX + } else if (idx > lua.LUA_REGISTRYINDEX) { assert(idx !== 0 && -idx <= L.top, "invalid index"); return L.stack[L.top + idx]; - // TODO: if (idx == LUA_REGISTRYINDEX) return &G(L)->l_registry; + } else if (idx === lua.LUA_REGISTRYINDEX) { + return L.l_G.l_registry; } else { /* upvalues */ - idx = -idx; + idx = lua.LUA_REGISTRYINDEX - idx; assert(idx <= MAXUPVAL + 1, "upvalue index too large"); if (ci.func.ttislcf()) /* light C function? */ return ldo.nil; /* it has no upvalues */ diff --git a/src/lua.js b/src/lua.js index 245c826..58cc0cd 100644 --- a/src/lua.js +++ b/src/lua.js @@ -1,8 +1,9 @@ /*jshint esversion: 6 */ "use strict"; -const assert = require('assert'); -const lualib = require('./lualib.js'); +const assert = require('assert'); +const lualib = require('./lualib.js'); +const luaconf = require('./luaconf.js'); const LUA_VERSION_MAJOR = "5"; const LUA_VERSION_MINOR = "3"; @@ -64,6 +65,12 @@ constant_types.LUA_TCCL = constant_types.LUA_TFUNCTION | (2 << 4); /* C closure const LUA_NUMTAGS = 9; const LUA_MINSTACK = 20; +const LUA_REGISTRYINDEX = -luaconf.LUAI_MAXSTACK - 1000; + +const lua_upvalueindex = function(i) { + LUA_REGISTRYINDEX - i; +}; + /* predefined values in the registry */ const LUA_RIDX_MAINTHREAD = 1; const LUA_RIDX_GLOBALS = 2; @@ -99,4 +106,6 @@ module.exports.LUA_NUMTAGS = LUA_NUMTAGS; module.exports.LUA_MINSTACK = LUA_MINSTACK; module.exports.LUA_RIDX_MAINTHREAD = LUA_RIDX_MAINTHREAD; module.exports.LUA_RIDX_GLOBALS = LUA_RIDX_GLOBALS; -module.exports.LUA_RIDX_LAST = LUA_RIDX_LAST; \ No newline at end of file +module.exports.LUA_RIDX_LAST = LUA_RIDX_LAST; +module.exports.LUA_REGISTRYINDEX = LUA_REGISTRYINDEX; +module.exports.lua_upvalueindex = lua_upvalueindex; \ No newline at end of file diff --git a/src/luaconf.js b/src/luaconf.js new file mode 100644 index 0000000..b0456e3 --- /dev/null +++ b/src/luaconf.js @@ -0,0 +1,12 @@ +/*jshint esversion: 6 */ +"use strict"; + +/* +@@ LUAI_MAXSTACK limits the size of the Lua stack. +** CHANGE it if you need a different limit. This limit is arbitrary; +** its only purpose is to stop Lua from consuming unlimited stack +** space (and to reserve some numbers for pseudo-indices). +*/ +const LUAI_MAXSTACK = 1000000; + +module.exports.LUAI_MAXSTACK = LUAI_MAXSTACK; \ No newline at end of file -- cgit v1.2.3-70-g09d2