Skip to content

Commit ace7831

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 ace7831

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

tools/link.py

Lines changed: 12 additions & 24 deletions
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
@@ -2449,22 +2455,12 @@ def generate_traditional_runtime_html(target, options, js_target, target_basenam
24492455
# We need to load the wasm file before anything else, it has to be synchronously ready TODO: optimize
24502456
script.un_src()
24512457
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);
2458+
fetch('%s')
2459+
.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:
@@ -2487,14 +2483,6 @@ def generate_traditional_runtime_html(target, options, js_target, target_basenam
24872483
}
24882484
''' % (script.inline, get_subresource_location(wasm_target) + '.js')
24892485

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-
24982486
# inline script for SINGLE_FILE output
24992487
if settings.SINGLE_FILE:
25002488
js_contents = script.inline or ''

0 commit comments

Comments
 (0)