cashapp / zipline

Run Kotlin/JS libraries in Kotlin/JVM and Kotlin/Native programs
Apache License 2.0
2.03k stars 153 forks source link

Define globalThis.console, globalThis.setTimeout etc. before user calls Zipline.get() #729

Open swankjesse opened 1 year ago

swankjesse commented 1 year ago

Right now calling Zipline.get() inside JS has a side-effect of wiring some of the global things that support coroutines and logging.

If the user calls println() before they call Zipline.get(), they get a surprising crash:

    app.cash.zipline.QuickJsException: 'console' is not defined
        at JavaScript.<anonymous>(../../../../../jsMainSources/libraries/stdlib/js/src/kotlin/console.kt:78)
        at JavaScript.<anonymous>(../../../../../jsMainSources/libraries/stdlib/js/src/kotlin/console.kt:71)
        at JavaScript.<anonymous>(../../../../../jsMainSources/libraries/stdlib/js/src/kotlin/console.kt:10)
        at JavaScript.<anonymous>(../../../../../jsMainSources/libraries/stdlib/js/src/kotlin/console.kt:15)
        at JavaScript.println(../../../../../jsMainSources/libraries/stdlib/js/src/kotlin/console.kt:100)
        at JavaScript.prepareTreehouse(../../../../../launch.kt:70)
        at JavaScript.<eval>(ZiplineLoader.kt)

We could implement this by making the engine-side call an init function for these things rather than doing it as a side-effect of getting the Zipline instance.

swankjesse commented 1 year ago

Looked into this with @adrw. It turns out to be difficult 'cause we still want the user serializers module to bring up Zipline.