expo-modules-macros 0.19.1 → 0.21.0
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 +132 -0
- package/.github/workflows/publish.yml +160 -11
- package/.github/workflows/windows.yml +3 -28
- package/AGENTS.md +11 -6
- package/README.md +15 -9
- package/apple/ExpoModulesMacros +0 -0
- package/apple/Sources/ExpoModulesMacros/ClosureArguments.swift +1 -1
- package/apple/Sources/ExpoModulesMacros/DecorateModuleBuilder.swift +23 -10
- package/apple/Sources/ExpoModulesMacros/EventMacro.swift +12 -1
- package/apple/Sources/ExpoModulesMacros/ExpoModuleMacro.swift +2 -0
- package/apple/Sources/ExpoModulesMacros/JSNameCollisions.swift +64 -0
- package/apple/Sources/ExpoModulesMacros/MacroHelpers.swift +2 -1
- package/apple/Sources/ExpoModulesMacros/RecordMacro.swift +176 -91
- package/apple/Sources/ExpoModulesMacros/SharedObjectMacro.swift +3 -0
- package/apple/Sources/ExpoModulesMacros/UnionMacro.swift +14 -1
- package/apple/Sources/ExpoModulesScanner/Exports/Availability.swift +259 -0
- package/apple/Sources/ExpoModulesScanner/Exports/ExportedSurface.swift +151 -32
- package/apple/Sources/ExpoModulesScanner/Exports/ResolveRefs.swift +48 -65
- package/apple/Sources/ExpoModulesScanner/Exports/ScanExports.swift +1 -1
- package/apple/Sources/ExpoModulesScanner/Exports/SurfaceVisitor.swift +179 -46
- package/build/index.d.ts +16 -6
- package/build/index.js +51 -28
- package/build/types.d.ts +86 -19
- package/build/types.js +1 -1
- package/package.json +5 -3
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
name: Linux
|
|
2
|
+
|
|
3
|
+
# Builds and tests the package on Linux, and checks the release tool and its package there.
|
|
4
|
+
|
|
5
|
+
on:
|
|
6
|
+
push:
|
|
7
|
+
branches: [main]
|
|
8
|
+
pull_request:
|
|
9
|
+
branches: [main]
|
|
10
|
+
workflow_dispatch: {}
|
|
11
|
+
|
|
12
|
+
concurrency:
|
|
13
|
+
group: ci-${{ github.workflow }}-${{ github.ref }}
|
|
14
|
+
cancel-in-progress: true
|
|
15
|
+
|
|
16
|
+
defaults:
|
|
17
|
+
run:
|
|
18
|
+
working-directory: apple
|
|
19
|
+
shell: bash
|
|
20
|
+
|
|
21
|
+
jobs:
|
|
22
|
+
linux:
|
|
23
|
+
name: Build & test (Linux ${{ matrix.arch }})
|
|
24
|
+
runs-on: ${{ matrix.runner }}
|
|
25
|
+
# The release tool needs the glibc version that it's built against or a newer one. Red Hat UBI 9
|
|
26
|
+
# has glibc 2.34, the oldest version that the package supports. The Publish workflow builds the
|
|
27
|
+
# tool in the same image.
|
|
28
|
+
container: swift:6.4.0-rhel-ubi9@sha256:802a003f1eacbca9c20664a8ba79621d13f4538c3d329e745b7f8d53c70604a3
|
|
29
|
+
# The limit stops a hang, for example in the plugin server, from holding a runner for the default
|
|
30
|
+
# 6 hours.
|
|
31
|
+
timeout-minutes: 30
|
|
32
|
+
env:
|
|
33
|
+
# The newest glibc symbol version that the release tool may need. The READMEs of the Linux
|
|
34
|
+
# packages name it.
|
|
35
|
+
MAX_GLIBC_VERSION: '2.34'
|
|
36
|
+
strategy:
|
|
37
|
+
fail-fast: false
|
|
38
|
+
matrix:
|
|
39
|
+
include:
|
|
40
|
+
- arch: x64
|
|
41
|
+
runner: ubuntu-24.04
|
|
42
|
+
- arch: arm64
|
|
43
|
+
runner: ubuntu-24.04-arm
|
|
44
|
+
steps:
|
|
45
|
+
- name: Checkout
|
|
46
|
+
uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
|
|
47
|
+
|
|
48
|
+
- name: Setup Node
|
|
49
|
+
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
|
|
50
|
+
with:
|
|
51
|
+
node-version: 24
|
|
52
|
+
|
|
53
|
+
- name: Cache SwiftPM build
|
|
54
|
+
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
|
|
55
|
+
with:
|
|
56
|
+
path: apple/.build
|
|
57
|
+
key: spm-${{ github.repository }}-${{ runner.os }}-${{ matrix.arch }}-${{ hashFiles('apple/Package.resolved') }}
|
|
58
|
+
restore-keys: |
|
|
59
|
+
spm-${{ github.repository }}-${{ runner.os }}-${{ matrix.arch }}-
|
|
60
|
+
|
|
61
|
+
- name: Build
|
|
62
|
+
run: swift build
|
|
63
|
+
|
|
64
|
+
- name: Test
|
|
65
|
+
run: swift test
|
|
66
|
+
|
|
67
|
+
- name: Build release tool
|
|
68
|
+
# Runs `npm run build` (scripts/build.js): builds the release plugin binary for this
|
|
69
|
+
# architecture as platforms/linux-<arch>/ExpoModulesMacros.
|
|
70
|
+
working-directory: ${{ github.workspace }}
|
|
71
|
+
run: npm run build
|
|
72
|
+
|
|
73
|
+
- name: Verify the release tool
|
|
74
|
+
# With no arguments it is the plugin server, which exits when stdin closes; with arguments it
|
|
75
|
+
# is the scanner CLI.
|
|
76
|
+
run: |
|
|
77
|
+
tool="../platforms/linux-${{ matrix.arch }}/ExpoModulesMacros"
|
|
78
|
+
# The Swift runtime must be linked in statically, so no Swift library may be a dependency.
|
|
79
|
+
needed="$(readelf --dynamic "$tool" | sed -n 's/.*(NEEDED).*\[\(.*\)\]$/\1/p')"
|
|
80
|
+
echo "Needed libraries:" $needed
|
|
81
|
+
if echo "$needed" | grep -E 'swift|Foundation|dispatch|BlocksRuntime'; then
|
|
82
|
+
echo "The release tool links a Swift library dynamically"
|
|
83
|
+
exit 1
|
|
84
|
+
fi
|
|
85
|
+
glibc="$(objdump -T "$tool" | grep -o 'GLIBC_[0-9][0-9.]*' | sed 's/^GLIBC_//' | sort -uV | tail -n 1)"
|
|
86
|
+
echo "Newest glibc symbol version: $glibc"
|
|
87
|
+
if [ "$(printf '%s\n' "$glibc" "$MAX_GLIBC_VERSION" | sort -V | tail -n 1)" != "$MAX_GLIBC_VERSION" ]; then
|
|
88
|
+
echo "The release tool needs glibc $glibc, newer than $MAX_GLIBC_VERSION"
|
|
89
|
+
exit 1
|
|
90
|
+
fi
|
|
91
|
+
"$tool" < /dev/null
|
|
92
|
+
"$tool" --help > /dev/null
|
|
93
|
+
|
|
94
|
+
- name: Pack the platform package
|
|
95
|
+
# Checks what `npm pack` would publish from `platforms/linux-<arch>`, where the release build
|
|
96
|
+
# put the tool: the executable with its executable bit, the README, the license and the
|
|
97
|
+
# manifest, with the `os`, `cpu` and `libc` fields for this machine.
|
|
98
|
+
working-directory: ${{ github.workspace }}
|
|
99
|
+
run: |
|
|
100
|
+
# The `./` matters: npm reads `platforms/linux-<arch>` as a GitHub repository.
|
|
101
|
+
npm pack "./platforms/linux-${{ matrix.arch }}" --pack-destination "$RUNNER_TEMP" --json > "$RUNNER_TEMP/pack.json"
|
|
102
|
+
node - "$RUNNER_TEMP/pack.json" "${{ matrix.arch }}" <<'EOF'
|
|
103
|
+
const fs = require('node:fs');
|
|
104
|
+
const [packPath, arch] = process.argv.slice(2);
|
|
105
|
+
const [pack] = JSON.parse(fs.readFileSync(packPath, 'utf8'));
|
|
106
|
+
const files = pack.files.map((file) => file.path).sort().join(',');
|
|
107
|
+
console.log(`${pack.filename}: ${files} (${pack.size} bytes packed)`);
|
|
108
|
+
if (files !== 'ExpoModulesMacros,LICENSE,README.md,package.json') {
|
|
109
|
+
throw new Error(`Unexpected files: ${files}`);
|
|
110
|
+
}
|
|
111
|
+
const tool = pack.files.find((file) => file.path === 'ExpoModulesMacros');
|
|
112
|
+
if ((tool.mode & 0o111) === 0) {
|
|
113
|
+
throw new Error(`ExpoModulesMacros is packed without the executable bit (mode ${tool.mode.toString(8)})`);
|
|
114
|
+
}
|
|
115
|
+
const manifest = JSON.parse(fs.readFileSync(`platforms/linux-${arch}/package.json`, 'utf8'));
|
|
116
|
+
const fields = [manifest.os, manifest.cpu, manifest.libc].join(' / ');
|
|
117
|
+
if (fields !== `linux / ${arch} / glibc`) {
|
|
118
|
+
throw new Error(`Unexpected os / cpu / libc: ${fields}`);
|
|
119
|
+
}
|
|
120
|
+
fs.appendFileSync(process.env.GITHUB_ENV, `PLATFORM_PACKAGE_TARBALL=${process.env.RUNNER_TEMP}/${pack.filename}\n`);
|
|
121
|
+
EOF
|
|
122
|
+
|
|
123
|
+
- name: Scan through the TypeScript wrapper
|
|
124
|
+
# Installs the packed platform package. The wrapper must find the executable in
|
|
125
|
+
# `@expo/modules-macros-linux-<arch>`, as it would in a project, not the local build.
|
|
126
|
+
working-directory: ${{ github.workspace }}
|
|
127
|
+
run: |
|
|
128
|
+
npm install
|
|
129
|
+
npm install --no-save "$PLATFORM_PACKAGE_TARBALL"
|
|
130
|
+
npm run build:ts
|
|
131
|
+
node scripts/check-scan.js .
|
|
132
|
+
|
|
@@ -70,11 +70,147 @@ jobs:
|
|
|
70
70
|
path: platforms/win32-${{ matrix.arch }}/ExpoModulesMacros.exe
|
|
71
71
|
if-no-files-found: error
|
|
72
72
|
|
|
73
|
+
build-linux:
|
|
74
|
+
name: Build Linux tool (${{ matrix.arch }})
|
|
75
|
+
# SwiftPM builds macro tools only for the host, so each architecture builds on a runner of its own.
|
|
76
|
+
runs-on: ${{ matrix.runner }}
|
|
77
|
+
# The tool needs the glibc version that it's built against or a newer one. Red Hat UBI 9 has
|
|
78
|
+
# glibc 2.34, the oldest version that the package supports. The Linux workflow uses the same image.
|
|
79
|
+
container: swift:6.4.0-rhel-ubi9@sha256:802a003f1eacbca9c20664a8ba79621d13f4538c3d329e745b7f8d53c70604a3
|
|
80
|
+
timeout-minutes: 30
|
|
81
|
+
strategy:
|
|
82
|
+
matrix:
|
|
83
|
+
include:
|
|
84
|
+
- arch: x64
|
|
85
|
+
runner: ubuntu-24.04
|
|
86
|
+
- arch: arm64
|
|
87
|
+
runner: ubuntu-24.04-arm
|
|
88
|
+
steps:
|
|
89
|
+
- name: Checkout
|
|
90
|
+
uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
|
|
91
|
+
|
|
92
|
+
- name: Setup Node
|
|
93
|
+
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
|
|
94
|
+
with:
|
|
95
|
+
node-version: 24
|
|
96
|
+
|
|
97
|
+
- name: Build release tool
|
|
98
|
+
# Runs scripts/build.js: builds the release plugin binary for this architecture as
|
|
99
|
+
# platforms/linux-<arch>/ExpoModulesMacros.
|
|
100
|
+
run: npm run build
|
|
101
|
+
|
|
102
|
+
- name: Upload tool
|
|
103
|
+
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
|
104
|
+
with:
|
|
105
|
+
# Named after the package directory, like the Windows artifacts.
|
|
106
|
+
name: linux-${{ matrix.arch }}
|
|
107
|
+
path: platforms/linux-${{ matrix.arch }}/ExpoModulesMacros
|
|
108
|
+
if-no-files-found: error
|
|
109
|
+
|
|
110
|
+
verify-windows:
|
|
111
|
+
name: Verify Windows tool (${{ matrix.arch }})
|
|
112
|
+
needs: build-windows
|
|
113
|
+
# Installs the release tool from its packed package and scans through the TypeScript wrapper,
|
|
114
|
+
# before anything is published. The runner has no Swift toolchain.
|
|
115
|
+
runs-on: ${{ matrix.runner }}
|
|
116
|
+
timeout-minutes: 15
|
|
117
|
+
strategy:
|
|
118
|
+
matrix:
|
|
119
|
+
include:
|
|
120
|
+
- arch: x64
|
|
121
|
+
runner: windows-latest
|
|
122
|
+
- arch: arm64
|
|
123
|
+
runner: windows-11-arm
|
|
124
|
+
steps:
|
|
125
|
+
- name: Checkout
|
|
126
|
+
uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
|
|
127
|
+
|
|
128
|
+
- name: Setup Node
|
|
129
|
+
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
|
|
130
|
+
with:
|
|
131
|
+
node-version: 24
|
|
132
|
+
|
|
133
|
+
- name: Download tool
|
|
134
|
+
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
|
|
135
|
+
with:
|
|
136
|
+
name: win32-${{ matrix.arch }}
|
|
137
|
+
path: platforms/win32-${{ matrix.arch }}
|
|
138
|
+
|
|
139
|
+
- name: Install the packed tool and scan
|
|
140
|
+
shell: pwsh
|
|
141
|
+
run: |
|
|
142
|
+
# The `./` matters: npm reads `platforms/win32-<arch>` as a GitHub repository.
|
|
143
|
+
$pack = npm pack "./platforms/win32-${{ matrix.arch }}" --pack-destination $env:RUNNER_TEMP --json | ConvertFrom-Json
|
|
144
|
+
if ($LASTEXITCODE -ne 0) { throw "npm pack failed" }
|
|
145
|
+
npm install
|
|
146
|
+
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
147
|
+
npm install --no-save (Join-Path $env:RUNNER_TEMP $pack[0].filename)
|
|
148
|
+
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
149
|
+
npm run build:ts
|
|
150
|
+
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
151
|
+
node scripts/check-scan.js .
|
|
152
|
+
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
153
|
+
|
|
154
|
+
verify-linux:
|
|
155
|
+
name: Verify Linux tool (${{ matrix.arch }})
|
|
156
|
+
needs: build-linux
|
|
157
|
+
# Installs the release tool from its packed package, then scans through the TypeScript wrapper in
|
|
158
|
+
# images of the oldest distribution releases that the package supports, before anything is
|
|
159
|
+
# published. The images have no Swift toolchain, and the runner's Node runs in them.
|
|
160
|
+
runs-on: ${{ matrix.runner }}
|
|
161
|
+
timeout-minutes: 15
|
|
162
|
+
defaults:
|
|
163
|
+
run:
|
|
164
|
+
shell: bash
|
|
165
|
+
strategy:
|
|
166
|
+
matrix:
|
|
167
|
+
include:
|
|
168
|
+
- arch: x64
|
|
169
|
+
runner: ubuntu-24.04
|
|
170
|
+
- arch: arm64
|
|
171
|
+
runner: ubuntu-24.04-arm
|
|
172
|
+
steps:
|
|
173
|
+
- name: Checkout
|
|
174
|
+
uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
|
|
175
|
+
|
|
176
|
+
- name: Setup Node
|
|
177
|
+
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
|
|
178
|
+
with:
|
|
179
|
+
node-version: 24
|
|
180
|
+
|
|
181
|
+
- name: Download tool
|
|
182
|
+
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
|
|
183
|
+
with:
|
|
184
|
+
name: linux-${{ matrix.arch }}
|
|
185
|
+
path: platforms/linux-${{ matrix.arch }}
|
|
186
|
+
|
|
187
|
+
- name: Install the packed tool
|
|
188
|
+
run: |
|
|
189
|
+
# Artifacts don't keep file modes, so the executable gets its executable bit back first.
|
|
190
|
+
chmod +x platforms/linux-${{ matrix.arch }}/ExpoModulesMacros
|
|
191
|
+
# The `./` matters: npm reads `platforms/linux-<arch>` as a GitHub repository.
|
|
192
|
+
npm pack "./platforms/linux-${{ matrix.arch }}" --pack-destination "$RUNNER_TEMP" --json > "$RUNNER_TEMP/pack.json"
|
|
193
|
+
tarball="$(node -p 'JSON.parse(require("node:fs").readFileSync(process.argv[1], "utf8"))[0].filename' "$RUNNER_TEMP/pack.json")"
|
|
194
|
+
npm install
|
|
195
|
+
npm install --no-save "$RUNNER_TEMP/$tarball"
|
|
196
|
+
npm run build:ts
|
|
197
|
+
|
|
198
|
+
- name: Scan on each distribution
|
|
199
|
+
run: |
|
|
200
|
+
node_dir="$(dirname "$(dirname "$(command -v node)")")"
|
|
201
|
+
for image in ubuntu:22.04 debian:12 amazonlinux:2023; do
|
|
202
|
+
echo "::group::$image"
|
|
203
|
+
run=(docker run --rm -v "$PWD:/work:ro" -v "$node_dir:/node:ro" -w /work "$image")
|
|
204
|
+
"${run[@]}" getconf GNU_LIBC_VERSION
|
|
205
|
+
"${run[@]}" /node/bin/node scripts/check-scan.js .
|
|
206
|
+
echo "::endgroup::"
|
|
207
|
+
done
|
|
208
|
+
|
|
73
209
|
publish:
|
|
74
210
|
name: Build and publish to npm
|
|
75
|
-
needs: build-windows
|
|
76
|
-
# Builds the native macOS tool binary, so it must run on macOS. The Windows binaries come
|
|
77
|
-
# build-windows jobs.
|
|
211
|
+
needs: [build-windows, build-linux, verify-windows, verify-linux]
|
|
212
|
+
# Builds the native macOS tool binary, so it must run on macOS. The Windows and Linux binaries come
|
|
213
|
+
# from the build-windows and build-linux jobs.
|
|
78
214
|
# The package declares swift-tools-version 6.2, which requires Xcode 26+.
|
|
79
215
|
runs-on: macos-26
|
|
80
216
|
permissions:
|
|
@@ -151,25 +287,38 @@ jobs:
|
|
|
151
287
|
# copies it to apple/ExpoModulesMacros, and strips it.
|
|
152
288
|
run: npm run build
|
|
153
289
|
|
|
154
|
-
- name:
|
|
155
|
-
#
|
|
290
|
+
- name: Verify the macOS tool
|
|
291
|
+
# Scans through the TypeScript wrapper, which must find the binary in `apple/`, before
|
|
292
|
+
# anything is published. The verify-windows and verify-linux jobs check the other binaries.
|
|
293
|
+
run: |
|
|
294
|
+
npm run build:ts
|
|
295
|
+
node scripts/check-scan.js .
|
|
296
|
+
|
|
297
|
+
- name: Download Windows and Linux tools
|
|
298
|
+
# Each artifact goes into the directory of its name: `platforms/<platform>-<arch>`.
|
|
156
299
|
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
|
|
157
300
|
with:
|
|
158
|
-
pattern: win32-*
|
|
301
|
+
pattern: '{win32,linux}-*'
|
|
159
302
|
path: platforms
|
|
160
303
|
|
|
161
|
-
- name: Verify Windows tools
|
|
162
|
-
|
|
304
|
+
- name: Verify Windows and Linux tools
|
|
305
|
+
# Artifacts don't keep file modes, so the Linux executables get their executable bit back
|
|
306
|
+
# here. `npm publish` keeps it in the package.
|
|
307
|
+
run: |
|
|
308
|
+
chmod +x platforms/linux-x64/ExpoModulesMacros platforms/linux-arm64/ExpoModulesMacros
|
|
309
|
+
ls -l \
|
|
310
|
+
platforms/win32-x64/ExpoModulesMacros.exe platforms/win32-arm64/ExpoModulesMacros.exe \
|
|
311
|
+
platforms/linux-x64/ExpoModulesMacros platforms/linux-arm64/ExpoModulesMacros
|
|
163
312
|
|
|
164
|
-
- name: Publish the Windows packages
|
|
313
|
+
- name: Publish the Windows and Linux packages
|
|
165
314
|
# Published before `expo-modules-macros`, whose optional dependencies point at them, so that
|
|
166
315
|
# package is never on npm without them. Each package needs trusted publishing (OIDC)
|
|
167
316
|
# configured on npmjs.com for this workflow.
|
|
168
317
|
env:
|
|
169
318
|
DRY_RUN: ${{ inputs.dry_run && '--dry-run' || '' }}
|
|
170
319
|
run: |
|
|
171
|
-
for
|
|
172
|
-
npm publish --access public --tag "${{ steps.bump.outputs.tag }}" $DRY_RUN "./platforms
|
|
320
|
+
for package in win32-x64 win32-arm64 linux-x64 linux-arm64; do
|
|
321
|
+
npm publish --access public --tag "${{ steps.bump.outputs.tag }}" $DRY_RUN "./platforms/$package"
|
|
173
322
|
done
|
|
174
323
|
|
|
175
324
|
- name: Publish
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
name: Windows
|
|
2
2
|
|
|
3
|
-
# Builds and tests the package on Windows, and checks the release tool
|
|
4
|
-
# aren't published yet, so this runs apart from the macOS workflow.
|
|
3
|
+
# Builds and tests the package on Windows, and checks the release tool and its package there.
|
|
5
4
|
|
|
6
5
|
on:
|
|
7
6
|
push:
|
|
@@ -116,29 +115,5 @@ jobs:
|
|
|
116
115
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
117
116
|
npm run build:ts
|
|
118
117
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
Set-Content (Join-Path $fixture "Module.swift") "@ExpoModule`npublic final class WindowsModule {}"
|
|
122
|
-
@'
|
|
123
|
-
const path = require('node:path');
|
|
124
|
-
const { getScannerBinaryPath, scanModules } = require(path.resolve('build'));
|
|
125
|
-
const binaryPath = getScannerBinaryPath();
|
|
126
|
-
const expected = path.join('node_modules', '@expo', `modules-macros-win32-${process.arch}`, 'ExpoModulesMacros.exe');
|
|
127
|
-
if (!binaryPath.endsWith(expected)) {
|
|
128
|
-
console.error(`getScannerBinaryPath() returned ${binaryPath}, not a path ending in ${expected}`);
|
|
129
|
-
process.exit(1);
|
|
130
|
-
}
|
|
131
|
-
console.log(`getScannerBinaryPath() returned ${binaryPath}`);
|
|
132
|
-
scanModules([process.argv[2]]).then((result) => {
|
|
133
|
-
const module = result.modules.find((candidate) => candidate.name === 'WindowsModule');
|
|
134
|
-
if (!module) {
|
|
135
|
-
console.error(JSON.stringify(result, null, 2));
|
|
136
|
-
process.exit(1);
|
|
137
|
-
}
|
|
138
|
-
console.log(`scanModules reported ${module.name} in ${module.file}`);
|
|
139
|
-
}, (error) => {
|
|
140
|
-
console.error(error);
|
|
141
|
-
process.exit(1);
|
|
142
|
-
});
|
|
143
|
-
'@ | Set-Content (Join-Path $env:RUNNER_TEMP "scan-check.js")
|
|
144
|
-
node (Join-Path $env:RUNNER_TEMP "scan-check.js") $fixture
|
|
118
|
+
node scripts/check-scan.js .
|
|
119
|
+
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
package/AGENTS.md
CHANGED
|
@@ -9,10 +9,10 @@
|
|
|
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` (a `package.json` with `os` and `cpu`, 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` is written there by `npm run build` on
|
|
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 `npm 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` (`npm run build`): builds the release binaries: the universal macOS `apple/ExpoModulesMacros` (committed and published), and on Windows `platforms/win32-<arch>/ExpoModulesMacros.exe` for the host architecture (not committed; published in that package).
|
|
15
|
+
- `scripts/build.js` (`npm 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
16
|
|
|
17
17
|
## Commands
|
|
18
18
|
|
|
@@ -25,13 +25,18 @@ npm run typecheck # TypeScript wrapper
|
|
|
25
25
|
npm run build # release binary (slow; on macOS uses Rosetta for x86_64)
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
-
CI runs on macOS (`.github/workflows/swift.yml`)
|
|
28
|
+
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 `npm 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. Code in `Sources` and `Tests` must not assume Apple platforms or POSIX paths.
|
|
29
29
|
|
|
30
30
|
## Windows
|
|
31
31
|
|
|
32
32
|
- Outside Apple platforms, `Sources` import `FoundationEssentials` instead of `Foundation` (`#if canImport(FoundationEssentials)`). Full Foundation brings in ICU, which would add tens of megabytes to the statically linked Windows binary. Don't use APIs that only `Foundation` has (`FileHandle`, `NSRegularExpression`, `CharacterSet`, `FileManager.enumerator`, `NSString` path methods); `Core/StandardStreams.swift` writes to stdout and stderr.
|
|
33
33
|
- The Windows binaries link the Swift runtime statically (`-static-stdlib`), because Swift has no stable ABI on Windows and the scanner must also run without a Swift toolchain. SwiftPM's `--static-swift-stdlib` has no effect there.
|
|
34
34
|
|
|
35
|
+
## Linux
|
|
36
|
+
|
|
37
|
+
- The `FoundationEssentials` rule above applies on Linux too.
|
|
38
|
+
- The Linux binaries link the Swift runtime statically (`-static-stdlib`; SwiftPM's `--static-swift-stdlib` doesn't apply to the macro tool here either) but link glibc and the GNU C++ runtime dynamically, so they need the glibc version they were built against or a newer one. CI and the Publish workflow build them in `swift:6.4.0-rhel-ubi9` (glibc 2.34), and the Linux workflow fails when the binary needs a newer glibc symbol than `MAX_GLIBC_VERSION`. A change to the build image must keep these and the glibc version in the READMEs in sync.
|
|
39
|
+
|
|
35
40
|
## Conventions
|
|
36
41
|
|
|
37
42
|
- Swift uses 2-space indentation. Write new doc comments as `///` line comments, not `/** */` blocks.
|
|
@@ -52,14 +57,14 @@ CI runs on macOS (`.github/workflows/swift.yml`) and on Windows x64 and arm64 (`
|
|
|
52
57
|
- **Macro declarations live in `expo-modules-core`** (`ios/Core/ExpoModulesMacros.swift` in `expo/expo`), as `#externalMacro(module: "ExpoModulesMacros", type: ...)`. Adding a macro, renaming a macro type or changing a macro's signature needs a matching edit there, and the type must be listed in `providingMacros` in `Plugin.swift`.
|
|
53
58
|
- **Generated code calls `expo-modules-core` API.** An expansion that uses a new core symbol only works with a core version that has it.
|
|
54
59
|
- **Scanner output is versioned.** `scanModulesSchemaVersion` (`Modules/ScanModules.swift`) and `scanExportsSchemaVersion` (`Exports/ExportedSurface.swift`) change independently. When the output shape of a command changes, bump its version and update the matching `SUPPORTED_SCAN_*_SCHEMA_VERSION` constant and mirror types in `src/types.ts`. `expo-modules-autolinking` also checks the `scan-modules` version.
|
|
55
|
-
- **The binary path is part of the contract.** `expo-modules-autolinking` resolves this package from `expo-modules-core` and passes `-load-plugin-executable <package>/apple/<binary>#ExpoModulesMacros` to the compiler. The scanner wrapper in autolinking uses the same path, and `expo-modules-cli` uses `getScannerBinaryPath()`. On Windows the binary is `ExpoModulesMacros.exe` in `@expo/modules-macros-win32-<arch>`. Renaming or moving a binary needs a matching change in `expo/expo`.
|
|
60
|
+
- **The binary path is part of the contract.** `expo-modules-autolinking` resolves this package from `expo-modules-core` and passes `-load-plugin-executable <package>/apple/<binary>#ExpoModulesMacros` to the compiler. The scanner wrapper in autolinking uses the same path, and `expo-modules-cli` uses `getScannerBinaryPath()`. On Windows the binary is `ExpoModulesMacros.exe` in `@expo/modules-macros-win32-<arch>`, and on Linux `ExpoModulesMacros` in `@expo/modules-macros-linux-<arch>`. Renaming or moving a binary needs a matching change in `expo/expo`.
|
|
56
61
|
|
|
57
62
|
## Releases
|
|
58
63
|
|
|
59
|
-
- 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 binaries and publishes them in the `platforms/` packages, with the same version.
|
|
64
|
+
- 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`.
|
|
60
65
|
- Do not edit the `version` in `package.json` by hand. The Publish workflow bumps it.
|
|
61
66
|
- Commit and PR titles are short and imperative, with code identifiers in backticks, for example ``Report `@Union` types in `scan-exports` ``.
|
|
62
67
|
|
|
63
68
|
## Installed copies
|
|
64
69
|
|
|
65
|
-
In `node_modules`, the compiler runs the prebuilt binary `apple/ExpoModulesMacros` (on Windows,
|
|
70
|
+
In `node_modules`, the compiler runs the prebuilt binary `apple/ExpoModulesMacros` (on Windows and Linux, the binary from `@expo/modules-macros-<platform>-<arch>`); editing the Swift sources there has no effect. Macro and scanner fixes belong in this repository and ship in a new release. The TypeScript wrapper ships compiled in `build/`, since `src/` is not published.
|
package/README.md
CHANGED
|
@@ -15,12 +15,17 @@ The same executable doubles as a source scanner CLI.
|
|
|
15
15
|
|
|
16
16
|
This package is not meant to be installed directly. It is a dependency of `expo-modules-core`, so every Expo project already has it. The published package contains a prebuilt universal (arm64 + x86_64) macOS binary at `apple/ExpoModulesMacros`, so consumers never build the plugin themselves.
|
|
17
17
|
|
|
18
|
-
The Windows binaries ship in their own packages,
|
|
18
|
+
The Windows and Linux binaries ship in their own packages, one per platform and architecture:
|
|
19
|
+
|
|
20
|
+
- `@expo/modules-macros-win32-x64` and `@expo/modules-macros-win32-arm64`, each with an `ExpoModulesMacros.exe`. They need only Windows system libraries and the Microsoft Visual C++ runtime.
|
|
21
|
+
- `@expo/modules-macros-linux-x64` and `@expo/modules-macros-linux-arm64`, each with an `ExpoModulesMacros`. They need glibc 2.34 or newer and the GNU C++ runtime (`libstdc++` and `libgcc_s`), for example on Ubuntu 22.04, Debian 12, Red Hat Enterprise Linux 9 or Amazon Linux 2023. They don't run on musl distributions such as Alpine Linux.
|
|
22
|
+
|
|
23
|
+
This package lists them as optional dependencies, and their `os`, `cpu` and (on Linux) `libc` fields make npm, pnpm and Yarn install only the one that matches the machine, so other machines don't download them. The Swift runtime is linked in statically, so the executables run without a Swift toolchain.
|
|
19
24
|
|
|
20
25
|
# Macros
|
|
21
26
|
|
|
22
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.
|
|
23
|
-
- **`@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. 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. 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. The `.concurrent` option moves an `async` body off the JavaScript thread while still decoding and encoding on it. 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.
|
|
24
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.
|
|
25
30
|
- **`@SharedObject(_ name: String? = nil)`** on a `SharedObject` subclass. Collects the class's `@JS` members into a class definition that a module exposes through `@ExpoModule(classes:)`.
|
|
26
31
|
- **`@Record()`** on a record type. 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.
|
|
@@ -81,11 +86,11 @@ Node consumers can call the scanner through this package instead of locating the
|
|
|
81
86
|
```ts
|
|
82
87
|
import { scanModules, scanExports } from 'expo-modules-macros';
|
|
83
88
|
|
|
84
|
-
const { modules, warnings } = await scanModules(['ios/'], { defines: ['DEBUG'] });
|
|
89
|
+
const { modules, warnings } = await scanModules(['ios/'], { defines: ['DEBUG'], timeout: 30_000 });
|
|
85
90
|
const { exports } = await scanExports(['ios/']);
|
|
86
91
|
```
|
|
87
92
|
|
|
88
|
-
The binary is a compiled executable, so each call still spawns a process. What the wrapper owns is the part consumers would otherwise duplicate: resolving the shipped binary, building the arguments, parsing the JSON, checking `schemaVersion`, and turning a non-zero exit into a `ScannerError`. The result types are hand-written mirrors of the Swift `Codable` types, which is what the version check guards against drifting.
|
|
93
|
+
The binary is a compiled executable, so each call still spawns a process. What the wrapper owns is the part consumers would otherwise duplicate: resolving the shipped binary, building the arguments, parsing the JSON, checking `schemaVersion`, and turning a non-zero exit or an exceeded `timeout` into a `ScannerError`. The result types are hand-written mirrors of the Swift `Codable` types, which is what the version check guards against drifting.
|
|
89
94
|
|
|
90
95
|
# How the plugin reaches the compiler
|
|
91
96
|
|
|
@@ -97,13 +102,13 @@ The binary is a compiled executable, so each call still spawns a process. What t
|
|
|
97
102
|
|
|
98
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.
|
|
99
104
|
|
|
100
|
-
On Windows, the compiler loads `ExpoModulesMacros.exe` from `@expo/modules-macros-win32-<arch>` with the same flag. `getScannerBinaryPath()` in the TypeScript wrapper returns the binary for the current platform and architecture. On Windows it falls back to the local `npm run build` output in `platforms
|
|
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 `npm run build` output in `platforms/<platform>-<arch>` when the platform package isn't installed, for example in this repository.
|
|
101
106
|
|
|
102
107
|
The module and type names in `#externalMacro` must stay in sync with `apple/Sources/ExpoModulesMacros/Plugin.swift`.
|
|
103
108
|
|
|
104
109
|
# Development
|
|
105
110
|
|
|
106
|
-
Requires a toolchain with Swift 6.2 or newer: on macOS 13 or newer that means Xcode 26 or newer, and on Windows the Swift toolchain from swift.org (CI uses 6.4).
|
|
111
|
+
Requires a toolchain with Swift 6.2 or newer: on macOS 13 or newer that means Xcode 26 or newer, and on Windows and Linux the Swift toolchain from swift.org (CI uses 6.4).
|
|
107
112
|
|
|
108
113
|
```sh
|
|
109
114
|
cd apple
|
|
@@ -115,16 +120,17 @@ swift test
|
|
|
115
120
|
|
|
116
121
|
- 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.
|
|
117
122
|
- 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.
|
|
123
|
+
- On Linux, it also builds for the host architecture only, with the Swift runtime linked in statically, and writes `platforms/linux-<arch>/ExpoModulesMacros`. It strips the executable with `strip` and checks the architecture in its ELF header. The executable needs the glibc version of the machine it's built on or a newer one, so CI and the Publish workflow build it in the `swift:6.4.0-rhel-ubi9` image (Red Hat UBI 9, glibc 2.34).
|
|
118
124
|
|
|
119
|
-
The macOS binary is committed to the repository. The Windows binaries aren't: the Publish workflow builds them and puts them into the packages in `platforms/`.
|
|
125
|
+
The macOS binary is committed to the repository. The Windows and Linux binaries aren't: the Publish workflow builds them and puts them into the packages in `platforms/`.
|
|
120
126
|
|
|
121
127
|
# Releasing
|
|
122
128
|
|
|
123
129
|
The **Publish** workflow is manual (`workflow_dispatch`) and takes a release type. It bumps the version, builds the universal binary, and publishes to npm through OIDC trusted publishing. The commit, tag and GitHub release are created only after the publish succeeds, so a failed build leaves the branch untouched.
|
|
124
130
|
|
|
125
|
-
Windows jobs (x64 and arm64) build the
|
|
131
|
+
Windows and Linux jobs (x64 and arm64 for each) build their binaries first. Verify jobs then install each binary from its packed package and scan through the TypeScript wrapper with `scripts/check-scan.js`, on Windows runners without Swift and, for Linux, in the Ubuntu 22.04, Debian 12 and Amazon Linux 2023 images. The publish job does the same for the macOS binary, so a failed check stops the release before anything is published. After the version bump, `scripts/set-platform-versions.js` gives the packages in `platforms/` the same version and lists them in this package's `optionalDependencies` with that exact version. The workflow publishes the four platform packages before `expo-modules-macros`, so this package is never on npm without them. Each package needs trusted publishing configured on npmjs.com for the Publish workflow.
|
|
126
132
|
|
|
127
|
-
Prerelease versions (`prepatch`, `preminor`, `premajor`, `prerelease`) are published with the `next` dist-tag, and the others with `latest`. With **Dry run** checked, the workflow builds everything and runs `npm publish --dry-run` for the
|
|
133
|
+
Prerelease versions (`prepatch`, `preminor`, `premajor`, `prerelease`) are published with the `next` dist-tag, and the others with `latest`. With **Dry run** checked, the workflow builds everything and runs `npm publish --dry-run` for the five packages, but doesn't publish, commit, tag or create a release. Use it to check a change to the workflow.
|
|
128
134
|
|
|
129
135
|
# Contributing
|
|
130
136
|
|
package/apple/ExpoModulesMacros
CHANGED
|
Binary file
|
|
@@ -344,7 +344,7 @@ internal func containsFunctionType(_ type: TypeSyntax) -> Bool {
|
|
|
344
344
|
}
|
|
345
345
|
|
|
346
346
|
/// The first function type in `type` or anywhere inside it, in source order.
|
|
347
|
-
|
|
347
|
+
internal func firstFunctionType(in type: some SyntaxProtocol) -> FunctionTypeSyntax? {
|
|
348
348
|
if let functionType = type.as(FunctionTypeSyntax.self) {
|
|
349
349
|
return functionType
|
|
350
350
|
}
|
|
@@ -23,10 +23,13 @@ internal struct JSFunction {
|
|
|
23
23
|
let returnFreeForm: FreeFormConversion?
|
|
24
24
|
let isThrowing: Bool
|
|
25
25
|
let isAsync: Bool
|
|
26
|
+
/// The function's name in the source, where a JavaScript name collision is reported.
|
|
27
|
+
let nameToken: TokenSyntax
|
|
26
28
|
|
|
27
29
|
init(funcDecl: FunctionDeclSyntax, attribute: AttributeSyntax) {
|
|
28
30
|
self.swiftName = funcDecl.name.text
|
|
29
31
|
self.jsName = jsNameArgument(of: attribute) ?? withoutBackticks(funcDecl.name.text)
|
|
32
|
+
self.nameToken = funcDecl.name
|
|
30
33
|
self.parameters = Array(funcDecl.signature.parameterClause.parameters)
|
|
31
34
|
|
|
32
35
|
let declaredReturnType = funcDecl.signature.returnClause?.type
|
|
@@ -206,8 +209,8 @@ internal struct JSFunction {
|
|
|
206
209
|
}
|
|
207
210
|
|
|
208
211
|
/// The flat (single-arity) call-and-encode lines used when no trailing parameter is omittable:
|
|
209
|
-
/// `let result = <callee>.f(...)` then the return encode (or
|
|
210
|
-
///
|
|
212
|
+
/// `let result = <callee>.f(...)` then the return encode (or just `<callee>.f(...)` for a function
|
|
213
|
+
/// that returns nothing).
|
|
211
214
|
private func callAndEncodeLines(receiver: Receiver, arity: Int, decodingFrom: Int) -> [String] {
|
|
212
215
|
var lines: [String] = []
|
|
213
216
|
for index in decodingFrom..<arity {
|
|
@@ -218,7 +221,6 @@ internal struct JSFunction {
|
|
|
218
221
|
lines.append(contentsOf: encodeResultLines())
|
|
219
222
|
} else {
|
|
220
223
|
lines.append(callExpression(receiver: receiver, arity: arity))
|
|
221
|
-
lines.append("return .undefined()")
|
|
222
224
|
}
|
|
223
225
|
return lines
|
|
224
226
|
}
|
|
@@ -229,7 +231,8 @@ internal struct JSFunction {
|
|
|
229
231
|
/// `toJavaScriptValue(in:)` except for `Int`/`UInt`, where it range-checks
|
|
230
232
|
/// and throws instead of silently encoding an out-of-safe-range value as a lossy number — the
|
|
231
233
|
/// catchable error is the right behavior, and matches how non-primitive integers already encode. A
|
|
232
|
-
/// no-return function returns
|
|
234
|
+
/// no-return function has nothing to encode: its binding closure returns `Void` (see
|
|
235
|
+
/// `decorateStatements(object:receiver:)`).
|
|
233
236
|
///
|
|
234
237
|
/// For an `async` function the encode must run on the JS thread. An `async` body may suspend and
|
|
235
238
|
/// resume on an arbitrary cooperative-pool thread, and encoding a heap-allocated JS value (a string,
|
|
@@ -239,7 +242,7 @@ internal struct JSFunction {
|
|
|
239
242
|
/// which always run on the JS thread — keep encoding directly without the wrapper.
|
|
240
243
|
private func encodeResultLines() -> [String] {
|
|
241
244
|
guard let returnType else {
|
|
242
|
-
return [
|
|
245
|
+
return []
|
|
243
246
|
}
|
|
244
247
|
let encode = "try \(encodeCall(returnType, freeForm: returnFreeForm, of: "result"))"
|
|
245
248
|
if isAsync {
|
|
@@ -272,11 +275,16 @@ internal struct JSFunction {
|
|
|
272
275
|
// a shorthand `{ [capture] name, name in }` parameter; for a sync binding the explicit
|
|
273
276
|
// `JavaScriptUnownedValue` also selects the (otherwise `@_disfavoredOverload`) unowned-`this`
|
|
274
277
|
// overload.
|
|
278
|
+
// A sync function that returns nothing binds through the `Void` overload, which leaves JS's
|
|
279
|
+
// result as `undefined` without building and writing a value. The closure declares `-> Void`
|
|
280
|
+
// because a body of several statements without a `return` is otherwise ambiguous between the
|
|
281
|
+
// two unowned-`this` overloads.
|
|
275
282
|
let captures = receiver.captureClause
|
|
276
283
|
let parameters = "(this: borrowing JavaScriptUnownedValue, arguments: consuming JavaScriptValuesBuffer)"
|
|
284
|
+
let returnClause = returnType == nil && !isAsync ? " -> Void" : ""
|
|
277
285
|
|
|
278
286
|
return """
|
|
279
|
-
\(object).setProperty("\(jsName)") { \(captures)\(parameters) in
|
|
287
|
+
\(object).setProperty("\(jsName)") { \(captures)\(parameters)\(returnClause) in
|
|
280
288
|
\(bodyStatements(receiver: receiver, indent: " "))
|
|
281
289
|
}
|
|
282
290
|
"""
|
|
@@ -308,6 +316,8 @@ internal struct JSProperty {
|
|
|
308
316
|
/// Whether the property is settable from JS: `true` for a stored `var` or a computed `var` with an
|
|
309
317
|
/// explicit `set` accessor; `false` for a getter-only computed `var` or a `let`.
|
|
310
318
|
let isSettable: Bool
|
|
319
|
+
/// The property's name in the source, where a JavaScript name collision is reported.
|
|
320
|
+
let nameToken: TokenSyntax
|
|
311
321
|
|
|
312
322
|
/// The statements that install this property's accessor on the JS object, indented for the
|
|
313
323
|
/// decorator body. Builds a descriptor object (`enumerable` + `get`, and `set` when settable) via
|
|
@@ -348,7 +358,7 @@ internal struct JSProperty {
|
|
|
348
358
|
let setDecode = "\(callee).\(swiftName) = try \(decode)"
|
|
349
359
|
lines.append(
|
|
350
360
|
accessorClosure(
|
|
351
|
-
descriptorName, "set", receiver: receiver, body: "\(unwrap)\(setDecode)
|
|
361
|
+
descriptorName, "set", receiver: receiver, returnsVoid: true, body: "\(unwrap)\(setDecode)"))
|
|
352
362
|
}
|
|
353
363
|
|
|
354
364
|
lines.append("\(object).defineProperty(\"\(jsName)\", descriptor: \(descriptorName))")
|
|
@@ -361,8 +371,10 @@ internal struct JSProperty {
|
|
|
361
371
|
|
|
362
372
|
/// One `descriptor.setProperty("get"/"set") { … }` accessor entry. The capture list follows the
|
|
363
373
|
/// receiver: a module captures `self` strong; a shared object captures nothing of the instance.
|
|
374
|
+
/// A setter passes `returnsVoid`, so its closure declares `-> Void` and binds through the `Void`
|
|
375
|
+
/// overload, like a function that returns nothing.
|
|
364
376
|
private func accessorClosure(
|
|
365
|
-
_ descriptorName: String, _ key: String, receiver: Receiver, body: String
|
|
377
|
+
_ descriptorName: String, _ key: String, receiver: Receiver, returnsVoid: Bool = false, body: String
|
|
366
378
|
) -> String {
|
|
367
379
|
let captures = receiver.captureClause
|
|
368
380
|
// Indent each line of a (possibly multi-line) body to sit one level inside the closure; a bare
|
|
@@ -377,8 +389,9 @@ internal struct JSProperty {
|
|
|
377
389
|
// `borrowing JavaScriptUnownedValue` selects the unowned-`this` overload. A module ignores `this`;
|
|
378
390
|
// a shared object unwraps it in the body.
|
|
379
391
|
let parameters = "(this: borrowing JavaScriptUnownedValue, arguments: consuming JavaScriptValuesBuffer)"
|
|
392
|
+
let returnClause = returnsVoid ? " -> Void" : ""
|
|
380
393
|
return """
|
|
381
|
-
\(descriptorName).setProperty("\(key)") { \(captures)\(parameters) in
|
|
394
|
+
\(descriptorName).setProperty("\(key)") { \(captures)\(parameters)\(returnClause) in
|
|
382
395
|
\(indentedBody)
|
|
383
396
|
}
|
|
384
397
|
"""
|
|
@@ -445,7 +458,7 @@ internal func buildDecorateSharedObjectPhase(
|
|
|
445
458
|
}
|
|
446
459
|
|
|
447
460
|
/// True when a return clause is absent or written as `Void` / `()` — i.e. the function returns
|
|
448
|
-
/// nothing JS-visible, so the binding returns `.
|
|
461
|
+
/// nothing JS-visible, so the binding closure returns `Void`.
|
|
449
462
|
private func isVoidType(_ type: TypeSyntax?) -> Bool {
|
|
450
463
|
guard let type else {
|
|
451
464
|
return true
|