Oleg Yukhnevich
01/13/2023, 9:05 AMSvyatoslav Kuzmich [JB]
01/13/2023, 9:27 AMOleg Yukhnevich
01/13/2023, 9:35 AMkotlin.js.Promise
is available only in 1.8.20-dev builds 🙂
Thx!js(code)
function, but on K/WASM we only have JsFun
annotation, right?
And so equivalent for JS:
js("eval('require')('node:crypto').webcrypto").unsafeCast<WebCrypto>
in WASM will be:
@JsFun("""() => eval('require')('node:crypto').webcrypto""")
private external fun getWebCrypto(): WebCrypto
(where WebCrypto is external interface)
Correct?Svyatoslav Kuzmich [JB]
01/13/2023, 9:42 AMeval
looks unnecessary.It’s a pure library class. You can copy it to use with earlier builds, if needed.is available only in 1.8.20-dev buildskotlin.js.Promise
Oleg Yukhnevich
01/13/2023, 9:52 AMeval
is needed to overcome this warning/error when running in browser:
Module build failed: UnhandledSchemeError: Reading from "node:crypto" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
related issue: https://youtrack.jetbrains.com/issue/KT-46082
But not sure, if it’s needed for K/WASMIt’s a pure library classYeah, I was trying to experiment with 1.8.0, but haven’t found any samples with Promise.
bashor
01/13/2023, 12:12 PMrequire(/* webpackIgnore: true */'node:crypto')
(require)('node:crypto')
• or var t = require; t('node:crypto')
Oleg Yukhnevich
01/13/2023, 12:17 PMbashor
01/13/2023, 12:59 PMOleg Yukhnevich
01/13/2023, 1:14 PMeval
works without warningWebCrypto.getRandomValues(array)
where array is Int8Array
in JS.
With K/JS we can path ByteArray
there, as on K/JS Int8Array == ByteArray
With K/WASM it’s not like this and ByteArray
has underlying storage hodlin wasm array.
Is it possible to path kotlin.ByteArray
in K/WASM via jsinterop where js.Int8Array
is expected?
For now I have something like this and copy per byte (not efficient at all):
private sealed external interface WasmByteArray
@JsFun("(size) => new Int8Array(size)")
private external fun newArray(size: Int): WasmByteArray
@JsFun("(array) => array.length")
private external fun arraySize(array: WasmByteArray): Int
@JsFun("(array, index) => array[index]")
private external fun arrayGet(array: WasmByteArray, index: Int): Byte
@JsFun("(array, index, value) => array[index] = value")
private external fun arraySet(array: WasmByteArray, index: Int, value: Byte): Byte
private sealed external interface WebCrypto {
fun getRandomValues(array: WasmByteArray): WasmByteArray
}
private fun WasmByteArray.copyTo(destination: ByteArray): ByteArray {
require(destination.size == arraySize(this))
repeat(destination.size) { index ->
destination[index] = arrayGet(this, index)
}
return destination
}
fun nextBytes(array: ByteArray): ByteArray {
return crypto.getRandomValues(newArray(array.size)).copyTo(array)
}
Svyatoslav Kuzmich [JB]
01/13/2023, 1:28 PMbashor
01/13/2023, 1:30 PMBTW, I have tried all those 3 variant, and all of them give the same warning…
Only variant withThanks for trying it out. It’s unexpected thatworks without warningeval
/* webpackIgnore: true */
doesn’t work for require
, since we are using it for dynamic import
. 🤷♂️Oleg Yukhnevich
01/13/2023, 1:43 PMwebpackIgnore
for require
was introduced only into webpack v5 and was not backported to v4 (as for what I’ve found)
And kotlin still uses webpack v4 🙂
webpackIgnore
for import
works for both v4 and v5Currently Wasm arrays cannot interop with JS arrays. Copy is probably the best thing one can do.OK, thx! will continue with copying…
Svyatoslav Kuzmich [JB]
01/13/2023, 1:57 PMarray[index]
JS API without copy or Proxy
• Maybe use primitive arrays as IntXArray without any adaptationOleg Yukhnevich
01/13/2023, 2:02 PMbashor
01/13/2023, 9:12 PMAnd kotlin still uses webpack v4 🙂I think we moved to v5 by default a long time ago (cc @Ilya Goncharov [JB])
hfhbd
01/13/2023, 9:24 PMkotlin.js.webpack.major.version=4
@Oleg Yukhnevich Maybe you still have this property stored in your gradle.properties.Oleg Yukhnevich
01/13/2023, 9:31 PMwebpackIgnore
inside require
is off by default - https://webpack.js.org/configuration/module/#moduleparserjavascriptcommonjsmagiccomments
https://github.com/webpack/webpack/issues/15975#issuecomment-1166609374Maybe you still have this property stored in your gradle.properties.No 🙂 Didn’t know even about this property.
bashor
01/16/2023, 8:32 PMbut it’s a mess to make it working…what exactly is issue for you?
Is there any timeline for having some experimental wasm coroutines, may be with kotlin 1.8.20 ?maybe, but not as part of official coroutines releases
Svyatoslav Kuzmich [JB]
01/16/2023, 8:35 PMbashor
01/16/2023, 8:36 PMOleg Yukhnevich
01/16/2023, 9:47 PMwhat exactly is issue for you?as mentioned upper, some artifacts were not available, and while I tried to ignore them and use latest kotlin dev (like 5***), there were errors with compilation regarding some WasmRef not available using kotlin 1.8.0 for one module, but 1.8.20 for other also could cause issues in my case…
maybe, but not as part of official coroutines releasesthat’s ok for me, if they will be based on non-expirable kotlin builds, at least with latest kotlin dev version (in my case, it’s needed only for running suspend tests) P.S. Im working on MPP cryptography library, so supporting K/WASM out of the box (at least via JS interop with WebCrypto), when it will be alpha/beta/stable sounds just interesting for me
Svyatoslav Kuzmich [JB]
01/17/2023, 7:25 AMOleg Yukhnevich
01/17/2023, 8:04 AMSvyatoslav Kuzmich [JB]
01/17/2023, 8:31 AMOleg Yukhnevich
01/17/2023, 8:33 AM