Reginald Louis
04/03/2024, 2:07 AM@OptIn(ExperimentalJsExport::class)
@JsExport
fun addOne(x: Int): Int = x + 1
Every other aspect of JS Interop work like Kotlin functions with JavaScript code like this one work:
fun setLocalSettings(value: String): Unit = js(
"""{
localStorage.setItem('settings', value);
}"""
In the doc (https://kotlinlang.org/docs/wasm-js-interop.html#kotlin-functions-with-javascript-code) it's written:
> Kotlin/Wasm functions marked with the @JsExport
annotation are visible as properties on a default
export of the generated .mjs
module. You can then use this function in JavaScript:
> // JavaScript
>
> import exports from "./module.mjs"
>
> exports.addOne(10)
But I don't see this file, well I'm confuse 😛William Walker
04/03/2024, 2:13 AMReginald Louis
04/03/2024, 3:10 AMwasmJsBroswerRun
Reginald Louis
04/03/2024, 3:13 AMexport async function instantiate(imports={}, runInitializer=true)
it end like this:
wasmExports = wasmInstance.exports;
if (runInitializer) {
wasmExports._initialize();
}
return { instance: wasmInstance, exports: wasmExports };
So I can see that my function are i the wasmExports. By doing some investigation, if I put a breakpoint anywhere in a my *.wasmJs.kt
file, I see an intance.exports
in the scope in chrome in where the exported functions are.Reginald Louis
04/03/2024, 3:14 AMinstance.exports.addOne(5)
it work. It is how I should access my exported function in javascript?William Walker
04/03/2024, 3:14 AMReginald Louis
04/03/2024, 3:23 AMaddOne
function in side a js() like this
fun anotherFun() = js(
"""
addOne(5)
"""
)
Reginald Louis
04/03/2024, 3:24 AMfun anotherFun() = js(
"""
wasmExports.addOne(5)
"""
)
Reginald Louis
04/03/2024, 3:24 AM@OptIn(ExperimentalJsExport::class)
@JsExport
fun addOne(x: Int): Int = x + 1
fun callingAddOne() = js(
"""
wasmExports.addOne(5)
"""
)