diff options
Diffstat (limited to 'src/preamble.js')
-rw-r--r-- | src/preamble.js | 76 |
1 files changed, 26 insertions, 50 deletions
diff --git a/src/preamble.js b/src/preamble.js index 7c2b1a50..ae00b796 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -76,7 +76,7 @@ function SAFE_HEAP_STORE(dest, value, type, ignore) { Module.print('SAFE_HEAP store: ' + [dest, type, value, ignore]); #endif - if (!ignore && !value && value !== 0 && value !== false && !isNaN(value)) { // false can be the result of a mathop comparator; NaN can be the result of a math function + if (!ignore && !value && (value === null || value === undefined)) { throw('Warning: Writing an invalid value of ' + JSON.stringify(value) + ' at ' + dest + ' :: ' + new Error().stack + '\n'); } SAFE_HEAP_ACCESS(dest, type, true, ignore); @@ -315,8 +315,10 @@ var globalScope = this; // -s EXPORTED_FUNCTIONS='["_func1","_func2"]' // // @param ident The name of the C function (note that C++ functions will be name-mangled - use extern "C") -// @param returnType The return type of the function, one of the JS types 'number' or 'string' (use 'number' for any C pointer). -// @param argTypes An array of the types of arguments for the function (if there are no arguments, this can be ommitted). Types are as in returnType. +// @param returnType The return type of the function, one of the JS types 'number', 'string' or 'array' (use 'number' for any C pointer, and +// 'array' for JavaScript arrays and typed arrays). +// @param argTypes An array of the types of arguments for the function (if there are no arguments, this can be ommitted). Types are as in returnType, +// except that 'array' is not possible (there is no way for us to know the length of the array) // @param args An array of the arguments to the function, as native JS values (as in returnType) // 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) @@ -324,10 +326,16 @@ function ccall(ident, returnType, argTypes, args) { var stack = 0; function toC(value, type) { if (type == 'string') { + if (value === null || value === undefined || value === 0) return 0; // null string if (!stack) stack = Runtime.stackSave(); var ret = Runtime.stackAlloc(value.length+1); writeStringToMemory(value, ret); return ret; + } else if (type == 'array') { + if (!stack) stack = Runtime.stackSave(); + var ret = Runtime.stackAlloc(value.length); + writeArrayToMemory(value, ret); + return ret; } return value; } @@ -335,6 +343,7 @@ function ccall(ident, returnType, argTypes, args) { if (type == 'string') { return Pointer_stringify(value); } + assert(type != 'array'); return value; } try { @@ -713,40 +722,6 @@ function exitRuntime() { CorrectionsMonitor.print(); } - -// Copies a list of num items on the HEAP into a -// a normal JavaScript array of numbers -function Array_copy(ptr, num) { -#if USE_TYPED_ARRAYS == 1 - // TODO: In the SAFE_HEAP case, do some reading here, for debugging purposes - currently this is an 'unnoticed read'. - return Array.prototype.slice.call(IHEAP.subarray(ptr, ptr+num)); // Make a normal array out of the typed 'view' - // Consider making a typed array here, for speed? -#endif -#if USE_TYPED_ARRAYS == 2 - return Array.prototype.slice.call(HEAP8.subarray(ptr, ptr+num)); // Make a normal array out of the typed 'view' - // Consider making a typed array here, for speed? -#endif - return HEAP.slice(ptr, ptr+num); -} -Module['Array_copy'] = Array_copy; - -#if USE_TYPED_ARRAYS -// Copies a list of num items on the HEAP into a -// JavaScript typed array. -function TypedArray_copy(ptr, num, offset /*optional*/) { - // TODO: optimize this! - if (offset === undefined) { - offset = 0; - } - var arr = new Uint8Array(num - offset); - for (var i = offset; i < num; ++i) { - arr[i - offset] = {{{ makeGetValue('ptr', 'i', 'i8') }}}; - } - return arr.buffer; -} -Module['TypedArray_copy'] = TypedArray_copy; -#endif - function String_len(ptr) { var i = 0; while ({{{ makeGetValue('ptr', 'i', 'i8') }}}) i++; // Note: should be |!= 0|, technically. But this helps catch bugs with undefineds @@ -754,17 +729,6 @@ function String_len(ptr) { } Module['String_len'] = String_len; -// Copies a C-style string, terminated by a zero, from the HEAP into -// a normal JavaScript array of numbers -function String_copy(ptr, addZero) { - var len = String_len(ptr); - if (addZero) len++; - var ret = Array_copy(ptr, len); - if (addZero) ret[len-1] = 0; - return ret; -} -Module['String_copy'] = String_copy; - // Tools // This processes a JS string into a C-line array of numbers, 0-terminated. @@ -830,6 +794,13 @@ function writeStringToMemory(string, buffer, dontAddNull) { } Module['writeStringToMemory'] = writeStringToMemory; +function writeArrayToMemory(array, buffer) { + for (var i = 0; i < array.length; i++) { + {{{ makeSetValue('buffer', 'i', 'array[i]', 'i8') }}}; + } +} +Module['writeArrayToMemory'] = writeArrayToMemory; + var STRING_TABLE = []; {{{ unSign }}} @@ -837,8 +808,11 @@ var STRING_TABLE = []; // A counter of dependencies for calling run(). If we need to // do asynchronous work before running, increment this and -// decrement it. Incrementing must happen in Module.preRun -// or PRE_RUN_ADDITIONS (used by emcc to add file preloading). +// decrement it. Incrementing must happen in a place like +// PRE_RUN_ADDITIONS (used by emcc to add file preloading). +// Note that you can add dependencies in preRun, even though +// it happens right before run - run will be postponed until +// the dependencies are met. var runDependencies = 0; function addRunDependency() { runDependencies++; @@ -846,6 +820,7 @@ function addRunDependency() { Module['monitorRunDependencies'](runDependencies); } } +Module['addRunDependency'] = addRunDependency; function removeRunDependency() { runDependencies--; if (Module['monitorRunDependencies']) { @@ -853,6 +828,7 @@ function removeRunDependency() { } if (runDependencies == 0) run(); } +Module['removeRunDependency'] = removeRunDependency; // === Body === |