Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions site/source/docs/api_reference/module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,25 @@ The following ``Module`` attributes affect code execution. Set them to customize

.. 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 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.
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.

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

.. 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

::

Module['locateFile'] = function(path, prefix) {
// if it's a mem init file, use a custom dir
if (path.endsWith(".mem")) return "https://mycdn.com/memory-init-dir/" + path;
// otherwise, use the default, the prefix (JS file's dir) + the path
return prefix + path;
}

.. js:attribute:: Module.logReadFiles

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


.. js:attribute:: Module.onAbort

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).
Expand Down
7 changes: 6 additions & 1 deletion src/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,13 @@ if (ENVIRONMENT_IS_SHELL) {
#if ENVIRONMENT_MAY_BE_WEB_OR_WORKER
if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) {
if (ENVIRONMENT_IS_WEB) {
// We do this for `-s MODULARIZE=1` where at the time when this code runs document.currentScript might already be unavailable
#if MODULARIZE
// When MODULARIZE, this JS may be executed later, after document.currentScript is gone, so we send it
// using this._currentScript.
var currentScript = this['_currentScript'] || document.currentScript;
#else
var currentScript = document.currentScript;
#endif
if (currentScript.src.indexOf('blob:') !== 0) {
scriptDirectory = currentScript.src.split('/').slice(0, -1).join('/') + '/';
}
Expand Down
11 changes: 6 additions & 5 deletions tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -8542,8 +8542,9 @@ def test_html_preprocess(self):

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