@@ -29,13 +29,11 @@ enum JavaScriptValueKind {
29
29
Function = 6 ,
30
30
}
31
31
32
- export class SwiftRuntime {
33
- private instance : WebAssembly . Instance | null ;
32
+ class SwiftRuntimeHeap {
34
33
private _heapValues : Map < number , any > ;
35
34
private _heapNextKey : number ;
36
35
37
36
constructor ( ) {
38
- this . instance = null ;
39
37
let _global : any ;
40
38
if ( typeof window !== "undefined" ) {
41
39
_global = window
@@ -48,6 +46,41 @@ export class SwiftRuntime {
48
46
this . _heapNextKey = 1 ;
49
47
}
50
48
49
+ allocHeap ( value : any ) {
50
+ const isObject = typeof value == "object"
51
+ if ( isObject && value . swjs_heap_id ) {
52
+ return value . swjs_heap_id
53
+ }
54
+ const id = this . _heapNextKey ++ ;
55
+ this . _heapValues . set ( id , value )
56
+ if ( isObject )
57
+ Reflect . set ( value , "swjs_heap_id" , id ) ;
58
+ return id
59
+ }
60
+
61
+ freeHeap ( ref : ref ) {
62
+ const value = this . _heapValues . get ( ref ) ;
63
+ const isObject = typeof value == "object"
64
+ if ( isObject && value . swjs_heap_id ) {
65
+ delete value . swjs_heap_id ;
66
+ }
67
+ this . _heapValues . delete ( ref )
68
+ }
69
+
70
+ referenceHeap ( ref : ref ) {
71
+ return this . _heapValues . get ( ref )
72
+ }
73
+ }
74
+
75
+ export class SwiftRuntime {
76
+ private instance : WebAssembly . Instance | null ;
77
+ private heap : SwiftRuntimeHeap
78
+
79
+ constructor ( ) {
80
+ this . instance = null ;
81
+ this . heap = new SwiftRuntimeHeap ( ) ;
82
+ }
83
+
51
84
setInsance ( instance : WebAssembly . Instance ) {
52
85
this . instance = instance
53
86
}
@@ -59,30 +92,6 @@ export class SwiftRuntime {
59
92
throw new Error ( "WebAssembly instance is not set yet" ) ;
60
93
}
61
94
62
- const allocHeap = ( value : any ) => {
63
- const isObject = typeof value == "object"
64
- if ( isObject && value . swjs_heap_id ) {
65
- return value . swjs_heap_id
66
- }
67
- const id = this . _heapNextKey ++ ;
68
- this . _heapValues . set ( id , value )
69
- if ( isObject )
70
- Reflect . set ( value , "swjs_heap_id" , id ) ;
71
- return id
72
- }
73
-
74
- const freeHeap = ( ref : ref ) => {
75
- const value = this . _heapValues . get ( ref ) ;
76
- const isObject = typeof value == "object"
77
- if ( isObject && value . swjs_heap_id ) {
78
- delete value . swjs_heap_id ;
79
- }
80
- this . _heapValues . delete ( ref )
81
- }
82
-
83
- const referenceHeap = ( ref : ref ) => {
84
- return this . _heapValues . get ( ref )
85
- }
86
95
87
96
const callHostFunction = ( host_func_id : number , args : any [ ] ) => {
88
97
if ( ! this . instance )
@@ -99,7 +108,7 @@ export class SwiftRuntime {
99
108
writeUint32 ( base + 8 , value . payload2 )
100
109
}
101
110
let output : any ;
102
- const callback_func_ref = allocHeap ( function ( result : any ) {
111
+ const callback_func_ref = this . heap . allocHeap ( function ( result : any ) {
103
112
output = result
104
113
} )
105
114
exports . swjs_call_host_function ( host_func_id , argv , argc , callback_func_ref )
@@ -126,9 +135,9 @@ export class SwiftRuntime {
126
135
const readUInt32 = ( ptr : pointer ) => {
127
136
const uint8Memory = new Uint8Array ( memory ( ) . buffer ) ;
128
137
return uint8Memory [ ptr + 0 ]
129
- + ( uint8Memory [ ptr + 1 ] << 8 )
130
- + ( uint8Memory [ ptr + 2 ] << 16 )
131
- + ( uint8Memory [ ptr + 3 ] << 24 )
138
+ + ( uint8Memory [ ptr + 1 ] << 8 )
139
+ + ( uint8Memory [ ptr + 2 ] << 16 )
140
+ + ( uint8Memory [ ptr + 3 ] << 24 )
132
141
}
133
142
134
143
const writeUint32 = ( ptr : pointer , value : number ) => {
@@ -157,7 +166,7 @@ export class SwiftRuntime {
157
166
return readString ( payload1 , payload2 )
158
167
}
159
168
case JavaScriptValueKind . Object : {
160
- return referenceHeap ( payload1 )
169
+ return this . heap . referenceHeap ( payload1 )
161
170
}
162
171
case JavaScriptValueKind . Null : {
163
172
return null
@@ -166,7 +175,7 @@ export class SwiftRuntime {
166
175
return undefined
167
176
}
168
177
case JavaScriptValueKind . Function : {
169
- return referenceHeap ( payload1 )
178
+ return this . heap . referenceHeap ( payload1 )
170
179
}
171
180
default :
172
181
throw new Error ( `Type kind "${ kind } " is not supported` )
@@ -199,7 +208,7 @@ export class SwiftRuntime {
199
208
case "string" : {
200
209
return {
201
210
kind : JavaScriptValueKind . String ,
202
- payload1 : allocHeap ( value ) ,
211
+ payload1 : this . heap . allocHeap ( value ) ,
203
212
payload2 : value . length ,
204
213
}
205
214
}
@@ -213,14 +222,14 @@ export class SwiftRuntime {
213
222
case "object" : {
214
223
return {
215
224
kind : JavaScriptValueKind . Object ,
216
- payload1 : allocHeap ( value ) ,
225
+ payload1 : this . heap . allocHeap ( value ) ,
217
226
payload2 : 0 ,
218
227
}
219
228
}
220
229
case "function" : {
221
230
return {
222
231
kind : JavaScriptValueKind . Function ,
223
- payload1 : allocHeap ( value ) ,
232
+ payload1 : this . heap . allocHeap ( value ) ,
224
233
payload2 : 0 ,
225
234
}
226
235
}
@@ -250,15 +259,15 @@ export class SwiftRuntime {
250
259
kind : JavaScriptValueKind ,
251
260
payload1 : number , payload2 : number
252
261
) => {
253
- const obj = referenceHeap ( ref ) ;
262
+ const obj = this . heap . referenceHeap ( ref ) ;
254
263
Reflect . set ( obj , readString ( name , length ) , decodeValue ( kind , payload1 , payload2 ) )
255
264
} ,
256
265
swjs_get_prop : (
257
266
ref : ref , name : pointer , length : number ,
258
267
kind_ptr : pointer ,
259
268
payload1_ptr : pointer , payload2_ptr : pointer
260
269
) => {
261
- const obj = referenceHeap ( ref ) ;
270
+ const obj = this . heap . referenceHeap ( ref ) ;
262
271
const result = Reflect . get ( obj , readString ( name , length ) ) ;
263
272
const { kind, payload1, payload2 } = encodeValue ( result ) ;
264
273
writeUint32 ( kind_ptr , kind ) ;
@@ -270,31 +279,31 @@ export class SwiftRuntime {
270
279
kind : JavaScriptValueKind ,
271
280
payload1 : number , payload2 : number
272
281
) => {
273
- const obj = referenceHeap ( ref ) ;
282
+ const obj = this . heap . referenceHeap ( ref ) ;
274
283
Reflect . set ( obj , index , decodeValue ( kind , payload1 , payload2 ) )
275
284
} ,
276
285
swjs_get_subscript : (
277
286
ref : ref , index : number ,
278
287
kind_ptr : pointer ,
279
288
payload1_ptr : pointer , payload2_ptr : pointer
280
289
) => {
281
- const obj = referenceHeap ( ref ) ;
290
+ const obj = this . heap . referenceHeap ( ref ) ;
282
291
const result = Reflect . get ( obj , index ) ;
283
292
const { kind, payload1, payload2 } = encodeValue ( result ) ;
284
293
writeUint32 ( kind_ptr , kind ) ;
285
294
writeUint32 ( payload1_ptr , payload1 ) ;
286
295
writeUint32 ( payload2_ptr , payload2 ) ;
287
296
} ,
288
297
swjs_load_string : ( ref : ref , buffer : pointer ) => {
289
- const string = referenceHeap ( ref ) ;
298
+ const string = this . heap . referenceHeap ( ref ) ;
290
299
writeString ( buffer , string ) ;
291
300
} ,
292
301
swjs_call_function : (
293
302
ref : ref , argv : pointer , argc : number ,
294
303
kind_ptr : pointer ,
295
304
payload1_ptr : pointer , payload2_ptr : pointer
296
305
) => {
297
- const func = referenceHeap ( ref )
306
+ const func = this . heap . referenceHeap ( ref )
298
307
const result = Reflect . apply ( func , undefined , decodeValues ( argv , argc ) )
299
308
const { kind, payload1, payload2 } = encodeValue ( result ) ;
300
309
writeUint32 ( kind_ptr , kind ) ;
@@ -307,8 +316,8 @@ export class SwiftRuntime {
307
316
kind_ptr : pointer ,
308
317
payload1_ptr : pointer , payload2_ptr : pointer
309
318
) => {
310
- const obj = referenceHeap ( obj_ref )
311
- const func = referenceHeap ( func_ref )
319
+ const obj = this . heap . referenceHeap ( obj_ref )
320
+ const func = this . heap . referenceHeap ( func_ref )
312
321
const result = Reflect . apply ( func , obj , decodeValues ( argv , argc ) )
313
322
const { kind, payload1, payload2 } = encodeValue ( result ) ;
314
323
writeUint32 ( kind_ptr , kind ) ;
@@ -319,7 +328,7 @@ export class SwiftRuntime {
319
328
host_func_id : number ,
320
329
func_ref_ptr : pointer ,
321
330
) => {
322
- const func_ref = allocHeap ( function ( ) {
331
+ const func_ref = this . heap . allocHeap ( function ( ) {
323
332
return callHostFunction ( host_func_id , Array . prototype . slice . call ( arguments ) )
324
333
} )
325
334
writeUint32 ( func_ref_ptr , func_ref )
@@ -328,14 +337,14 @@ export class SwiftRuntime {
328
337
ref : ref , argv : pointer , argc : number ,
329
338
result_obj : pointer
330
339
) => {
331
- const obj = referenceHeap ( ref )
340
+ const obj = this . heap . referenceHeap ( ref )
332
341
const result = Reflect . construct ( obj , decodeValues ( argv , argc ) )
333
342
if ( typeof result != "object" )
334
343
throw Error ( `Invalid result type of object constructor of "${ obj } ": "${ result } "` )
335
- writeUint32 ( result_obj , allocHeap ( result ) ) ;
344
+ writeUint32 ( result_obj , this . heap . allocHeap ( result ) ) ;
336
345
} ,
337
346
swjs_destroy_ref : ( ref : ref ) => {
338
- freeHeap ( ref )
347
+ this . heap . freeHeap ( ref )
339
348
}
340
349
}
341
350
}
0 commit comments