Skip to content

Export module for Node when MODULARIZE=1 #5239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 26, 2017
Merged

Export module for Node when MODULARIZE=1 #5239

merged 4 commits into from
May 26, 2017

Conversation

phraemer
Copy link
Contributor

As mentioned in this issue #5216

Basically if you turn on modularisation the function cannot be called if you require(...) it in a node environment.

This PR adds environment detection code if MODULARIZE=1 and exports the module.

Some tests failed but I don't believe as a result of these changes

======================================================================
ERROR: test_dylink_zlib (test_core.asm1)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 329, in run
    testMethod()
  File "/Users/james/Documents/mine/emscripten/emscripten/tests/test_core.py", line 3767, in test_dylink_zlib
    force_c=True)
  File "/Users/james/Documents/mine/emscripten/emscripten/tests/test_core.py", line 3062, in dylink_test
    shutil.move('liblib.cpp.o.' + side_suffix, 'liblib.so')
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move
    copy2(src, real_dst)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 130, in copy2
    copyfile(src, dst)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 82, in copyfile
    with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: 'liblib.cpp.o.js'

======================================================================
ERROR: test_dylink_zlib (test_core.asm2)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 329, in run
    testMethod()
  File "/Users/james/Documents/mine/emscripten/emscripten/tests/test_core.py", line 3767, in test_dylink_zlib
    force_c=True)
  File "/Users/james/Documents/mine/emscripten/emscripten/tests/test_core.py", line 3062, in dylink_test
    shutil.move('liblib.cpp.o.' + side_suffix, 'liblib.so')
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move
    copy2(src, real_dst)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 130, in copy2
    copyfile(src, dst)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 82, in copyfile
    with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: 'liblib.cpp.o.js'

======================================================================
ERROR: test_dylink_zlib (test_core.asm2g)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 329, in run
    testMethod()
  File "/Users/james/Documents/mine/emscripten/emscripten/tests/test_core.py", line 3767, in test_dylink_zlib
    force_c=True)
  File "/Users/james/Documents/mine/emscripten/emscripten/tests/test_core.py", line 3062, in dylink_test
    shutil.move('liblib.cpp.o.' + side_suffix, 'liblib.so')
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move
    copy2(src, real_dst)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 130, in copy2
    copyfile(src, dst)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 82, in copyfile
    with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: 'liblib.cpp.o.js'

======================================================================
ERROR: test_dylink_zlib (test_core.asm2i)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 329, in run
    testMethod()
  File "/Users/james/Documents/mine/emscripten/emscripten/tests/test_core.py", line 3767, in test_dylink_zlib
    force_c=True)
  File "/Users/james/Documents/mine/emscripten/emscripten/tests/test_core.py", line 3062, in dylink_test
    shutil.move('liblib.cpp.o.' + side_suffix, 'liblib.so')
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move
    copy2(src, real_dst)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 130, in copy2
    copyfile(src, dst)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 82, in copyfile
    with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: 'liblib.cpp.o.js'

======================================================================
ERROR: test_dylink_zlib (test_core.asm2nn)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 329, in run
    testMethod()
  File "/Users/james/Documents/mine/emscripten/emscripten/tests/test_core.py", line 3767, in test_dylink_zlib
    force_c=True)
  File "/Users/james/Documents/mine/emscripten/emscripten/tests/test_core.py", line 3062, in dylink_test
    shutil.move('liblib.cpp.o.' + side_suffix, 'liblib.so')
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move
    copy2(src, real_dst)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 130, in copy2
    copyfile(src, dst)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 82, in copyfile
    with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: 'liblib.cpp.o.js'

======================================================================
ERROR: test_dylink_zlib (test_core.asm3)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 329, in run
    testMethod()
  File "/Users/james/Documents/mine/emscripten/emscripten/tests/test_core.py", line 3767, in test_dylink_zlib
    force_c=True)
  File "/Users/james/Documents/mine/emscripten/emscripten/tests/test_core.py", line 3062, in dylink_test
    shutil.move('liblib.cpp.o.' + side_suffix, 'liblib.so')
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move
    copy2(src, real_dst)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 130, in copy2
    copyfile(src, dst)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 82, in copyfile
    with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: 'liblib.cpp.o.js'

======================================================================
ERROR: test_dylink_zlib (test_core.default)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 329, in run
    testMethod()
  File "/Users/james/Documents/mine/emscripten/emscripten/tests/test_core.py", line 3767, in test_dylink_zlib
    force_c=True)
  File "/Users/james/Documents/mine/emscripten/emscripten/tests/test_core.py", line 3062, in dylink_test
    shutil.move('liblib.cpp.o.' + side_suffix, 'liblib.so')
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move
    copy2(src, real_dst)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 130, in copy2
    copyfile(src, dst)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 82, in copyfile
    with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: 'liblib.cpp.o.js'

----------------------------------------------------------------------
Ran 3816 tests in 5690.772s

FAILED (errors=7)

@@ -0,0 +1,32 @@
var isNodeEnvironment = function() {
// Same as in shell.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation should be 2 spaces

// 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)

if (Module['ENVIRONMENT']) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Module may not exist here, first because we may have a different export name, second because we just have the modularize function, and it's parameter is the Module object, but it will only be called later.

I think all we can do is the code path that does not use Module. I think that should be enough here?

@kripken
Copy link
Member

kripken commented May 23, 2017

Thanks, overall looks good. A few comments above. Also, we should add a test for this (may be a test for node exporting already, where we can add to that test or add a new test alongside it).

@bvibber
Copy link
Collaborator

bvibber commented May 23, 2017

I think this would be handy for code using webpack/browserify/etc as well, but I think they would fail the runtime check for isNodeEnvironment() by matching positive for being a web or worker environment. Maybe test for 'require' and 'module', rather than for 'process' and not-being-web?

@phraemer
Copy link
Contributor Author

phraemer commented May 24, 2017

@Brion a good suggestion. For this PR would you mind if I keep it as similar to existing code first? I.e. what's in shell.js.

We could do a second PR later and make it even more general.

Perhaps this is relevant https://github.com/umdjs/umd/blob/master/templates/returnExportsGlobal.js

@phraemer
Copy link
Contributor Author

@kripken thanks for the review! Hope it's good now.

@kripken
Copy link
Member

kripken commented May 24, 2017

Looks good, just still thinking about the issue that @Brion mentioned. Is the issue also an identical existing issue with shell.js? In that case let's merge this and figure that out later. Or is it a separate issue, in which case we should address it here first?

I lean to thinking of it as a separate issue, as shell.js tries to identify the actual environment, while here we just need to know if there is a module object we need to export on.

@phraemer
Copy link
Contributor Author

phraemer commented May 24, 2017

Yeah it's the same in my opinion but definitely worth doing later. I.e. in a separate PR.

(edit for clarity)

@curiousdannii
Copy link
Contributor

curiousdannii commented May 26, 2017

This partially addresses #5093. I wouldn't say it fixes it though, because won't it not work if a browserified module is used in a browser? I think it should do as UMD does: if (typeof module === 'object' && module.exports). That code pattern is already so common that I think it's proved reliable. No need to complicate matters.

Also in Electron it will probably act as both a browser and node.

James Swift added 3 commits May 26, 2017 11:46
@phraemer
Copy link
Contributor Author

OK yeah it does make sense. Updated.

@kripken
Copy link
Member

kripken commented May 26, 2017

Great, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants