aboutsummaryrefslogtreecommitdiff
path: root/src/ldo.js
diff options
context:
space:
mode:
authorBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-02-13 14:37:01 +0100
committerBenoit Giannangeli <giann008@gmail.com>2017-02-13 21:56:55 +0100
commit59e549150996ec4c8a049f893dad9ec95a4677e9 (patch)
tree2a9d72d5b7863a80a7c145adbb3f1a311b8a1fd9 /src/ldo.js
parentab14ee4aa480b21a6e5c6c4d8cdb1823951ed7c3 (diff)
downloadfengari-59e549150996ec4c8a049f893dad9ec95a4677e9.tar.gz
fengari-59e549150996ec4c8a049f893dad9ec95a4677e9.tar.bz2
fengari-59e549150996ec4c8a049f893dad9ec95a4677e9.zip
Better use of module to avoid cyclic dependencies issues
Diffstat (limited to 'src/ldo.js')
-rw-r--r--src/ldo.js55
1 files changed, 47 insertions, 8 deletions
diff --git a/src/ldo.js b/src/ldo.js
index 26a4a0f..63b949c 100644
--- a/src/ldo.js
+++ b/src/ldo.js
@@ -11,6 +11,7 @@ const CallInfo = lstate.CallInfo;
const llimit = require('./llimit.js');
const ltm = require('./ltm.js');
const TMS = ltm.TMS;
+const lvm = require('./lvm.js');
const nil = new TValue(CT.LUA_TNIL, null);
@@ -141,11 +142,49 @@ const tryfuncTM = function(L, off, func) {
L.stack[off] = tm; /* tag method is the new function to be called */
};
-module.exports = {
- nil: nil,
- luaD_precall: luaD_precall,
- luaD_poscall: luaD_poscall,
- moveresults: moveresults,
- adjust_varargs: adjust_varargs,
- tryfuncTM: tryfuncTM
-}; \ No newline at end of file
+/*
+** Check appropriate error for stack overflow ("regular" overflow or
+** overflow while handling stack overflow). If 'nCalls' is larger than
+** LUAI_MAXCCALLS (which means it is handling a "regular" overflow) but
+** smaller than 9/8 of LUAI_MAXCCALLS, does not report an error (to
+** allow overflow handling to work)
+*/
+const stackerror = function(L) {
+ if (L.nCcalls === llimit.LUAI_MAXCCALLS)
+ throw new Error("JS stack overflow");
+ else if (L.nCcalls >= llimit.LUAI_MAXCCALLS + (llimit.LUAI_MAXCCALLS >> 3)) /* error while handing stack error */
+ throw new Error("stack overflow"); // TODO: luaD_throw(L, LUA_ERRERR);
+};
+
+/*
+** Call a function (JS or Lua). The function to be called is at func.
+** The arguments are on the stack, right after the function.
+** When returns, all the results are on the stack, starting at the original
+** function position.
+*/
+const luaD_call = function(L, off, nResults) {
+ if (++L.nCcalls >= llimit.LUAI_MAXCCALLS)
+ stackerror(L);
+ if (!luaD_precall(L, off, nResults))
+ lvm.luaV_execute(L);
+ L.nCcalls--;
+};
+
+/*
+** Similar to 'luaD_call', but does not allow yields during the call
+*/
+const luaD_callnoyield = function(L, off, nResults) {
+ L.nny++;
+ luaD_call(L, off, nResults);
+ L.nny--;
+};
+
+module.exports.nil = nil;
+module.exports.luaD_precall = luaD_precall;
+module.exports.luaD_poscall = luaD_poscall;
+module.exports.moveresults = moveresults;
+module.exports.adjust_varargs = adjust_varargs;
+module.exports.tryfuncTM = tryfuncTM;
+module.exports.stackerror = stackerror;
+module.exports.luaD_call = luaD_call;
+module.exports.luaD_callnoyield = luaD_callnoyield; \ No newline at end of file