Skip to content

Commit

Permalink
Remove unnecessary try/catch in instantiateSync. NFC (emscripten-core…
Browse files Browse the repository at this point in the history
…#19784)

The catch only existed to handle cases where `IMPORTED_MEMORY` was set
and `INITIAL_MEMORY` was overridden at runtime to a value that was too
low.  This is such a rare setup that I don't believe it warrents this
special case.  In the past, setting `INITIAL_MEMORY` at runtime was more
common, but since `IMPORTED_MEMORY` now defaults to false
`INITIAL_MEMORY` is not valid at all for most users.

Also, the default error message from the runtime seems seems clear
enough.

Also, simply and if-after-else.

Also, rename getBinary to getBinarySync, so it explicitly contrasts with
the other getBinaryPromise.
  • Loading branch information
sbc100 authored Jul 5, 2023
1 parent 047edb4 commit b6670d7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 57 deletions.
92 changes: 39 additions & 53 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ if (Module['locateFile']) {
}
#endif
function getBinary(file) {
function getBinarySync(file) {
if (file == wasmBinaryFile && wasmBinary) {
return new Uint8Array(wasmBinary);
}
Expand Down Expand Up @@ -677,22 +677,20 @@ function getBinaryPromise(binaryFile) {
throw "failed to load wasm binary file at '" + binaryFile + "'";
}
return response['arrayBuffer']();
}).catch(() => getBinary(binaryFile));
}).catch(() => getBinarySync(binaryFile));
}
#if ENVIRONMENT_MAY_BE_WEBVIEW
else {
if (readAsync) {
// fetch is not available or url is file => try XHR (readAsync uses XHR internally)
return new Promise((resolve, reject) => {
readAsync(binaryFile, (response) => resolve(new Uint8Array(/** @type{!ArrayBuffer} */(response))), reject)
});
}
else if (readAsync) {
// fetch is not available or url is file => try XHR (readAsync uses XHR internally)
return new Promise((resolve, reject) => {
readAsync(binaryFile, (response) => resolve(new Uint8Array(/** @type{!ArrayBuffer} */(response))), reject)
});
}
#endif
}
// Otherwise, getBinary should be able to get it synchronously
return Promise.resolve().then(() => getBinary(binaryFile));
// Otherwise, getBinarySync should be able to get it synchronously
return Promise.resolve().then(() => getBinarySync(binaryFile));
}
#if LOAD_SOURCE_MAP
Expand Down Expand Up @@ -742,58 +740,46 @@ function receiveSourceMapJSON(sourceMap) {

#if SPLIT_MODULE || !WASM_ASYNC_COMPILATION
function instantiateSync(file, info) {
var instance;
var module;
var binary;
try {
binary = getBinary(file);
var binary = getBinarySync(file);
#if NODE_CODE_CACHING
if (ENVIRONMENT_IS_NODE) {
var v8 = require('v8');
// Include the V8 version in the cache name, so that we don't try to
// load cached code from another version, which fails silently (it seems
// to load ok, but we do actually recompile the binary every time).
var cachedCodeFile = '{{{ WASM_BINARY_FILE }}}.' + v8.cachedDataVersionTag() + '.cached';
cachedCodeFile = locateFile(cachedCodeFile);
var hasCached = fs.existsSync(cachedCodeFile);
if (hasCached) {
if (ENVIRONMENT_IS_NODE) {
var v8 = require('v8');
// Include the V8 version in the cache name, so that we don't try to
// load cached code from another version, which fails silently (it seems
// to load ok, but we do actually recompile the binary every time).
var cachedCodeFile = '{{{ WASM_BINARY_FILE }}}.' + v8.cachedDataVersionTag() + '.cached';
cachedCodeFile = locateFile(cachedCodeFile);
var hasCached = fs.existsSync(cachedCodeFile);
if (hasCached) {
#if RUNTIME_DEBUG
dbg('NODE_CODE_CACHING: loading module');
#endif
try {
module = v8.deserialize(fs.readFileSync(cachedCodeFile));
} catch (e) {
err('NODE_CODE_CACHING: failed to deserialize, bad cache file? (' + cachedCodeFile + ')');
// Save the new compiled code when we have it.
hasCached = false;
}
dbg('NODE_CODE_CACHING: loading module');
#endif
try {
module = v8.deserialize(fs.readFileSync(cachedCodeFile));
} catch (e) {
err('NODE_CODE_CACHING: failed to deserialize, bad cache file? (' + cachedCodeFile + ')');
// Save the new compiled code when we have it.
hasCached = false;
}
}
if (!module) {
module = new WebAssembly.Module(binary);
}
if (ENVIRONMENT_IS_NODE && !hasCached) {
}
if (!module) {
module = new WebAssembly.Module(binary);
}
if (ENVIRONMENT_IS_NODE && !hasCached) {
#if RUNTIME_DEBUG
dbg('NODE_CODE_CACHING: saving module');
dbg('NODE_CODE_CACHING: saving module');
#endif
fs.writeFileSync(cachedCodeFile, v8.serialize(module));
}
fs.writeFileSync(cachedCodeFile, v8.serialize(module));
}
#else // NODE_CODE_CACHING
module = new WebAssembly.Module(binary);
module = new WebAssembly.Module(binary);
#endif // NODE_CODE_CACHING
instance = new WebAssembly.Instance(module, info);
var instance = new WebAssembly.Instance(module, info);
#if USE_OFFSET_CONVERTER
wasmOffsetConverter = new WasmOffsetConverter(binary, module);
#endif
} catch (e) {
var str = e.toString();
err('failed to compile wasm module: ' + str);
if (str.includes('imported Memory') ||
str.includes('memory import')) {
err('Memory size incompatibility issues may be due to changing INITIAL_MEMORY at runtime to something too large. Use ALLOW_MEMORY_GROWTH to allow any size memory (and also make sure not to set INITIAL_MEMORY at runtime to something smaller than it was at compile time).');
}
throw e;
}
wasmOffsetConverter = new WasmOffsetConverter(binary, module);
#endif
#if LOAD_SOURCE_MAP
receiveSourceMapJSON(getSourceMap());
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
59177
59169
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_no_asserts.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
32474
32466
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_strict.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
58143
58135
2 changes: 1 addition & 1 deletion test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -7708,7 +7708,7 @@ def test_binaryen_warn_mem(self):
self.run_process([EMXX, test_file('hello_world.cpp'), '-sINITIAL_MEMORY=' + str(16 * 1024 * 1024), '--pre-js', 'pre.js', '-sWASM_ASYNC_COMPILATION=0', '-sIMPORTED_MEMORY'])
out = self.run_js('a.out.js', assert_returncode=NON_ZERO)
self.assertContained('LinkError', out)
self.assertContained('Memory size incompatibility issues may be due to changing INITIAL_MEMORY at runtime to something too large. Use ALLOW_MEMORY_GROWTH to allow any size memory (and also make sure not to set INITIAL_MEMORY at runtime to something smaller than it was at compile time).', out)
self.assertContained("memory import 2 has a larger maximum size 800 than the module's declared maximum", out)
self.assertNotContained('hello, world!', out)
# and with memory growth, all should be good
self.run_process([EMXX, test_file('hello_world.cpp'), '-sINITIAL_MEMORY=' + str(16 * 1024 * 1024), '--pre-js', 'pre.js', '-sALLOW_MEMORY_GROWTH', '-sWASM_ASYNC_COMPILATION=0', '-sIMPORTED_MEMORY'])
Expand Down

0 comments on commit b6670d7

Please sign in to comment.