forked from swiftwasm/JavaScriptKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumAssociatedValue.Export.js
More file actions
497 lines (484 loc) · 21.6 KB
/
EnumAssociatedValue.Export.js
File metadata and controls
497 lines (484 loc) · 21.6 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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
// 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 const APIResult = {
Tag: {
Success: 0,
Failure: 1,
Flag: 2,
Rate: 3,
Precise: 4,
Info: 5,
}
};
const __bjs_createAPIResultHelpers = () => {
return (tmpParamInts, tmpParamF32s, tmpParamF64s, textEncoder, swift) => ({
lower: (value) => {
const enumTag = value.tag;
switch (enumTag) {
case APIResult.Tag.Success: {
const bytes = textEncoder.encode(value.param0);
const id = swift.memory.retain(bytes);
tmpParamInts.push(bytes.length);
tmpParamInts.push(id);
const cleanup = () => {
swift.memory.release(id);
};
return { caseId: APIResult.Tag.Success, cleanup };
}
case APIResult.Tag.Failure: {
tmpParamInts.push((value.param0 | 0));
const cleanup = undefined;
return { caseId: APIResult.Tag.Failure, cleanup };
}
case APIResult.Tag.Flag: {
tmpParamInts.push(value.param0 ? 1 : 0);
const cleanup = undefined;
return { caseId: APIResult.Tag.Flag, cleanup };
}
case APIResult.Tag.Rate: {
tmpParamF32s.push(Math.fround(value.param0));
const cleanup = undefined;
return { caseId: APIResult.Tag.Rate, cleanup };
}
case APIResult.Tag.Precise: {
tmpParamF64s.push(value.param0);
const cleanup = undefined;
return { caseId: APIResult.Tag.Precise, cleanup };
}
case APIResult.Tag.Info: {
const cleanup = undefined;
return { caseId: APIResult.Tag.Info, cleanup };
}
default: throw new Error("Unknown APIResult tag: " + String(enumTag));
}
},
raise: (tmpRetTag, tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s) => {
const tag = tmpRetTag | 0;
switch (tag) {
case APIResult.Tag.Success: {
const string = tmpRetStrings.pop();
return { tag: APIResult.Tag.Success, param0: string };
}
case APIResult.Tag.Failure: {
const int = tmpRetInts.pop();
return { tag: APIResult.Tag.Failure, param0: int };
}
case APIResult.Tag.Flag: {
const bool = tmpRetInts.pop();
return { tag: APIResult.Tag.Flag, param0: bool };
}
case APIResult.Tag.Rate: {
const f32 = tmpRetF32s.pop();
return { tag: APIResult.Tag.Rate, param0: f32 };
}
case APIResult.Tag.Precise: {
const f64 = tmpRetF64s.pop();
return { tag: APIResult.Tag.Precise, param0: f64 };
}
case APIResult.Tag.Info: return { tag: APIResult.Tag.Info };
default: throw new Error("Unknown APIResult tag returned from Swift: " + String(tag));
}
}
});
};
export const ComplexResult = {
Tag: {
Success: 0,
Error: 1,
Status: 2,
Coordinates: 3,
Comprehensive: 4,
Info: 5,
}
};
const __bjs_createComplexResultHelpers = () => {
return (tmpParamInts, tmpParamF32s, tmpParamF64s, textEncoder, swift) => ({
lower: (value) => {
const enumTag = value.tag;
switch (enumTag) {
case ComplexResult.Tag.Success: {
const bytes = textEncoder.encode(value.param0);
const id = swift.memory.retain(bytes);
tmpParamInts.push(bytes.length);
tmpParamInts.push(id);
const cleanup = () => {
swift.memory.release(id);
};
return { caseId: ComplexResult.Tag.Success, cleanup };
}
case ComplexResult.Tag.Error: {
tmpParamInts.push((value.param1 | 0));
const bytes = textEncoder.encode(value.param0);
const id = swift.memory.retain(bytes);
tmpParamInts.push(bytes.length);
tmpParamInts.push(id);
const cleanup = () => {
swift.memory.release(id);
};
return { caseId: ComplexResult.Tag.Error, cleanup };
}
case ComplexResult.Tag.Status: {
const bytes = textEncoder.encode(value.param2);
const id = swift.memory.retain(bytes);
tmpParamInts.push(bytes.length);
tmpParamInts.push(id);
tmpParamInts.push((value.param1 | 0));
tmpParamInts.push(value.param0 ? 1 : 0);
const cleanup = () => {
swift.memory.release(id);
};
return { caseId: ComplexResult.Tag.Status, cleanup };
}
case ComplexResult.Tag.Coordinates: {
tmpParamF64s.push(value.param2);
tmpParamF64s.push(value.param1);
tmpParamF64s.push(value.param0);
const cleanup = undefined;
return { caseId: ComplexResult.Tag.Coordinates, cleanup };
}
case ComplexResult.Tag.Comprehensive: {
const bytes = textEncoder.encode(value.param8);
const id = swift.memory.retain(bytes);
tmpParamInts.push(bytes.length);
tmpParamInts.push(id);
const bytes1 = textEncoder.encode(value.param7);
const id1 = swift.memory.retain(bytes1);
tmpParamInts.push(bytes1.length);
tmpParamInts.push(id1);
const bytes2 = textEncoder.encode(value.param6);
const id2 = swift.memory.retain(bytes2);
tmpParamInts.push(bytes2.length);
tmpParamInts.push(id2);
tmpParamF64s.push(value.param5);
tmpParamF64s.push(value.param4);
tmpParamInts.push((value.param3 | 0));
tmpParamInts.push((value.param2 | 0));
tmpParamInts.push(value.param1 ? 1 : 0);
tmpParamInts.push(value.param0 ? 1 : 0);
const cleanup = () => {
swift.memory.release(id);
swift.memory.release(id1);
swift.memory.release(id2);
};
return { caseId: ComplexResult.Tag.Comprehensive, cleanup };
}
case ComplexResult.Tag.Info: {
const cleanup = undefined;
return { caseId: ComplexResult.Tag.Info, cleanup };
}
default: throw new Error("Unknown ComplexResult tag: " + String(enumTag));
}
},
raise: (tmpRetTag, tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s) => {
const tag = tmpRetTag | 0;
switch (tag) {
case ComplexResult.Tag.Success: {
const string = tmpRetStrings.pop();
return { tag: ComplexResult.Tag.Success, param0: string };
}
case ComplexResult.Tag.Error: {
const int = tmpRetInts.pop();
const string = tmpRetStrings.pop();
return { tag: ComplexResult.Tag.Error, param0: string, param1: int };
}
case ComplexResult.Tag.Status: {
const string = tmpRetStrings.pop();
const int = tmpRetInts.pop();
const bool = tmpRetInts.pop();
return { tag: ComplexResult.Tag.Status, param0: bool, param1: int, param2: string };
}
case ComplexResult.Tag.Coordinates: {
const f64 = tmpRetF64s.pop();
const f641 = tmpRetF64s.pop();
const f642 = tmpRetF64s.pop();
return { tag: ComplexResult.Tag.Coordinates, param0: f642, param1: f641, param2: f64 };
}
case ComplexResult.Tag.Comprehensive: {
const string = tmpRetStrings.pop();
const string1 = tmpRetStrings.pop();
const string2 = tmpRetStrings.pop();
const f64 = tmpRetF64s.pop();
const f641 = tmpRetF64s.pop();
const int = tmpRetInts.pop();
const int1 = tmpRetInts.pop();
const bool = tmpRetInts.pop();
const bool1 = tmpRetInts.pop();
return { tag: ComplexResult.Tag.Comprehensive, param0: bool1, param1: bool, param2: int1, param3: int, param4: f641, param5: f64, param6: string2, param7: string1, param8: string };
}
case ComplexResult.Tag.Info: return { tag: ComplexResult.Tag.Info };
default: throw new Error("Unknown ComplexResult tag returned from Swift: " + String(tag));
}
}
});
};
export const Result = {
Tag: {
Success: 0,
Failure: 1,
Status: 2,
}
};
const __bjs_createResultHelpers = () => {
return (tmpParamInts, tmpParamF32s, tmpParamF64s, textEncoder, swift) => ({
lower: (value) => {
const enumTag = value.tag;
switch (enumTag) {
case Result.Tag.Success: {
const bytes = textEncoder.encode(value.param0);
const id = swift.memory.retain(bytes);
tmpParamInts.push(bytes.length);
tmpParamInts.push(id);
const cleanup = () => {
swift.memory.release(id);
};
return { caseId: Result.Tag.Success, cleanup };
}
case Result.Tag.Failure: {
tmpParamInts.push((value.param1 | 0));
const bytes = textEncoder.encode(value.param0);
const id = swift.memory.retain(bytes);
tmpParamInts.push(bytes.length);
tmpParamInts.push(id);
const cleanup = () => {
swift.memory.release(id);
};
return { caseId: Result.Tag.Failure, cleanup };
}
case Result.Tag.Status: {
const bytes = textEncoder.encode(value.param2);
const id = swift.memory.retain(bytes);
tmpParamInts.push(bytes.length);
tmpParamInts.push(id);
tmpParamInts.push((value.param1 | 0));
tmpParamInts.push(value.param0 ? 1 : 0);
const cleanup = () => {
swift.memory.release(id);
};
return { caseId: Result.Tag.Status, cleanup };
}
default: throw new Error("Unknown Result tag: " + String(enumTag));
}
},
raise: (tmpRetTag, tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s) => {
const tag = tmpRetTag | 0;
switch (tag) {
case Result.Tag.Success: {
const string = tmpRetStrings.pop();
return { tag: Result.Tag.Success, param0: string };
}
case Result.Tag.Failure: {
const int = tmpRetInts.pop();
const string = tmpRetStrings.pop();
return { tag: Result.Tag.Failure, param0: string, param1: int };
}
case Result.Tag.Status: {
const string = tmpRetStrings.pop();
const int = tmpRetInts.pop();
const bool = tmpRetInts.pop();
return { tag: Result.Tag.Status, param0: bool, param1: int, param2: string };
}
default: throw new Error("Unknown Result tag returned from Swift: " + String(tag));
}
}
});
};
export const NetworkingResult = {
Tag: {
Success: 0,
Failure: 1,
}
};
const __bjs_createNetworkingResultHelpers = () => {
return (tmpParamInts, tmpParamF32s, tmpParamF64s, textEncoder, swift) => ({
lower: (value) => {
const enumTag = value.tag;
switch (enumTag) {
case NetworkingResult.Tag.Success: {
const bytes = textEncoder.encode(value.param0);
const id = swift.memory.retain(bytes);
tmpParamInts.push(bytes.length);
tmpParamInts.push(id);
const cleanup = () => {
swift.memory.release(id);
};
return { caseId: NetworkingResult.Tag.Success, cleanup };
}
case NetworkingResult.Tag.Failure: {
tmpParamInts.push((value.param1 | 0));
const bytes = textEncoder.encode(value.param0);
const id = swift.memory.retain(bytes);
tmpParamInts.push(bytes.length);
tmpParamInts.push(id);
const cleanup = () => {
swift.memory.release(id);
};
return { caseId: NetworkingResult.Tag.Failure, cleanup };
}
default: throw new Error("Unknown NetworkingResult tag: " + String(enumTag));
}
},
raise: (tmpRetTag, tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s) => {
const tag = tmpRetTag | 0;
switch (tag) {
case NetworkingResult.Tag.Success: {
const string = tmpRetStrings.pop();
return { tag: NetworkingResult.Tag.Success, param0: string };
}
case NetworkingResult.Tag.Failure: {
const int = tmpRetInts.pop();
const string = tmpRetStrings.pop();
return { tag: NetworkingResult.Tag.Failure, param0: string, param1: int };
}
default: throw new Error("Unknown NetworkingResult tag returned from Swift: " + String(tag));
}
}
});
};
if (typeof globalThis.Utilities === 'undefined') {
globalThis.Utilities = {};
}
if (typeof globalThis.API === 'undefined') {
globalThis.API = {};
}
globalThis.Utilities.Result = Result;
globalThis.API.NetworkingResult = NetworkingResult;
export async function createInstantiator(options, swift) {
let instance;
let memory;
let setException;
const textDecoder = new TextDecoder("utf-8");
const textEncoder = new TextEncoder("utf-8");
let tmpRetString;
let tmpRetBytes;
let tmpRetException;
let tmpRetTag;
let tmpRetStrings = [];
let tmpRetInts = [];
let tmpRetF32s = [];
let tmpRetF64s = [];
let tmpParamInts = [];
let tmpParamF32s = [];
let tmpParamF64s = [];
const enumHelpers = {};
return {
/**
* @param {WebAssembly.Imports} importObject
*/
addImports: (importObject, importsContext) => {
const bjs = {};
importObject["bjs"] = bjs;
const imports = options.getImports(importsContext);
bjs["swift_js_return_string"] = function(ptr, len) {
const bytes = new Uint8Array(memory.buffer, ptr, len);
tmpRetString = textDecoder.decode(bytes);
}
bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) {
const source = swift.memory.getObject(sourceId);
const bytes = new Uint8Array(memory.buffer, bytesPtr);
bytes.set(source);
}
bjs["swift_js_make_js_string"] = function(ptr, len) {
const bytes = new Uint8Array(memory.buffer, ptr, len);
return swift.memory.retain(textDecoder.decode(bytes));
}
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_tag"] = function(tag) {
tmpRetTag = tag;
}
bjs["swift_js_push_int"] = function(v) {
tmpRetInts.push(v | 0);
}
bjs["swift_js_push_f32"] = function(v) {
tmpRetF32s.push(Math.fround(v));
}
bjs["swift_js_push_f64"] = function(v) {
tmpRetF64s.push(v);
}
bjs["swift_js_push_string"] = function(ptr, len) {
const bytes = new Uint8Array(memory.buffer, ptr, len);
const value = textDecoder.decode(bytes);
tmpRetStrings.push(value);
}
bjs["swift_js_pop_param_int32"] = function() {
return tmpParamInts.pop();
}
bjs["swift_js_pop_param_f32"] = function() {
return tmpParamF32s.pop();
}
bjs["swift_js_pop_param_f64"] = function() {
return tmpParamF64s.pop();
}
},
setInstance: (i) => {
instance = i;
memory = instance.exports.memory;
const APIResultHelpers = __bjs_createAPIResultHelpers()(tmpParamInts, tmpParamF32s, tmpParamF64s, textEncoder, swift);
enumHelpers.APIResult = APIResultHelpers;
const ComplexResultHelpers = __bjs_createComplexResultHelpers()(tmpParamInts, tmpParamF32s, tmpParamF64s, textEncoder, swift);
enumHelpers.ComplexResult = ComplexResultHelpers;
const ResultHelpers = __bjs_createResultHelpers()(tmpParamInts, tmpParamF32s, tmpParamF64s, textEncoder, swift);
enumHelpers.Result = ResultHelpers;
const NetworkingResultHelpers = __bjs_createNetworkingResultHelpers()(tmpParamInts, tmpParamF32s, tmpParamF64s, textEncoder, swift);
enumHelpers.NetworkingResult = NetworkingResultHelpers;
setException = (error) => {
instance.exports._swift_js_exception.value = swift.memory.retain(error)
}
},
/** @param {WebAssembly.Instance} instance */
createExports: (instance) => {
const js = swift.memory.heap;
return {
handle: function bjs_handle(result) {
const { caseId: resultCaseId, cleanup: resultCleanup } = enumHelpers.APIResult.lower(result);
instance.exports.bjs_handle(resultCaseId);
if (resultCleanup) { resultCleanup(); }
},
getResult: function bjs_getResult() {
instance.exports.bjs_getResult();
const ret = enumHelpers.APIResult.raise(tmpRetTag, tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s);
return ret;
},
roundtripAPIResult: function bjs_roundtripAPIResult(result) {
const { caseId: resultCaseId, cleanup: resultCleanup } = enumHelpers.APIResult.lower(result);
instance.exports.bjs_roundtripAPIResult(resultCaseId);
const ret = enumHelpers.APIResult.raise(tmpRetTag, tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s);
if (resultCleanup) { resultCleanup(); }
return ret;
},
handleComplex: function bjs_handleComplex(result) {
const { caseId: resultCaseId, cleanup: resultCleanup } = enumHelpers.ComplexResult.lower(result);
instance.exports.bjs_handleComplex(resultCaseId);
if (resultCleanup) { resultCleanup(); }
},
getComplexResult: function bjs_getComplexResult() {
instance.exports.bjs_getComplexResult();
const ret = enumHelpers.ComplexResult.raise(tmpRetTag, tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s);
return ret;
},
roundtripComplexResult: function bjs_roundtripComplexResult(result) {
const { caseId: resultCaseId, cleanup: resultCleanup } = enumHelpers.ComplexResult.lower(result);
instance.exports.bjs_roundtripComplexResult(resultCaseId);
const ret = enumHelpers.ComplexResult.raise(tmpRetTag, tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s);
if (resultCleanup) { resultCleanup(); }
return ret;
},
};
},
}
}