Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SINGLE_FILE html and worker fixes + tests #5736

Merged
merged 7 commits into from
Nov 7, 2017
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
33 changes: 26 additions & 7 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2413,16 +2413,16 @@ def generate_html(target, options, js_target, target_basename,
asm_mods = []

if options.proxy_to_worker:
proxy_worker_filename = shared.Settings.PROXY_TO_WORKER_FILENAME or target_basename
proxy_worker_filename = (shared.Settings.PROXY_TO_WORKER_FILENAME or target_basename) + '.js'
worker_js = worker_js_script(proxy_worker_filename)
script.inline = '''
script.inline = ('''
var filename = '%s';
if ((',' + window.location.search.substr(1) + ',').indexOf(',noProxy,') < 0) {
console.log('running code in a web worker');
''' + worker_js + '''
''' % shared.JS.get_subresource_location(proxy_worker_filename)) + worker_js + '''
} else {
// note: no support for code mods (PRECISE_F32==2)
console.log('running code on the main thread');
var filename = '%s';
var fileBytes = tryParseAsDataURI(filename);
var script = document.createElement('script');
if (fileBytes) {
Expand All @@ -2432,7 +2432,7 @@ def generate_html(target, options, js_target, target_basename,
}
document.body.appendChild(script);
}
''' % shared.JS.get_subresource_location(proxy_worker_filename + '.js')
'''
else:
# Normal code generation path
script.src = base_js_target
Expand All @@ -2442,6 +2442,7 @@ def generate_html(target, options, js_target, target_basename,
minified = 'minifyNames' in optimizer.queue_history,
separate_asm = options.separate_asm)

if not shared.Settings.SINGLE_FILE:
if shared.Settings.EMTERPRETIFY_FILE:
# We need to load the emterpreter file before anything else, it has to be synchronously ready
script.un_src()
Expand Down Expand Up @@ -2567,6 +2568,17 @@ def generate_html(target, options, js_target, target_basename,
script.inline = f.read() + script.inline
f.close()

# inline script for SINGLE_FILE output
if shared.Settings.SINGLE_FILE:
js_contents = script.inline or ''
if script.src:
js = open(js_target, 'r')
js_contents += js.read()
js.close()
shared.try_delete(js_target)
script.src = None
script.inline = js_contents

html = open(target, 'wb')
html_contents = shell.replace('{{{ SCRIPT }}}', script.replacement())
html_contents = tools.line_endings.convert_line_endings(html_contents, '\n', options.output_eol)
Expand All @@ -2575,9 +2587,16 @@ def generate_html(target, options, js_target, target_basename,


def generate_worker_js(target, js_target, target_basename):
shutil.move(js_target, unsuffixed(js_target) + '.worker.js') # compiler output goes in .worker.js file
# compiler output is embedded as base64
if shared.Settings.SINGLE_FILE:
proxy_worker_filename = shared.JS.get_subresource_location(js_target)

# compiler output goes in .worker.js file
else:
shutil.move(js_target, unsuffixed(js_target) + '.worker.js')
worker_target_basename = target_basename + '.worker'
proxy_worker_filename = shared.Settings.PROXY_TO_WORKER_FILENAME or worker_target_basename
proxy_worker_filename = (shared.Settings.PROXY_TO_WORKER_FILENAME or worker_target_basename) + '.js'

target_contents = worker_js_script(proxy_worker_filename)
open(target, 'w').write(target_contents)

Expand Down
7 changes: 5 additions & 2 deletions src/proxyClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ var SUPPORT_BASE64_EMBEDDING;

// Worker

var filename = '{{{ filename }}}.js';
var filename;
if (!filename) {
filename = '{{{ filename }}}';
}

var workerURL = filename;
if (SUPPORT_BASE64_EMBEDDING) {
Expand All @@ -110,7 +113,7 @@ if (SUPPORT_BASE64_EMBEDDING) {
workerURL = URL.createObjectURL(new Blob([fileBytes], {type: 'application/javascript'}));
}
}
var worker = new Worker(filename);
var worker = new Worker(workerURL);

WebGLClient.prefetch();

Expand Down
13 changes: 13 additions & 0 deletions tests/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3707,3 +3707,16 @@ def test_base64_atob_fallback(self):
</script>
''')
self.run_browser('a.html', '...', '/report_result?0')

# Tests that SINGLE_FILE works as intended in generated HTML (with and without Worker)
def test_single_file_html(self):
self.btest('emscripten_main_loop_setimmediate.cpp', '1', args=['-s', 'SINGLE_FILE=1', '-s', 'WASM=1', '-s', "BINARYEN_METHOD='native-wasm'"], also_proxied=True)
assert os.path.exists('test.html') and not os.path.exists('test.js') and not os.path.exists('test.worker.js')

# Tests that SINGLE_FILE works as intended in a Worker in JS output
def test_single_file_worker_js(self):
open('src.cpp', 'w').write(self.with_report_result(open(path_from_root('tests', 'browser_test_hello_world.c')).read()))
Popen([PYTHON, EMCC, 'src.cpp', '-o', 'test.js', '--proxy-to-worker', '-s', 'SINGLE_FILE=1', '-s', 'WASM=1', '-s', "BINARYEN_METHOD='native-wasm'"]).communicate()
open('test.html', 'w').write('<script src="test.js"></script>')
self.run_browser('test.html', None, '/report_result?0')
assert os.path.exists('test.js') and not os.path.exists('test.worker.js')