locuschain-lib 1.1.4 → 1.1.6

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.
package/dist/lclib-amd.js CHANGED
@@ -1,916 +1,865 @@
1
1
  define(['exports'], (function (exports) { 'use strict';
2
2
 
3
- /******************************************************************************
4
- Copyright (c) Microsoft Corporation.
5
-
6
- Permission to use, copy, modify, and/or distribute this software for any
7
- purpose with or without fee is hereby granted.
8
-
9
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
- PERFORMANCE OF THIS SOFTWARE.
16
- ***************************************************************************** */
17
- /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
18
-
19
-
20
- function __awaiter(thisArg, _arguments, P, generator) {
21
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
22
- return new (P || (P = Promise))(function (resolve, reject) {
23
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
24
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
25
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
26
- step((generator = generator.apply(thisArg, _arguments || [])).next());
27
- });
3
+ /* eslint-disable */
4
+
5
+
6
+ function toUint8Array(s) {
7
+ if (typeof atob === "function")
8
+ return new Uint8Array(atob(s).split("").map(charCodeAt));
9
+ // return (require('buf' + 'fer').Buffer).from(s, 'base64')
10
+ }
11
+
12
+ function charCodeAt(c) {
13
+ return c.charCodeAt(0);
14
+ }
15
+
16
+ const wasmJson = require("./lclib.json");
17
+ var wasm = toUint8Array(wasmJson.Base64);
18
+
19
+ (() => {
20
+ const enosys = () => {
21
+ const err = new Error("not implemented");
22
+ err.code = "ENOSYS";
23
+ return err;
24
+ };
25
+
26
+ if (!globalThis.fs) {
27
+ let outputBuf = "";
28
+ globalThis.fs = {
29
+ constants: {
30
+ O_WRONLY: -1,
31
+ O_RDWR: -1,
32
+ O_CREAT: -1,
33
+ O_TRUNC: -1,
34
+ O_APPEND: -1,
35
+ O_EXCL: -1,
36
+ }, // unused
37
+ writeSync(fd, buf) {
38
+ outputBuf += decoder.decode(buf);
39
+ const nl = outputBuf.lastIndexOf("\n");
40
+ if (nl != -1) {
41
+ console.log(outputBuf.substring(0, nl));
42
+ outputBuf = outputBuf.substring(nl + 1);
43
+ }
44
+ return buf.length;
45
+ },
46
+ write(fd, buf, offset, length, position, callback) {
47
+ if (offset !== 0 || length !== buf.length || position !== null) {
48
+ callback(enosys());
49
+ return;
50
+ }
51
+ const n = this.writeSync(fd, buf);
52
+ callback(null, n);
53
+ },
54
+ chmod(path, mode, callback) {
55
+ callback(enosys());
56
+ },
57
+ chown(path, uid, gid, callback) {
58
+ callback(enosys());
59
+ },
60
+ close(fd, callback) {
61
+ callback(enosys());
62
+ },
63
+ fchmod(fd, mode, callback) {
64
+ callback(enosys());
65
+ },
66
+ fchown(fd, uid, gid, callback) {
67
+ callback(enosys());
68
+ },
69
+ fstat(fd, callback) {
70
+ callback(enosys());
71
+ },
72
+ fsync(fd, callback) {
73
+ callback(null);
74
+ },
75
+ ftruncate(fd, length, callback) {
76
+ callback(enosys());
77
+ },
78
+ lchown(path, uid, gid, callback) {
79
+ callback(enosys());
80
+ },
81
+ link(path, link, callback) {
82
+ callback(enosys());
83
+ },
84
+ lstat(path, callback) {
85
+ callback(enosys());
86
+ },
87
+ mkdir(path, perm, callback) {
88
+ callback(enosys());
89
+ },
90
+ open(path, flags, mode, callback) {
91
+ callback(enosys());
92
+ },
93
+ read(fd, buffer, offset, length, position, callback) {
94
+ callback(enosys());
95
+ },
96
+ readdir(path, callback) {
97
+ callback(enosys());
98
+ },
99
+ readlink(path, callback) {
100
+ callback(enosys());
101
+ },
102
+ rename(from, to, callback) {
103
+ callback(enosys());
104
+ },
105
+ rmdir(path, callback) {
106
+ callback(enosys());
107
+ },
108
+ stat(path, callback) {
109
+ callback(enosys());
110
+ },
111
+ symlink(path, link, callback) {
112
+ callback(enosys());
113
+ },
114
+ truncate(path, length, callback) {
115
+ callback(enosys());
116
+ },
117
+ unlink(path, callback) {
118
+ callback(enosys());
119
+ },
120
+ utimes(path, atime, mtime, callback) {
121
+ callback(enosys());
122
+ },
123
+ };
28
124
  }
29
125
 
30
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
31
- var e = new Error(message);
32
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
33
- };
34
-
35
- /* eslint-disable */
126
+ // if (!globalThis.process) {
127
+ // globalThis.process = {
128
+ // getuid() {
129
+ // return -1;
130
+ // },
131
+ // getgid() {
132
+ // return -1;
133
+ // },
134
+ // geteuid() {
135
+ // return -1;
136
+ // },
137
+ // getegid() {
138
+ // return -1;
139
+ // },
140
+ // getgroups() {
141
+ // throw enosys();
142
+ // },
143
+ // pid: -1,
144
+ // ppid: -1,
145
+ // umask() {
146
+ // throw enosys();
147
+ // },
148
+ // cwd() {
149
+ // throw enosys();
150
+ // },
151
+ // chdir() {
152
+ // throw enosys();
153
+ // }
154
+ // };
155
+ // }
156
+
157
+ if (!globalThis.crypto) {
158
+ throw new Error(
159
+ "globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)"
160
+ );
161
+ }
36
162
 
163
+ if (!globalThis.performance) {
164
+ throw new Error(
165
+ "globalThis.performance is not available, polyfill required (performance.now only)"
166
+ );
167
+ }
37
168
 
38
- function toUint8Array(s) {
39
- if (typeof atob === "function")
40
- return new Uint8Array(atob(s).split("").map(charCodeAt));
41
- // return (require('buf' + 'fer').Buffer).from(s, 'base64')
169
+ if (!globalThis.TextEncoder) {
170
+ throw new Error(
171
+ "globalThis.TextEncoder is not available, polyfill required"
172
+ );
42
173
  }
43
174
 
44
- function charCodeAt(c) {
45
- return c.charCodeAt(0);
175
+ if (!globalThis.TextDecoder) {
176
+ throw new Error(
177
+ "globalThis.TextDecoder is not available, polyfill required"
178
+ );
46
179
  }
47
180
 
48
- const wasmJson = require("./lclib.json");
49
- var wasm = toUint8Array(wasmJson.Base64);
181
+ const encoder = new TextEncoder("utf-8");
182
+ const decoder = new TextDecoder("utf-8");
50
183
 
51
- (() => {
52
- const enosys = () => {
53
- const err = new Error("not implemented");
54
- err.code = "ENOSYS";
55
- return err;
56
- };
184
+ globalThis.Go = class {
185
+ constructor() {
186
+ this.argv = ["js"];
187
+ this.env = {};
188
+ this.exit = (code) => {
189
+ if (code !== 0) {
190
+ console.warn("exit code:", code);
191
+ }
192
+ };
193
+ this._exitPromise = new Promise((resolve) => {
194
+ this._resolveExitPromise = resolve;
195
+ });
196
+ this._pendingEvent = null;
197
+ this._scheduledTimeouts = new Map();
198
+ this._nextCallbackTimeoutID = 1;
57
199
 
58
- if (!globalThis.fs) {
59
- let outputBuf = "";
60
- globalThis.fs = {
61
- constants: {
62
- O_WRONLY: -1,
63
- O_RDWR: -1,
64
- O_CREAT: -1,
65
- O_TRUNC: -1,
66
- O_APPEND: -1,
67
- O_EXCL: -1,
68
- }, // unused
69
- writeSync(fd, buf) {
70
- outputBuf += decoder.decode(buf);
71
- const nl = outputBuf.lastIndexOf("\n");
72
- if (nl != -1) {
73
- console.log(outputBuf.substring(0, nl));
74
- outputBuf = outputBuf.substring(nl + 1);
75
- }
76
- return buf.length;
77
- },
78
- write(fd, buf, offset, length, position, callback) {
79
- if (offset !== 0 || length !== buf.length || position !== null) {
80
- callback(enosys());
200
+ const setInt64 = (addr, v) => {
201
+ this.mem.setUint32(addr + 0, v, true);
202
+ this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
203
+ };
204
+
205
+ const getInt64 = (addr) => {
206
+ const low = this.mem.getUint32(addr + 0, true);
207
+ const high = this.mem.getInt32(addr + 4, true);
208
+ return low + high * 4294967296;
209
+ };
210
+
211
+ const loadValue = (addr) => {
212
+ const f = this.mem.getFloat64(addr, true);
213
+ if (f === 0) {
214
+ return undefined;
215
+ }
216
+ if (!isNaN(f)) {
217
+ return f;
218
+ }
219
+
220
+ const id = this.mem.getUint32(addr, true);
221
+ return this._values[id];
222
+ };
223
+
224
+ const storeValue = (addr, v) => {
225
+ const nanHead = 0x7ff80000;
226
+
227
+ if (typeof v === "number" && v !== 0) {
228
+ if (isNaN(v)) {
229
+ this.mem.setUint32(addr + 4, nanHead, true);
230
+ this.mem.setUint32(addr, 0, true);
81
231
  return;
82
232
  }
83
- const n = this.writeSync(fd, buf);
84
- callback(null, n);
85
- },
86
- chmod(path, mode, callback) {
87
- callback(enosys());
88
- },
89
- chown(path, uid, gid, callback) {
90
- callback(enosys());
91
- },
92
- close(fd, callback) {
93
- callback(enosys());
94
- },
95
- fchmod(fd, mode, callback) {
96
- callback(enosys());
97
- },
98
- fchown(fd, uid, gid, callback) {
99
- callback(enosys());
100
- },
101
- fstat(fd, callback) {
102
- callback(enosys());
103
- },
104
- fsync(fd, callback) {
105
- callback(null);
106
- },
107
- ftruncate(fd, length, callback) {
108
- callback(enosys());
109
- },
110
- lchown(path, uid, gid, callback) {
111
- callback(enosys());
112
- },
113
- link(path, link, callback) {
114
- callback(enosys());
115
- },
116
- lstat(path, callback) {
117
- callback(enosys());
118
- },
119
- mkdir(path, perm, callback) {
120
- callback(enosys());
121
- },
122
- open(path, flags, mode, callback) {
123
- callback(enosys());
124
- },
125
- read(fd, buffer, offset, length, position, callback) {
126
- callback(enosys());
127
- },
128
- readdir(path, callback) {
129
- callback(enosys());
130
- },
131
- readlink(path, callback) {
132
- callback(enosys());
133
- },
134
- rename(from, to, callback) {
135
- callback(enosys());
136
- },
137
- rmdir(path, callback) {
138
- callback(enosys());
139
- },
140
- stat(path, callback) {
141
- callback(enosys());
142
- },
143
- symlink(path, link, callback) {
144
- callback(enosys());
145
- },
146
- truncate(path, length, callback) {
147
- callback(enosys());
148
- },
149
- unlink(path, callback) {
150
- callback(enosys());
151
- },
152
- utimes(path, atime, mtime, callback) {
153
- callback(enosys());
154
- },
233
+ this.mem.setFloat64(addr, v, true);
234
+ return;
235
+ }
236
+
237
+ if (v === undefined) {
238
+ this.mem.setFloat64(addr, 0, true);
239
+ return;
240
+ }
241
+
242
+ let id = this._ids.get(v);
243
+ if (id === undefined) {
244
+ id = this._idPool.pop();
245
+ if (id === undefined) {
246
+ id = this._values.length;
247
+ }
248
+ this._values[id] = v;
249
+ this._goRefCounts[id] = 0;
250
+ this._ids.set(v, id);
251
+ }
252
+ this._goRefCounts[id]++;
253
+ let typeFlag = 0;
254
+ switch (typeof v) {
255
+ case "object":
256
+ if (v !== null) {
257
+ typeFlag = 1;
258
+ }
259
+ break;
260
+ case "string":
261
+ typeFlag = 2;
262
+ break;
263
+ case "symbol":
264
+ typeFlag = 3;
265
+ break;
266
+ case "function":
267
+ typeFlag = 4;
268
+ break;
269
+ }
270
+ this.mem.setUint32(addr + 4, nanHead | typeFlag, true);
271
+ this.mem.setUint32(addr, id, true);
155
272
  };
156
- }
157
273
 
158
- // if (!globalThis.process) {
159
- // globalThis.process = {
160
- // getuid() {
161
- // return -1;
162
- // },
163
- // getgid() {
164
- // return -1;
165
- // },
166
- // geteuid() {
167
- // return -1;
168
- // },
169
- // getegid() {
170
- // return -1;
171
- // },
172
- // getgroups() {
173
- // throw enosys();
174
- // },
175
- // pid: -1,
176
- // ppid: -1,
177
- // umask() {
178
- // throw enosys();
179
- // },
180
- // cwd() {
181
- // throw enosys();
182
- // },
183
- // chdir() {
184
- // throw enosys();
185
- // }
186
- // };
187
- // }
188
-
189
- if (!globalThis.crypto) {
190
- throw new Error(
191
- "globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)"
192
- );
193
- }
274
+ const loadSlice = (addr) => {
275
+ const array = getInt64(addr + 0);
276
+ const len = getInt64(addr + 8);
277
+ return new Uint8Array(this._inst.exports.mem.buffer, array, len);
278
+ };
194
279
 
195
- if (!globalThis.performance) {
196
- throw new Error(
197
- "globalThis.performance is not available, polyfill required (performance.now only)"
198
- );
199
- }
280
+ const loadSliceOfValues = (addr) => {
281
+ const array = getInt64(addr + 0);
282
+ const len = getInt64(addr + 8);
283
+ const a = new Array(len);
284
+ for (let i = 0; i < len; i++) {
285
+ a[i] = loadValue(array + i * 8);
286
+ }
287
+ return a;
288
+ };
200
289
 
201
- if (!globalThis.TextEncoder) {
202
- throw new Error(
203
- "globalThis.TextEncoder is not available, polyfill required"
204
- );
205
- }
290
+ const loadString = (addr) => {
291
+ const saddr = getInt64(addr + 0);
292
+ const len = getInt64(addr + 8);
293
+ return decoder.decode(
294
+ new DataView(this._inst.exports.mem.buffer, saddr, len)
295
+ );
296
+ };
206
297
 
207
- if (!globalThis.TextDecoder) {
208
- throw new Error(
209
- "globalThis.TextDecoder is not available, polyfill required"
210
- );
211
- }
298
+ const timeOrigin = Date.now() - performance.now();
299
+ this.importObject = {
300
+ _gotest: {
301
+ add: (a, b) => a + b,
302
+ },
303
+ gojs: {
304
+ // Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
305
+ // may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
306
+ // function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
307
+ // This changes the SP, thus we have to update the SP used by the imported function.
308
+
309
+ // func wasmExit(code int32)
310
+ "runtime.wasmExit": (sp) => {
311
+ sp >>>= 0;
312
+ const code = this.mem.getInt32(sp + 8, true);
313
+ this.exited = true;
314
+ delete this._inst;
315
+ delete this._values;
316
+ delete this._goRefCounts;
317
+ delete this._ids;
318
+ delete this._idPool;
319
+ this.exit(code);
320
+ },
212
321
 
213
- const encoder = new TextEncoder("utf-8");
214
- const decoder = new TextDecoder("utf-8");
322
+ // func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
323
+ "runtime.wasmWrite": (sp) => {
324
+ sp >>>= 0;
325
+ const fd = getInt64(sp + 8);
326
+ const p = getInt64(sp + 16);
327
+ const n = this.mem.getInt32(sp + 24, true);
328
+ fs.writeSync(
329
+ fd,
330
+ new Uint8Array(this._inst.exports.mem.buffer, p, n)
331
+ );
332
+ },
215
333
 
216
- globalThis.Go = class {
217
- constructor() {
218
- this.argv = ["js"];
219
- this.env = {};
220
- this.exit = (code) => {
221
- if (code !== 0) {
222
- console.warn("exit code:", code);
223
- }
224
- };
225
- this._exitPromise = new Promise((resolve) => {
226
- this._resolveExitPromise = resolve;
227
- });
228
- this._pendingEvent = null;
229
- this._scheduledTimeouts = new Map();
230
- this._nextCallbackTimeoutID = 1;
231
-
232
- const setInt64 = (addr, v) => {
233
- this.mem.setUint32(addr + 0, v, true);
234
- this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
235
- };
236
-
237
- const getInt64 = (addr) => {
238
- const low = this.mem.getUint32(addr + 0, true);
239
- const high = this.mem.getInt32(addr + 4, true);
240
- return low + high * 4294967296;
241
- };
242
-
243
- const loadValue = (addr) => {
244
- const f = this.mem.getFloat64(addr, true);
245
- if (f === 0) {
246
- return undefined;
247
- }
248
- if (!isNaN(f)) {
249
- return f;
250
- }
334
+ // func resetMemoryDataView()
335
+ "runtime.resetMemoryDataView": (sp) => {
336
+ this.mem = new DataView(this._inst.exports.mem.buffer);
337
+ },
338
+
339
+ // func nanotime1() int64
340
+ "runtime.nanotime1": (sp) => {
341
+ sp >>>= 0;
342
+ setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
343
+ },
251
344
 
252
- const id = this.mem.getUint32(addr, true);
253
- return this._values[id];
254
- };
345
+ // func walltime() (sec int64, nsec int32)
346
+ "runtime.walltime": (sp) => {
347
+ sp >>>= 0;
348
+ const msec = new Date().getTime();
349
+ setInt64(sp + 8, msec / 1000);
350
+ this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true);
351
+ },
255
352
 
256
- const storeValue = (addr, v) => {
257
- const nanHead = 0x7ff80000;
353
+ // func scheduleTimeoutEvent(delay int64) int32
354
+ "runtime.scheduleTimeoutEvent": (sp) => {
355
+ sp >>>= 0;
356
+ const id = this._nextCallbackTimeoutID;
357
+ this._nextCallbackTimeoutID++;
358
+ this._scheduledTimeouts.set(
359
+ id,
360
+ setTimeout(() => {
361
+ this._resume();
362
+ while (this._scheduledTimeouts.has(id)) {
363
+ // for some reason Go failed to register the timeout event, log and try again
364
+ // (temporary workaround for https://github.com/golang/go/issues/28975)
365
+ console.warn("scheduleTimeoutEvent: missed timeout event");
366
+ this._resume();
367
+ }
368
+ }, getInt64(sp + 8))
369
+ );
370
+ this.mem.setInt32(sp + 16, id, true);
371
+ },
258
372
 
259
- if (typeof v === "number" && v !== 0) {
260
- if (isNaN(v)) {
261
- this.mem.setUint32(addr + 4, nanHead, true);
262
- this.mem.setUint32(addr, 0, true);
263
- return;
373
+ // func clearTimeoutEvent(id int32)
374
+ "runtime.clearTimeoutEvent": (sp) => {
375
+ sp >>>= 0;
376
+ const id = this.mem.getInt32(sp + 8, true);
377
+ clearTimeout(this._scheduledTimeouts.get(id));
378
+ this._scheduledTimeouts.delete(id);
379
+ },
380
+
381
+ // func getRandomData(r []byte)
382
+ "runtime.getRandomData": (sp) => {
383
+ sp >>>= 0;
384
+ crypto.getRandomValues(loadSlice(sp + 8));
385
+ },
386
+
387
+ // func finalizeRef(v ref)
388
+ "syscall/js.finalizeRef": (sp) => {
389
+ sp >>>= 0;
390
+ const id = this.mem.getUint32(sp + 8, true);
391
+ this._goRefCounts[id]--;
392
+ if (this._goRefCounts[id] === 0) {
393
+ const v = this._values[id];
394
+ this._values[id] = null;
395
+ this._ids.delete(v);
396
+ this._idPool.push(id);
264
397
  }
265
- this.mem.setFloat64(addr, v, true);
266
- return;
267
- }
398
+ },
268
399
 
269
- if (v === undefined) {
270
- this.mem.setFloat64(addr, 0, true);
271
- return;
272
- }
400
+ // func stringVal(value string) ref
401
+ "syscall/js.stringVal": (sp) => {
402
+ sp >>>= 0;
403
+ storeValue(sp + 24, loadString(sp + 8));
404
+ },
273
405
 
274
- let id = this._ids.get(v);
275
- if (id === undefined) {
276
- id = this._idPool.pop();
277
- if (id === undefined) {
278
- id = this._values.length;
406
+ // func valueGet(v ref, p string) ref
407
+ "syscall/js.valueGet": (sp) => {
408
+ sp >>>= 0;
409
+ const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));
410
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
411
+ storeValue(sp + 32, result);
412
+ },
413
+
414
+ // func valueSet(v ref, p string, x ref)
415
+ "syscall/js.valueSet": (sp) => {
416
+ sp >>>= 0;
417
+ Reflect.set(
418
+ loadValue(sp + 8),
419
+ loadString(sp + 16),
420
+ loadValue(sp + 32)
421
+ );
422
+ },
423
+
424
+ // func valueDelete(v ref, p string)
425
+ "syscall/js.valueDelete": (sp) => {
426
+ sp >>>= 0;
427
+ Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));
428
+ },
429
+
430
+ // func valueIndex(v ref, i int) ref
431
+ "syscall/js.valueIndex": (sp) => {
432
+ sp >>>= 0;
433
+ storeValue(
434
+ sp + 24,
435
+ Reflect.get(loadValue(sp + 8), getInt64(sp + 16))
436
+ );
437
+ },
438
+
439
+ // valueSetIndex(v ref, i int, x ref)
440
+ "syscall/js.valueSetIndex": (sp) => {
441
+ sp >>>= 0;
442
+ Reflect.set(
443
+ loadValue(sp + 8),
444
+ getInt64(sp + 16),
445
+ loadValue(sp + 24)
446
+ );
447
+ },
448
+
449
+ // func valueCall(v ref, m string, args []ref) (ref, bool)
450
+ "syscall/js.valueCall": (sp) => {
451
+ sp >>>= 0;
452
+ try {
453
+ const v = loadValue(sp + 8);
454
+ const m = Reflect.get(v, loadString(sp + 16));
455
+ const args = loadSliceOfValues(sp + 32);
456
+ const result = Reflect.apply(m, v, args);
457
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
458
+ storeValue(sp + 56, result);
459
+ this.mem.setUint8(sp + 64, 1);
460
+ } catch (err) {
461
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
462
+ storeValue(sp + 56, err);
463
+ this.mem.setUint8(sp + 64, 0);
279
464
  }
280
- this._values[id] = v;
281
- this._goRefCounts[id] = 0;
282
- this._ids.set(v, id);
283
- }
284
- this._goRefCounts[id]++;
285
- let typeFlag = 0;
286
- switch (typeof v) {
287
- case "object":
288
- if (v !== null) {
289
- typeFlag = 1;
290
- }
291
- break;
292
- case "string":
293
- typeFlag = 2;
294
- break;
295
- case "symbol":
296
- typeFlag = 3;
297
- break;
298
- case "function":
299
- typeFlag = 4;
300
- break;
301
- }
302
- this.mem.setUint32(addr + 4, nanHead | typeFlag, true);
303
- this.mem.setUint32(addr, id, true);
304
- };
305
-
306
- const loadSlice = (addr) => {
307
- const array = getInt64(addr + 0);
308
- const len = getInt64(addr + 8);
309
- return new Uint8Array(this._inst.exports.mem.buffer, array, len);
310
- };
311
-
312
- const loadSliceOfValues = (addr) => {
313
- const array = getInt64(addr + 0);
314
- const len = getInt64(addr + 8);
315
- const a = new Array(len);
316
- for (let i = 0; i < len; i++) {
317
- a[i] = loadValue(array + i * 8);
318
- }
319
- return a;
320
- };
321
-
322
- const loadString = (addr) => {
323
- const saddr = getInt64(addr + 0);
324
- const len = getInt64(addr + 8);
325
- return decoder.decode(
326
- new DataView(this._inst.exports.mem.buffer, saddr, len)
327
- );
328
- };
329
-
330
- const timeOrigin = Date.now() - performance.now();
331
- this.importObject = {
332
- _gotest: {
333
- add: (a, b) => a + b,
334
465
  },
335
- gojs: {
336
- // Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
337
- // may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
338
- // function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
339
- // This changes the SP, thus we have to update the SP used by the imported function.
340
-
341
- // func wasmExit(code int32)
342
- "runtime.wasmExit": (sp) => {
343
- sp >>>= 0;
344
- const code = this.mem.getInt32(sp + 8, true);
345
- this.exited = true;
346
- delete this._inst;
347
- delete this._values;
348
- delete this._goRefCounts;
349
- delete this._ids;
350
- delete this._idPool;
351
- this.exit(code);
352
- },
353
-
354
- // func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
355
- "runtime.wasmWrite": (sp) => {
356
- sp >>>= 0;
357
- const fd = getInt64(sp + 8);
358
- const p = getInt64(sp + 16);
359
- const n = this.mem.getInt32(sp + 24, true);
360
- fs.writeSync(
361
- fd,
362
- new Uint8Array(this._inst.exports.mem.buffer, p, n)
363
- );
364
- },
365
-
366
- // func resetMemoryDataView()
367
- "runtime.resetMemoryDataView": (sp) => {
368
- this.mem = new DataView(this._inst.exports.mem.buffer);
369
- },
370
-
371
- // func nanotime1() int64
372
- "runtime.nanotime1": (sp) => {
373
- sp >>>= 0;
374
- setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
375
- },
376
-
377
- // func walltime() (sec int64, nsec int32)
378
- "runtime.walltime": (sp) => {
379
- sp >>>= 0;
380
- const msec = new Date().getTime();
381
- setInt64(sp + 8, msec / 1000);
382
- this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true);
383
- },
384
-
385
- // func scheduleTimeoutEvent(delay int64) int32
386
- "runtime.scheduleTimeoutEvent": (sp) => {
387
- sp >>>= 0;
388
- const id = this._nextCallbackTimeoutID;
389
- this._nextCallbackTimeoutID++;
390
- this._scheduledTimeouts.set(
391
- id,
392
- setTimeout(() => {
393
- this._resume();
394
- while (this._scheduledTimeouts.has(id)) {
395
- // for some reason Go failed to register the timeout event, log and try again
396
- // (temporary workaround for https://github.com/golang/go/issues/28975)
397
- console.warn("scheduleTimeoutEvent: missed timeout event");
398
- this._resume();
399
- }
400
- }, getInt64(sp + 8))
401
- );
402
- this.mem.setInt32(sp + 16, id, true);
403
- },
404
-
405
- // func clearTimeoutEvent(id int32)
406
- "runtime.clearTimeoutEvent": (sp) => {
407
- sp >>>= 0;
408
- const id = this.mem.getInt32(sp + 8, true);
409
- clearTimeout(this._scheduledTimeouts.get(id));
410
- this._scheduledTimeouts.delete(id);
411
- },
412
-
413
- // func getRandomData(r []byte)
414
- "runtime.getRandomData": (sp) => {
415
- sp >>>= 0;
416
- crypto.getRandomValues(loadSlice(sp + 8));
417
- },
418
-
419
- // func finalizeRef(v ref)
420
- "syscall/js.finalizeRef": (sp) => {
421
- sp >>>= 0;
422
- const id = this.mem.getUint32(sp + 8, true);
423
- this._goRefCounts[id]--;
424
- if (this._goRefCounts[id] === 0) {
425
- const v = this._values[id];
426
- this._values[id] = null;
427
- this._ids.delete(v);
428
- this._idPool.push(id);
429
- }
430
- },
431
-
432
- // func stringVal(value string) ref
433
- "syscall/js.stringVal": (sp) => {
434
- sp >>>= 0;
435
- storeValue(sp + 24, loadString(sp + 8));
436
- },
437
-
438
- // func valueGet(v ref, p string) ref
439
- "syscall/js.valueGet": (sp) => {
440
- sp >>>= 0;
441
- const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));
466
+
467
+ // func valueInvoke(v ref, args []ref) (ref, bool)
468
+ "syscall/js.valueInvoke": (sp) => {
469
+ sp >>>= 0;
470
+ try {
471
+ const v = loadValue(sp + 8);
472
+ const args = loadSliceOfValues(sp + 16);
473
+ const result = Reflect.apply(v, undefined, args);
442
474
  sp = this._inst.exports.getsp() >>> 0; // see comment above
443
- storeValue(sp + 32, result);
444
- },
445
-
446
- // func valueSet(v ref, p string, x ref)
447
- "syscall/js.valueSet": (sp) => {
448
- sp >>>= 0;
449
- Reflect.set(
450
- loadValue(sp + 8),
451
- loadString(sp + 16),
452
- loadValue(sp + 32)
453
- );
454
- },
455
-
456
- // func valueDelete(v ref, p string)
457
- "syscall/js.valueDelete": (sp) => {
458
- sp >>>= 0;
459
- Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));
460
- },
461
-
462
- // func valueIndex(v ref, i int) ref
463
- "syscall/js.valueIndex": (sp) => {
464
- sp >>>= 0;
465
- storeValue(
466
- sp + 24,
467
- Reflect.get(loadValue(sp + 8), getInt64(sp + 16))
468
- );
469
- },
470
-
471
- // valueSetIndex(v ref, i int, x ref)
472
- "syscall/js.valueSetIndex": (sp) => {
473
- sp >>>= 0;
474
- Reflect.set(
475
- loadValue(sp + 8),
476
- getInt64(sp + 16),
477
- loadValue(sp + 24)
478
- );
479
- },
480
-
481
- // func valueCall(v ref, m string, args []ref) (ref, bool)
482
- "syscall/js.valueCall": (sp) => {
483
- sp >>>= 0;
484
- try {
485
- const v = loadValue(sp + 8);
486
- const m = Reflect.get(v, loadString(sp + 16));
487
- const args = loadSliceOfValues(sp + 32);
488
- const result = Reflect.apply(m, v, args);
489
- sp = this._inst.exports.getsp() >>> 0; // see comment above
490
- storeValue(sp + 56, result);
491
- this.mem.setUint8(sp + 64, 1);
492
- } catch (err) {
493
- sp = this._inst.exports.getsp() >>> 0; // see comment above
494
- storeValue(sp + 56, err);
495
- this.mem.setUint8(sp + 64, 0);
496
- }
497
- },
498
-
499
- // func valueInvoke(v ref, args []ref) (ref, bool)
500
- "syscall/js.valueInvoke": (sp) => {
501
- sp >>>= 0;
502
- try {
503
- const v = loadValue(sp + 8);
504
- const args = loadSliceOfValues(sp + 16);
505
- const result = Reflect.apply(v, undefined, args);
506
- sp = this._inst.exports.getsp() >>> 0; // see comment above
507
- storeValue(sp + 40, result);
508
- this.mem.setUint8(sp + 48, 1);
509
- } catch (err) {
510
- sp = this._inst.exports.getsp() >>> 0; // see comment above
511
- storeValue(sp + 40, err);
512
- this.mem.setUint8(sp + 48, 0);
513
- }
514
- },
515
-
516
- // func valueNew(v ref, args []ref) (ref, bool)
517
- "syscall/js.valueNew": (sp) => {
518
- sp >>>= 0;
519
- try {
520
- const v = loadValue(sp + 8);
521
- const args = loadSliceOfValues(sp + 16);
522
- const result = Reflect.construct(v, args);
523
- sp = this._inst.exports.getsp() >>> 0; // see comment above
524
- storeValue(sp + 40, result);
525
- this.mem.setUint8(sp + 48, 1);
526
- } catch (err) {
527
- sp = this._inst.exports.getsp() >>> 0; // see comment above
528
- storeValue(sp + 40, err);
529
- this.mem.setUint8(sp + 48, 0);
530
- }
531
- },
532
-
533
- // func valueLength(v ref) int
534
- "syscall/js.valueLength": (sp) => {
535
- sp >>>= 0;
536
- setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
537
- },
538
-
539
- // valuePrepareString(v ref) (ref, int)
540
- "syscall/js.valuePrepareString": (sp) => {
541
- sp >>>= 0;
542
- const str = encoder.encode(String(loadValue(sp + 8)));
543
- storeValue(sp + 16, str);
544
- setInt64(sp + 24, str.length);
545
- },
546
-
547
- // valueLoadString(v ref, b []byte)
548
- "syscall/js.valueLoadString": (sp) => {
549
- sp >>>= 0;
550
- const str = loadValue(sp + 8);
551
- loadSlice(sp + 16).set(str);
552
- },
553
-
554
- // func valueInstanceOf(v ref, t ref) bool
555
- "syscall/js.valueInstanceOf": (sp) => {
556
- sp >>>= 0;
557
- this.mem.setUint8(
558
- sp + 24,
559
- loadValue(sp + 8) instanceof loadValue(sp + 16) ? 1 : 0
560
- );
561
- },
562
-
563
- // func copyBytesToGo(dst []byte, src ref) (int, bool)
564
- "syscall/js.copyBytesToGo": (sp) => {
565
- sp >>>= 0;
566
- const dst = loadSlice(sp + 8);
567
- const src = loadValue(sp + 32);
568
- if (
569
- !(src instanceof Uint8Array || src instanceof Uint8ClampedArray)
570
- ) {
571
- this.mem.setUint8(sp + 48, 0);
572
- return;
573
- }
574
- const toCopy = src.subarray(0, dst.length);
575
- dst.set(toCopy);
576
- setInt64(sp + 40, toCopy.length);
475
+ storeValue(sp + 40, result);
577
476
  this.mem.setUint8(sp + 48, 1);
578
- },
579
-
580
- // func copyBytesToJS(dst ref, src []byte) (int, bool)
581
- "syscall/js.copyBytesToJS": (sp) => {
582
- sp >>>= 0;
583
- const dst = loadValue(sp + 8);
584
- const src = loadSlice(sp + 16);
585
- if (
586
- !(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)
587
- ) {
588
- this.mem.setUint8(sp + 48, 0);
589
- return;
590
- }
591
- const toCopy = src.subarray(0, dst.length);
592
- dst.set(toCopy);
593
- setInt64(sp + 40, toCopy.length);
477
+ } catch (err) {
478
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
479
+ storeValue(sp + 40, err);
480
+ this.mem.setUint8(sp + 48, 0);
481
+ }
482
+ },
483
+
484
+ // func valueNew(v ref, args []ref) (ref, bool)
485
+ "syscall/js.valueNew": (sp) => {
486
+ sp >>>= 0;
487
+ try {
488
+ const v = loadValue(sp + 8);
489
+ const args = loadSliceOfValues(sp + 16);
490
+ const result = Reflect.construct(v, args);
491
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
492
+ storeValue(sp + 40, result);
594
493
  this.mem.setUint8(sp + 48, 1);
595
- },
494
+ } catch (err) {
495
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
496
+ storeValue(sp + 40, err);
497
+ this.mem.setUint8(sp + 48, 0);
498
+ }
499
+ },
596
500
 
597
- debug: (value) => {
598
- console.log(value);
599
- },
501
+ // func valueLength(v ref) int
502
+ "syscall/js.valueLength": (sp) => {
503
+ sp >>>= 0;
504
+ setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
600
505
  },
601
- };
602
- }
603
506
 
604
- async run(instance) {
605
- if (!(instance instanceof WebAssembly.Instance)) {
606
- throw new Error("Go.run: WebAssembly.Instance expected");
607
- }
608
- this._inst = instance;
609
- this.mem = new DataView(this._inst.exports.mem.buffer);
610
- this._values = [
611
- // JS values that Go currently has references to, indexed by reference id
612
- NaN,
613
- 0,
614
- null,
615
- true,
616
- false,
617
- globalThis,
618
- this,
619
- ];
620
- this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id
621
- this._ids = new Map([
622
- // mapping from JS values to reference ids
623
- [0, 1],
624
- [null, 2],
625
- [true, 3],
626
- [false, 4],
627
- [globalThis, 5],
628
- [this, 6],
629
- ]);
630
- this._idPool = []; // unused ids that have been garbage collected
631
- this.exited = false; // whether the Go program has exited
632
-
633
- // Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
634
- let offset = 4096;
635
-
636
- const strPtr = (str) => {
637
- const ptr = offset;
638
- const bytes = encoder.encode(str + "\0");
639
- new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
640
- offset += bytes.length;
641
- if (offset % 8 !== 0) {
642
- offset += 8 - (offset % 8);
643
- }
644
- return ptr;
645
- };
646
-
647
- const argc = this.argv.length;
648
-
649
- const argvPtrs = [];
650
- this.argv.forEach((arg) => {
651
- argvPtrs.push(strPtr(arg));
652
- });
653
- argvPtrs.push(0);
654
-
655
- const keys = Object.keys(this.env).sort();
656
- keys.forEach((key) => {
657
- argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
658
- });
659
- argvPtrs.push(0);
660
-
661
- const argv = offset;
662
- argvPtrs.forEach((ptr) => {
663
- this.mem.setUint32(offset, ptr, true);
664
- this.mem.setUint32(offset + 4, 0, true);
665
- offset += 8;
666
- });
667
-
668
- // The linker guarantees global data starts from at least wasmMinDataAddr.
669
- // Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr.
670
- const wasmMinDataAddr = 4096 + 8192;
671
- if (offset >= wasmMinDataAddr) {
672
- throw new Error(
673
- "total length of command line and environment variables exceeds limit"
674
- );
675
- }
507
+ // valuePrepareString(v ref) (ref, int)
508
+ "syscall/js.valuePrepareString": (sp) => {
509
+ sp >>>= 0;
510
+ const str = encoder.encode(String(loadValue(sp + 8)));
511
+ storeValue(sp + 16, str);
512
+ setInt64(sp + 24, str.length);
513
+ },
514
+
515
+ // valueLoadString(v ref, b []byte)
516
+ "syscall/js.valueLoadString": (sp) => {
517
+ sp >>>= 0;
518
+ const str = loadValue(sp + 8);
519
+ loadSlice(sp + 16).set(str);
520
+ },
521
+
522
+ // func valueInstanceOf(v ref, t ref) bool
523
+ "syscall/js.valueInstanceOf": (sp) => {
524
+ sp >>>= 0;
525
+ this.mem.setUint8(
526
+ sp + 24,
527
+ loadValue(sp + 8) instanceof loadValue(sp + 16) ? 1 : 0
528
+ );
529
+ },
530
+
531
+ // func copyBytesToGo(dst []byte, src ref) (int, bool)
532
+ "syscall/js.copyBytesToGo": (sp) => {
533
+ sp >>>= 0;
534
+ const dst = loadSlice(sp + 8);
535
+ const src = loadValue(sp + 32);
536
+ if (
537
+ !(src instanceof Uint8Array || src instanceof Uint8ClampedArray)
538
+ ) {
539
+ this.mem.setUint8(sp + 48, 0);
540
+ return;
541
+ }
542
+ const toCopy = src.subarray(0, dst.length);
543
+ dst.set(toCopy);
544
+ setInt64(sp + 40, toCopy.length);
545
+ this.mem.setUint8(sp + 48, 1);
546
+ },
676
547
 
677
- this._inst.exports.run(argc, argv);
678
- if (this.exited) {
679
- this._resolveExitPromise();
548
+ // func copyBytesToJS(dst ref, src []byte) (int, bool)
549
+ "syscall/js.copyBytesToJS": (sp) => {
550
+ sp >>>= 0;
551
+ const dst = loadValue(sp + 8);
552
+ const src = loadSlice(sp + 16);
553
+ if (
554
+ !(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)
555
+ ) {
556
+ this.mem.setUint8(sp + 48, 0);
557
+ return;
558
+ }
559
+ const toCopy = src.subarray(0, dst.length);
560
+ dst.set(toCopy);
561
+ setInt64(sp + 40, toCopy.length);
562
+ this.mem.setUint8(sp + 48, 1);
563
+ },
564
+
565
+ debug: (value) => {
566
+ console.log(value);
567
+ },
568
+ },
569
+ };
570
+ }
571
+
572
+ async run(instance) {
573
+ if (!(instance instanceof WebAssembly.Instance)) {
574
+ throw new Error("Go.run: WebAssembly.Instance expected");
575
+ }
576
+ this._inst = instance;
577
+ this.mem = new DataView(this._inst.exports.mem.buffer);
578
+ this._values = [
579
+ // JS values that Go currently has references to, indexed by reference id
580
+ NaN,
581
+ 0,
582
+ null,
583
+ true,
584
+ false,
585
+ globalThis,
586
+ this,
587
+ ];
588
+ this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id
589
+ this._ids = new Map([
590
+ // mapping from JS values to reference ids
591
+ [0, 1],
592
+ [null, 2],
593
+ [true, 3],
594
+ [false, 4],
595
+ [globalThis, 5],
596
+ [this, 6],
597
+ ]);
598
+ this._idPool = []; // unused ids that have been garbage collected
599
+ this.exited = false; // whether the Go program has exited
600
+
601
+ // Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
602
+ let offset = 4096;
603
+
604
+ const strPtr = (str) => {
605
+ const ptr = offset;
606
+ const bytes = encoder.encode(str + "\0");
607
+ new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
608
+ offset += bytes.length;
609
+ if (offset % 8 !== 0) {
610
+ offset += 8 - (offset % 8);
680
611
  }
681
- await this._exitPromise;
612
+ return ptr;
613
+ };
614
+
615
+ const argc = this.argv.length;
616
+
617
+ const argvPtrs = [];
618
+ this.argv.forEach((arg) => {
619
+ argvPtrs.push(strPtr(arg));
620
+ });
621
+ argvPtrs.push(0);
622
+
623
+ const keys = Object.keys(this.env).sort();
624
+ keys.forEach((key) => {
625
+ argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
626
+ });
627
+ argvPtrs.push(0);
628
+
629
+ const argv = offset;
630
+ argvPtrs.forEach((ptr) => {
631
+ this.mem.setUint32(offset, ptr, true);
632
+ this.mem.setUint32(offset + 4, 0, true);
633
+ offset += 8;
634
+ });
635
+
636
+ // The linker guarantees global data starts from at least wasmMinDataAddr.
637
+ // Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr.
638
+ const wasmMinDataAddr = 4096 + 8192;
639
+ if (offset >= wasmMinDataAddr) {
640
+ throw new Error(
641
+ "total length of command line and environment variables exceeds limit"
642
+ );
682
643
  }
683
644
 
684
- _resume() {
685
- if (this.exited) {
686
- throw new Error("Go program has already exited");
687
- }
688
- this._inst.exports.resume();
689
- if (this.exited) {
690
- this._resolveExitPromise();
691
- }
645
+ this._inst.exports.run(argc, argv);
646
+ if (this.exited) {
647
+ this._resolveExitPromise();
692
648
  }
649
+ await this._exitPromise;
650
+ }
693
651
 
694
- _makeFuncWrapper(id) {
695
- const go = this;
696
- return function () {
697
- const event = { id: id, this: this, args: arguments };
698
- go._pendingEvent = event;
699
- go._resume();
700
- return event.result;
701
- };
652
+ _resume() {
653
+ if (this.exited) {
654
+ throw new Error("Go program has already exited");
702
655
  }
703
- };
704
- })();
705
-
706
- ((function () {
707
- try {
708
- if (
709
- typeof WebAssembly === "object" &&
710
- typeof WebAssembly.instantiate === "function"
711
- ) {
712
- const module = new WebAssembly.Module(
713
- Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00)
714
- );
715
- if (module instanceof WebAssembly.Module) {
716
- return new WebAssembly.Instance(module) instanceof WebAssembly.Instance;
717
- }
656
+ this._inst.exports.resume();
657
+ if (this.exited) {
658
+ this._resolveExitPromise();
718
659
  }
719
- } catch (e) {}
720
- return false;
721
- }))();
722
-
723
- const go = new Go();
724
- let locuschainLibLoaded = false;
725
- function loadLocusLibrary() {
726
- return new Promise((resolve, reject) => {
727
- WebAssembly.instantiate(wasm, go.importObject)
728
- .then((result) => {
729
- go.run(result.instance);
730
- locuschainLibLoaded = true;
731
- resolve();
732
- })
733
- .catch((err) => {
734
- console.error("err:", err);
735
- reject();
736
- });
737
- });
738
- }
660
+ }
661
+
662
+ _makeFuncWrapper(id) {
663
+ const go = this;
664
+ return function () {
665
+ const event = { id: id, this: this, args: arguments };
666
+ go._pendingEvent = event;
667
+ go._resume();
668
+ return event.result;
669
+ };
670
+ }
671
+ };
672
+ })();
673
+
674
+ ((function () {
675
+ try {
676
+ if (
677
+ typeof WebAssembly === "object" &&
678
+ typeof WebAssembly.instantiate === "function"
679
+ ) {
680
+ const module = new WebAssembly.Module(
681
+ Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00)
682
+ );
683
+ if (module instanceof WebAssembly.Module) {
684
+ return new WebAssembly.Instance(module) instanceof WebAssembly.Instance;
685
+ }
686
+ }
687
+ } catch (e) {}
688
+ return false;
689
+ }))();
690
+
691
+ const go = new Go();
692
+
693
+ (function loadLocusLibrary() {
694
+ const module = new WebAssembly.Module(wasm);
695
+ const instance = new WebAssembly.Instance(module, go.importObject);
696
+ go.run(instance);
697
+ })();
739
698
 
740
- loadLocusLibrary().then((res) => { });
741
- class LocusLib {
742
- static callWasm(wasmCmd, wasmParams) {
743
- return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
744
- if (!locuschainLibLoaded) {
745
- for (let i = 0; i < 1000; i++) {
746
- yield LocusLib.sleep(100);
747
- if (locuschainLibLoaded) {
748
- break;
749
- }
750
- }
751
- }
752
- if (Array.isArray(wasmParams)) {
753
- wasmParams = wasmParams[0];
754
- }
755
- // @ts-ignore
756
- if (typeof globalThis["CallLclib"] !== "function")
757
- return;
758
- // @ts-ignore
759
- const resultString = yield globalThis["CallLclib"](wasmCmd, JSON.stringify(wasmParams));
760
- const result = JSON.parse(resultString);
761
- if (result.Error && result.Error.Code != 0) {
762
- const error = Object.assign(Object.assign({}, result.Error), { message: result.Error.Message });
763
- delete error.Message;
764
- reject(error);
765
- }
766
- else {
767
- resolve(result.Result);
768
- }
769
- }));
770
- }
771
- //=================================================================================
772
- static GetLibInfo() {
773
- return wasmJson;
774
- }
775
- static GetLibraryVersions() {
776
- return LocusLib.callWasm("GetLibraryVersions", {});
777
- }
778
- static CreateAccountAndKeystore(passwordMaster, passwordNormal, mkeyAlgoNormal, nkeyAlgoNormal) {
779
- return LocusLib.callWasm("CreateAccountAndKeystore", {
780
- passwordMaster,
781
- passwordNormal,
782
- mkeyAlgoNormal,
783
- nkeyAlgoNormal,
784
- });
785
- }
786
- static CreateNormalKey(addrStr, keyAlgo) {
787
- return LocusLib.callWasm("CreateNormalKey", { addrStr, keyAlgo });
788
- }
789
- static CreateMasterKeystore(AddrStr, Password, MskStr, MpkStr) {
790
- return LocusLib.callWasm("CreateMasterKeystore", {
791
- AddrStr,
792
- Password,
793
- MskStr,
794
- MpkStr,
795
- });
796
- }
797
- static CreateNormalKeystore(AddrStr, Password, NskStr, NpkStr, MpkStr = "", KeySign = "") {
798
- return LocusLib.callWasm("CreateNormalKeystore", {
799
- AddrStr,
800
- Password,
801
- NskStr,
802
- NpkStr,
803
- MpkStr,
804
- KeySign,
805
- });
806
- }
807
- static LoadMasterKeystore(PassStr, KsJson) {
808
- return LocusLib.callWasm("LoadMasterKeystore", { PassStr, KsJson });
809
- }
810
- static LoadNormalKeystore(PassStr, KsJson) {
811
- return LocusLib.callWasm("LoadNormalKeystore", { PassStr, KsJson });
812
- }
813
- static ConvertToData(value, valueType) {
814
- return LocusLib.callWasm("ConvertToData", { value, valueType });
815
- }
816
- static ConvertDataTo(dataString, retType) {
817
- return LocusLib.callWasm("ConvertDataTo", { dataString, retType });
818
- }
819
- static ConvertStringToData(str) {
820
- return LocusLib.callWasm("ConvertStringToData", str);
821
- }
822
- static ConvertDataToString(dataString) {
823
- return LocusLib.callWasm("ConvertDataToString", dataString);
824
- }
825
- static ConvertAddressToHex(addr) {
826
- return LocusLib.callWasm("ConvertAddressToHex", addr);
827
- }
828
- static ConvertHexToAddress(hexString) {
829
- return LocusLib.callWasm("ConvertHexToAddress", hexString);
830
- }
831
- static ConvertAddressToData(addr) {
832
- return LocusLib.callWasm("ConvertAddressToData", addr);
833
- }
834
- static ConvertDataToAddress(dataString) {
835
- return LocusLib.callWasm("ConvertDataToAddress", dataString);
836
- }
837
- static ConvertBase32ToHex(encoded) {
838
- return LocusLib.callWasm("ConvertBase32ToHex", encoded);
839
- }
840
- static ConvertHexToBase32(hexString) {
841
- return LocusLib.callWasm("ConvertHexToBase32", hexString);
842
- }
843
- static ConvertHexToData(encoded) {
844
- return LocusLib.callWasm("ConvertHexToData", encoded);
845
- }
846
- static ConvertDataToHex(dataString) {
847
- return LocusLib.callWasm("ConvertDataToHex", dataString);
848
- }
849
- static ConvertBase32ToData(encoded) {
850
- return LocusLib.callWasm("ConvertBase32ToData", encoded);
851
- }
852
- static ConvertDataToBase32(dataString) {
853
- return LocusLib.callWasm("ConvertDataToBase32", dataString);
854
- }
855
- static SignByMasterKey(Msk, Message) {
856
- return LocusLib.callWasm("SignByMasterKey", { Msk, Message });
857
- }
858
- static Sign(Sk, Message) {
859
- return LocusLib.callWasm("Sign", { Sk, Message });
860
- }
861
- static Verify(Pk, Message, Sig) {
862
- return LocusLib.callWasm("Verify", { Pk, Message, Sig });
863
- }
864
- static VerifyTx(jsonTx) {
865
- return LocusLib.callWasm("VerifyTx", jsonTx);
866
- }
867
- static CompileCoreScript(Code) {
868
- return LocusLib.callWasm("CompileCoreScript", { Code });
869
- }
870
- static DisCompileCoreScript(Code) {
871
- return LocusLib.callWasm("DisCompileCoreScript", { Code });
872
- }
873
- static TestCoreScript(ScriptProvide, ScriptAccept, TxDataProvide, TxDataAccept) {
874
- return LocusLib.callWasm("TestCoreScript", {
875
- ScriptProvide,
876
- ScriptAccept,
877
- TxDataProvide,
878
- TxDataAccept,
879
- });
880
- }
881
- static GetDefFromCoreScript(Code) {
882
- return LocusLib.callWasm("GetDefFromCoreScript", { Code });
883
- }
884
- static EncodeTxNumber(Number, Type) {
885
- return LocusLib.callWasm("EncodeTxNumber", { Number, Type });
886
- }
887
- static EncodeTxCurrency(Currency) {
888
- return LocusLib.callWasm("EncodeTxCurrency", { Currency });
889
- }
890
- static Hash(Data, Type) {
891
- return LocusLib.callWasm("Hash", { Data, Type });
892
- }
893
- static VerifyMerkleProof(Proof, Hash, GoalHash) {
894
- return LocusLib.callWasm("VerifyMerkleProof", { Proof, Hash, GoalHash });
895
- }
896
- static CalculateTxLinkHash(Tx) {
897
- return LocusLib.callWasm("CalculateTxLinkHash", { Tx });
898
- }
899
- static DecodeTxs(Txs) {
900
- return LocusLib.callWasm("DecodeTxs", { Txs });
901
- }
902
- static GzipAndEncode(str) {
903
- return LocusLib.callWasm("GzipAndEncode", str);
904
- }
905
- static GenerateMnemonic(entropyBit = 256) {
906
- return LocusLib.callWasm("GenerateMnemonic", { entropyBit });
907
- }
908
- static DeriveKeysFromMnemonic(mnemonic, path) {
909
- return LocusLib.callWasm("DeriveKeysFromMnemonic", { mnemonic, path });
910
- }
911
- }
912
- LocusLib.sleep = (ms) => new Promise((resolve) => setTimeout(() => resolve(true), ms));
699
+ class LocusLib {
700
+ static callWasm(wasmCmd, wasmParams) {
701
+ if (Array.isArray(wasmParams)) {
702
+ wasmParams = wasmParams[0];
703
+ }
704
+ // @ts-ignore
705
+ if (typeof globalThis["CallLclib"] !== "function")
706
+ return;
707
+ // @ts-ignore
708
+ const resultString = globalThis["CallLclib"](wasmCmd, JSON.stringify(wasmParams));
709
+ const result = JSON.parse(resultString);
710
+ if (result.Error && result.Error.Code != 0) {
711
+ const error = Object.assign(Object.assign({}, result.Error), { message: result.Error.Message });
712
+ delete error.Message;
713
+ // return error;
714
+ throw error;
715
+ }
716
+ else {
717
+ return result.Result;
718
+ }
719
+ }
720
+ //=================================================================================
721
+ static GetLibInfo() {
722
+ return wasmJson;
723
+ }
724
+ static GetLibraryVersions() {
725
+ return LocusLib.callWasm("GetLibraryVersions", {});
726
+ }
727
+ static CreateAccountAndKeystore(passwordMaster, passwordNormal, mkeyAlgoNormal, nkeyAlgoNormal) {
728
+ return LocusLib.callWasm("CreateAccountAndKeystore", {
729
+ passwordMaster,
730
+ passwordNormal,
731
+ mkeyAlgoNormal,
732
+ nkeyAlgoNormal,
733
+ });
734
+ }
735
+ static CreateNormalKey(addrStr, keyAlgo) {
736
+ return LocusLib.callWasm("CreateNormalKey", { addrStr, keyAlgo });
737
+ }
738
+ static CreateMasterKeystore(AddrStr, Password, MskStr, MpkStr) {
739
+ return LocusLib.callWasm("CreateMasterKeystore", {
740
+ AddrStr,
741
+ Password,
742
+ MskStr,
743
+ MpkStr,
744
+ });
745
+ }
746
+ static CreateNormalKeystore(AddrStr, Password, NskStr, NpkStr, MpkStr = "", KeySign = "") {
747
+ return LocusLib.callWasm("CreateNormalKeystore", {
748
+ AddrStr,
749
+ Password,
750
+ NskStr,
751
+ NpkStr,
752
+ MpkStr,
753
+ KeySign,
754
+ });
755
+ }
756
+ static LoadMasterKeystore(PassStr, KsJson) {
757
+ return LocusLib.callWasm("LoadMasterKeystore", { PassStr, KsJson });
758
+ }
759
+ static LoadNormalKeystore(PassStr, KsJson) {
760
+ return LocusLib.callWasm("LoadNormalKeystore", { PassStr, KsJson });
761
+ }
762
+ static ConvertToData(value, valueType) {
763
+ return LocusLib.callWasm("ConvertToData", { value, valueType });
764
+ }
765
+ static ConvertDataTo(dataString, retType) {
766
+ return LocusLib.callWasm("ConvertDataTo", { dataString, retType });
767
+ }
768
+ static ConvertStringToData(str) {
769
+ return LocusLib.callWasm("ConvertStringToData", str);
770
+ }
771
+ static ConvertDataToString(dataString) {
772
+ return LocusLib.callWasm("ConvertDataToString", dataString);
773
+ }
774
+ static ConvertAddressToHex(addr) {
775
+ return LocusLib.callWasm("ConvertAddressToHex", addr);
776
+ }
777
+ static ConvertHexToAddress(hexString) {
778
+ return LocusLib.callWasm("ConvertHexToAddress", hexString);
779
+ }
780
+ static ConvertAddressToData(addr) {
781
+ return LocusLib.callWasm("ConvertAddressToData", addr);
782
+ }
783
+ static ConvertDataToAddress(dataString) {
784
+ return LocusLib.callWasm("ConvertDataToAddress", dataString);
785
+ }
786
+ static ConvertBase32ToHex(encoded) {
787
+ return LocusLib.callWasm("ConvertBase32ToHex", encoded);
788
+ }
789
+ static ConvertHexToBase32(hexString) {
790
+ return LocusLib.callWasm("ConvertHexToBase32", hexString);
791
+ }
792
+ static ConvertHexToData(encoded) {
793
+ return LocusLib.callWasm("ConvertHexToData", encoded);
794
+ }
795
+ static ConvertDataToHex(dataString) {
796
+ return LocusLib.callWasm("ConvertDataToHex", dataString);
797
+ }
798
+ static ConvertBase32ToData(encoded) {
799
+ return LocusLib.callWasm("ConvertBase32ToData", encoded);
800
+ }
801
+ static ConvertDataToBase32(dataString) {
802
+ return LocusLib.callWasm("ConvertDataToBase32", dataString);
803
+ }
804
+ static SignByMasterKey(Msk, Message) {
805
+ return LocusLib.callWasm("SignByMasterKey", { Msk, Message });
806
+ }
807
+ static Sign(Sk, Message) {
808
+ return LocusLib.callWasm("Sign", { Sk, Message });
809
+ }
810
+ static Verify(Pk, Message, Sig) {
811
+ return LocusLib.callWasm("Verify", { Pk, Message, Sig });
812
+ }
813
+ static VerifyTx(jsonTx) {
814
+ return LocusLib.callWasm("VerifyTx", jsonTx);
815
+ }
816
+ static CompileCoreScript(Code) {
817
+ return LocusLib.callWasm("CompileCoreScript", { Code });
818
+ }
819
+ static DisCompileCoreScript(Code) {
820
+ return LocusLib.callWasm("DisCompileCoreScript", { Code });
821
+ }
822
+ static TestCoreScript(ScriptProvide, ScriptAccept, TxDataProvide, TxDataAccept) {
823
+ return LocusLib.callWasm("TestCoreScript", {
824
+ ScriptProvide,
825
+ ScriptAccept,
826
+ TxDataProvide,
827
+ TxDataAccept,
828
+ });
829
+ }
830
+ static GetDefFromCoreScript(Code) {
831
+ return LocusLib.callWasm("GetDefFromCoreScript", { Code });
832
+ }
833
+ static EncodeTxNumber(Number, Type) {
834
+ return LocusLib.callWasm("EncodeTxNumber", { Number, Type });
835
+ }
836
+ static EncodeTxCurrency(Currency) {
837
+ return LocusLib.callWasm("EncodeTxCurrency", { Currency });
838
+ }
839
+ static Hash(Data, Type) {
840
+ return LocusLib.callWasm("Hash", { Data, Type });
841
+ }
842
+ static VerifyMerkleProof(Proof, Hash, GoalHash) {
843
+ return LocusLib.callWasm("VerifyMerkleProof", { Proof, Hash, GoalHash });
844
+ }
845
+ static CalculateTxLinkHash(Tx) {
846
+ return LocusLib.callWasm("CalculateTxLinkHash", { Tx });
847
+ }
848
+ static DecodeTxs(Txs) {
849
+ return LocusLib.callWasm("DecodeTxs", { Txs });
850
+ }
851
+ static GzipAndEncode(str) {
852
+ return LocusLib.callWasm("GzipAndEncode", str);
853
+ }
854
+ static GenerateMnemonic(entropyBit = 256) {
855
+ return LocusLib.callWasm("GenerateMnemonic", { entropyBit });
856
+ }
857
+ static DeriveKeysFromMnemonic(mnemonic, path) {
858
+ return LocusLib.callWasm("DeriveKeysFromMnemonic", { mnemonic, path });
859
+ }
860
+ }
861
+ LocusLib.sleep = (ms) => new Promise((resolve) => setTimeout(() => resolve(true), ms));
913
862
 
914
- exports.LocusLib = LocusLib;
863
+ exports.LocusLib = LocusLib;
915
864
 
916
865
  }));