From 9294302fc069e0b8d893521fa552b5ed66601024 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 22 May 2017 17:04:46 +1000 Subject: Fix lua_checkstack to check against LUAI_MAXSTACK. Adds luaD_growstack and luaD_reallocstack --- src/lapi.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'src/lapi.js') diff --git a/src/lapi.js b/src/lapi.js index cea0568..79c666c 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -91,8 +91,20 @@ const index2addr_ = function(L, idx) { }; const lua_checkstack = function(L, n) { + let res; let ci = L.ci; - let res = L.stack.length < luaconf.LUAI_MAXSTACK; + assert(n >= 0, "negative 'n'"); + if (L.stack.length - L.top > n) /* stack large enough? */ + res = true; + else { /* no; need to grow stack */ + let inuse = L.top + lstate.EXTRA_STACK; + if (inuse > luaconf.LUAI_MAXSTACK - n) /* can grow without overflow? */ + res = false; /* no */ + else { /* try to grow stack */ + ldo.luaD_growstack(L, n); + res = true; + } + } if (res && ci.top < L.top + n) ci.top = L.top + n; /* adjust frame top */ -- cgit v1.2.3-54-g00ecf