https://kotlinlang.org logo
#webassembly
Title
# webassembly
m

martmists

01/26/2023, 1:19 PM
How do I expose functions from wasm to js targets?
b

bashor

01/26/2023, 2:05 PM
Annotate it by
@JsExport
m

martmists

01/26/2023, 2:19 PM
What package is that annotation in? (intellij doesn't seem to recognize the wasm target)
And does that mean I need to set my jsMain sourceset to depend on my wasmMain sourceset, or does that happen automatically?
b

bashor

01/26/2023, 2:29 PM
it’s located in
kotlin.js.JsExport
, it’s part of Kotlin/Wasm stdlib, NO other dependencies is needed.
m

martmists

01/26/2023, 2:34 PM
Hmm, trying to depend on the wasm source from the js source does seem to finally load it as source set, but it also seems to not find the kotlin/native types like CPointer now; What is the solution here?
In fact, it seems the wasm target can't find any of the
kotlinx.cinterop
types
b

bashor

01/26/2023, 3:01 PM
Kotlin/Wasm is the new compiler backend, there is no cinterop, at least for now.
Could you please share more about your case?
m

martmists

01/26/2023, 3:04 PM
My use case is to read/modify in-memory webm/m4a files by statically linking against FFmpeg, and accessing it from commonized code with wrappers in JNI (desktop/android) and WASM (web)
b

bashor

01/26/2023, 3:09 PM
I guess this recent changes could help you https://youtrack.jetbrains.com/issue/KT-55589/Basic-support-of-WASI#focus=Comments-27-6806564.0-0 (cc @Svyatoslav Kuzmich [JB])
m

martmists

01/26/2023, 3:20 PM
WASI seems to deal with files though, while I'm handling bytearrays, and I don't see how WASI allows me to bundle and link ffmpeg.
s

Svyatoslav Kuzmich [JB]

01/26/2023, 3:59 PM
I need to set my jsMain sourceset to depend on my wasmMain sourceset
This is not supported. These targets are not compatible on Kotlin level. But they both can interact with JavaScript, and with quite a bit of effort it is possible to connect them this way. You would need to: • Annotate your K/Wasm top-level functions with @JsExport to expose them to JavaScript. At the moment only use numbers, strings and external types are supported as parameters. No byte arrays yet, unfortunately. • Declare corresponding external declarations with @JsModule on K/JS side. Note that K/Wasm currently exports functions as members on
default
object. • Make sure that when you run or bundle you project, module identifier in @JsModule points to
.mjs
file that K/Wasm generates. Alternatively, use other module resolution techniques like
node_modules
Without a decent C interop linking ffmpeg to K/Wasm could be a challenge, as K/Wasm at the moment provides only very basic API for access to native memory. If I wanted to include C library in K/JS project, I would likely to use emscripten to generate JS bindings and then use these bindings as a regular external JS library from K/JS without K/Wasm at all.
59 Views