Also is there a way to use function references and...
# webassembly
a
Also is there a way to use function references and
externref
in Kotlin?
b
I’m wondering why do you need that?
Any external class or interface are
externref
on a module border
For simplicity you can use
JsAny
if you ask about
wasm-wasi
target, it’s impossible now
a
Thanks, yes, I'm asking about the wasm/wasi target. There are several cases when this would have been useful. For example, I'm experimenting with a system that consists of several Wasm modules and all interaction between them has to be orchestrated through the host manually (until the component model is more widely supported). One issue with that is memory consumption. For instance when a large string is "piped" through several modules for processing, a lot of copies have to be made - at the very least there is a copy one it is passed from the host to the guest through the linear memory, then a copy to deserialize it into a byte array, then a copy to decode that into a string, then a similar process to return the updated result to a host. Since linear memory size can't be reduced, in one iteration all modules linear memories grow substantially and total memory consumption becomes unacceptable. I'm considering a number of options for dealing with - streaming data through a buffer (doesn't solve the issue in a case when the whole string has to be constructed in the guest memory eventually), using preopened files (sandboxing guarantees for this are weak for a number of runtimes at the moment). One other option is not to pass the data at all and instead pass
externref
and define host functions for working with them so that the data never leaves the host while the guest can operate on it anyway. This wouldn't work for all cases but still can be useful in some. I guess externref can be emulated but it would have been more convenient if they were available directly.
By the way, I would like to ask a sort of related memory consumption question while we're discussing this. When decoding a byte array into a string (encodeUtf8), there is an intermediate copy created and then often discarded in https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/js/src/kotlin/text/utf8Encoding.kt#L203. With large strings the memory impact becomes noticeable. But quick benchmarks show that having a preliminary pass through the string to determine the exact size of the byte array required first and then do not discard it seems to perform slightly better but memory consumption can be several times smaller. I wonder, what are the considerations for implementing it with an intermediate copy? Is it because in some cases preliminary pass is expensive?
b
^^ @ilya.gorbunov @Filipp Zhinkin any comments on this?
loading 1
Re:
externref
at wasm-wasi target please file an issue
a