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 - devxsby/JavaScriptKit: Swift framework to interact with JavaScript through WebAssembly.

BranchesTags
Open more actions menu
 
 

Latest commit

 

History

1,426 Commits
1,426 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
Benchmarks
Benchmarks
 
 
Examples
Examples
 
 
Plugins
Plugins
 
 
Runtime
Runtime
 
 
Sources
Sources
 
 
Tests
Tests
 
 
Utilities
Utilities
 
 
.editorconfig
.editorconfig
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
.prettierignore
.prettierignore
 
 
.prettierrc
.prettierrc
 
 
.spi.yml
.spi.yml
 
 
.swift-format
.swift-format
 
 
CHANGELOG.md
CHANGELOG.md
 
 
CONTRIBUTING.md
CONTRIBUTING.md
 
 
LICENSE
LICENSE
 
 
Makefile
Makefile
 
 
Package.swift
Package.swift
 
 
[email protected]
[email protected]
 
 
README.md
README.md
 
 
package-lock.json
package-lock.json
 
 
package.json
package.json
 
 
tsconfig.json
tsconfig.json
 
 

Repository files navigation

JavaScriptKit

Run unit tests Documentation

Swift framework to interact with JavaScript through WebAssembly.

Quick Start

Check out the Hello World tutorial for a step-by-step guide to getting started.

Overview

JavaScriptKit provides a seamless way to interact with JavaScript from Swift code when compiled to WebAssembly. It allows Swift developers to:

  • Access JavaScript objects and functions
  • Create closures that can be called from JavaScript
  • Convert between Swift and JavaScript data types
  • Use JavaScript promises with Swift's async/await
  • Work with multi-threading
import JavaScriptKit

// Access global JavaScript objects
let document = JSObject.global.document

// Create and manipulate DOM elements
var div = document.createElement("div")
div.innerText = "Hello from Swift!"
_ = document.body.appendChild(div)

// Handle events with Swift closures
var button = document.createElement("button")
button.innerText = "Click me"
button.onclick = .object(JSClosure { _ in
    JSObject.global.alert!("Button clicked!")
    return .undefined
})
_ = document.body.appendChild(button)

Learn more: JavaScript Interop Cheat Sheet

BridgeJS Plugin

Note: BridgeJS is experimental. APIs may change in future releases.

BridgeJS provides easy interoperability between Swift and JavaScript/TypeScript. It enables:

  • Export Swift to JavaScript - Expose Swift functions, classes, and types to JavaScript; the plugin also generates TypeScript definitions (.d.ts) for the exported API.
  • Import JavaScript into Swift - Make JavaScript/TypeScript APIs (functions, classes, globals like document or console) callable from Swift with type-safe bindings. You can declare bindings with macros in Swift or generate them from a TypeScript declaration file (bridge-js.d.ts).

For architecture details, see the BridgeJS Plugin README. For package and build setup, see Setting up BridgeJS in the documentation.

Exporting Swift to JavaScript

Mark Swift code with @JS to make it callable from JavaScript:

import JavaScriptKit

@JS class Greeter {
    @JS var name: String

    @JS init(name: String) {
        self.name = name
    }

    @JS func greet() -> String {
        return "Hello, \(name)!"
    }
}

JavaScript usage:

const greeter = new exports.Greeter("World");
console.log(greeter.greet()); // "Hello, World!"

Learn more: Exporting Swift to JavaScript

Importing JavaScript into Swift

Declare bindings in Swift with macros such as @JSFunction, @JSClass, @JSGetter, and @JSSetter:

import JavaScriptKit

@JSClass struct Document {
    @JSFunction func getElementById(_ id: String) throws(JSException) -> HTMLElement
    @JSFunction func createElement(_ tagName: String) throws(JSException) -> HTMLElement
}
@JSGetter(from: .global) var document: Document

@JSClass struct HTMLElement {
    @JSGetter var innerText: String
    @JSSetter func setInnerText(_ newValue: String) throws(JSException)
    @JSFunction func appendChild(_ child: HTMLElement) throws(JSException)
}

@JS func run() throws(JSException) {
    let button = try document.createElement("button")
    try button.setInnerText("Click Me")
    let container = try document.getElementById("app")
    try container.appendChild(button)
}

You can also generate the same macro-annotated Swift from a TypeScript file (bridge-js.d.ts). See Generating bindings from TypeScript in the documentation.

Learn more: Importing JavaScript into Swift

Try It Online

Use the BridgeJS Playground to preview what interfaces will be exposed on the Swift/TypeScript sides.

Examples

Check out the examples for more detailed usage patterns.

Contributing

Contributions are welcome! See CONTRIBUTING.md for details on how to contribute to the project.

Sponsoring

Become a gold or platinum sponsor and contact maintainers to add your logo on our README on Github with a link to your site.

About

Swift framework to interact with JavaScript through WebAssembly.

swiftpackageindex.com/swiftwasm/JavaScriptKit/documentation/javascriptkit

Resources

Readme
License

Contributing

Contributing
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.