Skip to content

Latest commit

 

History

History
94 lines (73 loc) · 2.73 KB

File metadata and controls

94 lines (73 loc) · 2.73 KB

Exporting Swift Classes to JS

Learn how to export Swift classes to JavaScript.

Overview

Tip: You can quickly preview what interfaces will be exposed on the Swift/TypeScript sides using the BridgeJS Playground.

To export a Swift class, mark both the class and any members you want to expose:

import JavaScriptKit

@JS class ShoppingCart {
    private var items: [(name: String, price: Double, quantity: Int)] = []

    @JS init() {}

    @JS public func addItem(name: String, price: Double, quantity: Int) {
        items.append((name, price, quantity))
    }

    @JS public func removeItem(atIndex index: Int) {
        guard index >= 0 && index < items.count else { return }
        items.remove(at: index)
    }

    @JS public func getTotal() -> Double {
        return items.reduce(0) { $0 + $1.price * Double($1.quantity) }
    }

    @JS public func getItemCount() -> Int {
        return items.count
    }

    // This method won't be accessible from JavaScript (no @JS)
    var debugDescription: String {
        return "Cart with \(items.count) items, total: \(getTotal())"
    }
}

In JavaScript:

import { init } from "./.build/plugins/PackageToJS/outputs/Package/index.js";
const { exports } = await init({});

const cart = new exports.ShoppingCart();
cart.addItem("Laptop", 999.99, 1);
cart.addItem("Mouse", 24.99, 2);
console.log(`Items in cart: ${cart.getItemCount()}`);
console.log(`Total: $${cart.getTotal().toFixed(2)}`);

The generated TypeScript declarations for this class would look like:

// Base interface for Swift reference types
export interface SwiftHeapObject {
    release(): void;
}

// ShoppingCart interface with all exported methods
export interface ShoppingCart extends SwiftHeapObject {
    addItem(name: string, price: number, quantity: number): void;
    removeItem(atIndex: number): void;
    getTotal(): number;
    getItemCount(): number;
}

export type Exports = {
    ShoppingCart: {
        new(): ShoppingCart;
    }
}

Supported Features

Swift Feature Status
Initializers: init()
Initializers that throw JSException: init() throws(JSException)
Initializers that throw any exception: init() throws
Async initializers: init() async
Deinitializers: deinit
Stored properties: var, let (with willSet, didSet)
Computed properties: var x: X { get set }
Computed properties with effects: var x: X { get async throws } 🚧
Methods: func ✅ (See doc:Exporting-Swift-Function )
Static/class methods: static func, class func 🚧
Subscripts: subscript()
Generics