https://kotlinlang.org logo
#javascript
Title
# javascript
s

Stefan Oltmann

03/15/2024, 2:15 PM
I want to add JavaScript support to Ashampoo Kim, but I have no experience in kotlin-js so far. The lib already has wasmJS support. Can someone tell me how I need to translate/change this to work for regular JavaScript? https://github.com/Ashampoo/kim/blob/main/src/wasmMain/kotlin/com/ashampoo/kim/common/ZLib.wasm.kt
1
a

Artem Kobzar

03/15/2024, 2:43 PM
Seems like you can just copy this file to js source set and it should work
s

Stefan Oltmann

03/15/2024, 2:43 PM
I tried that first, but it won't work. 🤷‍♂️
JsAny for example is unknown.
a

Arjan van Wieringen

03/15/2024, 2:45 PM
JsAny can be renamed to dynamic I think
But “it won’t work” is a little vague ;)
s

Stefan Oltmann

03/15/2024, 2:46 PM
Yes, it does not compile. I can try "dynamic"
Okay, no I got
When accessing module declarations from UMD, they must be marked by both @JsModule and @JsNonModule
on the
Pako.deflate(input)
call
I try adding
@JsNonModule
, but I don't understand what I'm doing to be honest. 😅
e

Edoardo Luppi

03/15/2024, 2:53 PM
I'll clone it and give it a look, have 30 minutes of free time.
s

Stefan Oltmann

03/15/2024, 2:53 PM
It starts to compile now, so maybe it works
I think I will set up a small kotlin/js project in the next days to test if it actually works
It compiled. Converting "JsAny" to "dynamic" and adding @JsNonModule (whatever that means) does the trick. Thank you all.
👍 1
e

Edoardo Luppi

03/15/2024, 2:58 PM
Ok! There is small difference you need to consider between
wasmJs
and
js
Copy code
private val toStringOptions: JsAny = js("({to: 'string'})")
wasmJs
requires
JsAny
,
js
doesn't, so you can just return
dynamic
or
Any
🙏 1
Ah yeah you already found out ahaha
s

Stefan Oltmann

03/15/2024, 2:58 PM
Thank you 🙂
I will try to remember that for the future. So they can't have the same sourceset
e

Edoardo Luppi

03/15/2024, 3:01 PM
On the JS side you can also optimize the byte array conversions:
Copy code
public actual inline fun Uint8Array.toByteArray(): ByteArray =
  Int8Array(buffer, byteOffset, length).unsafeCast<ByteArray>()
And
Copy code
public actual inline fun ByteArray.toUint8Array(): Uint8Array {
  val i8a = unsafeCast<Int8Array>()
  return Uint8Array(i8a.buffer, i8a.byteOffset, i8a.length)
}
👍 1
s

Stefan Oltmann

03/15/2024, 3:03 PM
Thanks a lot 🙂
3 Views