summaryrefslogtreecommitdiff
path: root/src/ldo.js
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2017-05-22 17:29:04 +1000
committerdaurnimator <quae@daurnimator.com>2017-05-22 18:46:27 +1000
commit6bd8fb685dd9a2c153ab040b1844cbcfecc699f0 (patch)
tree950e51bdef38a4d15deee7772bd90a485df70aa9 /src/ldo.js
parent20efa5c3b00e3a82ab135ec4a1f5d0563912aefe (diff)
downloadfengari-6bd8fb685dd9a2c153ab040b1844cbcfecc699f0.tar.gz
fengari-6bd8fb685dd9a2c153ab040b1844cbcfecc699f0.tar.bz2
fengari-6bd8fb685dd9a2c153ab040b1844cbcfecc699f0.zip
src/ldo.js: Add luaD_shrinkstack calls
Diffstat (limited to 'src/ldo.js')
-rw-r--r--src/ldo.js20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/ldo.js b/src/ldo.js
index 5243f1f..10be56a 100644
--- a/src/ldo.js
+++ b/src/ldo.js
@@ -68,6 +68,24 @@ const luaD_checkstack = function(L, n) {
luaD_growstack(L, n);
};
+const stackinuse = function(L) {
+ let lim = L.top;
+ for (let ci = L.ci; ci !== null; ci = ci.previous) {
+ if (lim < ci.top) lim = ci.top;
+ }
+ assert(lim <= L.stack.length);
+ return lim + 1; /* part of stack in use */
+};
+
+const luaD_shrinkstack = function(L) {
+ let inuse = stackinuse(L);
+ let goodsize = inuse + Math.floor(inuse / 8) + 2*lstate.EXTRA_STACK;
+ if (goodsize > luaconf.LUAI_MAXSTACK)
+ goodsize = luaconf.LUAI_MAXSTACK; /* respect stack limit */
+ if (inuse <= (luaconf.LUAI_MAXSTACK - lstate.EXTRA_STACK) && goodsize < L.stack.length)
+ luaD_reallocstack(L, goodsize);
+};
+
/*
** Prepares a function call: checks the stack, creates a new CallInfo
** entry, fills in the relevant information, calls hook if needed.
@@ -420,6 +438,7 @@ const recover = function(L, status) {
L.ci = ci;
L.allowhook = ci.callstatus & lstate.CIST_OAH; /* restore original 'allowhook' */
L.nny = 0; /* should be zero to be yieldable */
+ luaD_shrinkstack(L);
L.errfunc = ci.c_old_errfunc;
return 1; /* continue running the coroutine */
};
@@ -563,6 +582,7 @@ const luaD_pcall = function(L, func, u, old_top, ef) {
L.ci = old_ci;
L.allowhook = old_allowhooks;
L.nny = old_nny;
+ luaD_shrinkstack(L);
}
L.errfunc = old_errfunc;