Skip to content

Swift framework to interact with JavaScript through WebAssembly.

License

Notifications You must be signed in to change notification settings

GoodNotes/JavaScriptKit

Repository files navigation

JavaScriptKit

Run unit tests

Swift framework to interact with JavaScript through WebAssembly.

Requirements

This library only supports swiftwasm/swift distribution toolchain. The toolchains can be installed via swiftenv like official nightly toolchain. You can use the install-toolchain.sh helper script that does this for you:

$ ./scripts/install-toolchain.sh
$ swift --version
Swift version 5.3-dev (LLVM 09686f232a, Swift 5a196c7f13)
Target: x86_64-apple-darwin19.6.0

Usage

This JavaScript code

const alert = window.alert;
const document = window.document;

const divElement = document.createElement("div");
divElement.innerText = "Hello, world";
const body = document.body;
body.appendChild(divElement);

const pet = {
  age: 3,
  owner: {
    name: "Mike",
  },
};

alert("JavaScript is running on browser!");

Can be written in Swift using JavaScriptKit

import JavaScriptKit

let alert = JSObject.global.alert.function!
let document = JSObject.global.document.object!

let divElement = document.createElement!("div").object!
divElement.innerText = "Hello, world"
let body = document.body.object!
_ = body.appendChild!(divElement)

struct Owner: Codable {
  let name: String
}

struct Pet: Codable {
  let age: Int
  let owner: Owner
}

let jsPet = JSObject.global.pet
let swiftPet: Pet = try JSValueDecoder().decode(from: jsPet)

alert("Swift is running on browser!")

Please see Example directory for more information

About

Swift framework to interact with JavaScript through WebAssembly.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 68.1%
  • JavaScript 11.2%
  • TypeScript 10.5%
  • C 8.4%
  • Makefile 1.1%
  • Shell 0.6%
  • HTML 0.1%