diff options
-rw-r--r-- | AUTHORS | 1 | ||||
-rw-r--r-- | src/shell.js | 4 | ||||
-rw-r--r-- | tests/Module-exports/main.js | 30 | ||||
-rwxr-xr-x | tests/Module-exports/run-test | 13 | ||||
-rw-r--r-- | tests/Module-exports/setup.js | 9 | ||||
-rw-r--r-- | tests/Module-exports/test.c | 17 |
6 files changed, 73 insertions, 1 deletions
@@ -99,4 +99,5 @@ a license to everyone to use it as detailed in LICENSE.) * Tobias Vrinssen <tobias@vrinssen.de> * Patrick R. Martin <patrick.martin.r@gmail.com> * Richard Quirk <richard.quirk@gmail.com> +* Fraser Adams <fraser.adams@blueyonder.co.uk> diff --git a/src/shell.js b/src/shell.js index 7bfbe781..bb89ab6e 100644 --- a/src/shell.js +++ b/src/shell.js @@ -68,7 +68,9 @@ if (ENVIRONMENT_IS_NODE) { Module['arguments'] = process['argv'].slice(2); - module.exports = Module; + // Explicitly using associative array form instead of dot notation form prevents the closure compiler + // minifying the exports property, which is important if Module is to remain visible to node.js + module['exports'] = Module; } else if (ENVIRONMENT_IS_SHELL) { Module['print'] = print; diff --git a/tests/Module-exports/main.js b/tests/Module-exports/main.js new file mode 100644 index 00000000..20f36212 --- /dev/null +++ b/tests/Module-exports/main.js @@ -0,0 +1,30 @@ + +var Module = require("./test.js"); + +console.log("\nTesting main.js"); + +var length = 20; +var ptr = Module._malloc(length); // Get buffer from emscripten. +var buffer= new Uint8Array(Module.HEAPU8.buffer, ptr, length); // Get a bytes view on the newly allocated buffer. + +// Populate the buffer in JavaScript land. +console.log("buffer length = " + length + "\n"); +for (var i = 0; i < length; i++) { + buffer[i] = i + 20; // Add 20 just for a bit of interest. + console.log("setting buffer[" + i + "] = " + buffer[i]); +} + +// Export bufferTest function. +var bufferTest = Module.cwrap('bufferTest', 'number', ['number', 'number']); +console.log("\ncalling bufferTest\n"); + +bufferTest(ptr, length); // Call our exported C function to prove the buffer was passed correctly. + +console.log("\nbufferTest finished\n"); + +// free the heap buffer +Module._free(ptr); + + + + diff --git a/tests/Module-exports/run-test b/tests/Module-exports/run-test new file mode 100755 index 00000000..ca6830f1 --- /dev/null +++ b/tests/Module-exports/run-test @@ -0,0 +1,13 @@ +# This test tests the case where we wish to export a C function called bufferTest and have it called +# from JavaScript running on node.js. In particular this test checks that the C function is correctly +# exported when minified by running the Closure compiler, testing a change made to src/shell.js +# that replaced "module.exports = Module;" with "module['exports'] = Module;". The dot notation version +# gets minified by Closure which means that Module doesn't get correctly exported to node.js, the +# latter succeeds (and ironically actually gets minified to "module.exports = Module;" by Closure) + +rm -f test.js + +emcc -O2 --closure 1 -o test.js test.c --pre-js setup.js -s EXPORTED_FUNCTIONS="['_bufferTest']" + +node main.js + diff --git a/tests/Module-exports/setup.js b/tests/Module-exports/setup.js new file mode 100644 index 00000000..5c2f0ace --- /dev/null +++ b/tests/Module-exports/setup.js @@ -0,0 +1,9 @@ +/** + * This file provides some setup for the emscripten runtime. In particular it prevents the runtime exiting. + * This is necessary as otherwise things like printf don't seem to work from methods called by JavaScript. + */ +var Module = { + 'noExitRuntime' : true +}; + + diff --git a/tests/Module-exports/test.c b/tests/Module-exports/test.c new file mode 100644 index 00000000..787312ae --- /dev/null +++ b/tests/Module-exports/test.c @@ -0,0 +1,17 @@ +#include <stdio.h> + +/** + * This is a simple test method to verify that the contents of a buffer created in JavaScript can be + * passed into a C function. + */ +void bufferTest(const void* source, unsigned int sourceLen) { + int i = 0; + unsigned char* src = (unsigned char*)source; + + printf("in test method: size of source buffer = %d\n", sourceLen); + + for (i = 0; i < sourceLen; i++) { + printf("source[%d] = %d\n", i, src[i]); + } +} + |