Skip to content

Commit be5f44a

Browse files
authored
Minor followups to #5368 (#6888)
* whitespace * docs * clarify MODULARIZE usage of _currentScript, and ifdef it
1 parent cc2ffb2 commit be5f44a

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

site/source/docs/api_reference/module.rst

+14-2
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,25 @@ The following ``Module`` attributes affect code execution. Set them to customize
5353

5454
.. js:attribute:: Module.locateFile
5555

56-
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. NOTE: ``prefix`` might be empty string for memory initializer file is loaded in parallel with JS or in packager, so be careful with those.
56+
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 the main JavaScript file's directory), 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 the JavaScript file (which is the default expectation), for example if you want to host them on a CDN.
57+
58+
.. note:: ``prefix`` might be an empty string, if ``locateFile`` is called before we load the main JavaScript. For example, that can happen if a file package or a mememory initializer file are loaded beforehand (perhaps from the HTML, before it loads the main JavaScript).
59+
60+
.. note:: Several ``Module.*PrefixURL`` options have been deprecated in favor of ``locateFile``, which includes ``memoryInitializerPrefixURL``, ``pthreadMainPrefixURL``, ``cdInitializerPrefixURL``, ``filePackagePrefixURL``. To update your code, for example if you used ``Module.memoryInitializerPrefixURL`` equal to ``"https://mycdn.com/memory-init-dir/"``, then you can replace that with something like
61+
62+
::
63+
64+
Module['locateFile'] = function(path, prefix) {
65+
// if it's a mem init file, use a custom dir
66+
if (path.endsWith(".mem")) return "https://mycdn.com/memory-init-dir/" + path;
67+
// otherwise, use the default, the prefix (JS file's dir) + the path
68+
return prefix + path;
69+
}
5770

5871
.. js:attribute:: Module.logReadFiles
5972

6073
If set, stderr will log when any file is read.
6174

62-
6375
.. js:attribute:: Module.onAbort
6476

6577
If set, this function is called when abnormal program termination occurs. That can happen due to the C method ``abort()`` being called directly, or called from JavaScript, or due to a fatal problem such as being unable to fetch a necessary file during startup (like the wasm binary when running wasm), etc. After calling this function, program termination occurs (i.e., you can't use this to try to do something else instead of stopping; there is no possibility of recovering here).

src/shell.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,13 @@ if (ENVIRONMENT_IS_SHELL) {
230230
#if ENVIRONMENT_MAY_BE_WEB_OR_WORKER
231231
if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) {
232232
if (ENVIRONMENT_IS_WEB) {
233-
// We do this for `-s MODULARIZE=1` where at the time when this code runs document.currentScript might already be unavailable
233+
#if MODULARIZE
234+
// When MODULARIZE, this JS may be executed later, after document.currentScript is gone, so we send it
235+
// using this._currentScript.
234236
var currentScript = this['_currentScript'] || document.currentScript;
237+
#else
238+
var currentScript = document.currentScript;
239+
#endif
235240
if (currentScript.src.indexOf('blob:') !== 0) {
236241
scriptDirectory = currentScript.src.split('/').slice(0, -1).join('/') + '/';
237242
}

tests/test_other.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -8542,8 +8542,9 @@ def test_html_preprocess(self):
85428542

85438543
# Tests that Emscripten-compiled applications can be run from a relative path with node command line that is different than the current working directory.
85448544
def test_node_js_run_from_different_directory(self):
8545-
if not os.path.exists('subdir'):
8546-
os.mkdir('subdir')
8547-
run_process([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-o', os.path.join('subdir', 'a.js'), '-O3'])
8548-
ret = run_process(NODE_JS + [os.path.join('subdir', 'a.js')], stdout=PIPE).stdout
8549-
assert 'hello, world!' in ret
8545+
if not os.path.exists('subdir'):
8546+
os.mkdir('subdir')
8547+
run_process([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-o', os.path.join('subdir', 'a.js'), '-O3'])
8548+
ret = run_process(NODE_JS + [os.path.join('subdir', 'a.js')], stdout=PIPE).stdout
8549+
self.assertContained('hello, world!', ret)
8550+

0 commit comments

Comments
 (0)