aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2012-03-20 11:46:26 -0700
committerAlon Zakai <alonzakai@gmail.com>2012-03-20 11:46:26 -0700
commit1e13d4a41028cc5681768deb1eb4083f786ac091 (patch)
tree30ab9db6f85f0e07243cd1a37ac962c2773f9673 /src
parentdfeec1f826f22802fa0978def91cf94a278dcab9 (diff)
avoid stack explosion in ccall
Diffstat (limited to 'src')
-rw-r--r--src/preamble.js9
1 files changed, 6 insertions, 3 deletions
diff --git a/src/preamble.js b/src/preamble.js
index db092045..0e43af12 100644
--- a/src/preamble.js
+++ b/src/preamble.js
@@ -321,10 +321,11 @@ var globalScope = this;
// Note that string arguments will be stored on the stack (the JS string will become a C string on the stack).
// @return The return value, as a native JS value (as in returnType)
function ccall(ident, returnType, argTypes, args) {
+ var stack = 0;
function toC(value, type) {
if (type == 'string') {
- var ret = STACKTOP;
- Runtime.stackAlloc(value.length+1);
+ if (!stack) stack = Runtime.stackSave();
+ var ret = Runtime.stackAlloc(value.length+1);
writeStringToMemory(value, ret);
return ret;
}
@@ -348,7 +349,9 @@ function ccall(ident, returnType, argTypes, args) {
var cArgs = args ? args.map(function(arg) {
return toC(arg, argTypes[i++]);
}) : [];
- return fromC(func.apply(null, cArgs), returnType);
+ var ret = fromC(func.apply(null, cArgs), returnType);
+ if (stack) Runtime.stackRestore(stack);
+ return ret;
}
Module["ccall"] = ccall;