Is there a way while exporting suspend functions t...
# javascript
s
Is there a way while exporting suspend functions to JS, to automatically convert them to Promise?
b
Nope,you need manual conversions to and from Promises
t
Or compiler plugin
s
and such a compiler plugin doesn't exist, right?
d
Doh, I just hit this roadblock too. Seems like a pretty massive oversight and makes writing a kotlin multiplatform -> kotlin/js library incredibly tedious.
b
If there's actually a demand for it, writing a ksp processor to generate such bindings would not be that hard
e
At this point I'll add myself to the list of the ones wanting auto-generation of bindings. But beware I'm not talking of additional "static" functions, the suspending function should just wrap the coroutine internally
Example:
Copy code
suspend fun example(...): Int { ... }
should become:
Copy code
fun example(...): Promise<Int> =
   GlobalScope.promise { ... }
The same could also be valid for the JVM target, just using
CompletableFuture<T>
b
Main difference is that it's unsafe to use GlobalScope on jvm due to multithreading
e
That's true, maybe there is a neat way to infer the coroutine scope we want to use for the transformed method. But anyway, a starting point done in the JS target would already boost productivity and adoption imo
b
I'll play with ksp tonight to see how easy this might be
e
@Big Chungus thanks! I suppose then that KSP is actually able to transform the source or IR? (asking as I'm ignorant on KSP or compiler plugins)
b
No, ksp can only generate new code. For existing code transformation you need compiler plugin.
But for this case, you should not modify the existing code just so js could use it (as it will make it unusable kotlin-side)
I'm thinking some @JsPromiseExport annotation on suspend functions that would generate @JsExport non suspend function wrapping everything in promises
e
So the generated exported function would still be part of the entity containing the original function, or were you thinking of generating a sort of extension functions?
Asking because on the second case, it makes it weird using it on the JS/TS side.
Also just remembered one thing related to what @spierce7 said
while exporting suspend functions to JS
You can't really export suspending functions in JS (
[WRONG_EXPORTED_DECLARATION] Declaration of such kind (suspend function) can't be exported to JS
), so you're forced to create a wrapper anyway
b
Best explained by example... Given your code as
Copy code
@JsPromiseExport
suspend fun doShit(diarrhea: Boolean): Int { ... }
KSP would generate
Copy code
@JsExport
@JsName("doShit")
fun _doShit(diarrhea: Boolean): Promise<Int> = GlobalScope.promise{doShit(diarrhea)}
Then from js you only see the generated version with original name
Copy code
let shitSize = await doShit(true);
✔️ 1
e
@Big Chungus would that be the same if
doShit
is inside a
class
? E.g., to obtain
Copy code
let shitSize = await myInstance.doShit(true);
b
Yep
e
Ok that's basically what I was trying to understand. Thanks!
b
Well not exactly since you cannot modify class with ksp. It will likely be an extension fun so that from js you have
await doShit(instance, true);
e
😞 sad. That's why I was saying it would be weird to use it that way
b
But that's not KSP limitation as even with compiler plugin this would need to assume that entire class is @JsExport annotated which is very unlikely to be the case in most scenarious.
e
I think the "assumption" is delegated to the user, in the sense that if I place a
@JsPromiseExport
in a class that's not `@JsExport`ed, it's my fault I make something crash
b
But I'll see if there are any workarounds for class case once I get to my IDE. I'll keep you posted - sounds like a fun side project
e
Thanks! It's indeed strange no one ever attempted at it. Maybe KotlinJS is not used enough in an interoperable context
d
Hey guys, I just found this plugin which basically does this. No idea if it’s decent though. https://github.com/ForteScarlet/kotlin-suspend-transform-compiler-plugin
👀 1
e
@Dylan Sale pretty cool. Seems like the JS side is stuck on an issue tho
577 Views