Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Examples/Embedded/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ let package = Package(
swiftSettings: [
.enableExperimentalFeature("Extern")
],
plugins: [
.plugin(name: "BridgeJS", package: "JavaScriptKit")
]
)
],
swiftLanguageModes: [.v5]
Expand Down
21 changes: 21 additions & 0 deletions Examples/Embedded/Sources/EmbeddedApp/main.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import JavaScriptKit

// BridgeJS generic imports work under Embedded Swift: `echoValue` is
// implemented in JavaScript (see index.html) and a single piece of generated
// glue serves every conforming type, selected by a runtime type ID.
@JS struct CounterLabel {
var count: Int
var text: String
}

@JSFunction func echoValue<T: BridgedSwiftGenericBridgeable>(_ value: T) throws(JSException) -> T

let alert = JSObject.global.alert.object!
let document = JSObject.global.document

Expand Down Expand Up @@ -46,6 +56,17 @@ _ = encoderContainer.appendChild(textInputElement)
_ = encoderContainer.appendChild(encodeResultElement)
_ = document.body.appendChild(encoderContainer)

let genericResultElement = document.createElement("pre")
do {
let number = try echoValue(42)
let text = try echoValue("hello")
let label = try echoValue(CounterLabel(count: number, text: text))
genericResultElement.innerText = .string("Generic import round-trip: \(label.text) \(label.count)")
} catch {
genericResultElement.innerText = "Generic import round-trip failed"
}
_ = document.body.appendChild(genericResultElement)

func print(_ message: String) {
_ = JSObject.global.console.log(message)
}
Expand Down
9 changes: 8 additions & 1 deletion Examples/Embedded/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
<body>
<script type="module">
import { init } from "./.build/plugins/PackageToJS/outputs/Package/index.js";
init();
init({
getImports() {
return {
// Implements the generic `echoValue` imported by main.swift.
echoValue: (value) => value,
};
},
});
</script>
</body>

Expand Down
2 changes: 1 addition & 1 deletion Plugins/BridgeJS/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ graph LR
| `Dictionary<K, V>` | `Record<K, V>` | - | [#495](https://github.com/swiftwasm/JavaScriptKit/issues/495) |
| `Set<T>` | `Set<T>` | - | [#397](https://github.com/swiftwasm/JavaScriptKit/issues/397) |
| `Foundation.URL` | `string` | - | [#496](https://github.com/swiftwasm/JavaScriptKit/issues/496) |
| Generics | - | - | [#398](https://github.com/swiftwasm/JavaScriptKit/issues/398) |
| Generic function or method (`T`, `[T]`, `T?`, `[String: T]`) | `<T>(value: T, type: BridgeType<T>)` | Depends on `T` | ✅ |

### Import-specific (TypeScript -> Swift)

Expand Down
Loading
Loading