◐ Shell
clean mode source ↗

Experimental async/await support by kateinoigakukun · Pull Request #112 · swiftwasm/JavaScriptKit

@kateinoigakukun

import JavaScriptEventLoop
import JavaScriptKit

JavaScriptEventLoop.install()
let fetch = JSObject.global.fetch.function!.async

func printZen() async {
  let result = await try! fetch("https://api.github.com/zen").object!
  let text = await try! result.asyncing.text!()
  print(text)
}

JavaScriptEventLoop.runAsync {
  await printZen()
}

Current Issues

  • Many C++ headers are copied from LLVM and Swift repository even though the runtime uses only a few interfaces.
  • No tests
  • Use of unsafe flag -enable-experimental-concurrency. If we remove it from Package.swift, users have to pass it from command line.

j-f1

@github-actions

Time Change: -843ms (9%) ✅

Total Time: 9,170.5ms

Test name Duration Change
Serialization/Swift Int to JavaScript 2,759.75ms -455.25ms (16%) 👏
Serialization/Swift String to JavaScript 2,966ms -284.75ms (9%)
ℹ️ View Unchanged
Test name Duration Change
Serialization/Write JavaScript number directly 190.75ms +1.25ms (0%)
Serialization/Write JavaScript string directly 198.5ms +4.25ms (2%)
Object heap/Increment and decrement RC 3,055.5ms -108.5ms (3%)

performance-action

@kateinoigakukun kateinoigakukun changed the title [Do not merge] Experimental async/await support Experimental async/await support

Jan 11, 2021

@MaxDesiatov

Do I understand correctly that after this is merged we'll require our users to use dev toolchains and to compile with experimental concurrency enabled?

@MaxDesiatov

Also, could we link with Swift runtime parts that are already included in the toolchain/SDK? Is a copy of it and LLVM stuff in our codebase required for this to work?

@kateinoigakukun

Do I understand correctly that after this is merged we'll require our users to use dev toolchains and to compile with experimental concurrency enabled?

Yes, so I'm planning to merge this to next branch which will be used to use non-stable language features.

Also, could we link with Swift runtime parts that are already included in the toolchain/SDK? Is a copy of it and LLVM stuff in our codebase required for this to work?

JavaScriptEventLoop should be linked with libswiftCore.a and libswift_Concurrency.a to work. And LLVM headers are required for including swift/ABI/Task.h.

@MaxDesiatov

Fair enough. I'm only worried that contents of these headers can change in a breaking way, especially given that concurrency stuff isn't ABI-stable yet, and maybe there's a better way to track them. A submodule is probably not practical due to the size of repositories that have these headers, so I don't have a better alternative to propose... Maybe some kind of scripts that pull latest versions of these headers automatically? 🤔

@kateinoigakukun

MaxDesiatov

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems legit then 👍