expo-modules-macros 0.22.0 → 0.23.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/.github/workflows/linux.yml +8 -5
- package/.github/workflows/publish.yml +26 -11
- package/.github/workflows/swift-format.yml +4 -1
- package/.github/workflows/swift.yml +7 -4
- package/.github/workflows/windows.yml +8 -5
- package/AGENTS.md +12 -10
- package/README.md +7 -5
- package/apple/ExpoModulesMacros +0 -0
- package/apple/Sources/ExpoModulesMacros/DecorateModuleBuilder.swift +32 -5
- package/apple/Sources/ExpoModulesMacros/EventMacro.swift +5 -4
- package/apple/Sources/ExpoModulesMacros/ExpoModuleMacro.swift +14 -27
- package/apple/Sources/ExpoModulesMacros/JSMacro.swift +4 -4
- package/apple/Sources/ExpoModulesMacros/MacroHelpers.swift +9 -1
- package/apple/Sources/ExpoModulesMacros/RecordMacro.swift +18 -22
- package/apple/Sources/ExpoModulesMacros/SharedObjectMacro.swift +13 -33
- package/apple/Sources/ExpoModulesMacros/TypeConformanceAssertion.swift +39 -63
- package/apple/Sources/ExpoModulesMacros/UnionMacro.swift +1 -1
- package/apple/Sources/ExpoModulesScanner/Exports/ExportedSurface.swift +17 -2
- package/apple/Sources/ExpoModulesScanner/Exports/ResolveRefs.swift +3 -0
- package/apple/Sources/ExpoModulesScanner/Exports/SurfaceVisitor.swift +52 -1
- package/apple/Sources/ExpoModulesScanner/Exports/TypeNode.swift +26 -7
- package/build/index.d.ts +1 -1
- package/build/index.js +3 -3
- package/build/types.d.ts +25 -1
- package/build/types.js +1 -1
- package/package.json +6 -5
|
@@ -50,6 +50,9 @@ jobs:
|
|
|
50
50
|
with:
|
|
51
51
|
node-version: 24
|
|
52
52
|
|
|
53
|
+
- name: Setup pnpm
|
|
54
|
+
uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0
|
|
55
|
+
|
|
53
56
|
- name: Cache SwiftPM build
|
|
54
57
|
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
|
|
55
58
|
with:
|
|
@@ -65,10 +68,10 @@ jobs:
|
|
|
65
68
|
run: swift test
|
|
66
69
|
|
|
67
70
|
- name: Build release tool
|
|
68
|
-
# Runs `
|
|
71
|
+
# Runs `pnpm run build` (scripts/build.js): builds the release plugin binary for this
|
|
69
72
|
# architecture as platforms/linux-<arch>/ExpoModulesMacros.
|
|
70
73
|
working-directory: ${{ github.workspace }}
|
|
71
|
-
run:
|
|
74
|
+
run: pnpm run build
|
|
72
75
|
|
|
73
76
|
- name: Verify the release tool
|
|
74
77
|
# With no arguments it is the plugin server, which exits when stdin closes; with arguments it
|
|
@@ -125,8 +128,8 @@ jobs:
|
|
|
125
128
|
# `@expo/modules-macros-linux-<arch>`, as it would in a project, not the local build.
|
|
126
129
|
working-directory: ${{ github.workspace }}
|
|
127
130
|
run: |
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
+
pnpm install
|
|
132
|
+
pnpm add --save-optional "$PLATFORM_PACKAGE_TARBALL"
|
|
133
|
+
pnpm run build:ts
|
|
131
134
|
node scripts/check-scan.js .
|
|
132
135
|
|
|
@@ -56,10 +56,13 @@ jobs:
|
|
|
56
56
|
with:
|
|
57
57
|
node-version: 24
|
|
58
58
|
|
|
59
|
+
- name: Setup pnpm
|
|
60
|
+
uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0
|
|
61
|
+
|
|
59
62
|
- name: Build release tool
|
|
60
63
|
# Runs scripts/build.js: builds the release plugin binary for this architecture as
|
|
61
64
|
# platforms/win32-<arch>/ExpoModulesMacros.exe.
|
|
62
|
-
run:
|
|
65
|
+
run: pnpm run build
|
|
63
66
|
|
|
64
67
|
- name: Upload tool
|
|
65
68
|
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
|
@@ -94,10 +97,13 @@ jobs:
|
|
|
94
97
|
with:
|
|
95
98
|
node-version: 24
|
|
96
99
|
|
|
100
|
+
- name: Setup pnpm
|
|
101
|
+
uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0
|
|
102
|
+
|
|
97
103
|
- name: Build release tool
|
|
98
104
|
# Runs scripts/build.js: builds the release plugin binary for this architecture as
|
|
99
105
|
# platforms/linux-<arch>/ExpoModulesMacros.
|
|
100
|
-
run:
|
|
106
|
+
run: pnpm run build
|
|
101
107
|
|
|
102
108
|
- name: Upload tool
|
|
103
109
|
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
|
@@ -130,6 +136,9 @@ jobs:
|
|
|
130
136
|
with:
|
|
131
137
|
node-version: 24
|
|
132
138
|
|
|
139
|
+
- name: Setup pnpm
|
|
140
|
+
uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0
|
|
141
|
+
|
|
133
142
|
- name: Download tool
|
|
134
143
|
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
|
|
135
144
|
with:
|
|
@@ -142,11 +151,11 @@ jobs:
|
|
|
142
151
|
# The `./` matters: npm reads `platforms/win32-<arch>` as a GitHub repository.
|
|
143
152
|
$pack = npm pack "./platforms/win32-${{ matrix.arch }}" --pack-destination $env:RUNNER_TEMP --json | ConvertFrom-Json
|
|
144
153
|
if ($LASTEXITCODE -ne 0) { throw "npm pack failed" }
|
|
145
|
-
|
|
154
|
+
pnpm install
|
|
146
155
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
147
|
-
|
|
156
|
+
pnpm add --save-optional (Join-Path $env:RUNNER_TEMP $pack[0].filename)
|
|
148
157
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
149
|
-
|
|
158
|
+
pnpm run build:ts
|
|
150
159
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
151
160
|
node scripts/check-scan.js .
|
|
152
161
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
@@ -178,6 +187,9 @@ jobs:
|
|
|
178
187
|
with:
|
|
179
188
|
node-version: 24
|
|
180
189
|
|
|
190
|
+
- name: Setup pnpm
|
|
191
|
+
uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0
|
|
192
|
+
|
|
181
193
|
- name: Download tool
|
|
182
194
|
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
|
|
183
195
|
with:
|
|
@@ -191,9 +203,9 @@ jobs:
|
|
|
191
203
|
# The `./` matters: npm reads `platforms/linux-<arch>` as a GitHub repository.
|
|
192
204
|
npm pack "./platforms/linux-${{ matrix.arch }}" --pack-destination "$RUNNER_TEMP" --json > "$RUNNER_TEMP/pack.json"
|
|
193
205
|
tarball="$(node -p 'JSON.parse(require("node:fs").readFileSync(process.argv[1], "utf8"))[0].filename' "$RUNNER_TEMP/pack.json")"
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
206
|
+
pnpm install
|
|
207
|
+
pnpm add --save-optional "$RUNNER_TEMP/$tarball"
|
|
208
|
+
pnpm run build:ts
|
|
197
209
|
|
|
198
210
|
- name: Scan on each distribution
|
|
199
211
|
run: |
|
|
@@ -233,6 +245,9 @@ jobs:
|
|
|
233
245
|
node-version: 24
|
|
234
246
|
registry-url: https://registry.npmjs.org
|
|
235
247
|
|
|
248
|
+
- name: Setup pnpm
|
|
249
|
+
uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0
|
|
250
|
+
|
|
236
251
|
- name: Update npm
|
|
237
252
|
# Trusted publishing requires npm >= 11.5.1.
|
|
238
253
|
run: npm install -g npm@latest
|
|
@@ -280,18 +295,18 @@ jobs:
|
|
|
280
295
|
|
|
281
296
|
- name: Install dependencies
|
|
282
297
|
# The wrapper's `prepublishOnly` build needs `tsc`; the tool binary needs none.
|
|
283
|
-
run:
|
|
298
|
+
run: pnpm install
|
|
284
299
|
|
|
285
300
|
- name: Build release tool
|
|
286
301
|
# Runs scripts/build.js: builds the release plugin binary,
|
|
287
302
|
# copies it to apple/ExpoModulesMacros, and strips it.
|
|
288
|
-
run:
|
|
303
|
+
run: pnpm run build
|
|
289
304
|
|
|
290
305
|
- name: Verify the macOS tool
|
|
291
306
|
# Scans through the TypeScript wrapper, which must find the binary in `apple/`, before
|
|
292
307
|
# anything is published. The verify-windows and verify-linux jobs check the other binaries.
|
|
293
308
|
run: |
|
|
294
|
-
|
|
309
|
+
pnpm run build:ts
|
|
295
310
|
node scripts/check-scan.js .
|
|
296
311
|
|
|
297
312
|
- name: Download Windows and Linux tools
|
|
@@ -37,6 +37,9 @@ jobs:
|
|
|
37
37
|
with:
|
|
38
38
|
node-version: 24
|
|
39
39
|
|
|
40
|
+
- name: Setup pnpm
|
|
41
|
+
uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0
|
|
42
|
+
|
|
40
43
|
- name: Restore swift-format
|
|
41
44
|
id: swift-format-cache
|
|
42
45
|
uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
|
|
@@ -73,4 +76,4 @@ jobs:
|
|
|
73
76
|
# The script lists the files with `git ls-files`. The checkout belongs to another user than
|
|
74
77
|
# the container's root user, so Git needs it marked as safe.
|
|
75
78
|
git config --global --add safe.directory "$GITHUB_WORKSPACE"
|
|
76
|
-
|
|
79
|
+
pnpm run swift:lint
|
|
@@ -24,6 +24,9 @@ jobs:
|
|
|
24
24
|
- name: Checkout
|
|
25
25
|
uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
|
|
26
26
|
|
|
27
|
+
- name: Setup pnpm
|
|
28
|
+
uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0
|
|
29
|
+
|
|
27
30
|
- name: Select Xcode
|
|
28
31
|
# Pin to an Xcode that ships Swift 6.2 (required by Package.swift).
|
|
29
32
|
run: sudo xcode-select -switch /Applications/Xcode_26.4.1.app
|
|
@@ -40,11 +43,11 @@ jobs:
|
|
|
40
43
|
run: swift package resolve
|
|
41
44
|
|
|
42
45
|
- name: Build release tool
|
|
43
|
-
# Runs `
|
|
46
|
+
# Runs `pnpm run build` (scripts/build.js): builds the universal
|
|
44
47
|
# (arm64 + x86_64) release plugin binary, copies it to
|
|
45
48
|
# apple/ExpoModulesMacros, and strips it.
|
|
46
49
|
working-directory: ${{ github.workspace }}
|
|
47
|
-
run:
|
|
50
|
+
run: pnpm run build
|
|
48
51
|
|
|
49
52
|
- name: Verify universal binary
|
|
50
53
|
run: |
|
|
@@ -64,5 +67,5 @@ jobs:
|
|
|
64
67
|
- name: Typecheck the TypeScript wrapper
|
|
65
68
|
working-directory: ${{ github.workspace }}
|
|
66
69
|
run: |
|
|
67
|
-
|
|
68
|
-
|
|
70
|
+
pnpm install
|
|
71
|
+
pnpm run typecheck
|
|
@@ -50,6 +50,9 @@ jobs:
|
|
|
50
50
|
with:
|
|
51
51
|
node-version: 24
|
|
52
52
|
|
|
53
|
+
- name: Setup pnpm
|
|
54
|
+
uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0
|
|
55
|
+
|
|
53
56
|
- name: Cache SwiftPM build
|
|
54
57
|
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
|
|
55
58
|
with:
|
|
@@ -65,10 +68,10 @@ jobs:
|
|
|
65
68
|
run: swift test
|
|
66
69
|
|
|
67
70
|
- name: Build release tool
|
|
68
|
-
# Runs `
|
|
71
|
+
# Runs `pnpm run build` (scripts/build.js): builds the release plugin binary for this
|
|
69
72
|
# architecture as platforms/win32-<arch>/ExpoModulesMacros.exe.
|
|
70
73
|
working-directory: ${{ github.workspace }}
|
|
71
|
-
run:
|
|
74
|
+
run: pnpm run build
|
|
72
75
|
|
|
73
76
|
- name: Verify the release tool
|
|
74
77
|
# Runs it with only the Windows directories on PATH, so it must not need the Swift runtime DLLs.
|
|
@@ -109,11 +112,11 @@ jobs:
|
|
|
109
112
|
working-directory: ${{ github.workspace }}
|
|
110
113
|
shell: pwsh
|
|
111
114
|
run: |
|
|
112
|
-
|
|
115
|
+
pnpm install
|
|
113
116
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
114
|
-
|
|
117
|
+
pnpm add --save-optional $env:PLATFORM_PACKAGE_TARBALL
|
|
115
118
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
116
|
-
|
|
119
|
+
pnpm run build:ts
|
|
117
120
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
118
121
|
node scripts/check-scan.js .
|
|
119
122
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
package/AGENTS.md
CHANGED
|
@@ -9,26 +9,27 @@
|
|
|
9
9
|
- `apple/Sources/ExpoModulesScanner`: the scanner library. `Modules/` implements `scan-modules` (autolinking), `Exports/` implements `scan-exports` (type generation), `Core/` holds the shared parsing and `#if` evaluation, and `CLI.swift` is the command-line front end.
|
|
10
10
|
- `apple/Sources/ExpoModulesOptimized`: declarations for the `@OptimizedFunction` macro.
|
|
11
11
|
- `apple/Tests`: `ExpoModulesMacrosTests` (expansion tests) and `ExpoModulesScannerTests`.
|
|
12
|
-
- `platforms/`: the Windows packages `@expo/modules-macros-win32-x64` and `-arm64`, and the Linux packages `@expo/modules-macros-linux-x64` and `-arm64` (a `package.json` with `os` and `cpu`, plus `libc` on Linux, a README and a copy of `LICENSE`). The Publish workflow runs `scripts/set-platform-versions.js` to give them the version of this package. Their `ExpoModulesMacros.exe` (Windows) or `ExpoModulesMacros` (Linux) is written there by `
|
|
12
|
+
- `platforms/`: the Windows packages `@expo/modules-macros-win32-x64` and `-arm64`, and the Linux packages `@expo/modules-macros-linux-x64` and `-arm64` (a `package.json` with `os` and `cpu`, plus `libc` on Linux, a README and a copy of `LICENSE`). The Publish workflow runs `scripts/set-platform-versions.js` to give them the version of this package. Their `ExpoModulesMacros.exe` (Windows) or `ExpoModulesMacros` (Linux) is written there by `pnpm run build` on that platform and isn't committed.
|
|
13
13
|
- `apple/PodTests`: a stub test so that `expo/expo` native tests can install this package as a pod. Do not add real tests here.
|
|
14
14
|
- `src/`: the TypeScript wrapper around the scanner CLI. `types.ts` mirrors the Swift `Codable` output types by hand.
|
|
15
|
-
- `scripts/build.js` (`
|
|
16
|
-
- `scripts/swift-format.js` (`
|
|
15
|
+
- `scripts/build.js` (`pnpm run build`): builds the release binaries: the universal macOS `apple/ExpoModulesMacros` (committed and published), and on Windows and Linux `platforms/win32-<arch>/ExpoModulesMacros.exe` or `platforms/linux-<arch>/ExpoModulesMacros` for the host architecture (not committed; published in that package).
|
|
16
|
+
- `scripts/swift-format.js` (`pnpm run swift:format`, `pnpm run swift:lint`): runs swift-format on the `.swift` files that Git tracks or doesn't ignore, with the configuration in `.swift-format`.
|
|
17
17
|
|
|
18
18
|
## Commands
|
|
19
19
|
|
|
20
|
-
Swift 6.2 is required (Xcode 26 or newer on macOS).
|
|
20
|
+
Swift 6.2 is required (Xcode 26 or newer on macOS). The package manager is pnpm, at the version in `packageManager` in `package.json`.
|
|
21
21
|
|
|
22
22
|
```sh
|
|
23
23
|
cd apple && swift build # debug build
|
|
24
24
|
cd apple && swift test # all Swift tests
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
pnpm install # TypeScript dependencies
|
|
26
|
+
pnpm run typecheck # TypeScript wrapper
|
|
27
|
+
pnpm run build # release binary (slow; on macOS uses Rosetta for x86_64)
|
|
28
|
+
pnpm run swift:format # format the Swift sources in place
|
|
29
|
+
pnpm run swift:lint # check the formatting, as CI does
|
|
29
30
|
```
|
|
30
31
|
|
|
31
|
-
CI runs on macOS (`.github/workflows/swift.yml`), on Windows x64 and arm64 (`.github/workflows/windows.yml`), and on Linux x64 and arm64 (`.github/workflows/linux.yml`). On macOS it runs the release build, checks both binary slices, then runs `swift test` and `
|
|
32
|
+
CI runs on macOS (`.github/workflows/swift.yml`), on Windows x64 and arm64 (`.github/workflows/windows.yml`), and on Linux x64 and arm64 (`.github/workflows/linux.yml`). On macOS it runs the release build, checks both binary slices, then runs `swift test` and `pnpm run typecheck`. On Windows it builds and tests the package, builds the release binary, checks that it starts without the Swift runtime, packs it into its `platforms/` package (checking the files and the `os`/`cpu` fields), installs that package, and scans through the TypeScript wrapper with `scripts/check-scan.js`, which must find the executable there. On Linux it does the same in the `swift:6.4.0-rhel-ubi9` image, and also checks that no Swift library is linked dynamically, the newest glibc symbol version that the binary needs, and that the package keeps the executable bit. The Swift Format workflow (`.github/workflows/swift-format.yml`) builds the pinned swift-format version and runs `pnpm run swift:lint`. Code in `Sources` and `Tests` must not assume Apple platforms or POSIX paths.
|
|
32
33
|
|
|
33
34
|
## Windows
|
|
34
35
|
|
|
@@ -42,7 +43,7 @@ CI runs on macOS (`.github/workflows/swift.yml`), on Windows x64 and arm64 (`.gi
|
|
|
42
43
|
|
|
43
44
|
## Conventions
|
|
44
45
|
|
|
45
|
-
- swift-format formats the Swift sources. After you change Swift code, run `
|
|
46
|
+
- swift-format formats the Swift sources. After you change Swift code, run `pnpm run swift:format` and then `pnpm run swift:lint`, which also reports the rules that the formatter doesn't fix. `.swift-format` is a copy of the configuration in `expo/expo`, so keep the two the same. Each repository pins its own swift-format version.
|
|
46
47
|
- The scripts run the swift-format on `PATH`, or on macOS the one in Xcode when there is none. CI builds the version in `SWIFT_FORMAT_VERSION` (`.github/workflows/swift-format.yml`, also named in the README), which can format differently from the one in Xcode. When CI reports a finding that the local lint doesn't, fix it as CI reports it.
|
|
47
48
|
- Write new doc comments as `///` line comments, not `/** */` blocks.
|
|
48
49
|
- Tests use Swift Testing (`import Testing`, `@Test`) with backtick-quoted descriptive names, for example ``func `Prunes .build, Pods, .git, and node_modules directories`()``.
|
|
@@ -68,6 +69,7 @@ CI runs on macOS (`.github/workflows/swift.yml`), on Windows x64 and arm64 (`.gi
|
|
|
68
69
|
|
|
69
70
|
- Do not rebuild or commit the binary under `apple/` in a feature change. Only the **Publish** workflow (`.github/workflows/publish.yml`, manual) rebuilds it, and it commits the result as `Release vX.Y.Z` after the npm publish succeeds. It also builds the Windows and Linux binaries and publishes them in the `platforms/` packages, with the same version. Before it publishes anything, it installs each binary from its packed package and scans through the TypeScript wrapper with `scripts/check-scan.js`.
|
|
70
71
|
- Do not edit the `version` in `package.json` by hand. The Publish workflow bumps it.
|
|
72
|
+
- The workflows install dependencies and run scripts with pnpm, but they pack, version and publish with npm (`npm pack`, `npm version`, `npm publish`), because the packages use npm trusted publishing (OIDC). Keep those commands on npm.
|
|
71
73
|
- Commit and PR titles are short and imperative, with code identifiers in backticks, for example ``Report `@Union` types in `scan-exports` ``.
|
|
72
74
|
|
|
73
75
|
## Installed copies
|
package/README.md
CHANGED
|
@@ -25,9 +25,9 @@ This package lists them as optional dependencies, and their `os`, `cpu` and (on
|
|
|
25
25
|
# Macros
|
|
26
26
|
|
|
27
27
|
- **`@ExpoModule(_ name: String? = nil, classes: [Any.Type] = [])`** on a class. Turns the class into a module: binds its `@JS` members into the module's JavaScript object and resolves the module name from the argument, falling back to the class name. It also synthesizes everything inheriting from `Module` used to provide, so a module class can carry any superclass, or none.
|
|
28
|
-
- **`@JS(_ jsName: String? = nil, _ options: JSOptions...)`** on a member of a module or shared object. Marks a function, property or initializer for export. `@ExpoModule` and `@SharedObject` bind each marked member straight into the JavaScript object, with the argument decoding, the call and the result encoding inlined, so there is no dynamic per-call path. The JS name defaults to the Swift name; pass a string to override it. Two `@JS` members with the same JS name on one JavaScript object are a compile-time error, since JavaScript could reach only one of them. A shared object's static and instance members bind onto different objects, so they can share a name. The macro also checks that every type crossing the boundary is convertible in the direction it travels, so a bad type is reported on the author's own declaration rather than on the enclosing type. The `.concurrent` option moves an `async` body off the JavaScript thread while still decoding and encoding on it. The `.withAsync("name")` option binds a synchronous function a second time under that name, as a function that returns a promise and runs the body on the concurrent thread pool. That function isn't isolated to the JavaScript thread, so its body must be safe to run on any thread. The macro reports a function isolated to a global actor through its own attribute or its type's. When the isolation comes from elsewhere, such as a protocol or the module's default isolation, the error points into the generated code, so mark the function `nonisolated`. A function or initializer can take a closure argument, such as `(Point) throws -> Size` or `(String) async throws -> Data`. JavaScript passes a function for it, and native code can store the closure and call it from any thread. A closure that throws or returns a value blocks the caller until JavaScript returns, and an `async` closure suspends instead and awaits a promise that the function returns. A closure that returns a value must be `throws`, so that an error from JavaScript can reach the caller. A non-throwing `Void` closure returns before JavaScript reads its arguments, so don't change a class instance after passing it to one.
|
|
28
|
+
- **`@JS(_ jsName: String? = nil, _ options: JSOptions...)`** on a member of a module or shared object. Marks a function, property or initializer for export. `@ExpoModule` and `@SharedObject` bind each marked member straight into the JavaScript object, with the argument decoding, the call and the result encoding inlined, so there is no dynamic per-call path. The JS name defaults to the Swift name; pass a string to override it. Two `@JS` members with the same JS name on one JavaScript object are a compile-time error, since JavaScript could reach only one of them. A shared object's static and instance members bind onto different objects, so they can share a name. The macro also checks that every type crossing the boundary is convertible in the direction it travels, so a bad type is reported on the author's own declaration rather than on the enclosing type. A `lazy var` is read-only from JavaScript. On the first read, its getter stores the value as a data property on the JavaScript object, so later reads don't call into native code. On a frozen object the value isn't stored, and each read calls the getter. The `.concurrent` option moves an `async` body off the JavaScript thread while still decoding and encoding on it. The `.withAsync("name")` option binds a synchronous function a second time under that name, as a function that returns a promise and runs the body on the concurrent thread pool. That function isn't isolated to the JavaScript thread, so its body must be safe to run on any thread. The macro reports a function isolated to a global actor through its own attribute or its type's. When the isolation comes from elsewhere, such as a protocol or the module's default isolation, the error points into the generated code, so mark the function `nonisolated`. A function or initializer can take a closure argument, such as `(Point) throws -> Size` or `(String) async throws -> Data`. JavaScript passes a function for it, and native code can store the closure and call it from any thread. A closure that throws or returns a value blocks the caller until JavaScript returns, and an `async` closure suspends instead and awaits a promise that the function returns. A closure that returns a value must be `throws`, so that an error from JavaScript can reach the caller. A non-throwing `Void` closure returns before JavaScript reads its arguments, so don't change a class instance after passing it to one.
|
|
29
29
|
- **`@Event(_ name: String? = nil, sync: Bool = false)`** on a function-typed `var`. Expands the property into a closure that emits the event, so calling the property sends it to JavaScript with the closure's parameter as the payload. The JS name defaults to the property name with a leading `on` stripped. `sync: true` dispatches inline instead of asynchronously.
|
|
30
|
-
- **`@SharedObject(_ name: String? = nil)`** on a `SharedObject` subclass.
|
|
30
|
+
- **`@SharedObject(_ name: String? = nil)`** on a subclass of `SharedObject`, direct or not, such as a `SharedRef<T>` subclass. Binds the class's `@JS` members through the `SharedObject` hooks that core calls when it builds the JavaScript class, and emits the JS class name, which defaults to the class name. A module exposes the class through `@ExpoModule(classes:)`, which only accepts `SharedObject` subclasses.
|
|
31
31
|
- **`@Record()`** on a struct, or on a final class with no inheritance clause (declare a class's protocol conformances in an extension). Treats every non-static, non-private, non-computed stored property as a field, with no per-field wrapper, and synthesizes the memberwise initializer, the conversions in both directions, and the `Record` conformance. Requiredness is inferred: a default value makes a field optional, an optional type makes it nullable and optional.
|
|
32
32
|
- **`@Union()`** on an enum whose cases each carry one associated value. Models a TypeScript union. Synthesizes the conversions in both directions, a typed accessor per payload type, and the `JavaScriptDecodable` and `JavaScriptEncodable` conformances. Decoding is ordered: the first case whose payload decodes wins, so the more specific case goes first.
|
|
33
33
|
|
|
@@ -102,7 +102,7 @@ The binary is a compiled executable, so each call still spawns a process. What t
|
|
|
102
102
|
|
|
103
103
|
to `OTHER_SWIFT_FLAGS` for `ExpoModulesCore`, every pod that depends on it, and their test specs. Expo's SPM prebuilds pass the same flag when they generate `Package.swift`, so both build systems load the same binary.
|
|
104
104
|
|
|
105
|
-
On Windows and Linux, the compiler loads `ExpoModulesMacros.exe` from `@expo/modules-macros-win32-<arch>` or `ExpoModulesMacros` from `@expo/modules-macros-linux-<arch>` with the same flag. `getScannerBinaryPath()` in the TypeScript wrapper returns the binary for the current platform and architecture. On Windows and Linux it falls back to the local `
|
|
105
|
+
On Windows and Linux, the compiler loads `ExpoModulesMacros.exe` from `@expo/modules-macros-win32-<arch>` or `ExpoModulesMacros` from `@expo/modules-macros-linux-<arch>` with the same flag. `getScannerBinaryPath()` in the TypeScript wrapper returns the binary for the current platform and architecture. On Windows and Linux it falls back to the local `pnpm run build` output in `platforms/<platform>-<arch>` when the platform package isn't installed, for example in this repository.
|
|
106
106
|
|
|
107
107
|
The module and type names in `#externalMacro` must stay in sync with `apple/Sources/ExpoModulesMacros/Plugin.swift`.
|
|
108
108
|
|
|
@@ -116,7 +116,9 @@ swift build
|
|
|
116
116
|
swift test
|
|
117
117
|
```
|
|
118
118
|
|
|
119
|
-
The
|
|
119
|
+
The scripts and the TypeScript wrapper use [pnpm](https://pnpm.io), at the version in `packageManager` in `package.json`. Run `pnpm install` to install the TypeScript dependencies, and `pnpm run typecheck` to check the wrapper.
|
|
120
|
+
|
|
121
|
+
The Swift sources are formatted with [swift-format](https://github.com/swiftlang/swift-format), using the same configuration (`.swift-format`) as the `expo/expo` repository. `pnpm run swift:format` formats the `.swift` files that Git tracks or doesn't ignore in place, and `pnpm run swift:lint` checks them without changes, as CI does.
|
|
120
122
|
|
|
121
123
|
The scripts run the `swift-format` on `PATH`, or on macOS the one in the selected Xcode when there is none on `PATH`. CI uses swift-format `604.0.0`, which can format differently from the one in Xcode. To get the version that CI uses, build it from source:
|
|
122
124
|
|
|
@@ -129,7 +131,7 @@ cp "$(cd /tmp/swift-format && swift build -c release --show-bin-path)/swift-form
|
|
|
129
131
|
|
|
130
132
|
Then add `~/.local/bin` to `PATH`.
|
|
131
133
|
|
|
132
|
-
`
|
|
134
|
+
`pnpm run build` runs `scripts/build.js`, which builds the release binary with SwiftPM's native build system (Swift Build, the default since Swift 6.4, doesn't build a macro tool that no target in the package uses).
|
|
133
135
|
|
|
134
136
|
- On macOS, it builds for arm64 and x86_64, merges the slices into `apple/ExpoModulesMacros` with `lipo`, strips it, and verifies both slices are present. SwiftPM only builds macro tools for the host architecture, so the x86_64 slice is produced by running the toolchain under Rosetta; the script installs Rosetta if it is missing.
|
|
135
137
|
- On Windows, it builds for the host architecture only, with the Swift runtime linked in statically and without debug info, and writes `platforms/win32-<arch>/ExpoModulesMacros.exe` (`x64` or `arm64`, as Node's `process.arch` names them), the file that the package for that architecture publishes. It strips the executable with `llvm-strip` from the Swift toolchain and checks the architecture in its header.
|
package/apple/ExpoModulesMacros
CHANGED
|
Binary file
|
|
@@ -363,6 +363,13 @@ internal struct JSFunction {
|
|
|
363
363
|
/// writes `<callee>.<name> = …`. Decode/encode of the value go through `JavaScriptDecodable.decode` /
|
|
364
364
|
/// `JavaScriptEncodable.encode` (or the free-form methods for a type that mentions `Any`), the same
|
|
365
365
|
/// path as functions.
|
|
366
|
+
///
|
|
367
|
+
/// A `lazy var` binds a getter only, on a configurable accessor. On the first read the getter defines
|
|
368
|
+
/// the encoded value as a data property on the JS `this` with `defineProperty`, so later reads don't
|
|
369
|
+
/// call into native code. The data property goes on `this`, not on the object that holds the accessor,
|
|
370
|
+
/// so each shared object instance stores its own value. `defineProperty` leaves a frozen object
|
|
371
|
+
/// unchanged, so there the property stays an accessor. A `this` that isn't an object doesn't fail the read
|
|
372
|
+
/// either: the getter still returns the value.
|
|
366
373
|
internal struct JSProperty {
|
|
367
374
|
let swiftName: String
|
|
368
375
|
let jsName: String
|
|
@@ -373,8 +380,10 @@ internal struct JSProperty {
|
|
|
373
380
|
/// How the value converts when its type mentions `Any`, or `nil` for any other type.
|
|
374
381
|
let freeForm: FreeFormConversion?
|
|
375
382
|
/// Whether the property is settable from JS: `true` for a stored `var` or a computed `var` with an
|
|
376
|
-
/// explicit `set` accessor; `false` for a getter-only computed `var` or a `let`.
|
|
383
|
+
/// explicit `set` accessor; `false` for a getter-only computed `var`, a `lazy var` or a `let`.
|
|
377
384
|
let isSettable: Bool
|
|
385
|
+
/// Whether the property is a `lazy var`, whose getter replaces the accessor with the first value.
|
|
386
|
+
let isLazy: Bool
|
|
378
387
|
/// The property's name in the source, where a JavaScript name collision is reported.
|
|
379
388
|
let nameToken: TokenSyntax
|
|
380
389
|
|
|
@@ -395,18 +404,36 @@ internal struct JSProperty {
|
|
|
395
404
|
|
|
396
405
|
lines.append("let \(descriptorName) = runtime.createObject()")
|
|
397
406
|
lines.append("\(descriptorName).setProperty(\"enumerable\", value: true)")
|
|
407
|
+
if isLazy {
|
|
408
|
+
// The getter redefines the property on `this`, which needs a configurable accessor when `this`
|
|
409
|
+
// is the object that holds it (a module).
|
|
410
|
+
lines.append("\(descriptorName).setProperty(\"configurable\", value: true)")
|
|
411
|
+
}
|
|
398
412
|
|
|
399
413
|
// Getter: read `<callee>.<name>` and encode the result back to JS through `encode`. When the value
|
|
400
414
|
// type couldn't be inferred (no annotation and no literal default, rare for a stored var) there's
|
|
401
415
|
// no static type to call `encode` on, so the value's own `toJavaScriptValue(in:)` is the fallback.
|
|
402
|
-
let
|
|
416
|
+
let encode: String
|
|
403
417
|
if let valueType {
|
|
404
|
-
|
|
418
|
+
encode = "try \(encodeCall(valueType, freeForm: freeForm, of: "\(callee).\(swiftName)"))"
|
|
419
|
+
} else {
|
|
420
|
+
encode = "\(callee).\(swiftName).toJavaScriptValue(in: runtime)"
|
|
421
|
+
}
|
|
422
|
+
let getBody: String
|
|
423
|
+
if isLazy {
|
|
424
|
+
let define = "this.getObject(in: runtime).defineProperty(\"\(jsName)\", value: value, options: [.enumerable])"
|
|
425
|
+
// A module getter ignores `this` otherwise, so it can be anything, and only an object can hold the
|
|
426
|
+
// value. A shared object's unwrap has already checked that `this` is an object.
|
|
427
|
+
let defineStatement =
|
|
428
|
+
receiver.unwrapStatement == nil
|
|
429
|
+
? "if this.isObject() {\n \(define)\n}"
|
|
430
|
+
: define
|
|
431
|
+
getBody = "let value = \(encode)\n\(defineStatement)\nreturn value"
|
|
405
432
|
} else {
|
|
406
|
-
|
|
433
|
+
getBody = "return \(encode)"
|
|
407
434
|
}
|
|
408
435
|
lines.append(
|
|
409
|
-
accessorClosure(descriptorName, "get", receiver: receiver, body: "\(unwrap)\(
|
|
436
|
+
accessorClosure(descriptorName, "get", receiver: receiver, body: "\(unwrap)\(getBody)")
|
|
410
437
|
)
|
|
411
438
|
|
|
412
439
|
// Setter: decode argument 0 by the static type through `decode` and write `<callee>.<name>`. A
|
|
@@ -80,18 +80,19 @@ extension EventMacro: PeerMacro {
|
|
|
80
80
|
return []
|
|
81
81
|
}
|
|
82
82
|
// One nested helper asserts everything in a single call: the payload type is JS-convertible
|
|
83
|
-
// (the
|
|
84
|
-
// enclosing type can emit (the
|
|
83
|
+
// (the first parameter, dropped for no-payload events and known-conforming primitives) and the
|
|
84
|
+
// enclosing type can emit (the last parameter, always present since any `@Event` dispatches
|
|
85
85
|
// through `self.emit`). Named after the member so the member shows up in either diagnostic,
|
|
86
86
|
// and asserting the enclosing type by its spelled name so the conformance error names the
|
|
87
87
|
// user's type rather than 'Self'.
|
|
88
88
|
let name = event.swiftName
|
|
89
89
|
let payload = event.payloadType.flatMap { assertableBoundaryType($0, constraint: jsConvertibleProtocolName) }
|
|
90
90
|
let owner = enclosingTypeName(in: context) ?? "Self"
|
|
91
|
+
let emitterParameter = opaqueMetatypeParameter(constraint: eventEmitterProtocolName)
|
|
91
92
|
let helper =
|
|
92
93
|
payload != nil
|
|
93
|
-
? "func \(name)
|
|
94
|
-
: "func \(name)
|
|
94
|
+
? "func \(name)(\(opaqueMetatypeParameter(constraint: jsConvertibleProtocolName)), \(emitterParameter)) {}"
|
|
95
|
+
: "func \(name)(\(emitterParameter)) {}"
|
|
95
96
|
let call = payload.map { "\(name)(\($0).self, \(owner).self)" } ?? "\(name)(\(owner).self)"
|
|
96
97
|
let peerName = derivedIdentifier(from: name, prefix: "_assertTypesConformance_")
|
|
97
98
|
return [
|
|
@@ -7,10 +7,9 @@ import SwiftSyntaxMacros
|
|
|
7
7
|
///
|
|
8
8
|
/// - `MemberMacro`: binds `@JS`-marked members directly into the module's JS object via the
|
|
9
9
|
/// synthesized `_decorateModule`, and emits the resolved module name as a non-optional
|
|
10
|
-
/// `_jsName` static (no `Name(…)` DSL element).
|
|
11
|
-
///
|
|
12
|
-
///
|
|
13
|
-
/// definition. When the class doesn't already inherit `Module`/`BaseModule`, it also
|
|
10
|
+
/// `_jsName` static (no `Name(…)` DSL element). The `classes:` and `views:` arguments become
|
|
11
|
+
/// `_classTypes` and `_viewTypes` statics that `expo-modules-core` registers by type.
|
|
12
|
+
/// When the class doesn't already inherit `Module`/`BaseModule`, it also
|
|
14
13
|
/// synthesizes the `appContext` storage and `init(appContext:)` those base classes provide.
|
|
15
14
|
/// - `MemberAttributeMacro`: stamps `@JavaScriptActor` on `@JS` sync members and
|
|
16
15
|
/// `@ModuleDefinitionBuilder` on a `definition()` method.
|
|
@@ -44,18 +43,13 @@ public struct ExpoModuleMacro: MemberMacro {
|
|
|
44
43
|
// macro no longer emits a `Name(…)` DSL entry. Core reads the static for the module name,
|
|
45
44
|
// ahead of its type-name fallback (which then only applies to non-macro DSL modules).
|
|
46
45
|
let moduleName = jsNameArgument(of: node) ?? withoutBackticks(classDecl.name.text)
|
|
47
|
-
var entries: [String] = []
|
|
48
46
|
|
|
49
47
|
// `@JS func`s (sync and async) and `@JS var`s are bound directly into the JS object by the
|
|
50
48
|
// synthesized `_decorateModule` rather than described with a `Function(...)` / `Property(...)`
|
|
51
|
-
// DSL entry
|
|
49
|
+
// DSL entry.
|
|
52
50
|
var functions: [JSFunction] = []
|
|
53
51
|
var properties: [JSProperty] = []
|
|
54
52
|
|
|
55
|
-
for typeName in classListArgument(of: node, label: "classes") {
|
|
56
|
-
entries.append("\(typeName)._synthesizedClassDefinition()")
|
|
57
|
-
}
|
|
58
|
-
|
|
59
53
|
for member in classDecl.memberBlock.members {
|
|
60
54
|
let decl = member.decl
|
|
61
55
|
|
|
@@ -79,14 +73,6 @@ public struct ExpoModuleMacro: MemberMacro {
|
|
|
79
73
|
|
|
80
74
|
diagnoseJSNameCollisions(functions: functions, properties: properties, in: context)
|
|
81
75
|
|
|
82
|
-
let body: String
|
|
83
|
-
if entries.isEmpty {
|
|
84
|
-
body = " return []"
|
|
85
|
-
} else {
|
|
86
|
-
let lines = entries.map { " \($0)" }.joined(separator: ",\n")
|
|
87
|
-
body = " return [\n\(lines)\n ]"
|
|
88
|
-
}
|
|
89
|
-
|
|
90
76
|
var emitted: [DeclSyntax] = []
|
|
91
77
|
|
|
92
78
|
// The fully-resolved module name as a non-optional stored constant. Core reads this
|
|
@@ -94,6 +80,16 @@ public struct ExpoModuleMacro: MemberMacro {
|
|
|
94
80
|
// JS object name, so they can't diverge.
|
|
95
81
|
emitted.append("public static let _jsName = \"\(raw: moduleName)\"")
|
|
96
82
|
|
|
83
|
+
// Shared object classes are registered by type: core builds each JavaScript class from the
|
|
84
|
+
// `@SharedObject` hooks, with the name from the class's `_jsName`. The element type makes the
|
|
85
|
+
// compiler reject a class that isn't a `SharedObject` subclass. Emitted only when the argument is
|
|
86
|
+
// present, so a module without classes gains nothing.
|
|
87
|
+
let classTypes = classListArgument(of: node, label: "classes")
|
|
88
|
+
if !classTypes.isEmpty {
|
|
89
|
+
let entries = classTypes.map { "\($0).self" }.joined(separator: ", ")
|
|
90
|
+
emitted.append("public static let _classTypes: [SharedObject.Type] = [\(raw: entries)]")
|
|
91
|
+
}
|
|
92
|
+
|
|
97
93
|
// View classes are registered by type, not spliced as definitions: an `@ExpoView` emits no
|
|
98
94
|
// definition function, so there is nothing to call on each entry. Core reads a view's props
|
|
99
95
|
// type from its `Props` typealias and its event names from that type's `_eventNames`, both
|
|
@@ -123,15 +119,6 @@ public struct ExpoModuleMacro: MemberMacro {
|
|
|
123
119
|
}
|
|
124
120
|
}
|
|
125
121
|
|
|
126
|
-
// Always emitted: the definition is derived from the class name and `@JS` members,
|
|
127
|
-
// independent of any `definition()` the user wrote.
|
|
128
|
-
let method: DeclSyntax = """
|
|
129
|
-
public func _synthesizedDefinition() -> [AnyDefinition] {
|
|
130
|
-
\(raw: body)
|
|
131
|
-
}
|
|
132
|
-
"""
|
|
133
|
-
emitted.append(method)
|
|
134
|
-
|
|
135
122
|
// Direct JSI binding: one `_decorateModule` that binds each `@JS func` (inlined `setProperty`
|
|
136
123
|
// closure) and each `@JS var` (a `defineProperty` get/set accessor) into the module's JS object.
|
|
137
124
|
// Only emitted when there's at least one member to bind.
|
|
@@ -386,15 +386,15 @@ private func boundaryMember(of declaration: some DeclSyntaxProtocol) -> Boundary
|
|
|
386
386
|
let type = binding.typeAnnotation?.type,
|
|
387
387
|
!containsFunctionType(type)
|
|
388
388
|
{
|
|
389
|
-
// A `let`, or a `var` with no setter
|
|
390
|
-
// decoded on write, so its value type is asserted in both directions. A type that mentions
|
|
391
|
-
// converts through the free-form methods instead, so it isn't asserted at all.
|
|
389
|
+
// A `let`, a `lazy var`, or a `var` with no setter is read-only (encodable only). A settable `var`
|
|
390
|
+
// is also decoded on write, so its value type is asserted in both directions. A type that mentions
|
|
391
|
+
// `Any` converts through the free-form methods instead, so it isn't asserted at all.
|
|
392
392
|
guard !mentionsFreeFormAny(type) else {
|
|
393
393
|
return nil
|
|
394
394
|
}
|
|
395
395
|
let typeText = type.trimmedDescription
|
|
396
396
|
let isVar = varDecl.bindingSpecifier.tokenKind == .keyword(.var)
|
|
397
|
-
let isSettable = isVar && bindingIsSettable(binding)
|
|
397
|
+
let isSettable = isVar && !isLazy(varDecl.modifiers) && bindingIsSettable(binding)
|
|
398
398
|
return BoundaryMember(
|
|
399
399
|
name: identifier.identifier.text,
|
|
400
400
|
decodableTypes: isSettable ? [typeText] : [],
|
|
@@ -270,6 +270,12 @@ internal func isTypeLevel(_ modifiers: DeclModifierListSyntax) -> Bool {
|
|
|
270
270
|
}
|
|
271
271
|
}
|
|
272
272
|
|
|
273
|
+
/// True for a `lazy` property. A lazy `@JS` property is read-only from JS: its getter stores the
|
|
274
|
+
/// first value as a data property on the JS object, which a setter couldn't write through.
|
|
275
|
+
internal func isLazy(_ modifiers: DeclModifierListSyntax) -> Bool {
|
|
276
|
+
return modifiers.contains { $0.name.tokenKind == .keyword(.lazy) }
|
|
277
|
+
}
|
|
278
|
+
|
|
273
279
|
/// True if a trailing occurrence of this parameter may be omitted by the JS caller: it either has a
|
|
274
280
|
/// default value (Swift applies it) or is an optional type (an absent slot becomes `nil`). The arity
|
|
275
281
|
/// range and the per-arity call branches are derived from this.
|
|
@@ -545,6 +551,7 @@ internal func collectProperties(
|
|
|
545
551
|
let jsNameOverride = jsNameArgument(of: attribute)
|
|
546
552
|
// A `let` is never settable; only `var` bindings can carry a setter.
|
|
547
553
|
let isVar = varDecl.bindingSpecifier.tokenKind == .keyword(.var)
|
|
554
|
+
let isLazy = isLazy(varDecl.modifiers)
|
|
548
555
|
|
|
549
556
|
return varDecl.bindings.compactMap { binding in
|
|
550
557
|
guard let ident = binding.pattern.as(IdentifierPatternSyntax.self) else {
|
|
@@ -562,7 +569,8 @@ internal func collectProperties(
|
|
|
562
569
|
valueType: valueType,
|
|
563
570
|
// A literal-inferred type never mentions `Any`, so only an explicit annotation can be free-form.
|
|
564
571
|
freeForm: binding.typeAnnotation.flatMap { freeFormConversion(of: $0.type) },
|
|
565
|
-
isSettable: isVar && bindingIsSettable(binding),
|
|
572
|
+
isSettable: isVar && !isLazy && bindingIsSettable(binding),
|
|
573
|
+
isLazy: isLazy,
|
|
566
574
|
nameToken: ident.identifier
|
|
567
575
|
)
|
|
568
576
|
}
|