Skip to content

Commit 99a288f

Browse files
Merge pull request #427 from swiftwasm/yt/bjs-doc
2 parents 417ee16 + 1b19e62 commit 99a288f

12 files changed

+991
-598
lines changed

Sources/JavaScriptKit/Documentation.docc/Articles/BridgeJS/Exporting-Swift-to-JavaScript.md

Lines changed: 7 additions & 597 deletions
Large diffs are not rendered by default.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)