diff --git a/src/preamble.js b/src/preamble.js index fcb076e908b3f..afcae6766d5d2 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -471,8 +471,19 @@ function abort(what) { // TODO(https://github.com/google/closure-compiler/pull/3913): Remove if/when upstream closure gets fixed. #if WASM_EXCEPTIONS == 1 // See above, in the meantime, we resort to wasm code for trapping. - ___trap(); -#else + // + // In case abort() is called before the module is initialized, Module['asm'] + // and its exported '__trap' function is not available, in which case we throw + // a RuntimeError. + // + // We trap instead of throwing RuntimeError to prevent infinite-looping in + // Wasm EH code (because RuntimeError is considered as a foreign exception and + // caught by 'catch_all'), but in case throwing RuntimeError is fine because + // the module has not even been instantiated, even less running. + if (runtimeInitialized) { + ___trap(); + } +#endif /** @suppress {checkTypes} */ var e = new WebAssembly.RuntimeError(what); @@ -483,7 +494,6 @@ function abort(what) { // in code paths apart from instantiation where an exception is expected // to be thrown when abort is called. throw e; -#endif } #include "memoryprofiler.js"