karakum-team / karakum

Converter of TypeScript declaration files to Kotlin declarations
Apache License 2.0
42 stars 2 forks source link

add wasmJs support #12

Closed Gaubee closed 8 months ago

Gaubee commented 9 months ago

Are there any plans for jsWasm support?

sgrishchenko commented 9 months ago

Right now this tool supports only .d.ts -> .kt conversion, but I am open to discussion. What kind of conversion do you need? To be honest, I don't know what is a format for describing types in WASM, which language is used for it and how to adapt them to Kotlin.

Gaubee commented 9 months ago

I have tried to do these type transformations on my own, and after doing some experimenting, and I determined that it is not an easy job, kotlinWasm has more limitations than kotlinJs. And there are also many differences between kotlin and typescript type system, so I gave up in time and continue to use kotlinJs for now.

https://github.com/kowasm/kowasm/blob/main/kotlinx-nodejs-wasm/src/wasmJsMain/kotlin/org/nodejs/http/HttpModule.kt

https://github.com/Gaubee/node-types-to-kotlin-wasmJs/tree/main/org/node

for example,this is node:async_hooks:

@file:JsModule("node:async_hooks")

package org.node.async_hooks

external fun executionAsyncId(): Int

external fun executionAsyncResource(): JsAny /* object */

external fun triggerAsyncId(): Int

external fun createHook(callbacks: HookCallbacks): AsyncHook

open external class AsyncResource(
    type: String,
    triggerAsyncId: JsAny /* number | AsyncResourceOptions */
) {

  companion object {
    fun bind(fn: JsAny /* Func */, type: String, thisArg: JsAny /* ThisArg */): JsAny /* Func */
  }

  fun bind(fn: JsAny /* Func */): JsAny /* Func */

  fun runInAsyncScope(
      fn: JsAny /* (this: This, ...args: any[]) => Result */,
      thisArg: JsAny /* This */,
      vararg args: JsAny
  ): JsAny /* Result */

  fun emitDestroy(): AsyncResource /* this */

  fun asyncId(): Int

  fun triggerAsyncId(): Int
}

open external class AsyncLocalStorage() {

  companion object {
    fun bind(fn: JsAny /* Func */): JsAny /* Func */

    fun snapshot():
        JsAny /* <R, TArgs extends any[]>(fn: (...args: TArgs) => R, ...args: TArgs) => R */
  }

  fun disable(): Unit /* void */

  fun getStore(): JsAny? /* T */

  fun run(store: JsAny /* T */, callback: JsAny /* () => R */): JsAny /* R */

  fun run(
      store: JsAny /* T */,
      callback: JsAny /* (...args: TArgs) => R */,
      vararg args: JsAny
  ): JsAny /* R */

  fun exit(callback: JsAny /* (...args: TArgs) => R */, vararg args: JsAny): JsAny /* R */

  fun enterWith(store: JsAny /* T */): Unit /* void */
}

external interface HookCallbacks {
  val init:
      ((
          asyncId: Int,
          type: String,
          triggerAsyncId: Int,
          resource: JsAny /* object */) -> Unit /* void */)?

  val before: ((asyncId: Int) -> Unit /* void */)?

  val after: ((asyncId: Int) -> Unit /* void */)?

  val promiseResolve: ((asyncId: Int) -> Unit /* void */)?

  val destroy: ((asyncId: Int) -> Unit /* void */)?
}

external interface AsyncHook {
  fun enable(): AsyncHook /* this */

  fun disable(): AsyncHook /* this */
}

external interface AsyncResourceOptions {
  var triggerAsyncId: Int

  var requireManualDestroy: Boolean
}

wasmJs only supports a few type conversions, and in many cases we have to use JsAny instead.

lppedd commented 8 months ago

It seems to me that auto-generation of externals for wasmJs is still too premature.
We need to wait for the K/WASM team to relax some of the limitations.

Gaubee commented 8 months ago

We will discuss the possibility of this issues until a better time