From 91e09ea32148c34965809b8d69987d439d389870 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 22 May 2017 18:06:40 +1000 Subject: Compare allowed stack indices to stack_last Not L.stack.length which is more equivalent to C's L->stacksize --- src/lapi.js | 3 ++- src/ldo.js | 8 +++++--- src/lstate.js | 7 +++++-- 3 files changed, 12 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/lapi.js b/src/lapi.js index 79c666c..ca5a8a0 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -94,7 +94,7 @@ const lua_checkstack = function(L, n) { let res; let ci = L.ci; assert(n >= 0, "negative 'n'"); - if (L.stack.length - L.top > n) /* stack large enough? */ + if (L.stack_last - L.top > n) /* stack large enough? */ res = true; else { /* no; need to grow stack */ let inuse = L.top + lstate.EXTRA_STACK; @@ -152,6 +152,7 @@ const lua_pushvalue = function(L, idx) { const lua_settop = function(L, idx) { let func = L.ci.funcOff; if (idx >= 0) { + assert(idx <= L.stack_last - (func + 1), "new top too large"); while (L.top < func + 1 + idx) L.stack[L.top++] = new TValue(CT.LUA_TNIL, null); L.top = func + 1 + idx; diff --git a/src/ldo.js b/src/ldo.js index 10be56a..7154a7f 100644 --- a/src/ldo.js +++ b/src/ldo.js @@ -43,6 +43,7 @@ const ERRORSTACKSIZE = luaconf.LUAI_MAXSTACK + 200; const luaD_reallocstack = function(L, newsize) { L.stack.length = newsize; + L.stack_last = newsize - lstate.EXTRA_STACK; }; const luaD_growstack = function(L, n) { @@ -64,7 +65,7 @@ const luaD_growstack = function(L, n) { }; const luaD_checkstack = function(L, n) { - if (L.stack.length - L.top <= n) + if (L.stack_last - L.top <= n) luaD_growstack(L, n); }; @@ -73,7 +74,7 @@ const stackinuse = function(L) { for (let ci = L.ci; ci !== null; ci = ci.previous) { if (lim < ci.top) lim = ci.top; } - assert(lim <= L.stack.length); + assert(lim <= L.stack_last); return lim + 1; /* part of stack in use */ }; @@ -107,7 +108,7 @@ const luaD_precall = function(L, off, nresults) { ci.nresults = nresults; ci.func = func; ci.top = L.top + defs.LUA_MINSTACK; - assert(ci.top <= L.stack.length); + assert(ci.top <= L.stack_last); ci.callstatus = 0; if (L.hookmask & defs.LUA_MASKCALL) luaD_hook(L, defs.LUA_HOOKCALL, -1); @@ -219,6 +220,7 @@ const luaD_hook = function(L, event, line) { ar.currentline = line; ar.i_ci = ci; ci.top = L.top + defs.LUA_MINSTACK; + assert(ci.top <= L.stack_last); L.allowhook = 0; /* cannot call hooks inside a hook */ ci.callstatus |= lstate.CIST_HOOKED; hook(L, ar); diff --git a/src/lstate.js b/src/lstate.js index 5481f95..b71e011 100644 --- a/src/lstate.js +++ b/src/lstate.js @@ -49,7 +49,8 @@ class lua_State { this.base_ci = new CallInfo(); // Will be populated later this.top = 0; this.ci = null; - this.stack = []; + this.stack = null; + this.stack_last = NaN; this.openupval = null; this.status = TS.LUA_OK; this.next = null; @@ -86,8 +87,10 @@ const luaE_extendCI = function(L) { }; const stack_init = function(L1, L) { - L1.stack = new Array(BASIC_STACK_SIZE); // TODO: for now we don't care about the stack size + L1.stack = new Array(BASIC_STACK_SIZE); L1.top = 0; + L1.stack_last = BASIC_STACK_SIZE - EXTRA_STACK; + /* initialize first ci */ let ci = L1.base_ci; ci.next = ci.previous = null; ci.callstatus = 0; -- cgit v1.2.3-70-g09d2