Is there any information on calling JS from Kotlin...
# kotlin-native
f
Is there any information on calling JS from Kotlin when targeting WASM?
👍 2
j
@ftomassetti Have you seen the WASM documentation in Kotlin/Native? https://github.com/JetBrains/kotlin-native/blob/master/samples/html5Canvas
o
generally JS interop is experimental now, see https://github.com/JetBrains/kotlin-native/blob/master/Interop/JsRuntime/src/main/kotlin/jsinterop.kt for technical details of JS interop functionality
f
@jo yes, I have seen the documentation and the example you posted but I could only see 2 libraries imported using jsinterop, not how to use functions defined as external.
@olonho yes, I saw that file but it is not clear to me how to include JS functions. I know I can define them as external but then the application crashes at runtime. So far I only could mess up with the generated .wasm.js file to add into the “libraries” array entries for my JS functions that I defined as external but 1) I do not think this is the intended way to do that 2) It is a mess to convert the types
o
Practically speaking, now jsinterop is mostly an internal tool. So there’s no intended way to use JS interop with external JS code
f
Ok, then I guess I am stuck to my trick: I load the script without specifying the wasm attribute, so it fails after having setup a few data structures. At that point I add to the libraries array a new element with my mappings and when I launch WebAssembly directly. It works but it is just… awful 😄
o
actually for us it is very interesting to know how do you plan to use K/N WASM in applications? We need this information to properly prioritize K/N WASM target (currently we assume that most web users would use Kotlin/JS, so WASM is not the top priority target)
f
In this case I need to create some examples for a presentation to a local meetup. In practice I plan to write applications using several languages (e.g., Rust & Kotlin) compiling all of them to WASM. I understand this thing is not production ready but JS interoperability is really needed for even the most basic example
Oh, and I plan to publish an article on SuperKotlin.com, too 🙂
…and I just did https://superkotlin.com/kotlin-and-webassembly/ Thank you for your help!
👍 1