Skip to content
Closed
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
20 changes: 7 additions & 13 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3908,28 +3908,22 @@ def modularize():
# document.currentScript, so a simple export declaration is enough.
src = 'var %s=%s' % (settings.EXPORT_NAME, src)
else:
script_url_node = ''
script_url_web = ''
# When MODULARIZE this JS may be executed later,
# after document.currentScript is gone, so we save it.
# In EXPORT_ES6 + PTHREADS the 'thread' is actually an ES6 module webworker running in strict mode,
# so doesn't have access to 'document'. In this case use 'import.meta' instead.
if settings.EXPORT_ES6 and settings.USE_ES6_IMPORT_META:
script_url = 'import.meta.url'
else:
script_url = "typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined"
if shared.target_environment_may_be('node'):
script_url_node = "if (typeof __filename !== 'undefined') _scriptDir = _scriptDir || __filename;"
# In EXPORT_ES6 mode we can just use 'import.meta'.
if shared.target_environment_may_be('web') and \
not settings.EXPORT_ES6 or not settings.USE_ES6_IMPORT_META:
script_url_web = "var _scriptSrc = typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : '';"
src = '''%(node_imports)s
var %(EXPORT_NAME)s = (() => {
var _scriptDir = %(script_url)s;
%(script_url_node)s
%(script_url_web)s
return (%(src)s);
})();
''' % {
'node_imports': node_es6_imports(),
'EXPORT_NAME': settings.EXPORT_NAME,
'script_url': script_url,
'script_url_node': script_url_node,
'script_url_web': script_url_web,
'src': src
}

Expand Down
6 changes: 3 additions & 3 deletions src/closure-externs/closure-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,13 @@ var removeEventListener = function (type, listener) {};
*/
var close;

// Due to the way MODULARIZE works, Closure is run on generated code that does not define _scriptDir,
// but only after MODULARIZE has finished, _scriptDir is injected to the generated code.
// Due to the way MODULARIZE works, Closure is run on generated code that does not define _scriptSrc,
// but only after MODULARIZE has finished, _scriptSrc is injected to the generated code.
// Therefore it cannot be minified.
/**
* @suppress {duplicate, undefinedVars}
*/
var _scriptDir;
var _scriptSrc;

// Closure run on asm.js uses a hack to execute only on shell code, declare externs needed for it.
/**
Expand Down
2 changes: 1 addition & 1 deletion src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ var LibraryPThread = {
// independently load up the same main application file.
'urlOrBlob': Module['mainScriptUrlOrBlob']
#if !EXPORT_ES6
|| _scriptDir
|| currentScript
#endif
,
#if WASM2JS
Expand Down
2 changes: 1 addition & 1 deletion src/library_wasm_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ mergeInto(LibraryManager.library, {
'mem': wasmMemory,
#else
'wasm': wasmModule,
'js': Module['mainScriptUrlOrBlob'] || _scriptDir,
'js': Module['mainScriptUrlOrBlob'] || currentScript,
'wasmMemory': wasmMemory,
#endif
'sb': stackLowestAddress, // sb = stack bottom (lowest stack address, SP points at this when stack is full)
Expand Down
2 changes: 1 addition & 1 deletion src/library_webaudio.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ let LibraryWebAudio = {
#if MINIMAL_RUNTIME
Module['js']
#else
Module['mainScriptUrlOrBlob'] || _scriptDir
Module['mainScriptUrlOrBlob'] || currentScript
#endif
);
}).then(() => {
Expand Down
76 changes: 37 additions & 39 deletions src/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,36 @@ var ENVIRONMENT_IS_PTHREAD = Module['ENVIRONMENT_IS_PTHREAD'] || false;
var ENVIRONMENT_IS_WASM_WORKER = Module['$ww'];
#endif

#if SHARED_MEMORY && !MODULARIZE
// In MODULARIZE mode _scriptDir needs to be captured already at the very top of the page immediately when the page is parsed, so it is generated there
// before the page load. In non-MODULARIZE modes generate it here.
var _scriptDir = (typeof document != 'undefined' && document.currentScript) ? document.currentScript.src : undefined;
#if EXPORT_ES6 && USE_ES6_IMPORT_META
var currentScript = import.meta.url;
#else
var currentScript;

if (ENVIRONMENT_IS_WORKER) {
_scriptDir = self.location.href;
}
#if ENVIRONMENT_MAY_BE_NODE
else if (ENVIRONMENT_IS_NODE) {
_scriptDir = __filename;
}
if (typeof __filename !== 'undefined') { // Node
currentScript = __filename;
} else
#endif // ENVIRONMENT_MAY_BE_NODE
#endif // SHARED_MEMORY && !MODULARIZE
#if ENVIRONMENT_MAY_BE_WORKER
if (ENVIRONMENT_IS_WORKER) {
currentScript = self.location.href;
} else
#endif // ENVIRONMENT_MAY_BE_WORKER
#if ENVIRONMENT_MAY_BE_WEB
#if MODULARIZE
// When MODULARIZE this JS may be executed later, after document.currentScript is gone, so we store it in _scriptSrc.
if (ENVIRONMENT_IS_WEB) {
currentScript = _scriptSrc;
} else
#else
// In non-MODULARIZE mode we generate it here.
if (typeof document != 'undefined' && document.currentScript) { // web
currentScript = document.currentScript.src;
} else
#endif
#endif // ENVIRONMENT_MAY_BE_WEB
currentScript = '';
#endif // EXPORT_ES6 && USE_ES6_IMPORT_META

// `/` should be present at the end if `scriptDirectory` is not empty
var scriptDirectory = '';
Expand Down Expand Up @@ -203,18 +219,14 @@ if (ENVIRONMENT_IS_NODE) {
var fs = require('fs');
var nodePath = require('path');

if (ENVIRONMENT_IS_WORKER) {
scriptDirectory = nodePath.dirname(scriptDirectory) + '/';
} else {
#if EXPORT_ES6
// EXPORT_ES6 + ENVIRONMENT_IS_NODE always requires use of import.meta.url,
// since there's no way getting the current absolute path of the module when
// support for that is not available.
scriptDirectory = require('url').fileURLToPath(new URL('./', import.meta.url)); // includes trailing slash
// EXPORT_ES6 + ENVIRONMENT_IS_NODE always requires use of import.meta.url,
// since there's no way getting the current absolute path of the module when
// support for that is not available.
scriptDirectory = require('url').fileURLToPath(new URL('./', import.meta.url)); // includes trailing slash
#else
scriptDirectory = __dirname + '/';
scriptDirectory = __dirname + '/';
#endif
}

#include "node_shell_read.js"

Expand Down Expand Up @@ -379,28 +391,14 @@ if (ENVIRONMENT_IS_SHELL) {
// ENVIRONMENT_IS_NODE.
#if ENVIRONMENT_MAY_BE_WEB || ENVIRONMENT_MAY_BE_WORKER
if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) {
if (ENVIRONMENT_IS_WORKER) { // Check worker, not web, since window could be polyfilled
scriptDirectory = self.location.href;
} else if (typeof document != 'undefined' && document.currentScript) { // web
scriptDirectory = document.currentScript.src;
}
#if MODULARIZE
// When MODULARIZE, this JS may be executed later, after document.currentScript
// is gone, so we saved it, and we use it here instead of any other info.
if (_scriptDir) {
scriptDirectory = _scriptDir;
}
#endif
// blob urls look like blob:http://site.com/etc/etc and we cannot infer anything from them.
// otherwise, slice off the final part of the url to find the script directory.
// if scriptDirectory does not contain a slash, lastIndexOf will return -1,
// and scriptDirectory will correctly be replaced with an empty string.
// If scriptDirectory contains a query (starting with ?) or a fragment (starting with #),
// if currentScript does not contain a slash, lastIndexOf will return -1,
// and scriptDirectory will be an empty string.
// If currentScript contains a query (starting with ?) or a fragment (starting with #),
// they are removed because they could contain a slash.
if (scriptDirectory.indexOf('blob:') !== 0) {
scriptDirectory = scriptDirectory.substr(0, scriptDirectory.replace(/[?#].*/, "").lastIndexOf('/')+1);
} else {
scriptDirectory = '';
if (currentScript.indexOf('blob:') !== 0) {
scriptDirectory = currentScript.substr(0, currentScript.replace(/[?#].*/, "").lastIndexOf('/')+1);
}

#if ENVIRONMENT && ASSERTIONS
Expand Down
33 changes: 26 additions & 7 deletions src/shell_minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,6 @@ function ready() {

#if PTHREADS

#if !MODULARIZE
// In MODULARIZE mode _scriptDir needs to be captured already at the very top of the page immediately when the page is parsed, so it is generated there
// before the page load. In non-MODULARIZE modes generate it here.
var _scriptDir = (typeof document != 'undefined' && document.currentScript) ? document.currentScript.src : undefined;
#endif

// MINIMAL_RUNTIME does not support --proxy-to-worker option, so Worker and Pthread environments
// coincide.
#if WASM_WORKERS
Expand All @@ -174,5 +168,30 @@ var ENVIRONMENT_IS_WORKER = typeof importScripts == 'function',
var ENVIRONMENT_IS_WORKER = ENVIRONMENT_IS_PTHREAD = typeof importScripts == 'function';
#endif

var currentScriptUrl = typeof _scriptDir != 'undefined' ? _scriptDir : ((typeof document != 'undefined' && document.currentScript) ? document.currentScript.src : undefined);
#if EXPORT_ES6 && USE_ES6_IMPORT_META
var currentScript = import.meta.url;
#else
var currentScript;

#if ENVIRONMENT_MAY_BE_NODE
if (typeof __filename !== 'undefined') { // Node
currentScript = __filename;
} else
#endif // ENVIRONMENT_MAY_BE_NODE
#if ENVIRONMENT_MAY_BE_WEB
#if MODULARIZE
// When MODULARIZE this JS may be executed later, after document.currentScript is gone, so we store it in _scriptSrc.
if (ENVIRONMENT_IS_WEB) {
currentScript = _scriptSrc;
} else
#else
// In non-MODULARIZE mode we generate it here.
if (typeof document != 'undefined' && document.currentScript) { // web
currentScript = document.currentScript.src;
} else
#endif
#endif // ENVIRONMENT_MAY_BE_WEB
currentScript = '';
#endif // EXPORT_ES6 && USE_ES6_IMPORT_META

#endif // PTHREADS
7 changes: 4 additions & 3 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ if (ENVIRONMENT_IS_NODE) {
self: global,
require: require,
Module: Module,
location: {
href: __filename
},
#if !EXPORT_ES6
__filename: __filename,
__dirname: __dirname,
#endif
Worker: nodeWorkerThreads.Worker,
importScripts: function(f) {
(0, eval)(fs.readFileSync(f, 'utf8') + '//# sourceURL=' + f);
Expand Down
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors1.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
26183
26215
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors2.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
26147
26179
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
30720
30752
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except_wasm.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25862
25894
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_mangle.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
30720
30752
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_noexcept.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
26183
26215
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O0.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
23970
23998
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O1.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8555
8585
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O2.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6070
6100
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O3.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5896
5926
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_Os.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5896
5926
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_Oz.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5855
5885
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_dylink.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
27921
27954
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4639
4666
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5170
5197
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5238
5265
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6092
6123
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3_grow.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6436
6467
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5818
5848
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3_standalone.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5741
5771
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5256
5283
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5238
5265
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5238
5265
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_64.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4077
4104
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_O0.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20278
20306
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_O1.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4935
4962
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_O2.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3798
3826
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_O3.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3666
3693
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_Os.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3666
3693
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_Oz-ctors.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3647
3674
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_Oz.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3666
3693
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_pthreads.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15557
15432
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
64465
64391
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_no_asserts.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
37791
37717
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_strict.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
63499
63425
14 changes: 14 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -13330,3 +13330,17 @@ def test_missing_struct_info(self):
''')
err = self.expect_fail([EMCC, test_file('hello_world.c'), '--js-library=lib.js'])
self.assertContained('Error: Missing C define Foo! If you just added it to struct_info.json, you need to run ./tools/gen_struct_info.py', err)

@node_pthreads
def test_locate_file_abspath_pthread(self):
# Verify that `scriptDirectory` is an absolute path when `ENVIRONMENT_IS_WORKER`
self.emcc_args += ['-pthread', '--pre-js', 'pre.js']
self.set_setting('PROXY_TO_PTHREAD')
self.set_setting('EXIT_RUNTIME')
create_file('pre.js', '''
Module['locateFile'] = (fileName, scriptDirectory) => {
assert(nodePath['isAbsolute'](scriptDirectory), `scriptDirectory (${scriptDirectory}) should be an absolute path`);
return scriptDirectory + fileName;
};
''')
self.do_runf(test_file('hello_world.c'), 'hello, world!')