JetBrains / kotlin-wrappers

Kotlin wrappers for popular JavaScript libraries
Apache License 2.0
1.33k stars 165 forks source link

Add wasmJs target for `kotlin-node` wrappers #2170

Open vdshb opened 8 months ago

vdshb commented 8 months ago

wasmJs KMP target became available in NodeJs. This is a proposal to make kotlin-node wrappers available on this new target.

turansky commented 5 months ago

What is your use case? Which types you need in wasmJs?

vdshb commented 5 months ago

@turansky I'm creating a KMP class which starts child process in different targets. It supports kotlin/js and uses

node.childProcess.ChildProcess
node.events.Event

from kotlin-wrappers.

lppedd commented 5 months ago

I'm also looking for WASM interop with Node.js modules. An example of what I'd like to replace with proper types is:

@Suppress("UNUSED_PARAMETER")
private fun readFileSync(name: String): String =
  js(
    """
    {
      const fs = require('fs');
      return fs.readFileSync(name, {
        encoding: 'utf8'
      });
    }
    """
  )

public actual fun readBenchmarkFile(name: String): String =
  readFileSync("$basePath$name")
turansky commented 5 months ago

Why readFileSync isn't declared as external?

lppedd commented 5 months ago

@turansky because I'm waiting for kotlin-wrappers to offer external types 😆

turansky commented 5 months ago

I personally use following scripts

Script "I don't need fine externals"
  1. Don't test external on own use cases
    • js calls are fine/
    • No knowledge sharing with community
Script "I need fine externals"
  1. Write fine externals for yourself
  2. Test externals in custom projects
  3. Share detected problems in issue about externals
  4. Share externals, which I wrote, with community ...
lppedd commented 5 months ago

I think the main point here is kotlin-wrappers already have those externals, so why rewrite them?

We should find a way to share them between the js and wasmJs source sets.

turansky commented 5 months ago

already have those externals, so why rewrite them?

Because they aren't compatible with wasmJs as I know

We should find a way to share them between the js and wasmJs source sets.

If you need all Node.js declarations probably Kotlin/JS is what you need? :)

lppedd commented 5 months ago

If you need all Node.js declarations probably Kotlin/JS is what you need? :)

I don't need all of them, but I guess being WASM is interoperable with JS, people will adopt it by moving incrementally, only some bits, while still calling JS-first APIs.

Because they aren't compatible with wasmJs as I know

That's true. Looks like on WASM, external JS types must derive from JsAny.
So I think the best approach would be to wait for the K/WASM team to relax the externals requirements.
Maybe you can let them know about this?