diff --git a/emcc.py b/emcc.py index 902be1969e5b7..49a0a288135e1 100755 --- a/emcc.py +++ b/emcc.py @@ -2612,8 +2612,10 @@ def modularize(): return %(EXPORT_NAME)s; }; +// When MODULARIZE, this JS may be executed later, after document.currentScript +// is gone, so we save it. %(EXPORT_NAME)s = %(EXPORT_NAME)s.bind({ - _currentScript: typeof document !== 'undefined' ? document.currentScript : undefined + _scriptDir: typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined })%(instantiate)s; ''' % { 'EXPORT_NAME': shared.Settings.EXPORT_NAME, diff --git a/src/shell.js b/src/shell.js index 513835328517f..adcb3c7aad6b0 100644 --- a/src/shell.js +++ b/src/shell.js @@ -230,18 +230,25 @@ 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; + } + } else { // worker + scriptDirectory = self.location.href; + } #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; + // 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 - 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('/') + '/'; + // 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 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') +