ilyagulya
05/10/2024, 2:26 PM@JsFun
?
For instance, consider this code:
@JsFun(
"""
(value) => kotlinSquare(value)
"""
)
external fun square(value: Int): Int
@JsExport
fun kotlinSquare(value: Int): Int {
return value * value
}
What is the correct way to call kotlinSquare
?
I’m running wasmJs
on nodejs runtime.Artem Kobzar
05/10/2024, 3:10 PMilyagulya
05/10/2024, 4:09 PM@JsFun(
"""
(array) => ({
length: kotlinArrayLength(array),
[Symbol.iterator]: function* () {
let i = 0;
while (i < kotlinArrayLength(array)) {
yield kotlinArrayGet(i);
i++;
}
},
get: (index) => kotlinArrayGet(array, index),
set: (index, value) => kotlinArraySet(array, index, value)
})
"""
)
external fun wrapKotlinArrayWithJsMethods(array: JsReference<IntArray>): JsArray<JsNumber>
@JsExport
fun kotlinArrayLength(ref: JsReference<IntArray>): Int {
return ref.get().size
}
@JsExport
fun kotlinArrayGet(ref: JsReference<IntArray>, index: Int): Int {
return ref.get()[index]
}
@JsExport
fun kotlinArraySet(ref: JsReference<IntArray>, index: Int, value: Int) {
ref.get()[index] = value
}
But as you can see, I need to call kotlin exported functions to make this work.Artem Kobzar
05/10/2024, 4:16 PM@JsFun(
"""
(array) => ({
length: kotlinArrayLength(array),
[Symbol.iterator]: function* () {
let i = 0;
while (i < kotlinArrayLength(array)) {
yield wasmExports["kotlinArrayGet"](i);
i++;
}
},
get: (index) => kotlinArrayGet(array, index),
set: (index, value) => kotlinArraySet(array, index, value)
})
"""
)
external fun wrapKotlinArrayWithJsMethods(array: JsReference<IntArray>): JsArray<JsNumber>
Artem Kobzar
05/10/2024, 4:18 PMilyagulya
05/10/2024, 4:28 PMArtem Kobzar
05/10/2024, 4:28 PMilyagulya
05/10/2024, 4:30 PM