Skip to content

Commit 7ddf818

Browse files
index on main: 9511ee2 Update README
1 parent e211f07 commit 7ddf818

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed

Package.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ let package = Package(
4242
targets: [
4343
.target(
4444
name: "JavaScriptKit",
45-
dependencies: ["_CJavaScriptKit"],
45+
dependencies: ["_CJavaScriptKit", "JavaScriptKitMacros"],
4646
exclude: useLegacyResourceBundling ? [] : ["Runtime"],
4747
resources: useLegacyResourceBundling ? [.copy("Runtime")] : [],
4848
cSettings: shouldBuildForEmbedded
@@ -59,6 +59,14 @@ let package = Package(
5959
] : [])
6060
),
6161
.target(name: "_CJavaScriptKit"),
62+
.macro(
63+
name: "JavaScriptKitMacros",
64+
dependencies: [
65+
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
66+
.product(name: "SwiftCompilerPlugin", package: "swift-syntax")
67+
]
68+
),
69+
6270
.testTarget(
6371
name: "JavaScriptKitTests",
6472
dependencies: ["JavaScriptKit"],

Sources/JavaScriptKit/Macros.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,22 @@ public enum JSEnumStyle: String {
107107
/// - Important: This feature is still experimental. No API stability is guaranteed, and the API may change in future releases.
108108
@attached(peer)
109109
public macro JS(namespace: String? = nil, enumStyle: JSEnumStyle = .const) = Builtin.ExternalMacro
110+
111+
@attached(accessor)
112+
public macro JSImportVariable(from: String, jsName: String) = #externalMacro(module: "JavaScriptKitMacros", type: "JSImportVariableMacro")
113+
114+
@attached(body)
115+
@attached(peer)
116+
public macro JSImportFunction(from: String, jsName: String) = #externalMacro(module: "JavaScriptKitMacros", type: "JSImportFunctionMacro")
117+
118+
// @JSImportVariable(from: "globalThis", jsName: "console")
119+
public var console: JSObject {
120+
@_extern(wasm, module: "globalThis", name: "console")
121+
func console() -> UInt32
122+
}
123+
124+
@JSImportFunction(from: "globalThis", jsName: "document")
125+
public var document: JSObject
126+
127+
@JSImportFunction(from: "globalThis", jsName: "alert")
128+
public func alert(message: String) -> Void
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import SwiftSyntax
2+
import SwiftSyntaxBuilder
3+
import SwiftSyntaxMacros
4+
5+
enum JSImportFunctionMacro {}
6+
7+
extension JSImportFunctionMacro: BodyMacro {
8+
package static func expansion(
9+
of node: AttributeSyntax,
10+
providingBodyFor declaration: some DeclSyntaxProtocol & WithOptionalCodeBlockSyntax,
11+
in context: some MacroExpansionContext
12+
) throws -> [CodeBlockItemSyntax] {
13+
return [
14+
"fatalError(\"Not implemented\")"
15+
]
16+
}
17+
}
18+
19+
enum JSImportVariableMacro {}
20+
21+
extension JSImportVariableMacro: AccessorMacro {
22+
package static func expansion(
23+
of node: AttributeSyntax,
24+
providingAccessorsOf declaration: some DeclSyntaxProtocol,
25+
in context: some MacroExpansionContext
26+
) throws -> [AccessorDeclSyntax] {
27+
return [
28+
AccessorDeclSyntax(
29+
accessorSpecifier: .keyword(.get),
30+
effectSpecifiers: AccessorEffectSpecifiersSyntax(
31+
asyncSpecifier: nil,
32+
throwsClause: nil
33+
),
34+
body: CodeBlockSyntax {
35+
"fatalError(\"Not implemented\")"
36+
}
37+
)
38+
]
39+
}
40+
}
41+
42+
extension JSImportFunctionMacro: AccessorMacro {
43+
package static func expansion(
44+
of node: AttributeSyntax,
45+
providingAccessorsOf declaration: some DeclSyntaxProtocol,
46+
in context: some MacroExpansionContext
47+
) throws -> [AccessorDeclSyntax] {
48+
return [
49+
AccessorDeclSyntax(
50+
accessorSpecifier: .keyword(.get),
51+
effectSpecifiers: AccessorEffectSpecifiersSyntax(
52+
asyncSpecifier: nil,
53+
throwsClause: nil
54+
),
55+
body: CodeBlockSyntax {
56+
"fatalError(\"Not implemented\")"
57+
}
58+
)
59+
]
60+
}
61+
}
62+
63+
extension JSImportFunctionMacro: PeerMacro {
64+
package static func expansion(
65+
of node: AttributeSyntax,
66+
providingPeersOf declaration: some DeclSyntaxProtocol,
67+
in context: some MacroExpansionContext
68+
) throws -> [DeclSyntax] {
69+
let functionDecl: DeclSyntax = """
70+
func \(raw: context.makeUniqueName("jsImportFunction"))() -> UInt32 {
71+
fatalError("Not implemented")
72+
}
73+
"""
74+
return [functionDecl]
75+
}
76+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import SwiftCompilerPlugin
2+
import SwiftSyntaxMacros
3+
4+
@main
5+
struct JavaScriptKitMacrosPlugin: CompilerPlugin {
6+
var providingMacros: [Macro.Type] = [
7+
JSImportFunctionMacro.self,
8+
JSImportVariableMacro.self,
9+
]
10+
}

0 commit comments

Comments
 (0)