-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathindex.ts
440 lines (417 loc) · 14 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import { SwiftClosureDeallocator } from "./closure-heap.js";
import {
LibraryFeatures,
ExportedFunctions,
ref,
pointer,
TypedArray,
ImportedFunctions,
} from "./types.js";
import * as JSValue from "./js-value.js";
import { Memory } from "./memory.js";
export class SwiftRuntime {
private _instance: WebAssembly.Instance | null;
private _memory: Memory | null;
private _closureDeallocator: SwiftClosureDeallocator | null;
private version: number = 708;
private textDecoder = new TextDecoder("utf-8");
private textEncoder = new TextEncoder(); // Only support utf-8
constructor() {
this._instance = null;
this._memory = null;
this._closureDeallocator = null;
}
setInstance(instance: WebAssembly.Instance) {
this._instance = instance;
if (typeof (this.exports as any)._start === "function") {
throw new Error(
`JavaScriptKit supports only WASI reactor ABI.
Please make sure you are building with:
-Xswiftc -Xclang-linker -Xswiftc -mexec-model=reactor
`
);
}
if (this.exports.swjs_library_version() != this.version) {
throw new Error(
`The versions of JavaScriptKit are incompatible.
WebAssembly runtime ${this.exports.swjs_library_version()} != JS runtime ${
this.version
}`
);
}
}
private get instance() {
if (!this._instance)
throw new Error("WebAssembly instance is not set yet");
return this._instance;
}
private get exports() {
return this.instance.exports as any as ExportedFunctions;
}
private get memory() {
if (!this._memory) {
this._memory = new Memory(this.instance.exports);
}
return this._memory;
}
private get closureDeallocator(): SwiftClosureDeallocator | null {
if (this._closureDeallocator) return this._closureDeallocator;
const features = this.exports.swjs_library_features();
const librarySupportsWeakRef =
(features & LibraryFeatures.WeakRefs) != 0;
if (librarySupportsWeakRef) {
this._closureDeallocator = new SwiftClosureDeallocator(
this.exports
);
}
return this._closureDeallocator;
}
private callHostFunction(host_func_id: number, line: number, file: string, args: any[]) {
const argc = args.length;
const argv = this.exports.swjs_prepare_host_function_call(argc);
for (let index = 0; index < args.length; index++) {
const argument = args[index];
const base = argv + 16 * index;
JSValue.write(
argument,
base,
base + 4,
base + 8,
false,
this.memory
);
}
let output: any;
// This ref is released by the swjs_call_host_function implementation
const callback_func_ref = this.memory.retain((result: any) => {
output = result;
});
const alreadyReleased = this.exports.swjs_call_host_function(
host_func_id,
argv,
argc,
callback_func_ref
);
if (alreadyReleased) {
throw new Error(`The JSClosure has been already released by Swift side. The closure is created at ${file}:${line}`);
}
this.exports.swjs_cleanup_host_function_call(argv);
return output;
}
/** @deprecated Use `wasmImports` instead */
importObjects = () => this.wasmImports;
readonly wasmImports: ImportedFunctions = {
swjs_set_prop: (
ref: ref,
name: ref,
kind: JSValue.Kind,
payload1: number,
payload2: number
) => {
const obj = this.memory.getObject(ref);
const key = this.memory.getObject(name);
const value = JSValue.decode(kind, payload1, payload2, this.memory);
obj[key] = value;
},
swjs_get_prop: (
ref: ref,
name: ref,
kind_ptr: pointer,
payload1_ptr: pointer,
payload2_ptr: pointer
) => {
const obj = this.memory.getObject(ref);
const key = this.memory.getObject(name);
const result = obj[key];
JSValue.write(
result,
kind_ptr,
payload1_ptr,
payload2_ptr,
false,
this.memory
);
},
swjs_set_subscript: (
ref: ref,
index: number,
kind: JSValue.Kind,
payload1: number,
payload2: number
) => {
const obj = this.memory.getObject(ref);
const value = JSValue.decode(kind, payload1, payload2, this.memory);
obj[index] = value;
},
swjs_get_subscript: (
ref: ref,
index: number,
kind_ptr: pointer,
payload1_ptr: pointer,
payload2_ptr: pointer
) => {
const obj = this.memory.getObject(ref);
const result = obj[index];
JSValue.write(
result,
kind_ptr,
payload1_ptr,
payload2_ptr,
false,
this.memory
);
},
swjs_encode_string: (ref: ref, bytes_ptr_result: pointer) => {
const bytes = this.textEncoder.encode(this.memory.getObject(ref));
const bytes_ptr = this.memory.retain(bytes);
this.memory.writeUint32(bytes_ptr_result, bytes_ptr);
return bytes.length;
},
swjs_decode_string: (bytes_ptr: pointer, length: number) => {
const bytes = this.memory
.bytes()
.subarray(bytes_ptr, bytes_ptr + length);
const string = this.textDecoder.decode(bytes);
return this.memory.retain(string);
},
swjs_load_string: (ref: ref, buffer: pointer) => {
const bytes = this.memory.getObject(ref);
this.memory.writeBytes(buffer, bytes);
},
swjs_call_function: (
ref: ref,
argv: pointer,
argc: number,
kind_ptr: pointer,
payload1_ptr: pointer,
payload2_ptr: pointer
) => {
const func = this.memory.getObject(ref);
let result: any;
try {
const args = JSValue.decodeArray(argv, argc, this.memory);
result = func(...args);
} catch (error) {
JSValue.write(
error,
kind_ptr,
payload1_ptr,
payload2_ptr,
true,
this.memory
);
return;
}
JSValue.write(
result,
kind_ptr,
payload1_ptr,
payload2_ptr,
false,
this.memory
);
},
swjs_call_function_no_catch: (
ref: ref,
argv: pointer,
argc: number,
kind_ptr: pointer,
payload1_ptr: pointer,
payload2_ptr: pointer
) => {
const func = this.memory.getObject(ref);
let isException = true;
try {
const args = JSValue.decodeArray(argv, argc, this.memory);
const result = func(...args);
JSValue.write(
result,
kind_ptr,
payload1_ptr,
payload2_ptr,
false,
this.memory
);
isException = false;
} finally {
if (isException) {
JSValue.write(
undefined,
kind_ptr,
payload1_ptr,
payload2_ptr,
true,
this.memory
);
}
}
},
swjs_call_function_with_this: (
obj_ref: ref,
func_ref: ref,
argv: pointer,
argc: number,
kind_ptr: pointer,
payload1_ptr: pointer,
payload2_ptr: pointer
) => {
const obj = this.memory.getObject(obj_ref);
const func = this.memory.getObject(func_ref);
let result: any;
try {
const args = JSValue.decodeArray(argv, argc, this.memory);
result = func.apply(obj, args);
} catch (error) {
JSValue.write(
error,
kind_ptr,
payload1_ptr,
payload2_ptr,
true,
this.memory
);
return;
}
JSValue.write(
result,
kind_ptr,
payload1_ptr,
payload2_ptr,
false,
this.memory
);
},
swjs_call_function_with_this_no_catch: (
obj_ref: ref,
func_ref: ref,
argv: pointer,
argc: number,
kind_ptr: pointer,
payload1_ptr: pointer,
payload2_ptr: pointer
) => {
const obj = this.memory.getObject(obj_ref);
const func = this.memory.getObject(func_ref);
let isException = true;
try {
const args = JSValue.decodeArray(argv, argc, this.memory);
const result = func.apply(obj, args);
JSValue.write(
result,
kind_ptr,
payload1_ptr,
payload2_ptr,
false,
this.memory
);
isException = false;
} finally {
if (isException) {
JSValue.write(
undefined,
kind_ptr,
payload1_ptr,
payload2_ptr,
true,
this.memory
);
}
}
},
swjs_call_new: (ref: ref, argv: pointer, argc: number) => {
const constructor = this.memory.getObject(ref);
const args = JSValue.decodeArray(argv, argc, this.memory);
const instance = new constructor(...args);
return this.memory.retain(instance);
},
swjs_call_throwing_new: (
ref: ref,
argv: pointer,
argc: number,
exception_kind_ptr: pointer,
exception_payload1_ptr: pointer,
exception_payload2_ptr: pointer
) => {
const constructor = this.memory.getObject(ref);
let result: any;
try {
const args = JSValue.decodeArray(argv, argc, this.memory);
result = new constructor(...args);
} catch (error) {
JSValue.write(
error,
exception_kind_ptr,
exception_payload1_ptr,
exception_payload2_ptr,
true,
this.memory
);
return -1;
}
JSValue.write(
null,
exception_kind_ptr,
exception_payload1_ptr,
exception_payload2_ptr,
false,
this.memory
);
return this.memory.retain(result);
},
swjs_instanceof: (obj_ref: ref, constructor_ref: ref) => {
const obj = this.memory.getObject(obj_ref);
const constructor = this.memory.getObject(constructor_ref);
return obj instanceof constructor;
},
swjs_create_function: (host_func_id: number, line: number, file: ref) => {
const fileString = this.memory.getObject(file) as string;
const func = (...args: any[]) =>
this.callHostFunction(host_func_id, line, fileString, args);
const func_ref = this.memory.retain(func);
this.closureDeallocator?.track(func, func_ref);
return func_ref;
},
swjs_create_typed_array: (
constructor_ref: ref,
elementsPtr: pointer,
length: number
) => {
const ArrayType: TypedArray =
this.memory.getObject(constructor_ref);
const array = new ArrayType(
this.memory.rawMemory.buffer,
elementsPtr,
length
);
// Call `.slice()` to copy the memory
return this.memory.retain(array.slice());
},
swjs_load_typed_array: (ref: ref, buffer: pointer) => {
const typedArray = this.memory.getObject(ref);
const bytes = new Uint8Array(typedArray.buffer);
this.memory.writeBytes(buffer, bytes);
},
swjs_release: (ref: ref) => {
this.memory.release(ref);
},
swjs_i64_to_bigint: (value: bigint, signed: number) => {
return this.memory.retain(
signed ? value : BigInt.asUintN(64, value)
);
},
swjs_bigint_to_i64: (ref: ref, signed: number) => {
const object = this.memory.getObject(ref);
if (typeof object !== "bigint") {
throw new Error(`Expected a BigInt, but got ${typeof object}`);
}
if (signed) {
return object;
} else {
if (object < BigInt(0)) {
return BigInt(0);
}
return BigInt.asIntN(64, object);
}
},
};
}