forked from swiftwasm/JavaScriptKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncStaticImport.js
More file actions
426 lines (419 loc) · 16.9 KB
/
Copy pathAsyncStaticImport.js
File metadata and controls
426 lines (419 loc) · 16.9 KB
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
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
// DO NOT EDIT.
//
// To update this file, just rebuild your project or run
// `swift package bridge-js`.
export async function createInstantiator(options, swift) {
let instance;
let memory;
let setException;
let decodeString;
const textDecoder = new TextDecoder("utf-8");
const textEncoder = new TextEncoder("utf-8");
let tmpRetString;
let tmpRetBytes;
let tmpRetException;
let tmpRetOptionalBool;
let tmpRetOptionalInt;
let tmpRetOptionalFloat;
let tmpRetOptionalDouble;
let tmpRetOptionalHeapObject;
const _strEncCache = new Map();
const _strEncCacheMax = 4096;
function _cachedEncode(str) {
let encoded = _strEncCache.get(str);
if (encoded) {
_strEncCache.delete(str);
_strEncCache.set(str, encoded);
return encoded;
}
encoded = textEncoder.encode(str);
if (_strEncCache.size >= _strEncCacheMax) {
_strEncCache.delete(_strEncCache.keys().next().value);
}
_strEncCache.set(str, encoded);
return encoded;
}
function _maxUTF8Len(str) { return str.length * 3; }
let strStack = [];
let i32Stack = [];
let i64Stack = [];
let f32Stack = [];
let f64Stack = [];
let ptrStack = [];
const enumHelpers = {};
const structHelpers = {};
let _exports = null;
let bjs = null;
function __bjs_jsValueLower(value) {
let kind;
let payload1;
let payload2;
if (value === null) {
kind = 4;
payload1 = 0;
payload2 = 0;
} else {
switch (typeof value) {
case "boolean":
kind = 0;
payload1 = value ? 1 : 0;
payload2 = 0;
break;
case "number":
kind = 2;
payload1 = 0;
payload2 = value;
break;
case "string":
kind = 1;
payload1 = swift.memory.retain(value);
payload2 = 0;
break;
case "undefined":
kind = 5;
payload1 = 0;
payload2 = 0;
break;
case "object":
kind = 3;
payload1 = swift.memory.retain(value);
payload2 = 0;
break;
case "function":
kind = 3;
payload1 = swift.memory.retain(value);
payload2 = 0;
break;
case "symbol":
kind = 7;
payload1 = swift.memory.retain(value);
payload2 = 0;
break;
case "bigint":
kind = 8;
payload1 = swift.memory.retain(value);
payload2 = 0;
break;
default:
throw new TypeError("Unsupported JSValue type");
}
}
return [kind, payload1, payload2];
}
function __bjs_jsValueLift(kind, payload1, payload2) {
let jsValue;
switch (kind) {
case 0:
jsValue = payload1 !== 0;
break;
case 1:
jsValue = swift.memory.getObject(payload1);
break;
case 2:
jsValue = payload2;
break;
case 3:
jsValue = swift.memory.getObject(payload1);
break;
case 4:
jsValue = null;
break;
case 5:
jsValue = undefined;
break;
case 7:
jsValue = swift.memory.getObject(payload1);
break;
case 8:
jsValue = swift.memory.getObject(payload1);
break;
default:
throw new TypeError("Unsupported JSValue kind " + kind);
}
return jsValue;
}
const swiftClosureRegistry = (typeof FinalizationRegistry === "undefined") ? { register: () => {}, unregister: () => {} } : new FinalizationRegistry((state) => {
if (state.unregistered) { return; }
instance?.exports?.bjs_release_swift_closure(state.pointer);
});
const makeClosure = (pointer, file, line, func) => {
const state = { pointer, file, line, unregistered: false };
const real = (...args) => {
if (state.unregistered) {
const bytes = new Uint8Array(memory.buffer, state.file);
let length = 0;
while (bytes[length] !== 0) { length += 1; }
const fileID = decodeString(state.file, length);
throw new Error(`Attempted to call a released JSTypedClosure created at ${fileID}:${state.line}`);
}
return func(...args);
};
real.__unregister = () => {
if (state.unregistered) { return; }
state.unregistered = true;
swiftClosureRegistry.unregister(state);
};
swiftClosureRegistry.register(real, state, state);
return swift.memory.retain(real);
};
return {
/**
* @param {WebAssembly.Imports} importObject
*/
addImports: (importObject, importsContext) => {
bjs = {};
importObject["bjs"] = bjs;
bjs["swift_js_return_string"] = function(ptr, len) {
tmpRetString = decodeString(ptr, len);
}
bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) {
const source = swift.memory.getObject(sourceId);
swift.memory.release(sourceId);
const bytes = new Uint8Array(memory.buffer, bytesPtr);
bytes.set(source);
}
bjs["swift_js_init_memory_from_string"] = function(sourceId, bytesPtr) {
const str = swift.memory.getObject(sourceId);
swift.memory.release(sourceId);
const target = new Uint8Array(memory.buffer, bytesPtr);
const result = textEncoder.encodeInto(str, target);
return result.written;
}
bjs["swift_js_make_js_string"] = function(ptr, len) {
return swift.memory.retain(decodeString(ptr, len));
}
bjs["swift_js_init_memory_with_result"] = function(ptr, len) {
const target = new Uint8Array(memory.buffer, ptr, len);
target.set(tmpRetBytes);
tmpRetBytes = undefined;
}
bjs["swift_js_throw"] = function(id) {
tmpRetException = swift.memory.retainByRef(id);
}
bjs["swift_js_retain"] = function(id) {
return swift.memory.retainByRef(id);
}
bjs["swift_js_release"] = function(id) {
swift.memory.release(id);
}
bjs["swift_js_push_i32"] = function(v) {
i32Stack.push(v | 0);
}
bjs["swift_js_push_f32"] = function(v) {
f32Stack.push(Math.fround(v));
}
bjs["swift_js_push_f64"] = function(v) {
f64Stack.push(v);
}
bjs["swift_js_push_string"] = function(ptr, len) {
const value = decodeString(ptr, len);
strStack.push(value);
}
bjs["swift_js_pop_i32"] = function() {
return i32Stack.pop();
}
bjs["swift_js_pop_f32"] = function() {
return f32Stack.pop();
}
bjs["swift_js_pop_f64"] = function() {
return f64Stack.pop();
}
bjs["swift_js_push_pointer"] = function(pointer) {
ptrStack.push(pointer);
}
bjs["swift_js_pop_pointer"] = function() {
return ptrStack.pop();
}
bjs["swift_js_push_i64"] = function(v) {
i64Stack.push(v);
}
bjs["swift_js_pop_i64"] = function() {
return i64Stack.pop();
}
bjs["swift_js_return_optional_bool"] = function(isSome, value) {
if (isSome === 0) {
tmpRetOptionalBool = null;
} else {
tmpRetOptionalBool = value !== 0;
}
}
bjs["swift_js_return_optional_int"] = function(isSome, value) {
if (isSome === 0) {
tmpRetOptionalInt = null;
} else {
tmpRetOptionalInt = value | 0;
}
}
bjs["swift_js_return_optional_float"] = function(isSome, value) {
if (isSome === 0) {
tmpRetOptionalFloat = null;
} else {
tmpRetOptionalFloat = Math.fround(value);
}
}
bjs["swift_js_return_optional_double"] = function(isSome, value) {
if (isSome === 0) {
tmpRetOptionalDouble = null;
} else {
tmpRetOptionalDouble = value;
}
}
bjs["swift_js_return_optional_string"] = function(isSome, ptr, len) {
if (isSome === 0) {
tmpRetString = null;
} else {
tmpRetString = decodeString(ptr, len);
}
}
bjs["swift_js_return_optional_object"] = function(isSome, objectId) {
if (isSome === 0) {
tmpRetString = null;
} else {
tmpRetString = swift.memory.getObject(objectId);
}
}
bjs["swift_js_return_optional_heap_object"] = function(isSome, pointer) {
if (isSome === 0) {
tmpRetOptionalHeapObject = null;
} else {
tmpRetOptionalHeapObject = pointer;
}
}
bjs["swift_js_get_optional_int_presence"] = function() {
return tmpRetOptionalInt != null ? 1 : 0;
}
bjs["swift_js_get_optional_int_value"] = function() {
const value = tmpRetOptionalInt;
tmpRetOptionalInt = undefined;
return value;
}
bjs["swift_js_get_optional_string"] = function() {
const str = tmpRetString;
tmpRetString = undefined;
if (str == null) {
return -1;
} else {
const bytes = textEncoder.encode(str);
tmpRetBytes = bytes;
return bytes.length;
}
}
bjs["swift_js_get_optional_float_presence"] = function() {
return tmpRetOptionalFloat != null ? 1 : 0;
}
bjs["swift_js_get_optional_float_value"] = function() {
const value = tmpRetOptionalFloat;
tmpRetOptionalFloat = undefined;
return value;
}
bjs["swift_js_get_optional_double_presence"] = function() {
return tmpRetOptionalDouble != null ? 1 : 0;
}
bjs["swift_js_get_optional_double_value"] = function() {
const value = tmpRetOptionalDouble;
tmpRetOptionalDouble = undefined;
return value;
}
bjs["swift_js_get_optional_heap_object_pointer"] = function() {
const pointer = tmpRetOptionalHeapObject;
tmpRetOptionalHeapObject = undefined;
return pointer || 0;
}
bjs["swift_js_closure_unregister"] = function(funcRef) {}
bjs["swift_js_closure_unregister"] = function(funcRef) {
const func = swift.memory.getObject(funcRef);
func.__unregister();
}
bjs["invoke_js_callback_TestModule_10TestModules7JSValueV_y"] = function(callbackId, param0Kind, param0Payload1, param0Payload2) {
try {
const callback = swift.memory.getObject(callbackId);
const jsValue = __bjs_jsValueLift(param0Kind, param0Payload1, param0Payload2);
callback(jsValue);
} catch (error) {
setException(error);
}
}
bjs["make_swift_closure_TestModule_10TestModules7JSValueV_y"] = function(boxPtr, file, line) {
const lower_closure_TestModule_10TestModules7JSValueV_y = function(param0) {
const [param0Kind, param0Payload1, param0Payload2] = __bjs_jsValueLower(param0);
instance.exports.invoke_swift_closure_TestModule_10TestModules7JSValueV_y(boxPtr, param0Kind, param0Payload1, param0Payload2);
if (tmpRetException) {
const error = swift.memory.getObject(tmpRetException);
swift.memory.release(tmpRetException);
tmpRetException = undefined;
throw error;
}
};
return makeClosure(boxPtr, file, line, lower_closure_TestModule_10TestModules7JSValueV_y);
}
bjs["invoke_js_callback_TestModule_10TestModulesSd_y"] = function(callbackId, param0) {
try {
const callback = swift.memory.getObject(callbackId);
callback(param0);
} catch (error) {
setException(error);
}
}
bjs["make_swift_closure_TestModule_10TestModulesSd_y"] = function(boxPtr, file, line) {
const lower_closure_TestModule_10TestModulesSd_y = function(param0) {
instance.exports.invoke_swift_closure_TestModule_10TestModulesSd_y(boxPtr, param0);
if (tmpRetException) {
const error = swift.memory.getObject(tmpRetException);
swift.memory.release(tmpRetException);
tmpRetException = undefined;
throw error;
}
};
return makeClosure(boxPtr, file, line, lower_closure_TestModule_10TestModulesSd_y);
}
bjs["invoke_js_callback_TestModule_10TestModuley_y"] = function(callbackId) {
try {
const callback = swift.memory.getObject(callbackId);
callback();
} catch (error) {
setException(error);
}
}
bjs["make_swift_closure_TestModule_10TestModuley_y"] = function(boxPtr, file, line) {
const lower_closure_TestModule_10TestModuley_y = function() {
instance.exports.invoke_swift_closure_TestModule_10TestModuley_y(boxPtr);
if (tmpRetException) {
const error = swift.memory.getObject(tmpRetException);
swift.memory.release(tmpRetException);
tmpRetException = undefined;
throw error;
}
};
return makeClosure(boxPtr, file, line, lower_closure_TestModule_10TestModuley_y);
}
const TestModule = importObject["TestModule"] = importObject["TestModule"] || {};
TestModule["bjs_AsyncBox_asyncStaticRoundTrip_static"] = function bjs_AsyncBox_asyncStaticRoundTrip_static(resolveRef, rejectRef, v) {
const resolve = swift.memory.getObject(resolveRef);
const reject = swift.memory.getObject(rejectRef);
imports.AsyncBox.asyncStaticRoundTrip(v).then(resolve, reject);
}
TestModule["bjs_AsyncBox_asyncStaticVoid_static"] = function bjs_AsyncBox_asyncStaticVoid_static(resolveRef, rejectRef) {
const resolve = swift.memory.getObject(resolveRef);
const reject = swift.memory.getObject(rejectRef);
imports.AsyncBox.asyncStaticVoid().then(resolve, reject);
}
},
setInstance: (i) => {
instance = i;
memory = instance.exports.memory;
decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); }
setException = (error) => {
instance.exports._swift_js_exception.value = swift.memory.retain(error)
}
},
/** @param {WebAssembly.Instance} instance */
createExports: (instance) => {
const js = swift.memory.heap;
const exports = {
};
_exports = exports;
return exports;
},
}
}