From 85dfda320d2396821b3e47221aa7e408e9f9ea49 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Mon, 11 Sep 2017 06:54:40 +0300 Subject: [PATCH 1/3] Fix for repeated `require()` when `MODULARIZE=1` is used --- src/shell.js | 2 ++ tests/test_other.py | 3 +++ 2 files changed, 5 insertions(+) diff --git a/src/shell.js b/src/shell.js index 789de77778b9f..d790ffc10b312 100644 --- a/src/shell.js +++ b/src/shell.js @@ -111,9 +111,11 @@ if (ENVIRONMENT_IS_NODE) { Module['arguments'] = process['argv'].slice(2); +#if MODULARIZE == 0 if (typeof module !== 'undefined') { module['exports'] = Module; } +#endif #if NODEJS_CATCH_EXIT process['on']('uncaughtException', function(ex) { diff --git a/tests/test_other.py b/tests/test_other.py index be6b33e1c9959..14a6ecf42923f 100644 --- a/tests/test_other.py +++ b/tests/test_other.py @@ -5438,6 +5438,9 @@ def test_require_modularize(self): assert "module['exports'] = NotModule;" in src output = Popen(NODE_JS + ['-e', 'var m = require("./a.out.js"); m();'], stdout=PIPE, stderr=PIPE).communicate() assert output == ('hello, world!\n', ''), 'expected output, got\n===\nSTDOUT\n%s\n===\nSTDERR\n%s\n===\n' % output + Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-s', 'MODULARIZE=1', '-s', 'NO_EXIT_RUNTIME=1']).communicate() + output = Popen(NODE_JS + ['-e', 'require("./a.out.js")();var m = require("./a.out.js"); m();'], stdout=PIPE, stderr=PIPE).communicate() + assert output[0] == 'hello, world!\nhello, world!\n', 'expected output, got\n===\nSTDOUT\n%s\n===\nSTDERR\n%s\n===\n' % output def test_native_optimizer(self): def test(args, expected): From 38ae76efaee9b33e81da4e4f7a91b38d19f3cc4a Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Mon, 11 Sep 2017 21:57:58 +0300 Subject: [PATCH 2/3] Added comments on changes --- src/shell.js | 1 + tests/test_other.py | 1 + 2 files changed, 2 insertions(+) diff --git a/src/shell.js b/src/shell.js index d790ffc10b312..fd1a962c7c499 100644 --- a/src/shell.js +++ b/src/shell.js @@ -111,6 +111,7 @@ if (ENVIRONMENT_IS_NODE) { Module['arguments'] = process['argv'].slice(2); + // With MODULARIZE == 1 wrapper function will be exported instead of Module itself and re-exporting it second time will cause issues #if MODULARIZE == 0 if (typeof module !== 'undefined') { module['exports'] = Module; diff --git a/tests/test_other.py b/tests/test_other.py index 14a6ecf42923f..5c0fd0f52ec88 100644 --- a/tests/test_other.py +++ b/tests/test_other.py @@ -5439,6 +5439,7 @@ def test_require_modularize(self): output = Popen(NODE_JS + ['-e', 'var m = require("./a.out.js"); m();'], stdout=PIPE, stderr=PIPE).communicate() assert output == ('hello, world!\n', ''), 'expected output, got\n===\nSTDOUT\n%s\n===\nSTDERR\n%s\n===\n' % output Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-s', 'MODULARIZE=1', '-s', 'NO_EXIT_RUNTIME=1']).communicate() + # We call require() twice to ensure it returns wrapper function each time output = Popen(NODE_JS + ['-e', 'require("./a.out.js")();var m = require("./a.out.js"); m();'], stdout=PIPE, stderr=PIPE).communicate() assert output[0] == 'hello, world!\nhello, world!\n', 'expected output, got\n===\nSTDOUT\n%s\n===\nSTDERR\n%s\n===\n' % output From 4e29a39691c3b9481770ecc345308e85f2a74a3b Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Wed, 13 Sep 2017 20:43:07 +0300 Subject: [PATCH 3/3] Comment update --- src/shell.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shell.js b/src/shell.js index fd1a962c7c499..74d1410473511 100644 --- a/src/shell.js +++ b/src/shell.js @@ -111,7 +111,7 @@ if (ENVIRONMENT_IS_NODE) { Module['arguments'] = process['argv'].slice(2); - // With MODULARIZE == 1 wrapper function will be exported instead of Module itself and re-exporting it second time will cause issues + // MODULARIZE == 1 will export the module in the proper place outside, we don't need to export here #if MODULARIZE == 0 if (typeof module !== 'undefined') { module['exports'] = Module;