@expo/expo-module-optimized-macros-plugin 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/apple/ExpoModulesOptimizedMacros.podspec +20 -0
- package/apple/Package.resolved +15 -0
- package/apple/Package.swift +38 -0
- package/apple/Sources/ExpoModulesOptimized/ExpoModulesOptimized.swift +30 -0
- package/apple/Sources/ExpoModulesOptimizedMacros/ExpoModulesOptimizedMacro.swift +184 -0
- package/apple/build.js +25 -0
- package/apple/dist/ExpoModulesOptimized.swift +30 -0
- package/apple/dist/ExpoModulesOptimizedMacros-tool +0 -0
- package/package.json +15 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Expo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, '..', 'package.json')))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = 'ExpoModulesOptimizedMacro'
|
|
7
|
+
s.version = package['version']
|
|
8
|
+
s.summary = package['description']
|
|
9
|
+
s.description = package['description']
|
|
10
|
+
s.license = package['license']
|
|
11
|
+
s.author = package['author']
|
|
12
|
+
s.homepage = package['homepage']
|
|
13
|
+
s.platforms = {
|
|
14
|
+
:ios => '15.1',
|
|
15
|
+
:tvos => '15.1',
|
|
16
|
+
:osx => '11.0',
|
|
17
|
+
}
|
|
18
|
+
s.source = { git: 'https://github.com/expo/expo-module-optimized-macros-plugin.git' }
|
|
19
|
+
s.source_files = 'Sources/ExpoModulesOptimized/**/*.swift'
|
|
20
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"originHash" : "4832fa479257148c1acb46952c02540c156db6e744797638db69be31154d5ad2",
|
|
3
|
+
"pins" : [
|
|
4
|
+
{
|
|
5
|
+
"identity" : "swift-syntax",
|
|
6
|
+
"kind" : "remoteSourceControl",
|
|
7
|
+
"location" : "https://github.com/swiftlang/swift-syntax.git",
|
|
8
|
+
"state" : {
|
|
9
|
+
"revision" : "4799286537280063c85a32f09884cfbca301b1a1",
|
|
10
|
+
"version" : "602.0.0"
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
],
|
|
14
|
+
"version" : 3
|
|
15
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// swift-tools-version: 6.2
|
|
2
|
+
// The swift-tools-version declares the minimum version of Swift required to build this package.
|
|
3
|
+
|
|
4
|
+
import PackageDescription
|
|
5
|
+
import CompilerPluginSupport
|
|
6
|
+
|
|
7
|
+
let package = Package(
|
|
8
|
+
name: "ExpoModulesOptimized",
|
|
9
|
+
platforms: [.macOS(.v10_15)],
|
|
10
|
+
products: [
|
|
11
|
+
.executable(
|
|
12
|
+
name: "ExpoModulesOptimized",
|
|
13
|
+
targets: ["ExpoModulesOptimized"]
|
|
14
|
+
),
|
|
15
|
+
],
|
|
16
|
+
dependencies: [
|
|
17
|
+
.package(url: "https://github.com/swiftlang/swift-syntax.git", from: "602.0.0-latest"),
|
|
18
|
+
],
|
|
19
|
+
targets: [
|
|
20
|
+
.macro(
|
|
21
|
+
name: "ExpoModulesOptimizedMacros",
|
|
22
|
+
dependencies: [
|
|
23
|
+
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
|
|
24
|
+
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
|
|
25
|
+
]
|
|
26
|
+
),
|
|
27
|
+
|
|
28
|
+
.executableTarget(name: "ExpoModulesOptimized", dependencies: ["ExpoModulesOptimizedMacros"]),
|
|
29
|
+
|
|
30
|
+
.testTarget(
|
|
31
|
+
name: "ExpoModulesOptimizedTests",
|
|
32
|
+
dependencies: [
|
|
33
|
+
"ExpoModulesOptimizedMacros",
|
|
34
|
+
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
|
|
35
|
+
]
|
|
36
|
+
),
|
|
37
|
+
]
|
|
38
|
+
)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/// An attached macro that generates an optimized synchronous function descriptor.
|
|
2
|
+
/// Creates a peer function that returns `OptimizedFunctionDescriptor`, for use with
|
|
3
|
+
/// the `Function("name", descriptor)` overload in ModuleDefinition result builders.
|
|
4
|
+
///
|
|
5
|
+
/// Usage:
|
|
6
|
+
///
|
|
7
|
+
/// @OptimizedFunction
|
|
8
|
+
/// private func addNumbers(a: Double, b: Double) -> Double {
|
|
9
|
+
/// return a + b
|
|
10
|
+
/// }
|
|
11
|
+
///
|
|
12
|
+
/// // In definition():
|
|
13
|
+
/// Function("addNumbers", addNumbers())
|
|
14
|
+
///
|
|
15
|
+
/// You can also provide an explicit peer name if the Swift function name differs:
|
|
16
|
+
///
|
|
17
|
+
/// @OptimizedFunction("addNumbers")
|
|
18
|
+
/// private func addNumbersImpl(a: Double, b: Double) -> Double { ... }
|
|
19
|
+
///
|
|
20
|
+
/// // In definition():
|
|
21
|
+
/// Function("addNumbers", addNumbers())
|
|
22
|
+
///
|
|
23
|
+
/// The generated peer function uses the optimized JSI bridge path with
|
|
24
|
+
/// @convention(block) closures for maximum performance.
|
|
25
|
+
@attached(peer, names: arbitrary)
|
|
26
|
+
public macro OptimizedFunction(_ name: String) = #externalMacro(module: "ExpoModulesOptimizedMacros", type: "OptimizedFunctionAttachedMacro")
|
|
27
|
+
|
|
28
|
+
/// Variant that derives the peer function name from the Swift function name.
|
|
29
|
+
@attached(peer, names: arbitrary)
|
|
30
|
+
public macro OptimizedFunction() = #externalMacro(module: "ExpoModulesOptimizedMacros", type: "OptimizedFunctionAttachedMacro")
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import SwiftCompilerPlugin
|
|
2
|
+
import SwiftSyntax
|
|
3
|
+
import SwiftSyntaxBuilder
|
|
4
|
+
import SwiftSyntaxMacros
|
|
5
|
+
import Foundation
|
|
6
|
+
|
|
7
|
+
// MARK: - Helper Functions
|
|
8
|
+
|
|
9
|
+
/// Generates Objective-C type encoding string for the given signature
|
|
10
|
+
internal func generateTypeEncoding(returnType: String, paramTypes: [String]) throws -> String {
|
|
11
|
+
var encoding = ""
|
|
12
|
+
|
|
13
|
+
// Encode return type
|
|
14
|
+
guard let returnEncoding = typeToEncoding(returnType) else {
|
|
15
|
+
throw MacroExpansionErrorMessage("Unsupported return type: \(returnType)")
|
|
16
|
+
}
|
|
17
|
+
encoding += returnEncoding
|
|
18
|
+
|
|
19
|
+
// Add block marker
|
|
20
|
+
encoding += "@?"
|
|
21
|
+
|
|
22
|
+
// Encode parameters
|
|
23
|
+
for paramType in paramTypes {
|
|
24
|
+
guard let paramEncoding = typeToEncoding(paramType) else {
|
|
25
|
+
throw MacroExpansionErrorMessage("Unsupported parameter type: \(paramType)")
|
|
26
|
+
}
|
|
27
|
+
encoding += paramEncoding
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return encoding
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/// Maps Swift type to Objective-C type encoding character
|
|
34
|
+
internal func typeToEncoding(_ type: String) -> String? {
|
|
35
|
+
switch type {
|
|
36
|
+
case "Double":
|
|
37
|
+
return "d"
|
|
38
|
+
case "Int", "Int64":
|
|
39
|
+
return "q"
|
|
40
|
+
case "String":
|
|
41
|
+
return "@"
|
|
42
|
+
case "Bool":
|
|
43
|
+
return "B"
|
|
44
|
+
case "Void", "()":
|
|
45
|
+
return "v"
|
|
46
|
+
default:
|
|
47
|
+
return nil
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// MARK: - Macro Implementation
|
|
52
|
+
|
|
53
|
+
/// Implementation of the `@OptimizedFunction` attached macro.
|
|
54
|
+
/// Generates a peer function that returns `OptimizedFunctionDescriptor` for use with
|
|
55
|
+
/// the `Function("name", descriptor)` or `AsyncFunction("name", descriptor)` overload.
|
|
56
|
+
///
|
|
57
|
+
/// Usage:
|
|
58
|
+
///
|
|
59
|
+
/// @OptimizedFunction
|
|
60
|
+
/// private func addNumbers(a: Double, b: Double) -> Double {
|
|
61
|
+
/// return a + b
|
|
62
|
+
/// }
|
|
63
|
+
///
|
|
64
|
+
/// generates a peer function:
|
|
65
|
+
///
|
|
66
|
+
/// private func addNumbers() -> OptimizedFunctionDescriptor {
|
|
67
|
+
/// return OptimizedSyncFunctionDefinition.createDescriptor(
|
|
68
|
+
/// typeEncoding: "d@?dd",
|
|
69
|
+
/// argsCount: 2,
|
|
70
|
+
/// block: (addNumbers as @convention(block) (Double, Double) -> Double) as AnyObject
|
|
71
|
+
/// )
|
|
72
|
+
/// }
|
|
73
|
+
///
|
|
74
|
+
/// Then use in definition(): `Function("addNumbers", addNumbers())`
|
|
75
|
+
public struct OptimizedFunctionAttachedMacro: PeerMacro {
|
|
76
|
+
public static func expansion(
|
|
77
|
+
of node: AttributeSyntax,
|
|
78
|
+
providingPeersOf declaration: some DeclSyntaxProtocol,
|
|
79
|
+
in context: some MacroExpansionContext
|
|
80
|
+
) throws -> [DeclSyntax] {
|
|
81
|
+
guard let funcDecl = declaration.as(FunctionDeclSyntax.self) else {
|
|
82
|
+
throw MacroExpansionErrorMessage("@OptimizedFunction can only be attached to function declarations")
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Extract the function name from the macro argument, or default to the Swift function name
|
|
86
|
+
let functionName: String
|
|
87
|
+
if case let .argumentList(arguments) = node.arguments,
|
|
88
|
+
let firstArg = arguments.first,
|
|
89
|
+
let nameExpr = firstArg.expression.as(StringLiteralExprSyntax.self),
|
|
90
|
+
let nameSegment = nameExpr.segments.first?.as(StringSegmentSyntax.self) {
|
|
91
|
+
functionName = nameSegment.content.text
|
|
92
|
+
} else {
|
|
93
|
+
functionName = funcDecl.name.text
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
var paramTypes: [String] = []
|
|
97
|
+
for param in funcDecl.signature.parameterClause.parameters {
|
|
98
|
+
paramTypes.append(param.type.trimmedDescription)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
let returnType = funcDecl.signature.returnClause?.type.trimmedDescription ?? "Void"
|
|
102
|
+
let functionThrows = funcDecl.signature.effectSpecifiers?.throwsClause?.throwsSpecifier != nil
|
|
103
|
+
let typeEncoding = try generateTypeEncoding(returnType: returnType, paramTypes: paramTypes)
|
|
104
|
+
let implFuncName = funcDecl.name.text
|
|
105
|
+
let blockParamTypes = paramTypes.joined(separator: ", ")
|
|
106
|
+
let argNames = (0..<paramTypes.count).map { "arg\($0)" }.joined(separator: ", ")
|
|
107
|
+
|
|
108
|
+
let peerFunc: DeclSyntax
|
|
109
|
+
|
|
110
|
+
if functionThrows {
|
|
111
|
+
let isVoid = returnType == "Void" || returnType == "()"
|
|
112
|
+
let implType = paramTypes.isEmpty ? "() throws -> \(returnType)" : "(\(blockParamTypes)) throws -> \(returnType)"
|
|
113
|
+
let wrapperType = paramTypes.isEmpty ? "@convention(block) () -> \(returnType)" : "@convention(block) (\(blockParamTypes)) -> \(returnType)"
|
|
114
|
+
let closureArgs = paramTypes.isEmpty ? "" : " \(argNames) in"
|
|
115
|
+
let implCall = paramTypes.isEmpty ? "impl()" : "impl(\(argNames))"
|
|
116
|
+
let tryExpr = isVoid ? "try \(implCall)" : "return try \(implCall)"
|
|
117
|
+
let unreachable = isVoid ? "" : "\n fatalError(\"Unreachable\")"
|
|
118
|
+
|
|
119
|
+
peerFunc = """
|
|
120
|
+
private func \(raw: functionName)() -> OptimizedFunctionDescriptor {
|
|
121
|
+
let impl: \(raw: implType) = \(raw: implFuncName)
|
|
122
|
+
let wrapper: \(raw: wrapperType) = {\(raw: closureArgs)
|
|
123
|
+
do {
|
|
124
|
+
\(raw: tryExpr)
|
|
125
|
+
} catch {
|
|
126
|
+
let nsError: NSError
|
|
127
|
+
if let expoError = error as? Exception {
|
|
128
|
+
nsError = NSError(domain: "dev.expo.modules", code: 0, userInfo: [
|
|
129
|
+
"name": expoError.name,
|
|
130
|
+
"code": expoError.code,
|
|
131
|
+
"message": expoError.debugDescription,
|
|
132
|
+
])
|
|
133
|
+
} else {
|
|
134
|
+
nsError = error as NSError
|
|
135
|
+
}
|
|
136
|
+
let exception = NSException(
|
|
137
|
+
name: NSExceptionName(nsError.userInfo["name"] as? String ?? "SwiftError"),
|
|
138
|
+
reason: nsError.userInfo["message"] as? String ?? nsError.localizedDescription,
|
|
139
|
+
userInfo: nsError.userInfo
|
|
140
|
+
)
|
|
141
|
+
exception.raise()\(raw: unreachable)
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return OptimizedSyncFunctionDefinition.createDescriptor(
|
|
145
|
+
typeEncoding: "\(raw: typeEncoding)",
|
|
146
|
+
argsCount: \(raw: String(paramTypes.count)),
|
|
147
|
+
block: wrapper as AnyObject
|
|
148
|
+
)
|
|
149
|
+
}
|
|
150
|
+
"""
|
|
151
|
+
} else {
|
|
152
|
+
peerFunc = """
|
|
153
|
+
private func \(raw: functionName)() -> OptimizedFunctionDescriptor {
|
|
154
|
+
return OptimizedSyncFunctionDefinition.createDescriptor(
|
|
155
|
+
typeEncoding: "\(raw: typeEncoding)",
|
|
156
|
+
argsCount: \(raw: String(paramTypes.count)),
|
|
157
|
+
block: (\(raw: implFuncName) as @convention(block) (\(raw: blockParamTypes)) -> \(raw: returnType)) as AnyObject
|
|
158
|
+
)
|
|
159
|
+
}
|
|
160
|
+
"""
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return [peerFunc]
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
struct MacroExpansionErrorMessage: Error, CustomStringConvertible {
|
|
168
|
+
let message: String
|
|
169
|
+
|
|
170
|
+
init(_ message: String) {
|
|
171
|
+
self.message = message
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
var description: String {
|
|
175
|
+
return message
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
@main
|
|
180
|
+
struct ExpoModulesOptimizedPlugin: CompilerPlugin {
|
|
181
|
+
let providingMacros: [Macro.Type] = [
|
|
182
|
+
OptimizedFunctionAttachedMacro.self,
|
|
183
|
+
]
|
|
184
|
+
}
|
package/apple/build.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const { execSync } = require('node:child_process');
|
|
4
|
+
const fs = require('node:fs');
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
|
|
7
|
+
const distRoot = path.resolve(__dirname, 'dist');
|
|
8
|
+
|
|
9
|
+
try {
|
|
10
|
+
fs.mkdirSync(distRoot, { recursive: true });
|
|
11
|
+
execSync('swift build -c release', { stdio: 'inherit', cwd: __dirname });
|
|
12
|
+
|
|
13
|
+
fs.copyFileSync(
|
|
14
|
+
path.join(__dirname, '.build/arm64-apple-macosx/release/ExpoModulesOptimizedMacros-tool'),
|
|
15
|
+
path.join(distRoot, 'ExpoModulesOptimizedMacros-tool')
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
fs.copyFileSync(
|
|
19
|
+
path.join(__dirname, 'Sources/ExpoModulesOptimized/ExpoModulesOptimized.swift'),
|
|
20
|
+
path.join(distRoot, 'ExpoModulesOptimized.swift')
|
|
21
|
+
);
|
|
22
|
+
} catch (error) {
|
|
23
|
+
console.error('Build failed:', error.message);
|
|
24
|
+
process.exit(1);
|
|
25
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/// An attached macro that generates an optimized synchronous function descriptor.
|
|
2
|
+
/// Creates a peer function that returns `OptimizedFunctionDescriptor`, for use with
|
|
3
|
+
/// the `Function("name", descriptor)` overload in ModuleDefinition result builders.
|
|
4
|
+
///
|
|
5
|
+
/// Usage:
|
|
6
|
+
///
|
|
7
|
+
/// @OptimizedFunction
|
|
8
|
+
/// private func addNumbers(a: Double, b: Double) -> Double {
|
|
9
|
+
/// return a + b
|
|
10
|
+
/// }
|
|
11
|
+
///
|
|
12
|
+
/// // In definition():
|
|
13
|
+
/// Function("addNumbers", addNumbers())
|
|
14
|
+
///
|
|
15
|
+
/// You can also provide an explicit peer name if the Swift function name differs:
|
|
16
|
+
///
|
|
17
|
+
/// @OptimizedFunction("addNumbers")
|
|
18
|
+
/// private func addNumbersImpl(a: Double, b: Double) -> Double { ... }
|
|
19
|
+
///
|
|
20
|
+
/// // In definition():
|
|
21
|
+
/// Function("addNumbers", addNumbers())
|
|
22
|
+
///
|
|
23
|
+
/// The generated peer function uses the optimized JSI bridge path with
|
|
24
|
+
/// @convention(block) closures for maximum performance.
|
|
25
|
+
@attached(peer, names: arbitrary)
|
|
26
|
+
public macro OptimizedFunction(_ name: String) = #externalMacro(module: "ExpoModulesOptimizedMacros", type: "OptimizedFunctionAttachedMacro")
|
|
27
|
+
|
|
28
|
+
/// Variant that derives the peer function name from the Swift function name.
|
|
29
|
+
@attached(peer, names: arbitrary)
|
|
30
|
+
public macro OptimizedFunction() = #externalMacro(module: "ExpoModulesOptimizedMacros", type: "OptimizedFunctionAttachedMacro")
|
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@expo/expo-module-optimized-macros-plugin",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Swift macro plugin for optimized Expo module function definitions",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "650 Industries, Inc.",
|
|
7
|
+
"homepage": "https://github.com/expo/expo-module-optimized-macros-plugin",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/expo/expo-module-optimized-macros-plugin.git"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "node apple/build.js"
|
|
14
|
+
}
|
|
15
|
+
}
|