forked from swiftwasm/JavaScriptKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
42 lines (33 loc) · 1.09 KB
/
index.js
File metadata and controls
42 lines (33 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// @ts-check
import { BridgeJSPlayground } from './app.js';
Error.stackTraceLimit = Infinity;
const SWIFT_INPUT = `import JavaScriptKit
@JS public func calculateTotal(price: Double, quantity: Int) -> Double {
return price * Double(quantity)
}
@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 getTotal() -> Double {
return items.reduce(0) { $0 + $1.price * Double($1.quantity) }
}
}`
const DTS_INPUT = `export type Console = {
log(message: string): void;
}
export function console(): Console;`
// Initialize the playground when the page loads
document.addEventListener('DOMContentLoaded', async () => {
try {
const playground = new BridgeJSPlayground();
await playground.initialize({
swift: SWIFT_INPUT,
dts: DTS_INPUT
});
} catch (error) {
console.error('Failed to initialize playground:', error);
}
});