-
-
Notifications
You must be signed in to change notification settings - Fork 51
Introduce BridgeJS, a declarative JS interop system #330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d657069
Introduce BridgeJS, a declarative JS interop system
kateinoigakukun 7309d97
[skip ci] Mention `@dynamicMemberLookup`-based APIs
kateinoigakukun 5c596cb
Add snapshot tests for JS glue for importing TS
kateinoigakukun 0575dd1
[BridgeJS] Hide it behind an experimental feature flag
kateinoigakukun 0ff3ebf
Update README.md
kateinoigakukun 9752c5a
Explicitly enable `JAVASCRIPTKIT_EXPERIMENTAL_BRIDGEJS` for unittest
kateinoigakukun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// swift-tools-version:6.0 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "MyApp", | ||
platforms: [ | ||
.macOS(.v14) | ||
], | ||
dependencies: [.package(name: "JavaScriptKit", path: "../../")], | ||
targets: [ | ||
.executableTarget( | ||
name: "MyApp", | ||
dependencies: [ | ||
"JavaScriptKit" | ||
], | ||
swiftSettings: [ | ||
.enableExperimentalFeature("Extern") | ||
], | ||
plugins: [ | ||
.plugin(name: "BridgeJS", package: "JavaScriptKit") | ||
] | ||
) | ||
] | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import JavaScriptKit | ||
|
||
// Mark functions you want to export to JavaScript with the @JS attribute | ||
// This function will be available as `renderCircleSVG(size)` in JavaScript | ||
@JS public func renderCircleSVG(size: Int) -> String { | ||
let strokeWidth = 3 | ||
let strokeColor = "black" | ||
let fillColor = "red" | ||
let cx = size / 2 | ||
let cy = size / 2 | ||
let r = (size / 2) - strokeWidth | ||
var svg = "<svg width=\"\(size)px\" height=\"\(size)px\">" | ||
svg += | ||
"<circle cx=\"\(cx)\" cy=\"\(cy)\" r=\"\(r)\" stroke=\"\(strokeColor)\" stroke-width=\"\(strokeWidth)\" fill=\"\(fillColor)\" />" | ||
svg += "</svg>" | ||
return svg | ||
} | ||
|
||
// Classes can also be exported using the @JS attribute | ||
// This class will be available as a constructor in JavaScript: new Greeter("name") | ||
@JS class Greeter { | ||
var name: String | ||
|
||
// Use @JS for initializers you want to expose | ||
@JS init(name: String) { | ||
self.name = name | ||
} | ||
|
||
// Methods need the @JS attribute to be accessible from JavaScript | ||
// This method will be available as greeter.greet() in JavaScript | ||
@JS public func greet() -> String { | ||
"Hello, \(name)!" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<title>Getting Started</title> | ||
</head> | ||
|
||
<body> | ||
<script type="module" src="index.js"></script> | ||
</body> | ||
|
||
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { init } from "./.build/plugins/PackageToJS/outputs/Package/index.js"; | ||
const { exports } = await init({}); | ||
|
||
const Greeter = exports.Greeter; | ||
const greeter = new Greeter("World"); | ||
const circle = exports.renderCircleSVG(100); | ||
|
||
// Display the results | ||
const textOutput = document.createElement("div"); | ||
textOutput.innerText = greeter.greet() | ||
document.body.appendChild(textOutput); | ||
const circleOutput = document.createElement("div"); | ||
circleOutput.innerHTML = circle; | ||
document.body.appendChild(circleOutput); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// swift-tools-version:6.0 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "MyApp", | ||
platforms: [ | ||
.macOS(.v10_15), | ||
.iOS(.v13), | ||
.tvOS(.v13), | ||
.watchOS(.v6), | ||
.macCatalyst(.v13), | ||
], | ||
dependencies: [.package(name: "JavaScriptKit", path: "../../")], | ||
targets: [ | ||
.executableTarget( | ||
name: "MyApp", | ||
dependencies: [ | ||
"JavaScriptKit" | ||
], | ||
swiftSettings: [ | ||
.enableExperimentalFeature("Extern") | ||
], | ||
plugins: [ | ||
.plugin(name: "BridgeJS", package: "JavaScriptKit") | ||
] | ||
) | ||
] | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Function definition to expose console.log to Swift | ||
// Will be imported as a Swift function: consoleLog(message: String) | ||
export function consoleLog(message: string): void | ||
|
||
// TypeScript interface types are converted to Swift structs | ||
// This defines a subset of the browser's HTMLElement interface | ||
type HTMLElement = Pick<globalThis.HTMLElement, "innerText"> & { | ||
// Methods with object parameters are properly handled | ||
appendChild(child: HTMLElement): void | ||
} | ||
|
||
// TypeScript object type with read-only properties | ||
// Properties will become Swift properties with appropriate access level | ||
type Document = { | ||
// Regular property - will be read/write in Swift | ||
title: string | ||
// Read-only property - will be read-only in Swift | ||
readonly body: HTMLElement | ||
// Method returning an object - will become a Swift method returning an HTMLElement | ||
createElement(tagName: string): HTMLElement | ||
} | ||
// Function returning a complex object | ||
// Will be imported as a Swift function: getDocument() -> Document | ||
export function getDocument(): Document |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import JavaScriptKit | ||
|
||
// This function is automatically generated by the @JS plugin | ||
// It demonstrates how to use TypeScript functions and types imported from bridge.d.ts | ||
@JS public func run() { | ||
// Call the imported consoleLog function defined in bridge.d.ts | ||
consoleLog("Hello, World!") | ||
|
||
// Get the document object - this comes from the imported getDocument() function | ||
let document = getDocument() | ||
|
||
// Access and modify properties - the title property is read/write | ||
document.title = "Hello, World!" | ||
|
||
// Access read-only properties - body is defined as readonly in TypeScript | ||
let body = document.body | ||
|
||
// Create a new element using the document.createElement method | ||
let h1 = document.createElement("h1") | ||
|
||
// Set properties on the created element | ||
h1.innerText = "Hello, World!" | ||
|
||
// Call methods on objects - appendChild is defined in the HTMLElement interface | ||
body.appendChild(h1) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<title>Getting Started</title> | ||
</head> | ||
|
||
<body> | ||
<script type="module" src="index.js"></script> | ||
|
||
<div id="exports-result"></div> | ||
<div id="imports-result"></div> | ||
<pre id="code"></pre> | ||
</body> | ||
|
||
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { init } from "./.build/plugins/PackageToJS/outputs/Package/index.js"; | ||
const { exports } = await init({ | ||
imports: { | ||
consoleLog: (message) => { | ||
console.log(message); | ||
}, | ||
getDocument: () => { | ||
return document; | ||
}, | ||
} | ||
}); | ||
|
||
exports.run() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// swift-tools-version: 6.0 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "BridgeJS", | ||
platforms: [.macOS(.v13)], | ||
dependencies: [ | ||
.package(url: "https://github.com/swiftlang/swift-syntax", from: "600.0.1") | ||
], | ||
targets: [ | ||
.target(name: "BridgeJSBuildPlugin"), | ||
.target(name: "BridgeJSLink"), | ||
.executableTarget( | ||
name: "BridgeJSTool", | ||
dependencies: [ | ||
.product(name: "SwiftParser", package: "swift-syntax"), | ||
.product(name: "SwiftSyntax", package: "swift-syntax"), | ||
.product(name: "SwiftBasicFormat", package: "swift-syntax"), | ||
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"), | ||
] | ||
), | ||
.testTarget( | ||
name: "BridgeJSToolTests", | ||
dependencies: ["BridgeJSTool", "BridgeJSLink"], | ||
exclude: ["__Snapshots__", "Inputs"] | ||
), | ||
] | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.