Skip to content

Commit cee1c5c

Browse files
committed
Cleanup generate_traditional_runtime_html. NFC
- Use fetch() over XHR. - No need for tryParseAsDataURI. This code is already in a `not settings.SINGLE_FILE` block which means URL for the wasm binary will never be data URI. See `get_subresource_location`.
1 parent 58a03c7 commit cee1c5c

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

tools/link.py

+15-26
Original file line numberDiff line numberDiff line change
@@ -2427,6 +2427,12 @@ def generate_traditional_runtime_html(target, options, js_target, target_basenam
24272427
document.body.appendChild(script);
24282428
}
24292429
'''
2430+
# add required helper functions such as tryParseAsDataURI
2431+
for filename in ('arrayUtils.js', 'base64Utils.js', 'URIUtils.js'):
2432+
content = shared.read_and_preprocess(utils.path_from_root('src', filename))
2433+
script.inline = content + script.inline
2434+
2435+
script.inline = 'var ASSERTIONS = %s;\n%s' % (settings.ASSERTIONS, script.inline)
24302436
else:
24312437
# Normal code generation path
24322438
script.src = base_js_target
@@ -2446,29 +2452,20 @@ def generate_traditional_runtime_html(target, options, js_target, target_basenam
24462452
''' % get_subresource_location(memfile)) + script.inline
24472453

24482454
if not settings.WASM_ASYNC_COMPILATION:
2449-
# We need to load the wasm file before anything else, it has to be synchronously ready TODO: optimize
2455+
# We need to load the wasm file before anything else, since it
2456+
# has be synchronously ready.
24502457
script.un_src()
24512458
script.inline = '''
2452-
var wasmURL = '%s';
2453-
var wasmXHR = new XMLHttpRequest();
2454-
wasmXHR.open('GET', wasmURL, true);
2455-
wasmXHR.responseType = 'arraybuffer';
2456-
wasmXHR.onload = function() {
2457-
if (wasmXHR.status === 200 || wasmXHR.status === 0) {
2458-
Module.wasmBinary = wasmXHR.response;
2459-
} else {
2460-
var wasmURLBytes = tryParseAsDataURI(wasmURL);
2461-
if (wasmURLBytes) {
2462-
Module.wasmBinary = wasmURLBytes.buffer;
2463-
}
2464-
}
2465-
%s
2466-
};
2467-
wasmXHR.send(null);
2459+
fetch('%s').then((result) => result.arrayBuffer())
2460+
.then((buf) => {
2461+
Module.wasmBinary = buf;
2462+
%s;
2463+
});
24682464
''' % (get_subresource_location(wasm_target), script.inline)
24692465

24702466
if settings.WASM == 2:
2471-
# If target browser does not support WebAssembly, we need to load the .wasm.js file before the main .js file.
2467+
# If target browser does not support WebAssembly, we need to load
2468+
# the .wasm.js file before the main .js file.
24722469
script.un_src()
24732470
script.inline = '''
24742471
function loadMainJs() {
@@ -2487,14 +2484,6 @@ def generate_traditional_runtime_html(target, options, js_target, target_basenam
24872484
}
24882485
''' % (script.inline, get_subresource_location(wasm_target) + '.js')
24892486

2490-
# when script.inline isn't empty, add required helper functions such as tryParseAsDataURI
2491-
if script.inline:
2492-
for filename in ('arrayUtils.js', 'base64Utils.js', 'URIUtils.js'):
2493-
content = shared.read_and_preprocess(utils.path_from_root('src', filename))
2494-
script.inline = content + script.inline
2495-
2496-
script.inline = 'var ASSERTIONS = %s;\n%s' % (settings.ASSERTIONS, script.inline)
2497-
24982487
# inline script for SINGLE_FILE output
24992488
if settings.SINGLE_FILE:
25002489
js_contents = script.inline or ''

0 commit comments

Comments
 (0)