@locuschain/lib 0.1.7 → 0.1.10

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.
@@ -37,35 +37,34 @@ var wasmJson = {
37
37
 
38
38
  /* eslint-disable */
39
39
  function toUint8Array(s) {
40
- if (typeof atob === "function")
41
- return new Uint8Array(atob(s).split("").map(charCodeAt));
40
+ if (typeof atob === 'function')
41
+ return new Uint8Array(atob(s).split('').map(charCodeAt));
42
42
  // return (require('buf' + 'fer').Buffer).from(s, 'base64')
43
43
  }
44
44
  function charCodeAt(c) {
45
45
  return c.charCodeAt(0);
46
46
  }
47
47
  var wasm = toUint8Array(wasmJson.Base64);
48
- const Go = (() => {
48
+ (() => {
49
49
  const enosys = () => {
50
- const err = new Error("not implemented");
51
- err.code = "ENOSYS";
50
+ const err = new Error('not implemented');
51
+ err.code = 'ENOSYS';
52
52
  return err;
53
53
  };
54
- let localFs = globalThis.fs;
55
- if (!localFs) {
56
- let outputBuf = "";
57
- localFs = {
54
+ if (!globalThis.fs) {
55
+ let outputBuf = '';
56
+ globalThis.fs = {
58
57
  constants: {
59
58
  O_WRONLY: -1,
60
59
  O_RDWR: -1,
61
60
  O_CREAT: -1,
62
61
  O_TRUNC: -1,
63
62
  O_APPEND: -1,
64
- O_EXCL: -1,
63
+ O_EXCL: -1
65
64
  }, // unused
66
65
  writeSync(fd, buf) {
67
66
  outputBuf += decoder.decode(buf);
68
- const nl = outputBuf.lastIndexOf("\n");
67
+ const nl = outputBuf.lastIndexOf('\n');
69
68
  if (nl != -1) {
70
69
  console.log(outputBuf.substring(0, nl));
71
70
  outputBuf = outputBuf.substring(nl + 1);
@@ -148,7 +147,7 @@ const Go = (() => {
148
147
  },
149
148
  utimes(path, atime, mtime, callback) {
150
149
  callback(enosys());
151
- },
150
+ }
152
151
  };
153
152
  }
154
153
  // if (!globalThis.process) {
@@ -182,29 +181,29 @@ const Go = (() => {
182
181
  // };
183
182
  // }
184
183
  if (!globalThis.crypto) {
185
- throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)");
184
+ throw new Error('globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)');
186
185
  }
187
186
  if (!globalThis.performance) {
188
- throw new Error("globalThis.performance is not available, polyfill required (performance.now only)");
187
+ throw new Error('globalThis.performance is not available, polyfill required (performance.now only)');
189
188
  }
190
189
  if (!globalThis.TextEncoder) {
191
- throw new Error("globalThis.TextEncoder is not available, polyfill required");
190
+ throw new Error('globalThis.TextEncoder is not available, polyfill required');
192
191
  }
193
192
  if (!globalThis.TextDecoder) {
194
- throw new Error("globalThis.TextDecoder is not available, polyfill required");
193
+ throw new Error('globalThis.TextDecoder is not available, polyfill required');
195
194
  }
196
- const encoder = new TextEncoder("utf-8");
197
- const decoder = new TextDecoder("utf-8");
198
- return class {
195
+ const encoder = new TextEncoder('utf-8');
196
+ const decoder = new TextDecoder('utf-8');
197
+ globalThis.Go = class {
199
198
  constructor() {
200
- this.argv = ["js"];
199
+ this.argv = ['js'];
201
200
  this.env = {};
202
- this.exit = (code) => {
201
+ this.exit = code => {
203
202
  if (code !== 0) {
204
- console.warn("exit code:", code);
203
+ console.warn('exit code:', code);
205
204
  }
206
205
  };
207
- this._exitPromise = new Promise((resolve) => {
206
+ this._exitPromise = new Promise(resolve => {
208
207
  this._resolveExitPromise = resolve;
209
208
  });
210
209
  this._pendingEvent = null;
@@ -214,12 +213,12 @@ const Go = (() => {
214
213
  this.mem.setUint32(addr + 0, v, true);
215
214
  this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
216
215
  };
217
- const getInt64 = (addr) => {
216
+ const getInt64 = addr => {
218
217
  const low = this.mem.getUint32(addr + 0, true);
219
218
  const high = this.mem.getInt32(addr + 4, true);
220
219
  return low + high * 4294967296;
221
220
  };
222
- const loadValue = (addr) => {
221
+ const loadValue = addr => {
223
222
  const f = this.mem.getFloat64(addr, true);
224
223
  if (f === 0) {
225
224
  return undefined;
@@ -232,7 +231,7 @@ const Go = (() => {
232
231
  };
233
232
  const storeValue = (addr, v) => {
234
233
  const nanHead = 0x7ff80000;
235
- if (typeof v === "number" && v !== 0) {
234
+ if (typeof v === 'number' && v !== 0) {
236
235
  if (isNaN(v)) {
237
236
  this.mem.setUint32(addr + 4, nanHead, true);
238
237
  this.mem.setUint32(addr, 0, true);
@@ -258,30 +257,30 @@ const Go = (() => {
258
257
  this._goRefCounts[id]++;
259
258
  let typeFlag = 0;
260
259
  switch (typeof v) {
261
- case "object":
260
+ case 'object':
262
261
  if (v !== null) {
263
262
  typeFlag = 1;
264
263
  }
265
264
  break;
266
- case "string":
265
+ case 'string':
267
266
  typeFlag = 2;
268
267
  break;
269
- case "symbol":
268
+ case 'symbol':
270
269
  typeFlag = 3;
271
270
  break;
272
- case "function":
271
+ case 'function':
273
272
  typeFlag = 4;
274
273
  break;
275
274
  }
276
275
  this.mem.setUint32(addr + 4, nanHead | typeFlag, true);
277
276
  this.mem.setUint32(addr, id, true);
278
277
  };
279
- const loadSlice = (addr) => {
278
+ const loadSlice = addr => {
280
279
  const array = getInt64(addr + 0);
281
280
  const len = getInt64(addr + 8);
282
281
  return new Uint8Array(this._inst.exports.mem.buffer, array, len);
283
282
  };
284
- const loadSliceOfValues = (addr) => {
283
+ const loadSliceOfValues = addr => {
285
284
  const array = getInt64(addr + 0);
286
285
  const len = getInt64(addr + 8);
287
286
  const a = new Array(len);
@@ -290,7 +289,7 @@ const Go = (() => {
290
289
  }
291
290
  return a;
292
291
  };
293
- const loadString = (addr) => {
292
+ const loadString = addr => {
294
293
  const saddr = getInt64(addr + 0);
295
294
  const len = getInt64(addr + 8);
296
295
  return decoder.decode(new DataView(this._inst.exports.mem.buffer, saddr, len));
@@ -298,7 +297,7 @@ const Go = (() => {
298
297
  const timeOrigin = Date.now() - performance.now();
299
298
  this.importObject = {
300
299
  _gotest: {
301
- add: (a, b) => a + b,
300
+ add: (a, b) => a + b
302
301
  },
303
302
  gojs: {
304
303
  // Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
@@ -306,7 +305,7 @@ const Go = (() => {
306
305
  // function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
307
306
  // This changes the SP, thus we have to update the SP used by the imported function.
308
307
  // func wasmExit(code int32)
309
- "runtime.wasmExit": (sp) => {
308
+ 'runtime.wasmExit': sp => {
310
309
  sp >>>= 0;
311
310
  const code = this.mem.getInt32(sp + 8, true);
312
311
  this.exited = true;
@@ -318,31 +317,31 @@ const Go = (() => {
318
317
  this.exit(code);
319
318
  },
320
319
  // func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
321
- "runtime.wasmWrite": (sp) => {
320
+ 'runtime.wasmWrite': sp => {
322
321
  sp >>>= 0;
323
322
  const fd = getInt64(sp + 8);
324
323
  const p = getInt64(sp + 16);
325
324
  const n = this.mem.getInt32(sp + 24, true);
326
- localFs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n));
325
+ fs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n));
327
326
  },
328
327
  // func resetMemoryDataView()
329
- "runtime.resetMemoryDataView": (sp) => {
328
+ 'runtime.resetMemoryDataView': sp => {
330
329
  this.mem = new DataView(this._inst.exports.mem.buffer);
331
330
  },
332
331
  // func nanotime1() int64
333
- "runtime.nanotime1": (sp) => {
332
+ 'runtime.nanotime1': sp => {
334
333
  sp >>>= 0;
335
334
  setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
336
335
  },
337
336
  // func walltime() (sec int64, nsec int32)
338
- "runtime.walltime": (sp) => {
337
+ 'runtime.walltime': sp => {
339
338
  sp >>>= 0;
340
339
  const msec = new Date().getTime();
341
340
  setInt64(sp + 8, msec / 1000);
342
341
  this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true);
343
342
  },
344
343
  // func scheduleTimeoutEvent(delay int64) int32
345
- "runtime.scheduleTimeoutEvent": (sp) => {
344
+ 'runtime.scheduleTimeoutEvent': sp => {
346
345
  sp >>>= 0;
347
346
  const id = this._nextCallbackTimeoutID;
348
347
  this._nextCallbackTimeoutID++;
@@ -351,26 +350,26 @@ const Go = (() => {
351
350
  while (this._scheduledTimeouts.has(id)) {
352
351
  // for some reason Go failed to register the timeout event, log and try again
353
352
  // (temporary workaround for https://github.com/golang/go/issues/28975)
354
- console.warn("scheduleTimeoutEvent: missed timeout event");
353
+ console.warn('scheduleTimeoutEvent: missed timeout event');
355
354
  this._resume();
356
355
  }
357
356
  }, getInt64(sp + 8)));
358
357
  this.mem.setInt32(sp + 16, id, true);
359
358
  },
360
359
  // func clearTimeoutEvent(id int32)
361
- "runtime.clearTimeoutEvent": (sp) => {
360
+ 'runtime.clearTimeoutEvent': sp => {
362
361
  sp >>>= 0;
363
362
  const id = this.mem.getInt32(sp + 8, true);
364
363
  clearTimeout(this._scheduledTimeouts.get(id));
365
364
  this._scheduledTimeouts.delete(id);
366
365
  },
367
366
  // func getRandomData(r []byte)
368
- "runtime.getRandomData": (sp) => {
367
+ 'runtime.getRandomData': sp => {
369
368
  sp >>>= 0;
370
369
  crypto.getRandomValues(loadSlice(sp + 8));
371
370
  },
372
371
  // func finalizeRef(v ref)
373
- "syscall/js.finalizeRef": (sp) => {
372
+ 'syscall/js.finalizeRef': sp => {
374
373
  sp >>>= 0;
375
374
  const id = this.mem.getUint32(sp + 8, true);
376
375
  this._goRefCounts[id]--;
@@ -382,39 +381,39 @@ const Go = (() => {
382
381
  }
383
382
  },
384
383
  // func stringVal(value string) ref
385
- "syscall/js.stringVal": (sp) => {
384
+ 'syscall/js.stringVal': sp => {
386
385
  sp >>>= 0;
387
386
  storeValue(sp + 24, loadString(sp + 8));
388
387
  },
389
388
  // func valueGet(v ref, p string) ref
390
- "syscall/js.valueGet": (sp) => {
389
+ 'syscall/js.valueGet': sp => {
391
390
  sp >>>= 0;
392
391
  const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));
393
392
  sp = this._inst.exports.getsp() >>> 0; // see comment above
394
393
  storeValue(sp + 32, result);
395
394
  },
396
395
  // func valueSet(v ref, p string, x ref)
397
- "syscall/js.valueSet": (sp) => {
396
+ 'syscall/js.valueSet': sp => {
398
397
  sp >>>= 0;
399
398
  Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
400
399
  },
401
400
  // func valueDelete(v ref, p string)
402
- "syscall/js.valueDelete": (sp) => {
401
+ 'syscall/js.valueDelete': sp => {
403
402
  sp >>>= 0;
404
403
  Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));
405
404
  },
406
405
  // func valueIndex(v ref, i int) ref
407
- "syscall/js.valueIndex": (sp) => {
406
+ 'syscall/js.valueIndex': sp => {
408
407
  sp >>>= 0;
409
408
  storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
410
409
  },
411
410
  // valueSetIndex(v ref, i int, x ref)
412
- "syscall/js.valueSetIndex": (sp) => {
411
+ 'syscall/js.valueSetIndex': sp => {
413
412
  sp >>>= 0;
414
413
  Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));
415
414
  },
416
415
  // func valueCall(v ref, m string, args []ref) (ref, bool)
417
- "syscall/js.valueCall": (sp) => {
416
+ 'syscall/js.valueCall': sp => {
418
417
  sp >>>= 0;
419
418
  try {
420
419
  const v = loadValue(sp + 8);
@@ -432,7 +431,7 @@ const Go = (() => {
432
431
  }
433
432
  },
434
433
  // func valueInvoke(v ref, args []ref) (ref, bool)
435
- "syscall/js.valueInvoke": (sp) => {
434
+ 'syscall/js.valueInvoke': sp => {
436
435
  sp >>>= 0;
437
436
  try {
438
437
  const v = loadValue(sp + 8);
@@ -449,7 +448,7 @@ const Go = (() => {
449
448
  }
450
449
  },
451
450
  // func valueNew(v ref, args []ref) (ref, bool)
452
- "syscall/js.valueNew": (sp) => {
451
+ 'syscall/js.valueNew': sp => {
453
452
  sp >>>= 0;
454
453
  try {
455
454
  const v = loadValue(sp + 8);
@@ -466,30 +465,30 @@ const Go = (() => {
466
465
  }
467
466
  },
468
467
  // func valueLength(v ref) int
469
- "syscall/js.valueLength": (sp) => {
468
+ 'syscall/js.valueLength': sp => {
470
469
  sp >>>= 0;
471
470
  setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
472
471
  },
473
472
  // valuePrepareString(v ref) (ref, int)
474
- "syscall/js.valuePrepareString": (sp) => {
473
+ 'syscall/js.valuePrepareString': sp => {
475
474
  sp >>>= 0;
476
475
  const str = encoder.encode(String(loadValue(sp + 8)));
477
476
  storeValue(sp + 16, str);
478
477
  setInt64(sp + 24, str.length);
479
478
  },
480
479
  // valueLoadString(v ref, b []byte)
481
- "syscall/js.valueLoadString": (sp) => {
480
+ 'syscall/js.valueLoadString': sp => {
482
481
  sp >>>= 0;
483
482
  const str = loadValue(sp + 8);
484
483
  loadSlice(sp + 16).set(str);
485
484
  },
486
485
  // func valueInstanceOf(v ref, t ref) bool
487
- "syscall/js.valueInstanceOf": (sp) => {
486
+ 'syscall/js.valueInstanceOf': sp => {
488
487
  sp >>>= 0;
489
488
  this.mem.setUint8(sp + 24, loadValue(sp + 8) instanceof loadValue(sp + 16) ? 1 : 0);
490
489
  },
491
490
  // func copyBytesToGo(dst []byte, src ref) (int, bool)
492
- "syscall/js.copyBytesToGo": (sp) => {
491
+ 'syscall/js.copyBytesToGo': sp => {
493
492
  sp >>>= 0;
494
493
  const dst = loadSlice(sp + 8);
495
494
  const src = loadValue(sp + 32);
@@ -503,7 +502,7 @@ const Go = (() => {
503
502
  this.mem.setUint8(sp + 48, 1);
504
503
  },
505
504
  // func copyBytesToJS(dst ref, src []byte) (int, bool)
506
- "syscall/js.copyBytesToJS": (sp) => {
505
+ 'syscall/js.copyBytesToJS': sp => {
507
506
  sp >>>= 0;
508
507
  const dst = loadValue(sp + 8);
509
508
  const src = loadSlice(sp + 16);
@@ -516,16 +515,16 @@ const Go = (() => {
516
515
  setInt64(sp + 40, toCopy.length);
517
516
  this.mem.setUint8(sp + 48, 1);
518
517
  },
519
- debug: (value) => {
518
+ debug: value => {
520
519
  console.log(value);
521
- },
522
- },
520
+ }
521
+ }
523
522
  };
524
523
  }
525
524
  run(instance) {
526
525
  return __awaiter(this, void 0, void 0, function* () {
527
526
  if (!(instance instanceof WebAssembly.Instance)) {
528
- throw new Error("Go.run: WebAssembly.Instance expected");
527
+ throw new Error('Go.run: WebAssembly.Instance expected');
529
528
  }
530
529
  this._inst = instance;
531
530
  this.mem = new DataView(this._inst.exports.mem.buffer);
@@ -537,7 +536,7 @@ const Go = (() => {
537
536
  true,
538
537
  false,
539
538
  globalThis,
540
- this,
539
+ this
541
540
  ];
542
541
  this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id
543
542
  this._ids = new Map([
@@ -547,15 +546,15 @@ const Go = (() => {
547
546
  [true, 3],
548
547
  [false, 4],
549
548
  [globalThis, 5],
550
- [this, 6],
549
+ [this, 6]
551
550
  ]);
552
551
  this._idPool = []; // unused ids that have been garbage collected
553
552
  this.exited = false; // whether the Go program has exited
554
553
  // Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
555
554
  let offset = 4096;
556
- const strPtr = (str) => {
555
+ const strPtr = str => {
557
556
  const ptr = offset;
558
- const bytes = encoder.encode(str + "\0");
557
+ const bytes = encoder.encode(str + '\0');
559
558
  new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
560
559
  offset += bytes.length;
561
560
  if (offset % 8 !== 0) {
@@ -565,17 +564,17 @@ const Go = (() => {
565
564
  };
566
565
  const argc = this.argv.length;
567
566
  const argvPtrs = [];
568
- this.argv.forEach((arg) => {
567
+ this.argv.forEach(arg => {
569
568
  argvPtrs.push(strPtr(arg));
570
569
  });
571
570
  argvPtrs.push(0);
572
571
  const keys = Object.keys(this.env).sort();
573
- keys.forEach((key) => {
572
+ keys.forEach(key => {
574
573
  argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
575
574
  });
576
575
  argvPtrs.push(0);
577
576
  const argv = offset;
578
- argvPtrs.forEach((ptr) => {
577
+ argvPtrs.forEach(ptr => {
579
578
  this.mem.setUint32(offset, ptr, true);
580
579
  this.mem.setUint32(offset + 4, 0, true);
581
580
  offset += 8;
@@ -584,7 +583,7 @@ const Go = (() => {
584
583
  // Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr.
585
584
  const wasmMinDataAddr = 4096 + 8192;
586
585
  if (offset >= wasmMinDataAddr) {
587
- throw new Error("total length of command line and environment variables exceeds limit");
586
+ throw new Error('total length of command line and environment variables exceeds limit');
588
587
  }
589
588
  this._inst.exports.run(argc, argv);
590
589
  if (this.exited) {
@@ -595,7 +594,7 @@ const Go = (() => {
595
594
  }
596
595
  _resume() {
597
596
  if (this.exited) {
598
- throw new Error("Go program has already exited");
597
+ throw new Error('Go program has already exited');
599
598
  }
600
599
  this._inst.exports.resume();
601
600
  if (this.exited) {
@@ -615,8 +614,7 @@ const Go = (() => {
615
614
  })();
616
615
  ((function () {
617
616
  try {
618
- if (typeof WebAssembly === "object" &&
619
- typeof WebAssembly.instantiate === "function") {
617
+ if (typeof WebAssembly === 'object' && typeof WebAssembly.instantiate === 'function') {
620
618
  const module = new WebAssembly.Module(Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00));
621
619
  if (module instanceof WebAssembly.Module) {
622
620
  return new WebAssembly.Instance(module) instanceof WebAssembly.Instance;
@@ -626,8 +624,8 @@ const Go = (() => {
626
624
  catch (e) { }
627
625
  return false;
628
626
  }))();
629
- const go = new Go();
630
- const LCLIB_SEQ_KEY = "__LCLIB_WASM_SEQ__";
627
+ const go = new globalThis.Go();
628
+ const LCLIB_SEQ_KEY = '__LCLIB_WASM_SEQ__';
631
629
  if (globalThis[LCLIB_SEQ_KEY] === undefined) {
632
630
  globalThis[LCLIB_SEQ_KEY] = 0;
633
631
  }
@@ -644,14 +642,14 @@ function loadSync() {
644
642
  function load() {
645
643
  return new Promise((resolve, reject) => {
646
644
  WebAssembly.instantiate(wasm, go.importObject)
647
- .then((result) => {
645
+ .then(result => {
648
646
  globalThis.__LCLIB_WASM_CONFIG__ = lclibWasmConfig;
649
647
  go.run(result.instance);
650
648
  delete globalThis.__LCLIB_WASM_CONFIG__;
651
649
  resolve();
652
650
  })
653
- .catch((err) => {
654
- console.error("err:", err);
651
+ .catch(err => {
652
+ console.error('err:', err);
655
653
  reject();
656
654
  });
657
655
  });