Hi, I have a sample wasmjs app uses few dom api..e...
# webassembly
s
Hi, I have a sample wasmjs app uses few dom api..everything compile fine, but throws the following error at runtime
async module:63 Uncaught (in promise) LinkError: WebAssembly.instantiate(): Import #2 module="js_code" function="kotlin.wasm.internal.stringLength": function import requires a callable
- Has anybody seen this error before?
e
sounds like you're trying to load the wasm output without providing the imports it needs
that should be set up by the wrapper that Kotlin generates. for example, in
build/dist/wasmJs/productionLibrary
,
project-name-wasm-js.wasm
should be accompanied by
project-name-wasm-js.uninstantiated.mjs
and `project-name-wasm-js.mjs`; the last one sets everything up
s
that should be set up by the wrapper that Kotlin generates.
yes, i check the dist (
wasmJsBrowserDistribution
) output and everything seems ok.. i am trying to do some js interop by importing
Copy code
import kotlinx.browser.document
import kotlinx.browser.window
import org.w3c.dom.*
import org.w3c.dom.events.Event
Simple hello world is working fine though
e
how are you loading the wasm? it needs to be through the JS wrapper, e.g. https://github.com/Kotlin/kotlin-wasm-examples/blob/main/browser-example/src/wasmJsMain/resources/index.html#L65, or something equivalent
s
yes i am following the same.. all i did is, moved the wasm error handling from js to kotlin
Copy code
val WasmCompileError: JsAny = js("WebAssembly.CompileError")

fun main() {
  println("Hello, Kotlin Wasm!")
  window.addEventListener("error") {
    it as ErrorEvent
    unhandledError(it, it.error!!)
  }
  window.addEventListener("unhandledrejection") {
    it as PromiseRejectionEvent
    unhandledError(it, it.reason!!)
  }
}

fun unhandledError(event: Event, error: JsAny) {
  if (error::class == WasmCompileError) {
    val warn = document.getElementById("warning") as HTMLDivElement
    warn.style.display = "initial"
    // Hide a Scary Webpack Overlay which is less informative in this case.
    val webPack = document.getElementById("webpack-dev-server-client-overlay") as? HTMLDivElement
    webPack?.style?.display = "none"
  }
}
Everything else is same
e
hmm, then maybe it's a kotlin bug. repro?
s
yeah, i will submit a youtrack issue.
The issue was because of
applyBinaryen()
. Disabled it and seems fine now.
s
If
WebAssembly.CompileError
is thrown by compiling Kotlin
.wasm
module, then your Kotlin code will not be runnable, and you will not be able to handle the error in Kotlin. Or are you compiling some other WebAssembly and handling it in Kotlin?
s
@Svyatoslav Kuzmich [JB] thanks.. that was a mistake from my side.. after removing it and disabling applyBinaryen , things are working fine
👍 1
s
We would appreciate the report. Applying binaryen shouldn’t add crashes to apps.
👍 1
216 Views