Hello there, I have a Kotlin jvm+wasm project. No...
# multiplatform
r
Hello there, I have a Kotlin jvm+wasm project. Now I'm diving a little more in the wasm side, and this little thing doesn't work. The function doesn't get exported:
Copy code
@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:
Copy code
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: >
Copy code
// JavaScript
> 
> import exports from "./module.mjs"
> 
> exports.addOne(10)
But I don't see this file, well I'm confuse 😛
w
Are you running the gradle build tasks to generate that artifact you’re looking for?
👀 1
r
Yes, I'm running the
wasmJsBroswerRun
👍 1
By reading the code, I stumble on
export async function instantiate(imports={}, runInitializer=true)
it end like this:
Copy code
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.
If I wrote in the console
instance.exports.addOne(5)
it work. It is how I should access my exported function in javascript?
w
You need to run the Gradle build task to build that minified js file
r
Ok I found the solution. I should add that what I was tring to call the exported
addOne
function in side a js() like this
Copy code
fun anotherFun() = js(
"""
addOne(5)
"""
)
It work if I do this:
Copy code
fun anotherFun() = js(
"""
wasmExports.addOne(5)
"""
)
So to resume, this work:
Copy code
@OptIn(ExperimentalJsExport::class)
@JsExport
fun addOne(x: Int): Int = x + 1

fun callingAddOne() = js(
"""
wasmExports.addOne(5)
"""
)