|
| 1 | +# Exporting Swift Classes to JS |
| 2 | + |
| 3 | +Learn how to export Swift classes to JavaScript. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +> Tip: You can quickly preview what interfaces will be exposed on the Swift/TypeScript sides using the [BridgeJS Playground](https://swiftwasm.org/JavaScriptKit/PlayBridgeJS/). |
| 8 | +
|
| 9 | +To export a Swift class, mark both the class and any members you want to expose: |
| 10 | + |
| 11 | +```swift |
| 12 | +import JavaScriptKit |
| 13 | + |
| 14 | +@JS class ShoppingCart { |
| 15 | + private var items: [(name: String, price: Double, quantity: Int)] = [] |
| 16 | + |
| 17 | + @JS init() {} |
| 18 | + |
| 19 | + @JS public func addItem(name: String, price: Double, quantity: Int) { |
| 20 | + items.append((name, price, quantity)) |
| 21 | + } |
| 22 | + |
| 23 | + @JS public func removeItem(atIndex index: Int) { |
| 24 | + guard index >= 0 && index < items.count else { return } |
| 25 | + items.remove(at: index) |
| 26 | + } |
| 27 | + |
| 28 | + @JS public func getTotal() -> Double { |
| 29 | + return items.reduce(0) { $0 + $1.price * Double($1.quantity) } |
| 30 | + } |
| 31 | + |
| 32 | + @JS public func getItemCount() -> Int { |
| 33 | + return items.count |
| 34 | + } |
| 35 | + |
| 36 | + // This method won't be accessible from JavaScript (no @JS) |
| 37 | + var debugDescription: String { |
| 38 | + return "Cart with \(items.count) items, total: \(getTotal())" |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +In JavaScript: |
| 44 | + |
| 45 | +```javascript |
| 46 | +import { init } from "./.build/plugins/PackageToJS/outputs/Package/index.js"; |
| 47 | +const { exports } = await init({}); |
| 48 | + |
| 49 | +const cart = new exports.ShoppingCart(); |
| 50 | +cart.addItem("Laptop", 999.99, 1); |
| 51 | +cart.addItem("Mouse", 24.99, 2); |
| 52 | +console.log(`Items in cart: ${cart.getItemCount()}`); |
| 53 | +console.log(`Total: $${cart.getTotal().toFixed(2)}`); |
| 54 | +``` |
| 55 | + |
| 56 | +The generated TypeScript declarations for this class would look like: |
| 57 | + |
| 58 | +```typescript |
| 59 | +// Base interface for Swift reference types |
| 60 | +export interface SwiftHeapObject { |
| 61 | + release(): void; |
| 62 | +} |
| 63 | + |
| 64 | +// ShoppingCart interface with all exported methods |
| 65 | +export interface ShoppingCart extends SwiftHeapObject { |
| 66 | + addItem(name: string, price: number, quantity: number): void; |
| 67 | + removeItem(atIndex: number): void; |
| 68 | + getTotal(): number; |
| 69 | + getItemCount(): number; |
| 70 | +} |
| 71 | + |
| 72 | +export type Exports = { |
| 73 | + ShoppingCart: { |
| 74 | + new(): ShoppingCart; |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | +
|
| 79 | +## Supported Features |
| 80 | +
|
| 81 | +| Swift Feature | Status | |
| 82 | +|:--------------|:-------| |
| 83 | +| Initializers: `init()` | ✅ | |
| 84 | +| Initializers that throw JSException: `init() throws(JSException)` | ✅ | |
| 85 | +| Initializers that throw any exception: `init() throws` | ❌ | |
| 86 | +| Async initializers: `init() async` | ❌ | |
| 87 | +| Deinitializers: `deinit` | ✅ | |
| 88 | +| Stored properties: `var`, `let` (with `willSet`, `didSet`) | ✅ | |
| 89 | +| Computed properties: `var x: X { get set }` | ✅ | |
| 90 | +| Computed properties with effects: `var x: X { get async throws }` | 🚧 | |
| 91 | +| Methods: `func` | ✅ (See <doc:Exporting-Swift-Function> ) | |
| 92 | +| Static/class methods: `static func`, `class func` | 🚧 | |
| 93 | +| Subscripts: `subscript()` | ❌ | |
| 94 | +| Generics | ❌ | |
0 commit comments