@@ -84,21 +84,15 @@ export class SwiftRuntime {
84
84
) {
85
85
const argc = args . length ;
86
86
const argv = this . exports . swjs_prepare_host_function_call ( argc ) ;
87
+ const memory = this . memory ;
87
88
for ( let index = 0 ; index < args . length ; index ++ ) {
88
89
const argument = args [ index ] ;
89
90
const base = argv + 16 * index ;
90
- JSValue . write (
91
- argument ,
92
- base ,
93
- base + 4 ,
94
- base + 8 ,
95
- false ,
96
- this . memory
97
- ) ;
91
+ JSValue . write ( argument , base , base + 4 , base + 8 , false , memory ) ;
98
92
}
99
93
let output : any ;
100
94
// This ref is released by the swjs_call_host_function implementation
101
- const callback_func_ref = this . memory . retain ( ( result : any ) => {
95
+ const callback_func_ref = memory . retain ( ( result : any ) => {
102
96
output = result ;
103
97
} ) ;
104
98
const alreadyReleased = this . exports . swjs_call_host_function (
@@ -127,28 +121,28 @@ export class SwiftRuntime {
127
121
payload1 : number ,
128
122
payload2 : number
129
123
) => {
130
- const obj = this . memory . getObject ( ref ) ;
131
- const key = this . memory . getObject ( name ) ;
132
- const value = JSValue . decode ( kind , payload1 , payload2 , this . memory ) ;
124
+ const memory = this . memory ;
125
+ const obj = memory . getObject ( ref ) ;
126
+ const key = memory . getObject ( name ) ;
127
+ const value = JSValue . decode ( kind , payload1 , payload2 , memory ) ;
133
128
obj [ key ] = value ;
134
129
} ,
135
130
swjs_get_prop : (
136
131
ref : ref ,
137
132
name : ref ,
138
- kind_ptr : pointer ,
139
133
payload1_ptr : pointer ,
140
134
payload2_ptr : pointer
141
135
) => {
142
- const obj = this . memory . getObject ( ref ) ;
143
- const key = this . memory . getObject ( name ) ;
136
+ const memory = this . memory ;
137
+ const obj = memory . getObject ( ref ) ;
138
+ const key = memory . getObject ( name ) ;
144
139
const result = obj [ key ] ;
145
- JSValue . write (
140
+ return JSValue . writeAndReturnKindBits (
146
141
result ,
147
- kind_ptr ,
148
142
payload1_ptr ,
149
143
payload2_ptr ,
150
144
false ,
151
- this . memory
145
+ memory
152
146
) ;
153
147
} ,
154
148
@@ -159,22 +153,21 @@ export class SwiftRuntime {
159
153
payload1 : number ,
160
154
payload2 : number
161
155
) => {
162
- const obj = this . memory . getObject ( ref ) ;
163
- const value = JSValue . decode ( kind , payload1 , payload2 , this . memory ) ;
156
+ const memory = this . memory ;
157
+ const obj = memory . getObject ( ref ) ;
158
+ const value = JSValue . decode ( kind , payload1 , payload2 , memory ) ;
164
159
obj [ index ] = value ;
165
160
} ,
166
161
swjs_get_subscript : (
167
162
ref : ref ,
168
163
index : number ,
169
- kind_ptr : pointer ,
170
164
payload1_ptr : pointer ,
171
165
payload2_ptr : pointer
172
166
) => {
173
167
const obj = this . memory . getObject ( ref ) ;
174
168
const result = obj [ index ] ;
175
- JSValue . write (
169
+ return JSValue . writeAndReturnKindBits (
176
170
result ,
177
- kind_ptr ,
178
171
payload1_ptr ,
179
172
payload2_ptr ,
180
173
false ,
@@ -183,50 +176,50 @@ export class SwiftRuntime {
183
176
} ,
184
177
185
178
swjs_encode_string : ( ref : ref , bytes_ptr_result : pointer ) => {
186
- const bytes = this . textEncoder . encode ( this . memory . getObject ( ref ) ) ;
187
- const bytes_ptr = this . memory . retain ( bytes ) ;
188
- this . memory . writeUint32 ( bytes_ptr_result , bytes_ptr ) ;
179
+ const memory = this . memory ;
180
+ const bytes = this . textEncoder . encode ( memory . getObject ( ref ) ) ;
181
+ const bytes_ptr = memory . retain ( bytes ) ;
182
+ memory . writeUint32 ( bytes_ptr_result , bytes_ptr ) ;
189
183
return bytes . length ;
190
184
} ,
191
185
swjs_decode_string : ( bytes_ptr : pointer , length : number ) => {
192
- const bytes = this . memory
186
+ const memory = this . memory ;
187
+ const bytes = memory
193
188
. bytes ( )
194
189
. subarray ( bytes_ptr , bytes_ptr + length ) ;
195
190
const string = this . textDecoder . decode ( bytes ) ;
196
- return this . memory . retain ( string ) ;
191
+ return memory . retain ( string ) ;
197
192
} ,
198
193
swjs_load_string : ( ref : ref , buffer : pointer ) => {
199
- const bytes = this . memory . getObject ( ref ) ;
200
- this . memory . writeBytes ( buffer , bytes ) ;
194
+ const memory = this . memory ;
195
+ const bytes = memory . getObject ( ref ) ;
196
+ memory . writeBytes ( buffer , bytes ) ;
201
197
} ,
202
198
203
199
swjs_call_function : (
204
200
ref : ref ,
205
201
argv : pointer ,
206
202
argc : number ,
207
- kind_ptr : pointer ,
208
203
payload1_ptr : pointer ,
209
204
payload2_ptr : pointer
210
205
) => {
211
- const func = this . memory . getObject ( ref ) ;
212
- let result : any ;
206
+ const memory = this . memory ;
207
+ const func = memory . getObject ( ref ) ;
208
+ let result = undefined ;
213
209
try {
214
- const args = JSValue . decodeArray ( argv , argc , this . memory ) ;
210
+ const args = JSValue . decodeArray ( argv , argc , memory ) ;
215
211
result = func ( ...args ) ;
216
212
} catch ( error ) {
217
- JSValue . write (
213
+ return JSValue . writeAndReturnKindBits (
218
214
error ,
219
- kind_ptr ,
220
215
payload1_ptr ,
221
216
payload2_ptr ,
222
217
true ,
223
218
this . memory
224
219
) ;
225
- return ;
226
220
}
227
- JSValue . write (
221
+ return JSValue . writeAndReturnKindBits (
228
222
result ,
229
- kind_ptr ,
230
223
payload1_ptr ,
231
224
payload2_ptr ,
232
225
false ,
@@ -237,67 +230,48 @@ export class SwiftRuntime {
237
230
ref : ref ,
238
231
argv : pointer ,
239
232
argc : number ,
240
- kind_ptr : pointer ,
241
233
payload1_ptr : pointer ,
242
234
payload2_ptr : pointer
243
235
) => {
244
- const func = this . memory . getObject ( ref ) ;
245
- let isException = true ;
246
- try {
247
- const args = JSValue . decodeArray ( argv , argc , this . memory ) ;
248
- const result = func ( ...args ) ;
249
- JSValue . write (
250
- result ,
251
- kind_ptr ,
252
- payload1_ptr ,
253
- payload2_ptr ,
254
- false ,
255
- this . memory
256
- ) ;
257
- isException = false ;
258
- } finally {
259
- if ( isException ) {
260
- JSValue . write (
261
- undefined ,
262
- kind_ptr ,
263
- payload1_ptr ,
264
- payload2_ptr ,
265
- true ,
266
- this . memory
267
- ) ;
268
- }
269
- }
236
+ const memory = this . memory ;
237
+ const func = memory . getObject ( ref ) ;
238
+ const args = JSValue . decodeArray ( argv , argc , memory ) ;
239
+ const result = func ( ...args ) ;
240
+ return JSValue . writeAndReturnKindBits (
241
+ result ,
242
+ payload1_ptr ,
243
+ payload2_ptr ,
244
+ false ,
245
+ this . memory
246
+ ) ;
270
247
} ,
271
248
272
249
swjs_call_function_with_this : (
273
250
obj_ref : ref ,
274
251
func_ref : ref ,
275
252
argv : pointer ,
276
253
argc : number ,
277
- kind_ptr : pointer ,
278
254
payload1_ptr : pointer ,
279
255
payload2_ptr : pointer
280
256
) => {
281
- const obj = this . memory . getObject ( obj_ref ) ;
282
- const func = this . memory . getObject ( func_ref ) ;
257
+ const memory = this . memory ;
258
+ const obj = memory . getObject ( obj_ref ) ;
259
+ const func = memory . getObject ( func_ref ) ;
283
260
let result : any ;
284
261
try {
285
- const args = JSValue . decodeArray ( argv , argc , this . memory ) ;
262
+ const args = JSValue . decodeArray ( argv , argc , memory ) ;
286
263
result = func . apply ( obj , args ) ;
287
264
} catch ( error ) {
288
- JSValue . write (
265
+ return JSValue . writeAndReturnKindBits (
289
266
error ,
290
- kind_ptr ,
291
267
payload1_ptr ,
292
268
payload2_ptr ,
293
269
true ,
294
270
this . memory
295
271
) ;
296
- return ;
297
272
}
298
- JSValue . write (
273
+ return JSValue . writeAndReturnKindBits (
299
274
result ,
300
- kind_ptr ,
301
275
payload1_ptr ,
302
276
payload2_ptr ,
303
277
false ,
@@ -309,42 +283,28 @@ export class SwiftRuntime {
309
283
func_ref : ref ,
310
284
argv : pointer ,
311
285
argc : number ,
312
- kind_ptr : pointer ,
313
286
payload1_ptr : pointer ,
314
287
payload2_ptr : pointer
315
288
) => {
316
- const obj = this . memory . getObject ( obj_ref ) ;
317
- const func = this . memory . getObject ( func_ref ) ;
318
- let isException = true ;
319
- try {
320
- const args = JSValue . decodeArray ( argv , argc , this . memory ) ;
321
- const result = func . apply ( obj , args ) ;
322
- JSValue . write (
323
- result ,
324
- kind_ptr ,
325
- payload1_ptr ,
326
- payload2_ptr ,
327
- false ,
328
- this . memory
329
- ) ;
330
- isException = false ;
331
- } finally {
332
- if ( isException ) {
333
- JSValue . write (
334
- undefined ,
335
- kind_ptr ,
336
- payload1_ptr ,
337
- payload2_ptr ,
338
- true ,
339
- this . memory
340
- ) ;
341
- }
342
- }
289
+ const memory = this . memory ;
290
+ const obj = memory . getObject ( obj_ref ) ;
291
+ const func = memory . getObject ( func_ref ) ;
292
+ let result = undefined ;
293
+ const args = JSValue . decodeArray ( argv , argc , memory ) ;
294
+ result = func . apply ( obj , args ) ;
295
+ return JSValue . writeAndReturnKindBits (
296
+ result ,
297
+ payload1_ptr ,
298
+ payload2_ptr ,
299
+ false ,
300
+ this . memory
301
+ ) ;
343
302
} ,
344
303
345
304
swjs_call_new : ( ref : ref , argv : pointer , argc : number ) => {
346
- const constructor = this . memory . getObject ( ref ) ;
347
- const args = JSValue . decodeArray ( argv , argc , this . memory ) ;
305
+ const memory = this . memory ;
306
+ const constructor = memory . getObject ( ref ) ;
307
+ const args = JSValue . decodeArray ( argv , argc , memory ) ;
348
308
const instance = new constructor ( ...args ) ;
349
309
return this . memory . retain ( instance ) ;
350
310
} ,
@@ -356,10 +316,11 @@ export class SwiftRuntime {
356
316
exception_payload1_ptr : pointer ,
357
317
exception_payload2_ptr : pointer
358
318
) => {
359
- const constructor = this . memory . getObject ( ref ) ;
319
+ let memory = this . memory ;
320
+ const constructor = memory . getObject ( ref ) ;
360
321
let result : any ;
361
322
try {
362
- const args = JSValue . decodeArray ( argv , argc , this . memory ) ;
323
+ const args = JSValue . decodeArray ( argv , argc , memory ) ;
363
324
result = new constructor ( ...args ) ;
364
325
} catch ( error ) {
365
326
JSValue . write (
@@ -372,20 +333,22 @@ export class SwiftRuntime {
372
333
) ;
373
334
return - 1 ;
374
335
}
336
+ memory = this . memory ;
375
337
JSValue . write (
376
338
null ,
377
339
exception_kind_ptr ,
378
340
exception_payload1_ptr ,
379
341
exception_payload2_ptr ,
380
342
false ,
381
- this . memory
343
+ memory
382
344
) ;
383
- return this . memory . retain ( result ) ;
345
+ return memory . retain ( result ) ;
384
346
} ,
385
347
386
348
swjs_instanceof : ( obj_ref : ref , constructor_ref : ref ) => {
387
- const obj = this . memory . getObject ( obj_ref ) ;
388
- const constructor = this . memory . getObject ( constructor_ref ) ;
349
+ const memory = this . memory ;
350
+ const obj = memory . getObject ( obj_ref ) ;
351
+ const constructor = memory . getObject ( constructor_ref ) ;
389
352
return obj instanceof constructor ;
390
353
} ,
391
354
@@ -419,9 +382,10 @@ export class SwiftRuntime {
419
382
} ,
420
383
421
384
swjs_load_typed_array : ( ref : ref , buffer : pointer ) => {
422
- const typedArray = this . memory . getObject ( ref ) ;
385
+ const memory = this . memory ;
386
+ const typedArray = memory . getObject ( ref ) ;
423
387
const bytes = new Uint8Array ( typedArray . buffer ) ;
424
- this . memory . writeBytes ( buffer , bytes ) ;
388
+ memory . writeBytes ( buffer , bytes ) ;
425
389
} ,
426
390
427
391
swjs_release : ( ref : ref ) => {
0 commit comments