From 0405b4261972bd692d764000c6db96ab1cde63fc Mon Sep 17 00:00:00 2001 From: James Swift Date: Tue, 23 May 2017 16:43:50 +0200 Subject: [PATCH 1/3] Export module in Node when MODULARIZE=1 Added authorship Not passing this Simplify since Module may not exist. Fix tabs. --- AUTHORS | 3 ++- emcc.py | 5 +++++ src/detectNode.js | 17 +++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 src/detectNode.js diff --git a/AUTHORS b/AUTHORS index 35e6c1c39a65e..0390829f05576 100644 --- a/AUTHORS +++ b/AUTHORS @@ -293,4 +293,5 @@ a license to everyone to use it as detailed in LICENSE.) * Fumiya Chiba * Ryan C. Gordon * Inseok Lee -* Yair Levinson (copyright owned by Autodesk, Inc.) \ No newline at end of file +* Yair Levinson (copyright owned by Autodesk, Inc.) +* James Swift (copyright owned by PSPDFKit GmbH) diff --git a/emcc.py b/emcc.py index 78e0ddca223b9..03d93dd7c1160 100755 --- a/emcc.py +++ b/emcc.py @@ -2339,6 +2339,11 @@ def modularize(final): f.write('\n') f.write(' return ' + shared.Settings.EXPORT_NAME + ';\n') f.write('};\n') + # Export the function if this is for Node otherwise it is lost. + f.write(open(shared.path_from_root('src', 'detectNode.js')).read() + '\n') + f.write('if (isNodeEnvironment()) {\n') + f.write(" module['exports'] = " + shared.Settings.EXPORT_NAME + ';\n') + f.write('};\n') f.close() if DEBUG: save_intermediate('modularized', 'js') return final diff --git a/src/detectNode.js b/src/detectNode.js new file mode 100644 index 0000000000000..b410060099570 --- /dev/null +++ b/src/detectNode.js @@ -0,0 +1,17 @@ +var isNodeEnvironment = function() { + // Similar to shell.js + var ENVIRONMENT_IS_WEB = false; + var ENVIRONMENT_IS_WORKER = false; + var ENVIRONMENT_IS_NODE = false; + + // Three configurations we can be running in: + // 1) We could be the application main() thread running in the main JS UI thread. (ENVIRONMENT_IS_WORKER == false and ENVIRONMENT_IS_PTHREAD == false) + // 2) We could be the application main() thread proxied to worker. (with Emscripten -s PROXY_TO_WORKER=1) (ENVIRONMENT_IS_WORKER == true, ENVIRONMENT_IS_PTHREAD == false) + // 3) We could be an application pthread running in a worker. (ENVIRONMENT_IS_WORKER == true and ENVIRONMENT_IS_PTHREAD == true) + + ENVIRONMENT_IS_WEB = typeof window === 'object'; + ENVIRONMENT_IS_WORKER = typeof importScripts === 'function'; + ENVIRONMENT_IS_NODE = typeof process === 'object' && typeof require === 'function' && !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_WORKER; + + return ENVIRONMENT_IS_NODE; +}; \ No newline at end of file From a0a779bd056130d5435f21c997b2a0360c712f3d Mon Sep 17 00:00:00 2001 From: James Swift Date: Wed, 24 May 2017 10:44:44 +0200 Subject: [PATCH 2/3] Test NodeJS export if MODULARIZE=1 --- tests/test_other.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/test_other.py b/tests/test_other.py index 18bf7c256a1ba..ec88876271c4d 100644 --- a/tests/test_other.py +++ b/tests/test_other.py @@ -5298,6 +5298,18 @@ def test_require(self): Building.emcc(inname, output_filename='a.out.js') output = Popen(NODE_JS + ['-e', 'require("./a.out.js")'], stdout=PIPE, stderr=PIPE).communicate() assert output == ('hello, world!\n', ''), 'expected no output, got\n===\nSTDOUT\n%s\n===\nSTDERR\n%s\n===\n' % output + + def test_require_modularize(self): + Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-s', 'MODULARIZE=1']).communicate() + src = open('a.out.js').read() + assert "module['exports'] = Module;" 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', 'EXPORT_NAME="NotModule"']).communicate() + src = open('a.out.js').read() + 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 def test_native_optimizer(self): def test(args, expected): From cea92254c5f634805914ebeaf0967660d6f7abe5 Mon Sep 17 00:00:00 2001 From: James Swift Date: Fri, 26 May 2017 11:52:12 +0200 Subject: [PATCH 3/3] The UMD style detection is more general and simpler. --- emcc.py | 3 +-- src/detectNode.js | 17 ----------------- 2 files changed, 1 insertion(+), 19 deletions(-) delete mode 100644 src/detectNode.js diff --git a/emcc.py b/emcc.py index 03d93dd7c1160..4841a27b25607 100755 --- a/emcc.py +++ b/emcc.py @@ -2340,8 +2340,7 @@ def modularize(final): f.write(' return ' + shared.Settings.EXPORT_NAME + ';\n') f.write('};\n') # Export the function if this is for Node otherwise it is lost. - f.write(open(shared.path_from_root('src', 'detectNode.js')).read() + '\n') - f.write('if (isNodeEnvironment()) {\n') + f.write('if (typeof module === \'object\' && module.exports) {\n') f.write(" module['exports'] = " + shared.Settings.EXPORT_NAME + ';\n') f.write('};\n') f.close() diff --git a/src/detectNode.js b/src/detectNode.js deleted file mode 100644 index b410060099570..0000000000000 --- a/src/detectNode.js +++ /dev/null @@ -1,17 +0,0 @@ -var isNodeEnvironment = function() { - // Similar to shell.js - var ENVIRONMENT_IS_WEB = false; - var ENVIRONMENT_IS_WORKER = false; - var ENVIRONMENT_IS_NODE = false; - - // Three configurations we can be running in: - // 1) We could be the application main() thread running in the main JS UI thread. (ENVIRONMENT_IS_WORKER == false and ENVIRONMENT_IS_PTHREAD == false) - // 2) We could be the application main() thread proxied to worker. (with Emscripten -s PROXY_TO_WORKER=1) (ENVIRONMENT_IS_WORKER == true, ENVIRONMENT_IS_PTHREAD == false) - // 3) We could be an application pthread running in a worker. (ENVIRONMENT_IS_WORKER == true and ENVIRONMENT_IS_PTHREAD == true) - - ENVIRONMENT_IS_WEB = typeof window === 'object'; - ENVIRONMENT_IS_WORKER = typeof importScripts === 'function'; - ENVIRONMENT_IS_NODE = typeof process === 'object' && typeof require === 'function' && !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_WORKER; - - return ENVIRONMENT_IS_NODE; -}; \ No newline at end of file