Commit ccd8250
committed
BridgeJS: Add
The new API allows managing JS closures converted from Swift closures
from Swift side. It allows us to get the underlying `JSObject` and
manage its lifetime manually from Swift.
```swift
@js func makeIntToInt() throws(JSException) -> JSTypedClosure<(Int) -> Int> {
return JSTypedClosure { x in
return x + 1
}
}
@JSFunction func takeIntToInt(_ transform: JSTypedClosure<(Int) -> Int>) throws(JSException)
let closure = JSTypedClosure<(Int) -> Int> { x in
return x * 2
}
defer { closure.release() }
try takeIntToInt(closure)
```
Likewise to `JSClosure`, API users are responsible for "manually" releasing the
closure when it's no longer needed by calling `release()`. After releasing,
the closure becomes unusable and calling it will throw a JS exception
(note that we will not segfault even if the closure is called after releasing).
```swift
let closure = JSTypedClosure<(Int) -> Int> { x in
return x * 2
}
closure.release()
try closure(10) // "JSException: Attempted to call a released JSTypedClosure created at <file>:<line>"
```
As a belt-and-suspenders measure, the underlying JS closure is also
registered with a `FinalizationRegistry` to ensure that the Swift closure box
is released when the JS closure is garbage collected, in case the API user
forgets to call `release()`. However, relying on this mechanism is **not recommended**
because the timing of garbage collection is non-deterministic and it's
not guaranteed that it will happen in a timely manner.
----
On the top of the new API, this commit also fixes memory leak issues of
closures exported to JS.JSTypedClosure API1 parent 444393f commit ccd8250
File tree
26 files changed
+4099
-1665
lines changed- Plugins
- BridgeJS
- Sources
- BridgeJSCore
- BridgeJSLink
- BridgeJSSkeleton
- Tests/BridgeJSToolTests/__Snapshots__
- BridgeJSCodegenTests
- BridgeJSLinkTests
- PackageToJS/Templates
- Sources/JavaScriptKit
- FundamentalObjects
- Tests
- BridgeJSRuntimeTests
- Generated
- JavaScript
- JavaScript
- JavaScriptKitTests
26 files changed
+4099
-1665
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
18 | 21 | | |
19 | 22 | | |
20 | 23 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
25 | 28 | | |
26 | 29 | | |
27 | 30 | | |
| |||
Lines changed: 39 additions & 42 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
36 | 35 | | |
37 | 36 | | |
38 | 37 | | |
39 | 38 | | |
40 | 39 | | |
41 | 40 | | |
42 | 41 | | |
43 | | - | |
| 42 | + | |
44 | 43 | | |
45 | 44 | | |
46 | 45 | | |
| |||
69 | 68 | | |
70 | 69 | | |
71 | 70 | | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
78 | 78 | | |
| 79 | + | |
79 | 80 | | |
80 | 81 | | |
81 | 82 | | |
| |||
84 | 85 | | |
85 | 86 | | |
86 | 87 | | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | 88 | | |
115 | 89 | | |
116 | 90 | | |
| |||
128 | 102 | | |
129 | 103 | | |
130 | 104 | | |
131 | | - | |
| 105 | + | |
132 | 106 | | |
133 | 107 | | |
134 | 108 | | |
| |||
178 | 152 | | |
179 | 153 | | |
180 | 154 | | |
181 | | - | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
182 | 171 | | |
183 | 172 | | |
184 | 173 | | |
185 | | - | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
186 | 181 | | |
187 | 182 | | |
188 | 183 | | |
| |||
205 | 200 | | |
206 | 201 | | |
207 | 202 | | |
208 | | - | |
| 203 | + | |
209 | 204 | | |
210 | 205 | | |
211 | 206 | | |
| |||
217 | 212 | | |
218 | 213 | | |
219 | 214 | | |
| 215 | + | |
| 216 | + | |
220 | 217 | | |
221 | 218 | | |
222 | 219 | | |
| |||
225 | 222 | | |
226 | 223 | | |
227 | 224 | | |
228 | | - | |
| 225 | + | |
229 | 226 | | |
230 | 227 | | |
231 | 228 | | |
| |||
315 | 312 | | |
316 | 313 | | |
317 | 314 | | |
318 | | - | |
| 315 | + | |
319 | 316 | | |
320 | 317 | | |
321 | 318 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
124 | 124 | | |
125 | 125 | | |
126 | 126 | | |
127 | | - | |
| 127 | + | |
128 | 128 | | |
129 | 129 | | |
130 | 130 | | |
| |||
364 | 364 | | |
365 | 365 | | |
366 | 366 | | |
367 | | - | |
368 | | - | |
| 367 | + | |
| 368 | + | |
369 | 369 | | |
370 | 370 | | |
371 | 371 | | |
| |||
423 | 423 | | |
424 | 424 | | |
425 | 425 | | |
426 | | - | |
427 | | - | |
428 | | - | |
429 | | - | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
| 426 | + | |
434 | 427 | | |
435 | 428 | | |
436 | 429 | | |
| |||
1749 | 1742 | | |
1750 | 1743 | | |
1751 | 1744 | | |
1752 | | - | |
| 1745 | + | |
1753 | 1746 | | |
1754 | 1747 | | |
1755 | | - | |
| 1748 | + | |
| 1749 | + | |
1756 | 1750 | | |
1757 | 1751 | | |
1758 | 1752 | | |
| 1753 | + | |
| 1754 | + | |
| 1755 | + | |
| 1756 | + | |
| 1757 | + | |
1759 | 1758 | | |
1760 | 1759 | | |
1761 | 1760 | | |
| |||
1853 | 1852 | | |
1854 | 1853 | | |
1855 | 1854 | | |
1856 | | - | |
| 1855 | + | |
1857 | 1856 | | |
1858 | 1857 | | |
1859 | 1858 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
| 88 | + | |
| 89 | + | |
88 | 90 | | |
89 | 91 | | |
90 | 92 | | |
| |||
95 | 97 | | |
96 | 98 | | |
97 | 99 | | |
98 | | - | |
99 | 100 | | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
104 | 107 | | |
105 | | - | |
| 108 | + | |
106 | 109 | | |
| 110 | + | |
107 | 111 | | |
108 | 112 | | |
109 | 113 | | |
| |||
193 | 197 | | |
194 | 198 | | |
195 | 199 | | |
196 | | - | |
| 200 | + | |
197 | 201 | | |
198 | 202 | | |
199 | 203 | | |
| |||
204 | 208 | | |
205 | 209 | | |
206 | 210 | | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
213 | 238 | | |
214 | 239 | | |
215 | 240 | | |
| |||
249 | 274 | | |
250 | 275 | | |
251 | 276 | | |
252 | | - | |
| 277 | + | |
253 | 278 | | |
254 | 279 | | |
255 | 280 | | |
| |||
722 | 747 | | |
723 | 748 | | |
724 | 749 | | |
725 | | - | |
726 | | - | |
727 | 750 | | |
728 | 751 | | |
729 | | - | |
| 752 | + | |
730 | 753 | | |
731 | 754 | | |
732 | 755 | | |
| |||
930 | 953 | | |
931 | 954 | | |
932 | 955 | | |
933 | | - | |
934 | | - | |
| 956 | + | |
| 957 | + | |
935 | 958 | | |
936 | 959 | | |
937 | 960 | | |
| |||
0 commit comments