@onflow/cadence-parser 0.26.0 → 1.0.0-preview.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,689 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __commonJS = (cb, mod) => function __require() {
8
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
19
+ // If the importer is in node compatibility mode or this is not an ESM
20
+ // file that has been converted to a CommonJS file using a Babel-
21
+ // compatible transform (i.e. "__esModule" has not been set), then set
22
+ // "default" to the CommonJS "module.exports" for node compatibility.
23
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
24
+ mod
25
+ ));
26
+
27
+ // node_modules/global/window.js
28
+ var require_window = __commonJS({
29
+ "node_modules/global/window.js"(exports, module) {
30
+ var win;
31
+ if (typeof window !== "undefined") {
32
+ win = window;
33
+ } else if (typeof global !== "undefined") {
34
+ win = global;
35
+ } else if (typeof self !== "undefined") {
36
+ win = self;
37
+ } else {
38
+ win = {};
39
+ }
40
+ module.exports = win;
41
+ }
42
+ });
43
+
44
+ // (disabled):crypto
45
+ var require_crypto = __commonJS({
46
+ "(disabled):crypto"() {
47
+ }
48
+ });
49
+
50
+ // node_modules/get-random-values/index.js
51
+ var require_get_random_values = __commonJS({
52
+ "node_modules/get-random-values/index.js"(exports, module) {
53
+ var window2 = require_window();
54
+ var nodeCrypto = require_crypto();
55
+ function getRandomValues2(buf) {
56
+ if (window2.crypto && window2.crypto.getRandomValues) {
57
+ return window2.crypto.getRandomValues(buf);
58
+ }
59
+ if (typeof window2.msCrypto === "object" && typeof window2.msCrypto.getRandomValues === "function") {
60
+ return window2.msCrypto.getRandomValues(buf);
61
+ }
62
+ if (nodeCrypto.randomBytes) {
63
+ if (!(buf instanceof Uint8Array)) {
64
+ throw new TypeError("expected Uint8Array");
65
+ }
66
+ if (buf.length > 65536) {
67
+ var e = new Error();
68
+ e.code = 22;
69
+ e.message = "Failed to execute 'getRandomValues' on 'Crypto': The ArrayBufferView's byte length (" + buf.length + ") exceeds the number of bytes of entropy available via this API (65536).";
70
+ e.name = "QuotaExceededError";
71
+ throw e;
72
+ }
73
+ var bytes = nodeCrypto.randomBytes(buf.length);
74
+ buf.set(bytes);
75
+ return buf;
76
+ } else {
77
+ throw new Error("No secure random number generator available.");
78
+ }
79
+ }
80
+ module.exports = getRandomValues2;
81
+ }
82
+ });
83
+
84
+ // src/go.js
85
+ var import_get_random_values = __toESM(require_get_random_values());
86
+ var encoder = new TextEncoder("utf-8");
87
+ var decoder = new TextDecoder("utf-8");
88
+ var enosys = () => {
89
+ const err = new Error("not implemented");
90
+ err.code = "ENOSYS";
91
+ return err;
92
+ };
93
+ var outputBuf = "";
94
+ global.fs = {
95
+ constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 },
96
+ // unused
97
+ writeSync(fd, buf) {
98
+ outputBuf += decoder.decode(buf);
99
+ const nl = outputBuf.lastIndexOf("\n");
100
+ if (nl != -1) {
101
+ console.log(outputBuf.substr(0, nl));
102
+ outputBuf = outputBuf.substr(nl + 1);
103
+ }
104
+ return buf.length;
105
+ },
106
+ write(fd, buf, offset, length, position, callback) {
107
+ if (offset !== 0 || length !== buf.length || position !== null) {
108
+ callback(enosys());
109
+ return;
110
+ }
111
+ const n = this.writeSync(fd, buf);
112
+ callback(null, n);
113
+ },
114
+ chmod(path, mode, callback) {
115
+ callback(enosys());
116
+ },
117
+ chown(path, uid, gid, callback) {
118
+ callback(enosys());
119
+ },
120
+ close(fd, callback) {
121
+ callback(enosys());
122
+ },
123
+ fchmod(fd, mode, callback) {
124
+ callback(enosys());
125
+ },
126
+ fchown(fd, uid, gid, callback) {
127
+ callback(enosys());
128
+ },
129
+ fstat(fd, callback) {
130
+ callback(enosys());
131
+ },
132
+ fsync(fd, callback) {
133
+ callback(null);
134
+ },
135
+ ftruncate(fd, length, callback) {
136
+ callback(enosys());
137
+ },
138
+ lchown(path, uid, gid, callback) {
139
+ callback(enosys());
140
+ },
141
+ link(path, link, callback) {
142
+ callback(enosys());
143
+ },
144
+ lstat(path, callback) {
145
+ callback(enosys());
146
+ },
147
+ mkdir(path, perm, callback) {
148
+ callback(enosys());
149
+ },
150
+ open(path, flags, mode, callback) {
151
+ callback(enosys());
152
+ },
153
+ read(fd, buffer, offset, length, position, callback) {
154
+ callback(enosys());
155
+ },
156
+ readdir(path, callback) {
157
+ callback(enosys());
158
+ },
159
+ readlink(path, callback) {
160
+ callback(enosys());
161
+ },
162
+ rename(from, to, callback) {
163
+ callback(enosys());
164
+ },
165
+ rmdir(path, callback) {
166
+ callback(enosys());
167
+ },
168
+ stat(path, callback) {
169
+ callback(enosys());
170
+ },
171
+ symlink(path, link, callback) {
172
+ callback(enosys());
173
+ },
174
+ truncate(path, length, callback) {
175
+ callback(enosys());
176
+ },
177
+ unlink(path, callback) {
178
+ callback(enosys());
179
+ },
180
+ utimes(path, atime, mtime, callback) {
181
+ callback(enosys());
182
+ }
183
+ };
184
+ if (!global.process) {
185
+ global.process = {
186
+ getuid() {
187
+ return -1;
188
+ },
189
+ getgid() {
190
+ return -1;
191
+ },
192
+ geteuid() {
193
+ return -1;
194
+ },
195
+ getegid() {
196
+ return -1;
197
+ },
198
+ getgroups() {
199
+ throw enosys();
200
+ },
201
+ pid: -1,
202
+ ppid: -1,
203
+ umask() {
204
+ throw enosys();
205
+ },
206
+ cwd() {
207
+ throw enosys();
208
+ },
209
+ chdir() {
210
+ throw enosys();
211
+ }
212
+ };
213
+ }
214
+ var Go = class {
215
+ constructor() {
216
+ this.argv = ["js"];
217
+ this.env = {};
218
+ this.exit = (code) => {
219
+ if (code !== 0) {
220
+ console.warn("exit code:", code);
221
+ }
222
+ };
223
+ this._exitPromise = new Promise((resolve) => {
224
+ this._resolveExitPromise = resolve;
225
+ });
226
+ this._pendingEvent = null;
227
+ this._scheduledTimeouts = /* @__PURE__ */ new Map();
228
+ this._nextCallbackTimeoutID = 1;
229
+ const setInt64 = (addr, v) => {
230
+ this.mem.setUint32(addr + 0, v, true);
231
+ this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
232
+ };
233
+ const getInt64 = (addr) => {
234
+ const low = this.mem.getUint32(addr + 0, true);
235
+ const high = this.mem.getInt32(addr + 4, true);
236
+ return low + high * 4294967296;
237
+ };
238
+ const loadValue = (addr) => {
239
+ const f = this.mem.getFloat64(addr, true);
240
+ if (f === 0) {
241
+ return void 0;
242
+ }
243
+ if (!isNaN(f)) {
244
+ return f;
245
+ }
246
+ const id = this.mem.getUint32(addr, true);
247
+ return this._values[id];
248
+ };
249
+ const storeValue = (addr, v) => {
250
+ const nanHead = 2146959360;
251
+ if (typeof v === "number" && v !== 0) {
252
+ if (isNaN(v)) {
253
+ this.mem.setUint32(addr + 4, nanHead, true);
254
+ this.mem.setUint32(addr, 0, true);
255
+ return;
256
+ }
257
+ this.mem.setFloat64(addr, v, true);
258
+ return;
259
+ }
260
+ if (v === void 0) {
261
+ this.mem.setFloat64(addr, 0, true);
262
+ return;
263
+ }
264
+ let id = this._ids.get(v);
265
+ if (id === void 0) {
266
+ id = this._idPool.pop();
267
+ if (id === void 0) {
268
+ id = this._values.length;
269
+ }
270
+ this._values[id] = v;
271
+ this._goRefCounts[id] = 0;
272
+ this._ids.set(v, id);
273
+ }
274
+ this._goRefCounts[id]++;
275
+ let typeFlag = 0;
276
+ switch (typeof v) {
277
+ case "object":
278
+ if (v !== null) {
279
+ typeFlag = 1;
280
+ }
281
+ break;
282
+ case "string":
283
+ typeFlag = 2;
284
+ break;
285
+ case "symbol":
286
+ typeFlag = 3;
287
+ break;
288
+ case "function":
289
+ typeFlag = 4;
290
+ break;
291
+ }
292
+ this.mem.setUint32(addr + 4, nanHead | typeFlag, true);
293
+ this.mem.setUint32(addr, id, true);
294
+ };
295
+ const loadSlice = (addr) => {
296
+ const array = getInt64(addr + 0);
297
+ const len = getInt64(addr + 8);
298
+ return new Uint8Array(this._inst.exports.mem.buffer, array, len);
299
+ };
300
+ const loadSliceOfValues = (addr) => {
301
+ const array = getInt64(addr + 0);
302
+ const len = getInt64(addr + 8);
303
+ const a = new Array(len);
304
+ for (let i = 0; i < len; i++) {
305
+ a[i] = loadValue(array + i * 8);
306
+ }
307
+ return a;
308
+ };
309
+ const loadString = (addr) => {
310
+ const saddr = getInt64(addr + 0);
311
+ const len = getInt64(addr + 8);
312
+ return decoder.decode(new DataView(this._inst.exports.mem.buffer, saddr, len));
313
+ };
314
+ const timeOrigin = Date.now() - performance.now();
315
+ this.importObject = {
316
+ gojs: {
317
+ // Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
318
+ // may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
319
+ // function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
320
+ // This changes the SP, thus we have to update the SP used by the imported function.
321
+ // func wasmExit(code int32)
322
+ "runtime.wasmExit": (sp) => {
323
+ sp >>>= 0;
324
+ const code = this.mem.getInt32(sp + 8, true);
325
+ this.exited = true;
326
+ delete this._inst;
327
+ delete this._values;
328
+ delete this._goRefCounts;
329
+ delete this._ids;
330
+ delete this._idPool;
331
+ this.exit(code);
332
+ },
333
+ // func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
334
+ "runtime.wasmWrite": (sp) => {
335
+ sp >>>= 0;
336
+ const fd = getInt64(sp + 8);
337
+ const p = getInt64(sp + 16);
338
+ const n = this.mem.getInt32(sp + 24, true);
339
+ fs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n));
340
+ },
341
+ // func resetMemoryDataView()
342
+ "runtime.resetMemoryDataView": (sp) => {
343
+ sp >>>= 0;
344
+ this.mem = new DataView(this._inst.exports.mem.buffer);
345
+ },
346
+ // func nanotime1() int64
347
+ "runtime.nanotime1": (sp) => {
348
+ sp >>>= 0;
349
+ setInt64(sp + 8, (timeOrigin + performance.now()) * 1e6);
350
+ },
351
+ // func walltime() (sec int64, nsec int32)
352
+ "runtime.walltime": (sp) => {
353
+ sp >>>= 0;
354
+ const msec = (/* @__PURE__ */ new Date()).getTime();
355
+ setInt64(sp + 8, msec / 1e3);
356
+ this.mem.setInt32(sp + 16, msec % 1e3 * 1e6, true);
357
+ },
358
+ // func scheduleTimeoutEvent(delay int64) int32
359
+ "runtime.scheduleTimeoutEvent": (sp) => {
360
+ sp >>>= 0;
361
+ const id = this._nextCallbackTimeoutID;
362
+ this._nextCallbackTimeoutID++;
363
+ this._scheduledTimeouts.set(id, setTimeout(
364
+ () => {
365
+ this._resume();
366
+ while (this._scheduledTimeouts.has(id)) {
367
+ console.warn("scheduleTimeoutEvent: missed timeout event");
368
+ this._resume();
369
+ }
370
+ },
371
+ getInt64(sp + 8) + 1
372
+ // setTimeout has been seen to fire up to 1 millisecond early
373
+ ));
374
+ this.mem.setInt32(sp + 16, id, true);
375
+ },
376
+ // func clearTimeoutEvent(id int32)
377
+ "runtime.clearTimeoutEvent": (sp) => {
378
+ sp >>>= 0;
379
+ const id = this.mem.getInt32(sp + 8, true);
380
+ clearTimeout(this._scheduledTimeouts.get(id));
381
+ this._scheduledTimeouts.delete(id);
382
+ },
383
+ // func getRandomData(r []byte)
384
+ "runtime.getRandomData": (sp) => {
385
+ sp >>>= 0;
386
+ (0, import_get_random_values.default)(loadSlice(sp + 8));
387
+ },
388
+ // func finalizeRef(v ref)
389
+ "syscall/js.finalizeRef": (sp) => {
390
+ sp >>>= 0;
391
+ const id = this.mem.getUint32(sp + 8, true);
392
+ this._goRefCounts[id]--;
393
+ if (this._goRefCounts[id] === 0) {
394
+ const v = this._values[id];
395
+ this._values[id] = null;
396
+ this._ids.delete(v);
397
+ this._idPool.push(id);
398
+ }
399
+ },
400
+ // func stringVal(value string) ref
401
+ "syscall/js.stringVal": (sp) => {
402
+ sp >>>= 0;
403
+ storeValue(sp + 24, loadString(sp + 8));
404
+ },
405
+ // func valueGet(v ref, p string) ref
406
+ "syscall/js.valueGet": (sp) => {
407
+ sp >>>= 0;
408
+ const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));
409
+ sp = this._inst.exports.getsp() >>> 0;
410
+ storeValue(sp + 32, result);
411
+ },
412
+ // func valueSet(v ref, p string, x ref)
413
+ "syscall/js.valueSet": (sp) => {
414
+ sp >>>= 0;
415
+ Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
416
+ },
417
+ // func valueDelete(v ref, p string)
418
+ "syscall/js.valueDelete": (sp) => {
419
+ sp >>>= 0;
420
+ Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));
421
+ },
422
+ // func valueIndex(v ref, i int) ref
423
+ "syscall/js.valueIndex": (sp) => {
424
+ sp >>>= 0;
425
+ storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
426
+ },
427
+ // valueSetIndex(v ref, i int, x ref)
428
+ "syscall/js.valueSetIndex": (sp) => {
429
+ sp >>>= 0;
430
+ Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));
431
+ },
432
+ // func valueCall(v ref, m string, args []ref) (ref, bool)
433
+ "syscall/js.valueCall": (sp) => {
434
+ sp >>>= 0;
435
+ try {
436
+ const v = loadValue(sp + 8);
437
+ const m = Reflect.get(v, loadString(sp + 16));
438
+ const args = loadSliceOfValues(sp + 32);
439
+ const result = Reflect.apply(m, v, args);
440
+ sp = this._inst.exports.getsp() >>> 0;
441
+ storeValue(sp + 56, result);
442
+ this.mem.setUint8(sp + 64, 1);
443
+ } catch (err) {
444
+ sp = this._inst.exports.getsp() >>> 0;
445
+ storeValue(sp + 56, err);
446
+ this.mem.setUint8(sp + 64, 0);
447
+ }
448
+ },
449
+ // func valueInvoke(v ref, args []ref) (ref, bool)
450
+ "syscall/js.valueInvoke": (sp) => {
451
+ sp >>>= 0;
452
+ try {
453
+ const v = loadValue(sp + 8);
454
+ const args = loadSliceOfValues(sp + 16);
455
+ const result = Reflect.apply(v, void 0, args);
456
+ sp = this._inst.exports.getsp() >>> 0;
457
+ storeValue(sp + 40, result);
458
+ this.mem.setUint8(sp + 48, 1);
459
+ } catch (err) {
460
+ sp = this._inst.exports.getsp() >>> 0;
461
+ storeValue(sp + 40, err);
462
+ this.mem.setUint8(sp + 48, 0);
463
+ }
464
+ },
465
+ // func valueNew(v ref, args []ref) (ref, bool)
466
+ "syscall/js.valueNew": (sp) => {
467
+ sp >>>= 0;
468
+ try {
469
+ const v = loadValue(sp + 8);
470
+ const args = loadSliceOfValues(sp + 16);
471
+ const result = Reflect.construct(v, args);
472
+ sp = this._inst.exports.getsp() >>> 0;
473
+ storeValue(sp + 40, result);
474
+ this.mem.setUint8(sp + 48, 1);
475
+ } catch (err) {
476
+ sp = this._inst.exports.getsp() >>> 0;
477
+ storeValue(sp + 40, err);
478
+ this.mem.setUint8(sp + 48, 0);
479
+ }
480
+ },
481
+ // func valueLength(v ref) int
482
+ "syscall/js.valueLength": (sp) => {
483
+ sp >>>= 0;
484
+ setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
485
+ },
486
+ // valuePrepareString(v ref) (ref, int)
487
+ "syscall/js.valuePrepareString": (sp) => {
488
+ sp >>>= 0;
489
+ const str = encoder.encode(String(loadValue(sp + 8)));
490
+ storeValue(sp + 16, str);
491
+ setInt64(sp + 24, str.length);
492
+ },
493
+ // valueLoadString(v ref, b []byte)
494
+ "syscall/js.valueLoadString": (sp) => {
495
+ sp >>>= 0;
496
+ const str = loadValue(sp + 8);
497
+ loadSlice(sp + 16).set(str);
498
+ },
499
+ // func valueInstanceOf(v ref, t ref) bool
500
+ "syscall/js.valueInstanceOf": (sp) => {
501
+ sp >>>= 0;
502
+ this.mem.setUint8(sp + 24, loadValue(sp + 8) instanceof loadValue(sp + 16) ? 1 : 0);
503
+ },
504
+ // func copyBytesToGo(dst []byte, src ref) (int, bool)
505
+ "syscall/js.copyBytesToGo": (sp) => {
506
+ sp >>>= 0;
507
+ const dst = loadSlice(sp + 8);
508
+ const src = loadValue(sp + 32);
509
+ if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) {
510
+ this.mem.setUint8(sp + 48, 0);
511
+ return;
512
+ }
513
+ const toCopy = src.subarray(0, dst.length);
514
+ dst.set(toCopy);
515
+ setInt64(sp + 40, toCopy.length);
516
+ this.mem.setUint8(sp + 48, 1);
517
+ },
518
+ // func copyBytesToJS(dst ref, src []byte) (int, bool)
519
+ "syscall/js.copyBytesToJS": (sp) => {
520
+ sp >>>= 0;
521
+ const dst = loadValue(sp + 8);
522
+ const src = loadSlice(sp + 16);
523
+ if (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) {
524
+ this.mem.setUint8(sp + 48, 0);
525
+ return;
526
+ }
527
+ const toCopy = src.subarray(0, dst.length);
528
+ dst.set(toCopy);
529
+ setInt64(sp + 40, toCopy.length);
530
+ this.mem.setUint8(sp + 48, 1);
531
+ },
532
+ "debug": (value) => {
533
+ console.log(value);
534
+ }
535
+ }
536
+ };
537
+ }
538
+ async run(instance) {
539
+ if (!(instance instanceof WebAssembly.Instance)) {
540
+ throw new Error("Go.run: WebAssembly.Instance expected");
541
+ }
542
+ this._inst = instance;
543
+ this.mem = new DataView(this._inst.exports.mem.buffer);
544
+ this._values = [
545
+ // JS values that Go currently has references to, indexed by reference id
546
+ NaN,
547
+ 0,
548
+ null,
549
+ true,
550
+ false,
551
+ global,
552
+ this
553
+ ];
554
+ this._goRefCounts = new Array(this._values.length).fill(Infinity);
555
+ this._ids = /* @__PURE__ */ new Map([
556
+ // mapping from JS values to reference ids
557
+ [0, 1],
558
+ [null, 2],
559
+ [true, 3],
560
+ [false, 4],
561
+ [global, 5],
562
+ [this, 6]
563
+ ]);
564
+ this._idPool = [];
565
+ this.exited = false;
566
+ let offset = 4096;
567
+ const strPtr = (str) => {
568
+ const ptr = offset;
569
+ const bytes = encoder.encode(str + "\0");
570
+ new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
571
+ offset += bytes.length;
572
+ if (offset % 8 !== 0) {
573
+ offset += 8 - offset % 8;
574
+ }
575
+ return ptr;
576
+ };
577
+ const argc = this.argv.length;
578
+ const argvPtrs = [];
579
+ this.argv.forEach((arg) => {
580
+ argvPtrs.push(strPtr(arg));
581
+ });
582
+ argvPtrs.push(0);
583
+ const keys = Object.keys(this.env).sort();
584
+ keys.forEach((key) => {
585
+ argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
586
+ });
587
+ argvPtrs.push(0);
588
+ const argv = offset;
589
+ argvPtrs.forEach((ptr) => {
590
+ this.mem.setUint32(offset, ptr, true);
591
+ this.mem.setUint32(offset + 4, 0, true);
592
+ offset += 8;
593
+ });
594
+ const wasmMinDataAddr = 4096 + 8192;
595
+ if (offset >= wasmMinDataAddr) {
596
+ throw new Error("total length of command line and environment variables exceeds limit");
597
+ }
598
+ this._inst.exports.run(argc, argv);
599
+ if (this.exited) {
600
+ this._resolveExitPromise();
601
+ }
602
+ await this._exitPromise;
603
+ }
604
+ _resume() {
605
+ if (this.exited) {
606
+ throw new Error("Go program has already exited");
607
+ }
608
+ this._inst.exports.resume();
609
+ if (this.exited) {
610
+ this._resolveExitPromise();
611
+ }
612
+ }
613
+ _makeFuncWrapper(id) {
614
+ const go2 = this;
615
+ return function() {
616
+ const event = { id, this: this, args: arguments };
617
+ go2._pendingEvent = event;
618
+ go2._resume();
619
+ return event.result;
620
+ };
621
+ }
622
+ };
623
+ var go = new Go();
624
+
625
+ // src/index.ts
626
+ var CadenceParser = class _CadenceParser {
627
+ static {
628
+ this.functionNamePrefix = "CADENCE_PARSER";
629
+ }
630
+ static {
631
+ this.loaded = false;
632
+ }
633
+ static functionName(name) {
634
+ return `__${_CadenceParser.functionNamePrefix}_${name}__`;
635
+ }
636
+ static async create(binaryLocation) {
637
+ await this.ensureLoaded(binaryLocation);
638
+ return new _CadenceParser();
639
+ }
640
+ static async ensureLoaded(urlOrBinary) {
641
+ if (this.loaded) {
642
+ return;
643
+ }
644
+ this.setWriteSync();
645
+ await this.load(urlOrBinary);
646
+ this.loaded = true;
647
+ }
648
+ static async load(urlOrBinary) {
649
+ let instantiatedSource;
650
+ if (typeof urlOrBinary === "string") {
651
+ const binaryRequest = fetch(urlOrBinary);
652
+ instantiatedSource = await WebAssembly.instantiateStreaming(binaryRequest, go.importObject);
653
+ } else {
654
+ instantiatedSource = await WebAssembly.instantiate(urlOrBinary, go.importObject);
655
+ }
656
+ go.run(instantiatedSource.instance).then(() => {
657
+ });
658
+ }
659
+ constructor() {
660
+ }
661
+ parse(code) {
662
+ const result = global[_CadenceParser.functionName("parse")](code);
663
+ return JSON.parse(result);
664
+ }
665
+ // setWriteSync installs the writeSync filesystem handler that the Go WebAssembly binary calls
666
+ static setWriteSync() {
667
+ const outputBuffers = /* @__PURE__ */ new Map();
668
+ const decoder2 = new TextDecoder("utf-8");
669
+ global.fs.writeSync = function(fileDescriptor, buf) {
670
+ let outputBuffer = outputBuffers.get(fileDescriptor);
671
+ if (!outputBuffer) {
672
+ outputBuffer = "";
673
+ }
674
+ outputBuffer += decoder2.decode(buf);
675
+ const nl = outputBuffer.lastIndexOf("\n");
676
+ if (nl != -1) {
677
+ const lines = outputBuffer.substr(0, nl + 1);
678
+ console.debug(`(FD ${fileDescriptor}):`, lines);
679
+ outputBuffer = outputBuffer.substr(nl + 1);
680
+ }
681
+ outputBuffers.set(fileDescriptor, outputBuffer);
682
+ return buf.length;
683
+ };
684
+ }
685
+ };
686
+ export {
687
+ CadenceParser
688
+ };
689
+ //# sourceMappingURL=index.mjs.map