From 0bc026ca9905412fc6dc8d80a3f7976db0739640 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 23 Jul 2018 14:29:57 -0700 Subject: [PATCH 1/5] generalize blob url handling in scriptDirectory computation to also handle workers --- src/shell.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/shell.js b/src/shell.js index 513835328517f..910bf9c9b9a41 100644 --- a/src/shell.js +++ b/src/shell.js @@ -233,15 +233,19 @@ if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) { #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; + scriptDirectory = this['_currentScript'] || document.currentScript.src; #else - var currentScript = document.currentScript; + scriptDirectory = document.currentScript.src; #endif - if (currentScript.src.indexOf('blob:') !== 0) { - scriptDirectory = currentScript.src.split('/').slice(0, -1).join('/') + '/'; - } - } else if (ENVIRONMENT_IS_WORKER) { - scriptDirectory = self.location.href.split('/').slice(0, -1).join('/') + '/'; + } else { // worker + scriptDirectory = self.location.href; + } + // 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.indexOf('blob:') !== 0) { + scriptDirectory = scriptDirectory.split('/').slice(0, -1).join('/') + '/'; + } else { + scriptDirectory = ''; } #if ENVIRONMENT From f2af37d07c5a3b438bda8c5138a1cd226c7daa75 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 23 Jul 2018 16:29:25 -0700 Subject: [PATCH 2/5] fix --- emcc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/emcc.py b/emcc.py index 54fae6494b7a5..b738974040130 100755 --- a/emcc.py +++ b/emcc.py @@ -2604,7 +2604,7 @@ def modularize(): return %(EXPORT_NAME)s; }; %(EXPORT_NAME)s = %(EXPORT_NAME)s.bind({ - _currentScript: typeof document !== 'undefined' ? document.currentScript : undefined + _currentScript: typeof document !== 'undefined' ? document.currentScript.src : undefined })%(instantiate)s; ''' % { 'EXPORT_NAME': shared.Settings.EXPORT_NAME, From 04491ed5104219e2ffc601f8d47dc37449d63086 Mon Sep 17 00:00:00 2001 From: "Alon Zakai (kripken)" Date: Wed, 25 Jul 2018 08:48:35 -0700 Subject: [PATCH 3/5] more careful use of currentScript --- emcc.py | 6 +++--- src/shell.js | 13 ++++++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/emcc.py b/emcc.py index b738974040130..176211a1e33da 100755 --- a/emcc.py +++ b/emcc.py @@ -2603,9 +2603,9 @@ def modularize(): return %(EXPORT_NAME)s; }; -%(EXPORT_NAME)s = %(EXPORT_NAME)s.bind({ - _currentScript: typeof document !== 'undefined' ? document.currentScript.src : undefined -})%(instantiate)s; +// When MODULARIZE, this JS may be executed later, after document.currentScript +// is gone, so we save it. +%(EXPORT_NAME)s._currentScript = typeof document !== 'undefined' && document ? document.currentScript : undefined; ''' % { 'EXPORT_NAME': shared.Settings.EXPORT_NAME, 'src': src, diff --git a/src/shell.js b/src/shell.js index 910bf9c9b9a41..2757b25f86d52 100644 --- a/src/shell.js +++ b/src/shell.js @@ -230,12 +230,15 @@ if (ENVIRONMENT_IS_SHELL) { #if ENVIRONMENT_MAY_BE_WEB_OR_WORKER if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) { if (ENVIRONMENT_IS_WEB) { + if (document.currentScript) { + scriptDirectory = document.currentScript.src; + } #if MODULARIZE - // When MODULARIZE, this JS may be executed later, after document.currentScript is gone, so we send it - // using this._currentScript. - scriptDirectory = this['_currentScript'] || document.currentScript.src; -#else - scriptDirectory = document.currentScript.src; + // 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 (Module['_currentScript']) { + scriptDirectory = Module['_currentScript'].src; + } #endif } else { // worker scriptDirectory = self.location.href; From 63c3ab9d021446c6a9f4a1e0c83e7e7670221c2c Mon Sep 17 00:00:00 2001 From: "Alon Zakai (kripken)" Date: Wed, 25 Jul 2018 11:58:33 -0700 Subject: [PATCH 4/5] handle a missing document.scriptDirectory, and careful handling of this['_scriptDir'] --- emcc.py | 4 +++- src/shell.js | 14 +++++++------- tests/test_browser.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/emcc.py b/emcc.py index 8258d6cc6054c..b5f42f1ffabb9 100755 --- a/emcc.py +++ b/emcc.py @@ -2614,7 +2614,9 @@ def modularize(): }; // When MODULARIZE, this JS may be executed later, after document.currentScript // is gone, so we save it. -%(EXPORT_NAME)s._currentScript = typeof document !== 'undefined' && document ? document.currentScript : undefined; +%(EXPORT_NAME)s = %(EXPORT_NAME)s.bind({ + _scriptDir: typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined +}); ''' % { 'EXPORT_NAME': shared.Settings.EXPORT_NAME, 'src': src, diff --git a/src/shell.js b/src/shell.js index 2757b25f86d52..adcb3c7aad6b0 100644 --- a/src/shell.js +++ b/src/shell.js @@ -233,16 +233,16 @@ if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) { if (document.currentScript) { 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 (Module['_currentScript']) { - scriptDirectory = Module['_currentScript'].src; - } -#endif } else { // worker scriptDirectory = self.location.href; } +#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 (this['_scriptDir']) { + scriptDirectory = this['_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.indexOf('blob:') !== 0) { diff --git a/tests/test_browser.py b/tests/test_browser.py index 67783728af10c..b0c619333c4ef 100644 --- a/tests/test_browser.py +++ b/tests/test_browser.py @@ -3972,3 +3972,34 @@ def test_browser_run_from_different_directory_async(self): ''') self.run_browser('test-subdir.html', None, '/report_result?0') + + # Similar to `test_browser_run_from_different_directory`, but + # also also we eval the initial code, so currentScript is not present. That prevents us + # from finding the file in a subdir, but here we at least check we do not regress compared to the + # normal case of finding in the current dir. + # In addition, check for new Module(), which overrides the bind() and replaces the object + # which saved the _scriptDir. Again, we can't get the script dir that way, but at least we + # should not regress compared to the normal case. + def test_browser_modularize_no_current_script(self): + src = open(path_from_root('tests', 'browser_test_hello_world.c')).read() + open('test.c', 'w').write(self.with_report_result(src)) + # compile the code with the modularize feature and the preload-file option enabled + Popen([PYTHON, EMCC, 'test.c', '-o', 'test.js', '-s', 'MODULARIZE=1']).communicate() + for creation in ( + 'Module();', + 'new Module();' + ): + print(creation) + open('test.html', 'w').write(''' + + ''' % creation) + self.run_browser('test.html', None, '/report_result?0') + From 11d983a3fb63d64f327c6623c32dce2caf1a6fc9 Mon Sep 17 00:00:00 2001 From: "Alon Zakai (kripken)" Date: Wed, 25 Jul 2018 12:48:34 -0700 Subject: [PATCH 5/5] fix --- emcc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/emcc.py b/emcc.py index b5f42f1ffabb9..49a0a288135e1 100755 --- a/emcc.py +++ b/emcc.py @@ -2616,7 +2616,7 @@ def modularize(): // is gone, so we save it. %(EXPORT_NAME)s = %(EXPORT_NAME)s.bind({ _scriptDir: typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined -}); +})%(instantiate)s; ''' % { 'EXPORT_NAME': shared.Settings.EXPORT_NAME, 'src': src,