From 4f420ed35d0342a131cdbdf87c69eeaf35dea565 Mon Sep 17 00:00:00 2001 From: Nicholas Wilson Date: Fri, 5 Dec 2014 18:39:07 +0000 Subject: [PATCH] fix bug in closure messing up cwrap (my fault) --- src/preamble.js | 13 +++++++++++-- tests/core/test_ccall.in | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/preamble.js b/src/preamble.js index 597a5762e..30f5b9d30 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -341,6 +341,15 @@ function getCFunc(ident) { var cwrap, ccall; (function(){ var JSfuncs = { + // Helpers for cwrap -- it can't refer to Runtime directly because it might + // be renamed by closure, instead it calls JSfuncs['stackSave'].body to find + // out what the minified function name is. + 'stackSave': function() { + Runtime.stackSave() + }, + 'stackRestore': function() { + Runtime.stackRestore() + }, // type conversion from js to c 'arrayToC' : function(arr) { var ret = Runtime.stackAlloc(arr.length); @@ -419,7 +428,7 @@ var cwrap, ccall; if (!numericArgs) { // Generate the code needed to convert the arguments from javascript // values to pointers - funcstr += 'var stack = Runtime.stackSave();'; + funcstr += 'var stack = ' + JSsource['stackSave'].body + ';'; for (var i = 0; i < nargs; i++) { var arg = argNames[i], type = argTypes[i]; if (type === 'number') continue; @@ -441,7 +450,7 @@ var cwrap, ccall; } if (!numericArgs) { // If we had a stack, restore it - funcstr += 'Runtime.stackRestore(stack);'; + funcstr += JSsource['stackRestore'].body.replace('()', '(stack)') + ';'; } funcstr += 'return ret})'; return eval(funcstr); diff --git a/tests/core/test_ccall.in b/tests/core/test_ccall.in index b13781458..c0ad0b07a 100644 --- a/tests/core/test_ccall.in +++ b/tests/core/test_ccall.in @@ -26,7 +26,7 @@ struct test_struct { int arg1, arg2, arg3; }; static int* stackChecker = 0; -int get_stack() { return EM_ASM_INT_V({ return Runtime.stackSave(); }); } +int get_stack() { int i; return (int)&i; } int uses_stack(test_struct* t1) { if (stackChecker == 0) stackChecker = (int*)malloc(sizeof(int)); *stackChecker = get_stack();