GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - literalpie/JavaScriptKit: Swift framework to interact with JavaScript through WebAssembly.

BranchesTags
Open more actions menu
 
 

Latest commit

 

History

234 Commits
234 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
Example
Example
 
 
IntegrationTests
IntegrationTests
 
 
Runtime
Runtime
 
 
Sources
Sources
 
 
ci/perf-tester
ci/perf-tester
 
 
scripts
scripts
 
 
.gitignore
.gitignore
 
 
.prettierignore
.prettierignore
 
 
.prettierrc
.prettierrc
 
 
.swift-version
.swift-version
 
 
CHANGELOG.md
CHANGELOG.md
 
 
LICENSE
LICENSE
 
 
Makefile
Makefile
 
 
Package.swift
Package.swift
 
 
README.md
README.md
 
 
package-lock.json
package-lock.json
 
 
package.json
package.json
 
 

Repository files navigation

JavaScriptKit

Run unit tests

Swift framework to interact with JavaScript through WebAssembly.

Getting started

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 document = JSObject.global.document

var divElement = document.createElement("div")
divElement.innerText = "Hello, world"
_ = document.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)

JSObject.global.alert!("Swift is running in the browser!")

Usage in a browser application

The easiest way to get started with JavaScriptKit in your browser app is with the carton bundler.

As a part of these steps you'll install carton via Homebrew on macOS (unfortunately you'll have to build it manually on Linux). Assuming you already have Homebrew installed, you can create a new app that uses JavaScriptKit by following these steps:

  1. Install carton:
brew install swiftwasm/tap/carton

If you had carton installed before this, make sure you have version 0.6.1 or greater:

carton --version
  1. Create a directory for your project and make it current:
mkdir SwiftWasmApp && cd SwiftWasmApp
  1. Initialize the project from a template with carton:
carton init --template basic
  1. Build the project and start the development server, carton dev can be kept running during development:
carton dev
  1. Open http://127.0.0.1:8080/ in your browser and a developer console within it. You'll see Hello, world! output in the console. You can edit the app source code in your favorite editor and save it, carton will immediately rebuild the app and reload all browser tabs that have the app open.

You can also build your project with webpack.js and a manually installed SwiftWasm toolchain. Please see the following sections and the Example directory for more information in this more advanced use case.

Manual toolchain installation

This library only supports swiftwasm/swift distribution toolchain. The toolchain can be installed via swiftenv, in the same way as the official Swift nightly toolchain.

You have to install the toolchain manually when working on the source code JavaScriptKit itself, especially if you change anything in the JavaScript runtime parts. This is because the runtime is embedded in carton and currently can't be replaced dynamically with the JavaScript code you've updated locally.

Just pass a toolchain archive URL for the latest SwiftWasm 5.3 snapshot appropriate for your platform:

$ swiftenv install https://github.com/swiftwasm/swift/releases/download/swift-wasm-5.3-SNAPSHOT-2020-10-20-a/swift-wasm-5.3-SNAPSHOT-2020-10-20-a-macos_x86_64.pkg

You can also use the install-toolchain.sh helper script that uses a hardcoded toolchain snapshot:

$ ./scripts/install-toolchain.sh
$ swift --version
Swift version 5.3 (swiftlang-5.3.0)
Target: x86_64-apple-darwin19.6.0

About

Swift framework to interact with JavaScript through WebAssembly.

swiftwasm.github.io/JavaScriptKit/

Resources

Readme
MIT license
Activity

Stars

0 stars

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.