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