diff options
-rw-r--r-- | AUTHORS | 1 | ||||
-rw-r--r-- | src/shell.js | 2 | ||||
-rw-r--r-- | tests/Module-exports/main.js | 30 | ||||
-rw-r--r-- | tests/Module-exports/setup.js | 9 | ||||
-rw-r--r-- | tests/Module-exports/test.c | 17 | ||||
-rw-r--r-- | tests/test_other.py | 48 |
6 files changed, 106 insertions, 1 deletions
@@ -103,4 +103,5 @@ a license to everyone to use it as detailed in LICENSE.) * Antoine Lambert <antoine.lambert33@gmail.com> * Daniel Aquino <mr.danielaquino@gmail.com> * Remi Papillie <remi.papillie@gmail.com> +* Fraser Adams <fraser.adams@blueyonder.co.uk> diff --git a/src/shell.js b/src/shell.js index 7bfbe781..be23b3c1 100644 --- a/src/shell.js +++ b/src/shell.js @@ -68,7 +68,7 @@ if (ENVIRONMENT_IS_NODE) { Module['arguments'] = process['argv'].slice(2); - module.exports = Module; + 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/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]); + } +} + diff --git a/tests/test_other.py b/tests/test_other.py index c8b6d4b1..f44dfc5f 100644 --- a/tests/test_other.py +++ b/tests/test_other.py @@ -1960,3 +1960,51 @@ a(int [32], char [5]*) assert 'one(int)' in output assert 'two(char)' in output + def test_module_exports_with_closure(self): + # This test checks that module.export is retained when JavaScript is minified by compiling with --closure 1 + # This is important as if module.export is not present the Module object will not be visible to node.js + # Run with ./runner.py other.test_module_exports_with_closure + + # First make sure test.js isn't present. + try_delete(path_from_root('tests', 'Module-exports', 'test.js')) + assert not os.path.exists(path_from_root('tests', 'Module-exports', 'test.js')) + + # compile with -O2 --closure 0 + Popen([PYTHON, EMCC, path_from_root('tests', 'Module-exports', 'test.c'), '-o', path_from_root('tests', 'Module-exports', 'test.js'), '-O2', '--closure', '0', '--pre-js', path_from_root('tests', 'Module-exports', 'setup.js'), '-s', 'EXPORTED_FUNCTIONS=["_bufferTest"]'], stdout=PIPE, stderr=PIPE).communicate() + + # Check that compilation was successful + assert os.path.exists(path_from_root('tests', 'Module-exports', 'test.js')) + test_js_closure_0 = open(path_from_root('tests', 'Module-exports', 'test.js')).read() + + # Check that test.js compiled with --closure 0 contains "module['exports'] = Module;" + assert "module['exports'] = Module;" in test_js_closure_0 + + # Check that main.js (which requires test.js) completes successfully when run in node.js + # in order to check that the exports are indeed functioning correctly. + if NODE_JS in JS_ENGINES: + self.assertContained('bufferTest finished', run_js(path_from_root('tests', 'Module-exports', 'main.js'), engine=NODE_JS)) + + # Delete test.js again and check it's gone. + try_delete(path_from_root('tests', 'Module-exports', 'test.js')) + assert not os.path.exists(path_from_root('tests', 'Module-exports', 'test.js')) + + # compile with -O2 --closure 1 + Popen([PYTHON, EMCC, path_from_root('tests', 'Module-exports', 'test.c'), '-o', path_from_root('tests', 'Module-exports', 'test.js'), '-O2', '--closure', '1', '--pre-js', path_from_root('tests', 'Module-exports', 'setup.js'), '-s', 'EXPORTED_FUNCTIONS=["_bufferTest"]'], stdout=PIPE, stderr=PIPE).communicate() + + # Check that compilation was successful + assert os.path.exists(path_from_root('tests', 'Module-exports', 'test.js')) + test_js_closure_1 = open(path_from_root('tests', 'Module-exports', 'test.js')).read() + + # Check that test.js compiled with --closure 1 contains "module.exports", we want to verify that + # "module['exports']" got minified to "module.exports" when compiling with --closure 1 + assert "module.exports" in test_js_closure_1 + + # Check that main.js (which requires test.js) completes successfully when run in node.js + # in order to check that the exports are indeed functioning correctly. + if NODE_JS in JS_ENGINES: + self.assertContained('bufferTest finished', run_js(path_from_root('tests', 'Module-exports', 'main.js'), engine=NODE_JS)) + + # Tidy up files that might have been created by this test. + try_delete(path_from_root('tests', 'Module-exports', 'test.js')) + try_delete(path_from_root('tests', 'Module-exports', 'test.js.map')) + |