-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Fix for loading wasm files under Node.js and in browser when files we… #5368
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
Changes from 25 commits
907dc04
deb8520
011fe85
ffb2f56
5bf91f9
027380d
0ee1e2f
a1dd1db
82ef1e0
25c65b9
07ed0bc
4099e28
f859edf
1bfa6b0
4ca757c
0e3d19e
d0deafd
d221d2e
11ae2e0
7d453dd
70a1abc
c84f4f0
96f56a1
63bf3fc
5f3601b
59eabb3
4bd0e76
ff119a9
d1354f1
7a3ec9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2598,7 +2598,10 @@ def modularize(): | |
| %(src)s | ||
|
|
||
| return %(EXPORT_NAME)s; | ||
| }%(instantiate)s; | ||
| }; | ||
| %(EXPORT_NAME)s = %(EXPORT_NAME)s.bind({ | ||
| _currentScript: typeof document !== 'undefined' ? document.currentScript : undefined | ||
| })%(instantiate)s; | ||
| ''' % { | ||
| 'EXPORT_NAME': shared.Settings.EXPORT_NAME, | ||
| 'src': src, | ||
|
|
@@ -2704,11 +2707,7 @@ def generate_html(target, options, js_target, target_basename, | |
| script.un_src() | ||
| script.inline = (''' | ||
| var memoryInitializer = '%s'; | ||
| if (typeof Module['locateFile'] === 'function') { | ||
| memoryInitializer = Module['locateFile'](memoryInitializer); | ||
| } else if (Module['memoryInitializerPrefixURL']) { | ||
| memoryInitializer = Module['memoryInitializerPrefixURL'] + memoryInitializer; | ||
| } | ||
| memoryInitializer = Module['locateFile'] ? Module['locateFile'](memoryInitializer, '') : memoryInitializer; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why does this not call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not very familiar with this part of the code base and it turns out that in tests
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, right, this is code in the HTML, which declares |
||
| Module['memoryInitializerRequestURL'] = memoryInitializer; | ||
| var meminitXHR = Module['memoryInitializerRequest'] = new XMLHttpRequest(); | ||
| meminitXHR.open('GET', memoryInitializer, true); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| Module object | ||
| ============= | ||
|
|
||
| ``Module`` is a global JavaScript object with attributes that Emscripten-generated code calls at various points in its execution. | ||
| ``Module`` is a global JavaScript object with attributes that Emscripten-generated code calls at various points in its execution. | ||
|
|
||
| Developers can provide an implementation of ``Module`` to control the execution of code. For example, to define how notification messages from Emscripten are displayed, developers implement the :js:attr:`Module.print` attribute. | ||
|
|
||
|
|
@@ -20,7 +20,7 @@ Developers can provide an implementation of ``Module`` to control the execution | |
| Creating the Module object | ||
| ========================== | ||
|
|
||
| Use emcc's :ref:`pre-js option<emcc-pre-js>` to add JavaScript code that defines (or extends) the ``Module`` object with the behaviour you need. | ||
| Use emcc's :ref:`pre-js option<emcc-pre-js>` to add JavaScript code that defines (or extends) the ``Module`` object with the behaviour you need. | ||
|
|
||
| When generating only JavaScript (as opposed to HTML), no ``Module`` object is created by default, and the behaviour is entirely defined by the developer. For example, creating a ``Module`` object with the following code will cause all notifications from the program to be calls to ``alert()``. | ||
|
|
||
|
|
@@ -51,15 +51,9 @@ The following ``Module`` attributes affect code execution. Set them to customize | |
|
|
||
| The commandline arguments. The value of ``arguments`` contains the values returned if compiled code checks ``argc`` and ``argv``. | ||
|
|
||
|
|
||
| .. js:attribute:: Module.filePackagePrefixURL | ||
|
|
||
| This is the "prefix" URL for a preloaded data file that is hosted separately from its JavaScript and HTML files (it includes the full path up to, but not including, the data file). See :ref:`packaging-files-data-file-location` for more information. | ||
|
|
||
|
|
||
| .. js:attribute:: Module.locateFile | ||
|
|
||
| If set, this method will be called when the runtime needs to load a file, such as a ``.wasm`` WebAssembly file, ``.mem`` memory init file, or a file generated by the file packager. The function receives the URL, and should return the actual URL. This lets you host file packages or the ``.mem`` file etc. on a different location than the current directory (which is the default expectation), for example if you want to host them on a CDN. Note that ``locateFile`` is sort of a generalization of ``Module.filePackagePrefixURL``. | ||
| If set, this method will be called when the runtime needs to load a file, such as a ``.wasm`` WebAssembly file, ``.mem`` memory init file, or a file generated by the file packager. The function receives the relative path to the file as configured in build process and a prefix (path to JavaScript file), and should return the actual URL. This lets you host file packages or the ``.mem`` file etc. on a different location than the directory of JavaScript file (which is the default expectation), for example if you want to host them on a CDN. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe I am reading this doc wrong, but it seems to suggest that the second param to Reading the code here, it seems like we send
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, there are some places where I've added a note in latest commits about these cases to the degree I understand them. |
||
|
|
||
| .. js:attribute:: Module.logReadFiles | ||
|
|
||
|
|
@@ -87,7 +81,7 @@ The following ``Module`` attributes affect code execution. Set them to customize | |
| .. js:attribute:: Module.preInit | ||
|
|
||
| A function (or array of functions) that must be called before global initializers run, but after basic initialization of the JavaScript runtime. This is typically used for :ref:`File System operations <Filesystem-API>`. | ||
|
|
||
| .. js:attribute:: Module.preinitializedWebGLContext | ||
|
|
||
| If building with -s GL_PREINITIALIZED_CONTEXT=1 set, you can set ``Module.preinitializedWebGLContext`` to a precreated instance of a WebGL context, which will be used later when initializing WebGL in C/C++ side. Precreating the GL context is useful if doing GL side loading (shader compilation, texture loading etc.) parallel to other page startup actions, and/or for detecting WebGL feature support, such as GL version or compressed texture support up front on a page before or in parallel to loading up any compiled code. | ||
|
|
@@ -101,7 +95,7 @@ The following ``Module`` attributes affect code execution. Set them to customize | |
| .. js:attribute:: Module.print | ||
|
|
||
| Called when something is printed to standard output (stdout) | ||
|
|
||
| .. js:attribute:: Module.printErr | ||
|
|
||
| Called when something is printed to standard error (stderr) | ||
|
|
@@ -140,7 +134,7 @@ The generated program is able to detect its execution environment by checking th | |
|
|
||
| However, sometimes it may be needed to override the detected environment: a typical use case would be module bundlers (like webpack): they are executed by nodejs but the final output is for browser. | ||
|
|
||
| In order to do that, you can dictate your preferred execution environment by setting the ``Module.ENVIRONMENT`` variable to one of those allowed values: | ||
| In order to do that, you can dictate your preferred execution environment by setting the ``Module.ENVIRONMENT`` variable to one of those allowed values: | ||
|
|
||
| ``WEB`` | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did you find if there's a test that this is necessary to get passing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, there is no corresponding test unfortunately. I did this after using earlier version of this PR in my projects. Not a perfect or bullet-proof solution, but it works more often than without it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about leaving this up to the user, instead? We could document that
document.currentScriptis used, and so if the page will modify that, the user should implementlocateFilein order to get things working properly.Alternatively, we could make more of an effort here: write up tests that ensure the right behavior, and document what cases exactly we handle. Basically, it's a "best-effort" without clear guidelines and tests that I'd prefer to avoid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, my idea was to make it just work by default and it does. I'd like to avoid user to require doing anything special just to get this basic thing working, re-implementing
locateFilewould be a big pain, especially since it would be necessary to get script'sdocument.currentScriptfrom the outside.I'm more inclined to write more tests here rather than dealing with it every time I use Emscripten.