aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-10-16 10:44:11 -0700
committerAlon Zakai <alonzakai@gmail.com>2013-10-16 10:44:11 -0700
commit9c98148da0a3888d1d3fcb52361ba2fe52718d20 (patch)
tree86c0cbf5cb7b8f085108fa21bc80c10cd6572fb1 /tests
parent86fe72e8bfdcf5e6518c7e615ce599d1c6c491dd (diff)
parent0c7ec24c3f7c50d90d3b6f32c941115fc0610984 (diff)
Merge branch 'fix-module-exports-with-closure-try2' of github.com:fadams/emscripten into incoming
Conflicts: AUTHORS tests/test_other.py
Diffstat (limited to 'tests')
-rw-r--r--tests/Module-exports/main.js30
-rw-r--r--tests/Module-exports/setup.js9
-rw-r--r--tests/Module-exports/test.c17
-rw-r--r--tests/test_other.py48
4 files changed, 104 insertions, 0 deletions
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'))
+