Hi, we are trying currently to migrate a Kotlin mu...
# javascript
p
Hi, we are trying currently to migrate a Kotlin multiplatform project to Web. For that we plan to cross-compile all our native dependencies via emscripten to WebAssembly (that works so far pretty well). Bundling works pretty well using resources for Module JS + WASM files. However, as required by many WASM engines, we utilize asynchronous compilation (https://github.com/emscripten-core/emscripten/blob/main/src/settings.js#L1306). Can we somehow tell Kotlin/JS to resolve functions async?
Copy code
@file:JsModule("./wasm-js-module.js")
@file:JsNonModule
package pkg.native.wasm.library
//typealias WasmPointer = Int
@JsName("_some_simd_function")
external fun fast_simd_calculus(input: WasmPointer, output: WasmPointer, size: Int)
The generated code now looks like the following and yields of course an
undefined
method because the module is a
Promise
object
Copy code
(function (root, factory) {
  if (typeof define === 'function' && define.amd)
    define(['exports', './wasm-js-module.js'], factory);
  else if (typeof exports === 'object')
    factory(module.exports, require('./wasm-js-module.js'));
  else {
    if (typeof this['./wasm-js-module.js'] === 'undefined') {
      throw new Error("Error loading module 'pkg.native.wasm.library'....");
    }root['pkg'] = factory(typeof this['pkg'] === 'undefined' ? {} : this['pkg'], this['./wasm-js-module.js']);
  }
}(this, function (_, $module$__wasm_js_module_js) {
  var fast_simd_calculus = $module$__wasm_js_module_js._some_simd_function;
b
Copy code
external fun require(module: String): dynamic

suspend fun main() {
  val myWasmModule = require ("wasm.module.js").unsafeCast<Promise<dynamic>>().await()
}
p
Now imagine you have a low-level C API exposed to Kotlin Multiplatform. Would you really introduce everywhere for the sake of symbols a supsend?
b
Not sure what you're asking here
You can get rid of suspend and handle promise with callbacks if that's your issue
p
Copy code
@file:JsModule("./wasm-js-module.js")
@file:JsNonModule
@EagerResolve <-- allow to eagerly resolve it
package pkg.native.wasm.library
//typealias WasmPointer = Int
@JsName("_some_simd_function")
external fun fast_simd_calculus(input: WasmPointer, output: WasmPointer, size: Int)
b
Are you trying to suggest stdlib improvement or asking how to solve your immediate problem? My comment was for the latter
p
The issue is the breaking usage for a Kotlin multiplatform project. For a pure Kotlin/JS solution I would totally agree with you. But when working in a multiplatform project, symbols should be resolved before invoking Kotlin/JS (if required).
I think it is two fold. One is the immediate solution for others using Kotlin/JS. There I agree with your idea of utilizing promises. For Kotlin Multiplatform there needs to be a better solution to avoid leaking control flow logic into the common code
b
Well platforms will always have their differences that you'll need to handle