[Wasm] Print an instruction if CompileError was thrown while instantiating a module
#KT-56160 Fixed
This commit is contained in:
@@ -216,6 +216,8 @@ fun WasmCompiledModuleFragment.generateAsyncJsWrapper(
|
|||||||
}.sorted()
|
}.sorted()
|
||||||
.joinToString("\n")
|
.joinToString("\n")
|
||||||
|
|
||||||
|
val d = "$"
|
||||||
|
|
||||||
//language=js
|
//language=js
|
||||||
return """
|
return """
|
||||||
export async function instantiate(imports={}, runInitializer=true) {
|
export async function instantiate(imports={}, runInitializer=true) {
|
||||||
@@ -263,27 +265,57 @@ $jsCodeBodyIndented
|
|||||||
$imports
|
$imports
|
||||||
};
|
};
|
||||||
|
|
||||||
if (isNodeJs) {
|
try {
|
||||||
const module = await import(/* webpackIgnore: true */'node:module');
|
if (isNodeJs) {
|
||||||
require = module.default.createRequire(import.meta.url);
|
const module = await import(/* webpackIgnore: true */'node:module');
|
||||||
const fs = require('fs');
|
require = module.default.createRequire(import.meta.url);
|
||||||
const path = require('path');
|
const fs = require('fs');
|
||||||
const url = require('url');
|
const path = require('path');
|
||||||
const filepath = url.fileURLToPath(import.meta.url);
|
const url = require('url');
|
||||||
const dirpath = path.dirname(filepath);
|
const filepath = url.fileURLToPath(import.meta.url);
|
||||||
const wasmBuffer = fs.readFileSync(path.resolve(dirpath, wasmFilePath));
|
const dirpath = path.dirname(filepath);
|
||||||
const wasmModule = new WebAssembly.Module(wasmBuffer);
|
const wasmBuffer = fs.readFileSync(path.resolve(dirpath, wasmFilePath));
|
||||||
wasmInstance = new WebAssembly.Instance(wasmModule, importObject);
|
const wasmModule = new WebAssembly.Module(wasmBuffer);
|
||||||
}
|
wasmInstance = new WebAssembly.Instance(wasmModule, importObject);
|
||||||
|
}
|
||||||
if (isStandaloneJsVM) {
|
|
||||||
const wasmBuffer = read(wasmFilePath, 'binary');
|
if (isStandaloneJsVM) {
|
||||||
const wasmModule = new WebAssembly.Module(wasmBuffer);
|
const wasmBuffer = read(wasmFilePath, 'binary');
|
||||||
wasmInstance = new WebAssembly.Instance(wasmModule, importObject);
|
const wasmModule = new WebAssembly.Module(wasmBuffer);
|
||||||
}
|
wasmInstance = new WebAssembly.Instance(wasmModule, importObject);
|
||||||
|
}
|
||||||
if (isBrowser) {
|
|
||||||
wasmInstance = (await WebAssembly.instantiateStreaming(fetch(wasmFilePath), importObject)).instance;
|
if (isBrowser) {
|
||||||
|
wasmInstance = (await WebAssembly.instantiateStreaming(fetch(wasmFilePath), importObject)).instance;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof WebAssembly.CompileError) {
|
||||||
|
const styles = [];
|
||||||
|
const styled = (t, css, escSeq) => isBrowser ? (styles.push(css, /* reset */""), `%c$d{t}%c`) : `\x1b[$d{escSeq}m$d{t}\x1b[m`;
|
||||||
|
const name = t => styled(t, "font-weight:bold", 1);
|
||||||
|
const uri = t => styled(t, "text-decoration:underline", 4);
|
||||||
|
const cli = t => styled(t, "font-family:monospace", 2);
|
||||||
|
|
||||||
|
let text = `Using experimental Kotlin/Wasm may require enabling experimental features in the target environment.
|
||||||
|
|
||||||
|
- $d{name("Chrome")}: enable $d{name("WebAssembly Garbage Collection")} at $d{uri("chrome://flags/#enable-webassembly-garbage-collection")} or run the program with the $d{cli("--js-flags=--experimental-wasm-gc")} command line argument.
|
||||||
|
- $d{name("Firefox")}: enable $d{name("javascript.options.wasm_function_references")} and $d{name("javascript.options.wasm_gc")} at $d{uri("about:config")}.
|
||||||
|
- $d{name("Edge")}: run the program with the $d{cli("--js-flags=--experimental-wasm-gc")} command line argument.
|
||||||
|
- $d{name("Node.js")}: run the program with the $d{cli("--experimental-wasm-gc")} command line argument.
|
||||||
|
|
||||||
|
For more information see $d{uri("https://kotl.in/wasm_help/")}.
|
||||||
|
`;
|
||||||
|
if (isBrowser) {
|
||||||
|
console.error(text, ...styles);
|
||||||
|
} else {
|
||||||
|
const t = "\n" + text;
|
||||||
|
if (typeof console !== "undefined" && console.log !== void 0)
|
||||||
|
console.log(t);
|
||||||
|
else
|
||||||
|
print(t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
wasmExports = wasmInstance.exports;
|
wasmExports = wasmInstance.exports;
|
||||||
|
|||||||
Reference in New Issue
Block a user