[WASM] Workaround for compiled wasm runs under node
This commit is contained in:
@@ -365,6 +365,7 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
|
|||||||
"esm" -> WasmLoaderKind.BROWSER
|
"esm" -> WasmLoaderKind.BROWSER
|
||||||
"nodejs" -> WasmLoaderKind.NODE
|
"nodejs" -> WasmLoaderKind.NODE
|
||||||
"d8" -> WasmLoaderKind.D8
|
"d8" -> WasmLoaderKind.D8
|
||||||
|
"d8NodeCompatible" -> WasmLoaderKind.D8NodeCompatible
|
||||||
else -> throw IllegalArgumentException("Unrecognized flavor for the wasm launcher")
|
else -> throw IllegalArgumentException("Unrecognized flavor for the wasm launcher")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -122,13 +122,18 @@ fun WasmCompiledModuleFragment.generateJs(): String {
|
|||||||
|
|
||||||
enum class WasmLoaderKind {
|
enum class WasmLoaderKind {
|
||||||
D8,
|
D8,
|
||||||
|
D8NodeCompatible,
|
||||||
NODE,
|
NODE,
|
||||||
BROWSER,
|
BROWSER,
|
||||||
}
|
}
|
||||||
|
|
||||||
fun generateJsWasmLoader(kind: WasmLoaderKind, wasmFilePath: String, externalJs: String): String {
|
fun generateJsWasmLoader(kind: WasmLoaderKind, wasmFilePath: String, externalJs: String): String {
|
||||||
|
val nodeExitOnD8 =
|
||||||
|
if (kind == WasmLoaderKind.D8NodeCompatible) "if ((typeof process !== 'undefined') && (typeof process.versions.node !== 'undefined')) process.exit(0)\n"
|
||||||
|
else ""
|
||||||
|
|
||||||
val instantiation = when (kind) {
|
val instantiation = when (kind) {
|
||||||
WasmLoaderKind.D8 ->
|
WasmLoaderKind.D8, WasmLoaderKind.D8NodeCompatible ->
|
||||||
"""
|
"""
|
||||||
const wasmModule = new WebAssembly.Module(read('$wasmFilePath', 'binary'));
|
const wasmModule = new WebAssembly.Module(read('$wasmFilePath', 'binary'));
|
||||||
const wasmInstance = new WebAssembly.Instance(wasmModule, { js_code });
|
const wasmInstance = new WebAssembly.Instance(wasmModule, { js_code });
|
||||||
@@ -164,9 +169,11 @@ fun generateJsWasmLoader(kind: WasmLoaderKind, wasmFilePath: String, externalJs:
|
|||||||
|
|
||||||
WasmLoaderKind.NODE ->
|
WasmLoaderKind.NODE ->
|
||||||
"module.exports = wasmExports;\n"
|
"module.exports = wasmExports;\n"
|
||||||
|
|
||||||
|
WasmLoaderKind.D8NodeCompatible -> ""
|
||||||
}
|
}
|
||||||
|
|
||||||
return externalJs + instantiation + init + export
|
return nodeExitOnD8 + externalJs + instantiation + init + export
|
||||||
}
|
}
|
||||||
|
|
||||||
fun writeCompilationResult(
|
fun writeCompilationResult(
|
||||||
|
|||||||
Reference in New Issue
Block a user