@lifi/perps-sdk-provider-lighter 22.0.0 → 22.1.1

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.
Files changed (91) hide show
  1. package/README.md +20 -1
  2. package/dist/cjs/LighterProvider.d.ts.map +1 -1
  3. package/dist/cjs/LighterProvider.js +11 -6
  4. package/dist/cjs/LighterProvider.js.map +1 -1
  5. package/dist/cjs/accountConfig.js +3 -4
  6. package/dist/cjs/accountConfig.js.map +1 -1
  7. package/dist/cjs/constants.d.ts +2 -1
  8. package/dist/cjs/constants.d.ts.map +1 -1
  9. package/dist/cjs/constants.js +3 -1
  10. package/dist/cjs/constants.js.map +1 -1
  11. package/dist/cjs/signers/generated/wasmExecRuntime.d.ts +4 -1
  12. package/dist/cjs/signers/generated/wasmExecRuntime.d.ts.map +1 -1
  13. package/dist/cjs/signers/generated/wasmExecRuntime.js +470 -566
  14. package/dist/cjs/signers/generated/wasmExecRuntime.js.map +1 -1
  15. package/dist/cjs/signers/wasmLoader.d.ts.map +1 -1
  16. package/dist/cjs/signers/wasmLoader.js +1 -3
  17. package/dist/cjs/signers/wasmLoader.js.map +1 -1
  18. package/dist/cjs/types/account.d.ts +3 -3
  19. package/dist/cjs/types/account.d.ts.map +1 -1
  20. package/dist/cjs/types/action.d.ts +2 -0
  21. package/dist/cjs/types/action.d.ts.map +1 -1
  22. package/dist/cjs/types/action.js +3 -1
  23. package/dist/cjs/types/action.js.map +1 -1
  24. package/dist/cjs/types/fill.d.ts +2 -2
  25. package/dist/cjs/types/fill.d.ts.map +1 -1
  26. package/dist/cjs/utils/apiClient.d.ts.map +1 -1
  27. package/dist/cjs/utils/apiClient.js +8 -3
  28. package/dist/cjs/utils/apiClient.js.map +1 -1
  29. package/dist/cjs/utils/mapFill.d.ts.map +1 -1
  30. package/dist/cjs/utils/mapFill.js +2 -2
  31. package/dist/cjs/utils/mapFill.js.map +1 -1
  32. package/dist/cjs/utils/mapPosition.js +1 -1
  33. package/dist/cjs/utils/mapPosition.js.map +1 -1
  34. package/dist/esm/LighterProvider.d.ts.map +1 -1
  35. package/dist/esm/LighterProvider.js +16 -7
  36. package/dist/esm/LighterProvider.js.map +1 -1
  37. package/dist/esm/accountConfig.js +5 -7
  38. package/dist/esm/accountConfig.js.map +1 -1
  39. package/dist/esm/constants.d.ts +10 -1
  40. package/dist/esm/constants.d.ts.map +1 -1
  41. package/dist/esm/constants.js +10 -1
  42. package/dist/esm/constants.js.map +1 -1
  43. package/dist/esm/signers/generated/wasmExecRuntime.d.ts +5 -7
  44. package/dist/esm/signers/generated/wasmExecRuntime.d.ts.map +1 -1
  45. package/dist/esm/signers/generated/wasmExecRuntime.js +507 -571
  46. package/dist/esm/signers/generated/wasmExecRuntime.js.map +1 -1
  47. package/dist/esm/signers/wasmLoader.d.ts.map +1 -1
  48. package/dist/esm/signers/wasmLoader.js +2 -11
  49. package/dist/esm/signers/wasmLoader.js.map +1 -1
  50. package/dist/esm/types/account.d.ts +15 -4
  51. package/dist/esm/types/account.d.ts.map +1 -1
  52. package/dist/esm/types/action.d.ts +13 -0
  53. package/dist/esm/types/action.d.ts.map +1 -1
  54. package/dist/esm/types/action.js +13 -0
  55. package/dist/esm/types/action.js.map +1 -1
  56. package/dist/esm/types/fill.d.ts +2 -2
  57. package/dist/esm/types/fill.d.ts.map +1 -1
  58. package/dist/esm/utils/apiClient.d.ts.map +1 -1
  59. package/dist/esm/utils/apiClient.js +9 -4
  60. package/dist/esm/utils/apiClient.js.map +1 -1
  61. package/dist/esm/utils/mapFill.d.ts.map +1 -1
  62. package/dist/esm/utils/mapFill.js +3 -2
  63. package/dist/esm/utils/mapFill.js.map +1 -1
  64. package/dist/esm/utils/mapPosition.js +1 -1
  65. package/dist/esm/utils/mapPosition.js.map +1 -1
  66. package/dist/types/LighterProvider.d.ts.map +1 -1
  67. package/dist/types/constants.d.ts +10 -1
  68. package/dist/types/constants.d.ts.map +1 -1
  69. package/dist/types/signers/generated/wasmExecRuntime.d.ts +5 -7
  70. package/dist/types/signers/generated/wasmExecRuntime.d.ts.map +1 -1
  71. package/dist/types/signers/wasmLoader.d.ts.map +1 -1
  72. package/dist/types/types/account.d.ts +15 -4
  73. package/dist/types/types/account.d.ts.map +1 -1
  74. package/dist/types/types/action.d.ts +13 -0
  75. package/dist/types/types/action.d.ts.map +1 -1
  76. package/dist/types/types/fill.d.ts +2 -2
  77. package/dist/types/types/fill.d.ts.map +1 -1
  78. package/dist/types/utils/apiClient.d.ts.map +1 -1
  79. package/dist/types/utils/mapFill.d.ts.map +1 -1
  80. package/package.json +1 -1
  81. package/src/LighterProvider.ts +26 -7
  82. package/src/accountConfig.ts +8 -8
  83. package/src/constants.ts +11 -0
  84. package/src/signers/generated/wasmExecRuntime.ts +570 -572
  85. package/src/signers/wasmLoader.ts +2 -17
  86. package/src/types/account.ts +15 -4
  87. package/src/types/action.ts +15 -0
  88. package/src/types/fill.ts +3 -2
  89. package/src/utils/apiClient.ts +11 -4
  90. package/src/utils/mapFill.ts +5 -3
  91. package/src/utils/mapPosition.ts +1 -1
@@ -1,569 +1,473 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WASM_EXEC_JS = void 0;
4
- const WASM_EXEC_LINES = [
5
- "// Copyright 2018 The Go Authors. All rights reserved.",
6
- "// Use of this source code is governed by a BSD-style",
7
- "// license that can be found in the LICENSE file.",
8
- "",
9
- "\"use strict\";",
10
- "",
11
- "(() => {",
12
- "\tconst enosys = () => {",
13
- "\t\tconst err = new Error(\"not implemented\");",
14
- "\t\terr.code = \"ENOSYS\";",
15
- "\t\treturn err;",
16
- "\t};",
17
- "",
18
- "\tif (!globalThis.fs) {",
19
- "\t\tlet outputBuf = \"\";",
20
- "\t\tglobalThis.fs = {",
21
- "\t\t\tconstants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused",
22
- "\t\t\twriteSync(fd, buf) {",
23
- "\t\t\t\toutputBuf += decoder.decode(buf);",
24
- "\t\t\t\tconst nl = outputBuf.lastIndexOf(\"\\n\");",
25
- "\t\t\t\tif (nl != -1) {",
26
- "\t\t\t\t\tconsole.log(outputBuf.substring(0, nl));",
27
- "\t\t\t\t\toutputBuf = outputBuf.substring(nl + 1);",
28
- "\t\t\t\t}",
29
- "\t\t\t\treturn buf.length;",
30
- "\t\t\t},",
31
- "\t\t\twrite(fd, buf, offset, length, position, callback) {",
32
- "\t\t\t\tif (offset !== 0 || length !== buf.length || position !== null) {",
33
- "\t\t\t\t\tcallback(enosys());",
34
- "\t\t\t\t\treturn;",
35
- "\t\t\t\t}",
36
- "\t\t\t\tconst n = this.writeSync(fd, buf);",
37
- "\t\t\t\tcallback(null, n);",
38
- "\t\t\t},",
39
- "\t\t\tchmod(path, mode, callback) { callback(enosys()); },",
40
- "\t\t\tchown(path, uid, gid, callback) { callback(enosys()); },",
41
- "\t\t\tclose(fd, callback) { callback(enosys()); },",
42
- "\t\t\tfchmod(fd, mode, callback) { callback(enosys()); },",
43
- "\t\t\tfchown(fd, uid, gid, callback) { callback(enosys()); },",
44
- "\t\t\tfstat(fd, callback) { callback(enosys()); },",
45
- "\t\t\tfsync(fd, callback) { callback(null); },",
46
- "\t\t\tftruncate(fd, length, callback) { callback(enosys()); },",
47
- "\t\t\tlchown(path, uid, gid, callback) { callback(enosys()); },",
48
- "\t\t\tlink(path, link, callback) { callback(enosys()); },",
49
- "\t\t\tlstat(path, callback) { callback(enosys()); },",
50
- "\t\t\tmkdir(path, perm, callback) { callback(enosys()); },",
51
- "\t\t\topen(path, flags, mode, callback) { callback(enosys()); },",
52
- "\t\t\tread(fd, buffer, offset, length, position, callback) { callback(enosys()); },",
53
- "\t\t\treaddir(path, callback) { callback(enosys()); },",
54
- "\t\t\treadlink(path, callback) { callback(enosys()); },",
55
- "\t\t\trename(from, to, callback) { callback(enosys()); },",
56
- "\t\t\trmdir(path, callback) { callback(enosys()); },",
57
- "\t\t\tstat(path, callback) { callback(enosys()); },",
58
- "\t\t\tsymlink(path, link, callback) { callback(enosys()); },",
59
- "\t\t\ttruncate(path, length, callback) { callback(enosys()); },",
60
- "\t\t\tunlink(path, callback) { callback(enosys()); },",
61
- "\t\t\tutimes(path, atime, mtime, callback) { callback(enosys()); },",
62
- "\t\t};",
63
- "\t}",
64
- "",
65
- "\tif (!globalThis.process) {",
66
- "\t\tglobalThis.process = {",
67
- "\t\t\tgetuid() { return -1; },",
68
- "\t\t\tgetgid() { return -1; },",
69
- "\t\t\tgeteuid() { return -1; },",
70
- "\t\t\tgetegid() { return -1; },",
71
- "\t\t\tgetgroups() { throw enosys(); },",
72
- "\t\t\tpid: -1,",
73
- "\t\t\tppid: -1,",
74
- "\t\t\tumask() { throw enosys(); },",
75
- "\t\t\tcwd() { throw enosys(); },",
76
- "\t\t\tchdir() { throw enosys(); },",
77
- "\t\t}",
78
- "\t}",
79
- "",
80
- "\tif (!globalThis.crypto) {",
81
- "\t\tthrow new Error(\"globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)\");",
82
- "\t}",
83
- "",
84
- "\tif (!globalThis.performance) {",
85
- "\t\tthrow new Error(\"globalThis.performance is not available, polyfill required (performance.now only)\");",
86
- "\t}",
87
- "",
88
- "\tif (!globalThis.TextEncoder) {",
89
- "\t\tthrow new Error(\"globalThis.TextEncoder is not available, polyfill required\");",
90
- "\t}",
91
- "",
92
- "\tif (!globalThis.TextDecoder) {",
93
- "\t\tthrow new Error(\"globalThis.TextDecoder is not available, polyfill required\");",
94
- "\t}",
95
- "",
96
- "\tconst encoder = new TextEncoder(\"utf-8\");",
97
- "\tconst decoder = new TextDecoder(\"utf-8\");",
98
- "",
99
- "\tglobalThis.Go = class {",
100
- "\t\tconstructor() {",
101
- "\t\t\tthis.argv = [\"js\"];",
102
- "\t\t\tthis.env = {};",
103
- "\t\t\tthis.exit = (code) => {",
104
- "\t\t\t\tif (code !== 0) {",
105
- "\t\t\t\t\tconsole.warn(\"exit code:\", code);",
106
- "\t\t\t\t}",
107
- "\t\t\t};",
108
- "\t\t\tthis._exitPromise = new Promise((resolve) => {",
109
- "\t\t\t\tthis._resolveExitPromise = resolve;",
110
- "\t\t\t});",
111
- "\t\t\tthis._pendingEvent = null;",
112
- "\t\t\tthis._scheduledTimeouts = new Map();",
113
- "\t\t\tthis._nextCallbackTimeoutID = 1;",
114
- "",
115
- "\t\t\tconst setInt64 = (addr, v) => {",
116
- "\t\t\t\tthis.mem.setUint32(addr + 0, v, true);",
117
- "\t\t\t\tthis.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);",
118
- "\t\t\t}",
119
- "",
120
- "\t\t\tconst setInt32 = (addr, v) => {",
121
- "\t\t\t\tthis.mem.setUint32(addr + 0, v, true);",
122
- "\t\t\t}",
123
- "",
124
- "\t\t\tconst getInt64 = (addr) => {",
125
- "\t\t\t\tconst low = this.mem.getUint32(addr + 0, true);",
126
- "\t\t\t\tconst high = this.mem.getInt32(addr + 4, true);",
127
- "\t\t\t\treturn low + high * 4294967296;",
128
- "\t\t\t}",
129
- "",
130
- "\t\t\tconst loadValue = (addr) => {",
131
- "\t\t\t\tconst f = this.mem.getFloat64(addr, true);",
132
- "\t\t\t\tif (f === 0) {",
133
- "\t\t\t\t\treturn undefined;",
134
- "\t\t\t\t}",
135
- "\t\t\t\tif (!isNaN(f)) {",
136
- "\t\t\t\t\treturn f;",
137
- "\t\t\t\t}",
138
- "",
139
- "\t\t\t\tconst id = this.mem.getUint32(addr, true);",
140
- "\t\t\t\treturn this._values[id];",
141
- "\t\t\t}",
142
- "",
143
- "\t\t\tconst storeValue = (addr, v) => {",
144
- "\t\t\t\tconst nanHead = 0x7FF80000;",
145
- "",
146
- "\t\t\t\tif (typeof v === \"number\" && v !== 0) {",
147
- "\t\t\t\t\tif (isNaN(v)) {",
148
- "\t\t\t\t\t\tthis.mem.setUint32(addr + 4, nanHead, true);",
149
- "\t\t\t\t\t\tthis.mem.setUint32(addr, 0, true);",
150
- "\t\t\t\t\t\treturn;",
151
- "\t\t\t\t\t}",
152
- "\t\t\t\t\tthis.mem.setFloat64(addr, v, true);",
153
- "\t\t\t\t\treturn;",
154
- "\t\t\t\t}",
155
- "",
156
- "\t\t\t\tif (v === undefined) {",
157
- "\t\t\t\t\tthis.mem.setFloat64(addr, 0, true);",
158
- "\t\t\t\t\treturn;",
159
- "\t\t\t\t}",
160
- "",
161
- "\t\t\t\tlet id = this._ids.get(v);",
162
- "\t\t\t\tif (id === undefined) {",
163
- "\t\t\t\t\tid = this._idPool.pop();",
164
- "\t\t\t\t\tif (id === undefined) {",
165
- "\t\t\t\t\t\tid = this._values.length;",
166
- "\t\t\t\t\t}",
167
- "\t\t\t\t\tthis._values[id] = v;",
168
- "\t\t\t\t\tthis._goRefCounts[id] = 0;",
169
- "\t\t\t\t\tthis._ids.set(v, id);",
170
- "\t\t\t\t}",
171
- "\t\t\t\tthis._goRefCounts[id]++;",
172
- "\t\t\t\tlet typeFlag = 0;",
173
- "\t\t\t\tswitch (typeof v) {",
174
- "\t\t\t\t\tcase \"object\":",
175
- "\t\t\t\t\t\tif (v !== null) {",
176
- "\t\t\t\t\t\t\ttypeFlag = 1;",
177
- "\t\t\t\t\t\t}",
178
- "\t\t\t\t\t\tbreak;",
179
- "\t\t\t\t\tcase \"string\":",
180
- "\t\t\t\t\t\ttypeFlag = 2;",
181
- "\t\t\t\t\t\tbreak;",
182
- "\t\t\t\t\tcase \"symbol\":",
183
- "\t\t\t\t\t\ttypeFlag = 3;",
184
- "\t\t\t\t\t\tbreak;",
185
- "\t\t\t\t\tcase \"function\":",
186
- "\t\t\t\t\t\ttypeFlag = 4;",
187
- "\t\t\t\t\t\tbreak;",
188
- "\t\t\t\t}",
189
- "\t\t\t\tthis.mem.setUint32(addr + 4, nanHead | typeFlag, true);",
190
- "\t\t\t\tthis.mem.setUint32(addr, id, true);",
191
- "\t\t\t}",
192
- "",
193
- "\t\t\tconst loadSlice = (addr) => {",
194
- "\t\t\t\tconst array = getInt64(addr + 0);",
195
- "\t\t\t\tconst len = getInt64(addr + 8);",
196
- "\t\t\t\treturn new Uint8Array(this._inst.exports.mem.buffer, array, len);",
197
- "\t\t\t}",
198
- "",
199
- "\t\t\tconst loadSliceOfValues = (addr) => {",
200
- "\t\t\t\tconst array = getInt64(addr + 0);",
201
- "\t\t\t\tconst len = getInt64(addr + 8);",
202
- "\t\t\t\tconst a = new Array(len);",
203
- "\t\t\t\tfor (let i = 0; i < len; i++) {",
204
- "\t\t\t\t\ta[i] = loadValue(array + i * 8);",
205
- "\t\t\t\t}",
206
- "\t\t\t\treturn a;",
207
- "\t\t\t}",
208
- "",
209
- "\t\t\tconst loadString = (addr) => {",
210
- "\t\t\t\tconst saddr = getInt64(addr + 0);",
211
- "\t\t\t\tconst len = getInt64(addr + 8);",
212
- "\t\t\t\treturn decoder.decode(new DataView(this._inst.exports.mem.buffer, saddr, len));",
213
- "\t\t\t}",
214
- "",
215
- "\t\t\tconst timeOrigin = Date.now() - performance.now();",
216
- "\t\t\tthis.importObject = {",
217
- "\t\t\t\t_gotest: {",
218
- "\t\t\t\t\tadd: (a, b) => a + b,",
219
- "\t\t\t\t},",
220
- "\t\t\t\tgojs: {",
221
- "\t\t\t\t\t// Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)",
222
- "\t\t\t\t\t// may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported",
223
- "\t\t\t\t\t// function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).",
224
- "\t\t\t\t\t// This changes the SP, thus we have to update the SP used by the imported function.",
225
- "",
226
- "\t\t\t\t\t// func wasmExit(code int32)",
227
- "\t\t\t\t\t\"runtime.wasmExit\": (sp) => {",
228
- "\t\t\t\t\t\tsp >>>= 0;",
229
- "\t\t\t\t\t\tconst code = this.mem.getInt32(sp + 8, true);",
230
- "\t\t\t\t\t\tthis.exited = true;",
231
- "\t\t\t\t\t\tdelete this._inst;",
232
- "\t\t\t\t\t\tdelete this._values;",
233
- "\t\t\t\t\t\tdelete this._goRefCounts;",
234
- "\t\t\t\t\t\tdelete this._ids;",
235
- "\t\t\t\t\t\tdelete this._idPool;",
236
- "\t\t\t\t\t\tthis.exit(code);",
237
- "\t\t\t\t\t},",
238
- "",
239
- "\t\t\t\t\t// func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)",
240
- "\t\t\t\t\t\"runtime.wasmWrite\": (sp) => {",
241
- "\t\t\t\t\t\tsp >>>= 0;",
242
- "\t\t\t\t\t\tconst fd = getInt64(sp + 8);",
243
- "\t\t\t\t\t\tconst p = getInt64(sp + 16);",
244
- "\t\t\t\t\t\tconst n = this.mem.getInt32(sp + 24, true);",
245
- "\t\t\t\t\t\tfs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n));",
246
- "\t\t\t\t\t},",
247
- "",
248
- "\t\t\t\t\t// func resetMemoryDataView()",
249
- "\t\t\t\t\t\"runtime.resetMemoryDataView\": (sp) => {",
250
- "\t\t\t\t\t\tsp >>>= 0;",
251
- "\t\t\t\t\t\tthis.mem = new DataView(this._inst.exports.mem.buffer);",
252
- "\t\t\t\t\t},",
253
- "",
254
- "\t\t\t\t\t// func nanotime1() int64",
255
- "\t\t\t\t\t\"runtime.nanotime1\": (sp) => {",
256
- "\t\t\t\t\t\tsp >>>= 0;",
257
- "\t\t\t\t\t\tsetInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);",
258
- "\t\t\t\t\t},",
259
- "",
260
- "\t\t\t\t\t// func walltime() (sec int64, nsec int32)",
261
- "\t\t\t\t\t\"runtime.walltime\": (sp) => {",
262
- "\t\t\t\t\t\tsp >>>= 0;",
263
- "\t\t\t\t\t\tconst msec = (new Date).getTime();",
264
- "\t\t\t\t\t\tsetInt64(sp + 8, msec / 1000);",
265
- "\t\t\t\t\t\tthis.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true);",
266
- "\t\t\t\t\t},",
267
- "",
268
- "\t\t\t\t\t// func scheduleTimeoutEvent(delay int64) int32",
269
- "\t\t\t\t\t\"runtime.scheduleTimeoutEvent\": (sp) => {",
270
- "\t\t\t\t\t\tsp >>>= 0;",
271
- "\t\t\t\t\t\tconst id = this._nextCallbackTimeoutID;",
272
- "\t\t\t\t\t\tthis._nextCallbackTimeoutID++;",
273
- "\t\t\t\t\t\tthis._scheduledTimeouts.set(id, setTimeout(",
274
- "\t\t\t\t\t\t\t() => {",
275
- "\t\t\t\t\t\t\t\tthis._resume();",
276
- "\t\t\t\t\t\t\t\twhile (this._scheduledTimeouts.has(id)) {",
277
- "\t\t\t\t\t\t\t\t\t// for some reason Go failed to register the timeout event, log and try again",
278
- "\t\t\t\t\t\t\t\t\t// (temporary workaround for https://github.com/golang/go/issues/28975)",
279
- "\t\t\t\t\t\t\t\t\tconsole.warn(\"scheduleTimeoutEvent: missed timeout event\");",
280
- "\t\t\t\t\t\t\t\t\tthis._resume();",
281
- "\t\t\t\t\t\t\t\t}",
282
- "\t\t\t\t\t\t\t},",
283
- "\t\t\t\t\t\t\tgetInt64(sp + 8),",
284
- "\t\t\t\t\t\t));",
285
- "\t\t\t\t\t\tthis.mem.setInt32(sp + 16, id, true);",
286
- "\t\t\t\t\t},",
287
- "",
288
- "\t\t\t\t\t// func clearTimeoutEvent(id int32)",
289
- "\t\t\t\t\t\"runtime.clearTimeoutEvent\": (sp) => {",
290
- "\t\t\t\t\t\tsp >>>= 0;",
291
- "\t\t\t\t\t\tconst id = this.mem.getInt32(sp + 8, true);",
292
- "\t\t\t\t\t\tclearTimeout(this._scheduledTimeouts.get(id));",
293
- "\t\t\t\t\t\tthis._scheduledTimeouts.delete(id);",
294
- "\t\t\t\t\t},",
295
- "",
296
- "\t\t\t\t\t// func getRandomData(r []byte)",
297
- "\t\t\t\t\t\"runtime.getRandomData\": (sp) => {",
298
- "\t\t\t\t\t\tsp >>>= 0;",
299
- "\t\t\t\t\t\tcrypto.getRandomValues(loadSlice(sp + 8));",
300
- "\t\t\t\t\t},",
301
- "",
302
- "\t\t\t\t\t// func finalizeRef(v ref)",
303
- "\t\t\t\t\t\"syscall/js.finalizeRef\": (sp) => {",
304
- "\t\t\t\t\t\tsp >>>= 0;",
305
- "\t\t\t\t\t\tconst id = this.mem.getUint32(sp + 8, true);",
306
- "\t\t\t\t\t\tthis._goRefCounts[id]--;",
307
- "\t\t\t\t\t\tif (this._goRefCounts[id] === 0) {",
308
- "\t\t\t\t\t\t\tconst v = this._values[id];",
309
- "\t\t\t\t\t\t\tthis._values[id] = null;",
310
- "\t\t\t\t\t\t\tthis._ids.delete(v);",
311
- "\t\t\t\t\t\t\tthis._idPool.push(id);",
312
- "\t\t\t\t\t\t}",
313
- "\t\t\t\t\t},",
314
- "",
315
- "\t\t\t\t\t// func stringVal(value string) ref",
316
- "\t\t\t\t\t\"syscall/js.stringVal\": (sp) => {",
317
- "\t\t\t\t\t\tsp >>>= 0;",
318
- "\t\t\t\t\t\tstoreValue(sp + 24, loadString(sp + 8));",
319
- "\t\t\t\t\t},",
320
- "",
321
- "\t\t\t\t\t// func valueGet(v ref, p string) ref",
322
- "\t\t\t\t\t\"syscall/js.valueGet\": (sp) => {",
323
- "\t\t\t\t\t\tsp >>>= 0;",
324
- "\t\t\t\t\t\tconst result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));",
325
- "\t\t\t\t\t\tsp = this._inst.exports.getsp() >>> 0; // see comment above",
326
- "\t\t\t\t\t\tstoreValue(sp + 32, result);",
327
- "\t\t\t\t\t},",
328
- "",
329
- "\t\t\t\t\t// func valueSet(v ref, p string, x ref)",
330
- "\t\t\t\t\t\"syscall/js.valueSet\": (sp) => {",
331
- "\t\t\t\t\t\tsp >>>= 0;",
332
- "\t\t\t\t\t\tReflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));",
333
- "\t\t\t\t\t},",
334
- "",
335
- "\t\t\t\t\t// func valueDelete(v ref, p string)",
336
- "\t\t\t\t\t\"syscall/js.valueDelete\": (sp) => {",
337
- "\t\t\t\t\t\tsp >>>= 0;",
338
- "\t\t\t\t\t\tReflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));",
339
- "\t\t\t\t\t},",
340
- "",
341
- "\t\t\t\t\t// func valueIndex(v ref, i int) ref",
342
- "\t\t\t\t\t\"syscall/js.valueIndex\": (sp) => {",
343
- "\t\t\t\t\t\tsp >>>= 0;",
344
- "\t\t\t\t\t\tstoreValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));",
345
- "\t\t\t\t\t},",
346
- "",
347
- "\t\t\t\t\t// valueSetIndex(v ref, i int, x ref)",
348
- "\t\t\t\t\t\"syscall/js.valueSetIndex\": (sp) => {",
349
- "\t\t\t\t\t\tsp >>>= 0;",
350
- "\t\t\t\t\t\tReflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));",
351
- "\t\t\t\t\t},",
352
- "",
353
- "\t\t\t\t\t// func valueCall(v ref, m string, args []ref) (ref, bool)",
354
- "\t\t\t\t\t\"syscall/js.valueCall\": (sp) => {",
355
- "\t\t\t\t\t\tsp >>>= 0;",
356
- "\t\t\t\t\t\ttry {",
357
- "\t\t\t\t\t\t\tconst v = loadValue(sp + 8);",
358
- "\t\t\t\t\t\t\tconst m = Reflect.get(v, loadString(sp + 16));",
359
- "\t\t\t\t\t\t\tconst args = loadSliceOfValues(sp + 32);",
360
- "\t\t\t\t\t\t\tconst result = Reflect.apply(m, v, args);",
361
- "\t\t\t\t\t\t\tsp = this._inst.exports.getsp() >>> 0; // see comment above",
362
- "\t\t\t\t\t\t\tstoreValue(sp + 56, result);",
363
- "\t\t\t\t\t\t\tthis.mem.setUint8(sp + 64, 1);",
364
- "\t\t\t\t\t\t} catch (err) {",
365
- "\t\t\t\t\t\t\tsp = this._inst.exports.getsp() >>> 0; // see comment above",
366
- "\t\t\t\t\t\t\tstoreValue(sp + 56, err);",
367
- "\t\t\t\t\t\t\tthis.mem.setUint8(sp + 64, 0);",
368
- "\t\t\t\t\t\t}",
369
- "\t\t\t\t\t},",
370
- "",
371
- "\t\t\t\t\t// func valueInvoke(v ref, args []ref) (ref, bool)",
372
- "\t\t\t\t\t\"syscall/js.valueInvoke\": (sp) => {",
373
- "\t\t\t\t\t\tsp >>>= 0;",
374
- "\t\t\t\t\t\ttry {",
375
- "\t\t\t\t\t\t\tconst v = loadValue(sp + 8);",
376
- "\t\t\t\t\t\t\tconst args = loadSliceOfValues(sp + 16);",
377
- "\t\t\t\t\t\t\tconst result = Reflect.apply(v, undefined, args);",
378
- "\t\t\t\t\t\t\tsp = this._inst.exports.getsp() >>> 0; // see comment above",
379
- "\t\t\t\t\t\t\tstoreValue(sp + 40, result);",
380
- "\t\t\t\t\t\t\tthis.mem.setUint8(sp + 48, 1);",
381
- "\t\t\t\t\t\t} catch (err) {",
382
- "\t\t\t\t\t\t\tsp = this._inst.exports.getsp() >>> 0; // see comment above",
383
- "\t\t\t\t\t\t\tstoreValue(sp + 40, err);",
384
- "\t\t\t\t\t\t\tthis.mem.setUint8(sp + 48, 0);",
385
- "\t\t\t\t\t\t}",
386
- "\t\t\t\t\t},",
387
- "",
388
- "\t\t\t\t\t// func valueNew(v ref, args []ref) (ref, bool)",
389
- "\t\t\t\t\t\"syscall/js.valueNew\": (sp) => {",
390
- "\t\t\t\t\t\tsp >>>= 0;",
391
- "\t\t\t\t\t\ttry {",
392
- "\t\t\t\t\t\t\tconst v = loadValue(sp + 8);",
393
- "\t\t\t\t\t\t\tconst args = loadSliceOfValues(sp + 16);",
394
- "\t\t\t\t\t\t\tconst result = Reflect.construct(v, args);",
395
- "\t\t\t\t\t\t\tsp = this._inst.exports.getsp() >>> 0; // see comment above",
396
- "\t\t\t\t\t\t\tstoreValue(sp + 40, result);",
397
- "\t\t\t\t\t\t\tthis.mem.setUint8(sp + 48, 1);",
398
- "\t\t\t\t\t\t} catch (err) {",
399
- "\t\t\t\t\t\t\tsp = this._inst.exports.getsp() >>> 0; // see comment above",
400
- "\t\t\t\t\t\t\tstoreValue(sp + 40, err);",
401
- "\t\t\t\t\t\t\tthis.mem.setUint8(sp + 48, 0);",
402
- "\t\t\t\t\t\t}",
403
- "\t\t\t\t\t},",
404
- "",
405
- "\t\t\t\t\t// func valueLength(v ref) int",
406
- "\t\t\t\t\t\"syscall/js.valueLength\": (sp) => {",
407
- "\t\t\t\t\t\tsp >>>= 0;",
408
- "\t\t\t\t\t\tsetInt64(sp + 16, parseInt(loadValue(sp + 8).length));",
409
- "\t\t\t\t\t},",
410
- "",
411
- "\t\t\t\t\t// valuePrepareString(v ref) (ref, int)",
412
- "\t\t\t\t\t\"syscall/js.valuePrepareString\": (sp) => {",
413
- "\t\t\t\t\t\tsp >>>= 0;",
414
- "\t\t\t\t\t\tconst str = encoder.encode(String(loadValue(sp + 8)));",
415
- "\t\t\t\t\t\tstoreValue(sp + 16, str);",
416
- "\t\t\t\t\t\tsetInt64(sp + 24, str.length);",
417
- "\t\t\t\t\t},",
418
- "",
419
- "\t\t\t\t\t// valueLoadString(v ref, b []byte)",
420
- "\t\t\t\t\t\"syscall/js.valueLoadString\": (sp) => {",
421
- "\t\t\t\t\t\tsp >>>= 0;",
422
- "\t\t\t\t\t\tconst str = loadValue(sp + 8);",
423
- "\t\t\t\t\t\tloadSlice(sp + 16).set(str);",
424
- "\t\t\t\t\t},",
425
- "",
426
- "\t\t\t\t\t// func valueInstanceOf(v ref, t ref) bool",
427
- "\t\t\t\t\t\"syscall/js.valueInstanceOf\": (sp) => {",
428
- "\t\t\t\t\t\tsp >>>= 0;",
429
- "\t\t\t\t\t\tthis.mem.setUint8(sp + 24, (loadValue(sp + 8) instanceof loadValue(sp + 16)) ? 1 : 0);",
430
- "\t\t\t\t\t},",
431
- "",
432
- "\t\t\t\t\t// func copyBytesToGo(dst []byte, src ref) (int, bool)",
433
- "\t\t\t\t\t\"syscall/js.copyBytesToGo\": (sp) => {",
434
- "\t\t\t\t\t\tsp >>>= 0;",
435
- "\t\t\t\t\t\tconst dst = loadSlice(sp + 8);",
436
- "\t\t\t\t\t\tconst src = loadValue(sp + 32);",
437
- "\t\t\t\t\t\tif (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) {",
438
- "\t\t\t\t\t\t\tthis.mem.setUint8(sp + 48, 0);",
439
- "\t\t\t\t\t\t\treturn;",
440
- "\t\t\t\t\t\t}",
441
- "\t\t\t\t\t\tconst toCopy = src.subarray(0, dst.length);",
442
- "\t\t\t\t\t\tdst.set(toCopy);",
443
- "\t\t\t\t\t\tsetInt64(sp + 40, toCopy.length);",
444
- "\t\t\t\t\t\tthis.mem.setUint8(sp + 48, 1);",
445
- "\t\t\t\t\t},",
446
- "",
447
- "\t\t\t\t\t// func copyBytesToJS(dst ref, src []byte) (int, bool)",
448
- "\t\t\t\t\t\"syscall/js.copyBytesToJS\": (sp) => {",
449
- "\t\t\t\t\t\tsp >>>= 0;",
450
- "\t\t\t\t\t\tconst dst = loadValue(sp + 8);",
451
- "\t\t\t\t\t\tconst src = loadSlice(sp + 16);",
452
- "\t\t\t\t\t\tif (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) {",
453
- "\t\t\t\t\t\t\tthis.mem.setUint8(sp + 48, 0);",
454
- "\t\t\t\t\t\t\treturn;",
455
- "\t\t\t\t\t\t}",
456
- "\t\t\t\t\t\tconst toCopy = src.subarray(0, dst.length);",
457
- "\t\t\t\t\t\tdst.set(toCopy);",
458
- "\t\t\t\t\t\tsetInt64(sp + 40, toCopy.length);",
459
- "\t\t\t\t\t\tthis.mem.setUint8(sp + 48, 1);",
460
- "\t\t\t\t\t},",
461
- "",
462
- "\t\t\t\t\t\"debug\": (value) => {",
463
- "\t\t\t\t\t\tconsole.log(value);",
464
- "\t\t\t\t\t},",
465
- "\t\t\t\t}",
466
- "\t\t\t};",
467
- "\t\t}",
468
- "",
469
- "\t\tasync run(instance) {",
470
- "\t\t\tif (!(instance instanceof WebAssembly.Instance)) {",
471
- "\t\t\t\tthrow new Error(\"Go.run: WebAssembly.Instance expected\");",
472
- "\t\t\t}",
473
- "\t\t\tthis._inst = instance;",
474
- "\t\t\tthis.mem = new DataView(this._inst.exports.mem.buffer);",
475
- "\t\t\tthis._values = [ // JS values that Go currently has references to, indexed by reference id",
476
- "\t\t\t\tNaN,",
477
- "\t\t\t\t0,",
478
- "\t\t\t\tnull,",
479
- "\t\t\t\ttrue,",
480
- "\t\t\t\tfalse,",
481
- "\t\t\t\tglobalThis,",
482
- "\t\t\t\tthis,",
483
- "\t\t\t];",
484
- "\t\t\tthis._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id",
485
- "\t\t\tthis._ids = new Map([ // mapping from JS values to reference ids",
486
- "\t\t\t\t[0, 1],",
487
- "\t\t\t\t[null, 2],",
488
- "\t\t\t\t[true, 3],",
489
- "\t\t\t\t[false, 4],",
490
- "\t\t\t\t[globalThis, 5],",
491
- "\t\t\t\t[this, 6],",
492
- "\t\t\t]);",
493
- "\t\t\tthis._idPool = []; // unused ids that have been garbage collected",
494
- "\t\t\tthis.exited = false; // whether the Go program has exited",
495
- "",
496
- "\t\t\t// Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.",
497
- "\t\t\tlet offset = 4096;",
498
- "",
499
- "\t\t\tconst strPtr = (str) => {",
500
- "\t\t\t\tconst ptr = offset;",
501
- "\t\t\t\tconst bytes = encoder.encode(str + \"\\0\");",
502
- "\t\t\t\tnew Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);",
503
- "\t\t\t\toffset += bytes.length;",
504
- "\t\t\t\tif (offset % 8 !== 0) {",
505
- "\t\t\t\t\toffset += 8 - (offset % 8);",
506
- "\t\t\t\t}",
507
- "\t\t\t\treturn ptr;",
508
- "\t\t\t};",
509
- "",
510
- "\t\t\tconst argc = this.argv.length;",
511
- "",
512
- "\t\t\tconst argvPtrs = [];",
513
- "\t\t\tthis.argv.forEach((arg) => {",
514
- "\t\t\t\targvPtrs.push(strPtr(arg));",
515
- "\t\t\t});",
516
- "\t\t\targvPtrs.push(0);",
517
- "",
518
- "\t\t\tconst keys = Object.keys(this.env).sort();",
519
- "\t\t\tkeys.forEach((key) => {",
520
- "\t\t\t\targvPtrs.push(strPtr(`${key}=${this.env[key]}`));",
521
- "\t\t\t});",
522
- "\t\t\targvPtrs.push(0);",
523
- "",
524
- "\t\t\tconst argv = offset;",
525
- "\t\t\targvPtrs.forEach((ptr) => {",
526
- "\t\t\t\tthis.mem.setUint32(offset, ptr, true);",
527
- "\t\t\t\tthis.mem.setUint32(offset + 4, 0, true);",
528
- "\t\t\t\toffset += 8;",
529
- "\t\t\t});",
530
- "",
531
- "\t\t\t// The linker guarantees global data starts from at least wasmMinDataAddr.",
532
- "\t\t\t// Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr.",
533
- "\t\t\tconst wasmMinDataAddr = 4096 + 8192;",
534
- "\t\t\tif (offset >= wasmMinDataAddr) {",
535
- "\t\t\t\tthrow new Error(\"total length of command line and environment variables exceeds limit\");",
536
- "\t\t\t}",
537
- "",
538
- "\t\t\tthis._inst.exports.run(argc, argv);",
539
- "\t\t\tif (this.exited) {",
540
- "\t\t\t\tthis._resolveExitPromise();",
541
- "\t\t\t}",
542
- "\t\t\tawait this._exitPromise;",
543
- "\t\t}",
544
- "",
545
- "\t\t_resume() {",
546
- "\t\t\tif (this.exited) {",
547
- "\t\t\t\tthrow new Error(\"Go program has already exited\");",
548
- "\t\t\t}",
549
- "\t\t\tthis._inst.exports.resume();",
550
- "\t\t\tif (this.exited) {",
551
- "\t\t\t\tthis._resolveExitPromise();",
552
- "\t\t\t}",
553
- "\t\t}",
554
- "",
555
- "\t\t_makeFuncWrapper(id) {",
556
- "\t\t\tconst go = this;",
557
- "\t\t\treturn function () {",
558
- "\t\t\t\tconst event = { id: id, this: this, args: arguments };",
559
- "\t\t\t\tgo._pendingEvent = event;",
560
- "\t\t\t\tgo._resume();",
561
- "\t\t\t\treturn event.result;",
562
- "\t\t\t};",
563
- "\t\t}",
564
- "\t}",
565
- "})();",
566
- "",
567
- ];
568
- exports.WASM_EXEC_JS = WASM_EXEC_LINES.join('\n');
3
+ exports.createGoRuntime = createGoRuntime;
4
+ function createGoRuntime() {
5
+ "use strict";
6
+ (() => {
7
+ const enosys = () => {
8
+ const err = new Error("not implemented");
9
+ err.code = "ENOSYS";
10
+ return err;
11
+ };
12
+ if (!globalThis.fs) {
13
+ let outputBuf = "";
14
+ globalThis.fs = {
15
+ constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 },
16
+ writeSync(fd, buf) {
17
+ outputBuf += decoder.decode(buf);
18
+ const nl = outputBuf.lastIndexOf("\n");
19
+ if (nl != -1) {
20
+ console.log(outputBuf.substring(0, nl));
21
+ outputBuf = outputBuf.substring(nl + 1);
22
+ }
23
+ return buf.length;
24
+ },
25
+ write(fd, buf, offset, length, position, callback) {
26
+ if (offset !== 0 || length !== buf.length || position !== null) {
27
+ callback(enosys());
28
+ return;
29
+ }
30
+ const n = this.writeSync(fd, buf);
31
+ callback(null, n);
32
+ },
33
+ chmod(path, mode, callback) { callback(enosys()); },
34
+ chown(path, uid, gid, callback) { callback(enosys()); },
35
+ close(fd, callback) { callback(enosys()); },
36
+ fchmod(fd, mode, callback) { callback(enosys()); },
37
+ fchown(fd, uid, gid, callback) { callback(enosys()); },
38
+ fstat(fd, callback) { callback(enosys()); },
39
+ fsync(fd, callback) { callback(null); },
40
+ ftruncate(fd, length, callback) { callback(enosys()); },
41
+ lchown(path, uid, gid, callback) { callback(enosys()); },
42
+ link(path, link, callback) { callback(enosys()); },
43
+ lstat(path, callback) { callback(enosys()); },
44
+ mkdir(path, perm, callback) { callback(enosys()); },
45
+ open(path, flags, mode, callback) { callback(enosys()); },
46
+ read(fd, buffer, offset, length, position, callback) { callback(enosys()); },
47
+ readdir(path, callback) { callback(enosys()); },
48
+ readlink(path, callback) { callback(enosys()); },
49
+ rename(from, to, callback) { callback(enosys()); },
50
+ rmdir(path, callback) { callback(enosys()); },
51
+ stat(path, callback) { callback(enosys()); },
52
+ symlink(path, link, callback) { callback(enosys()); },
53
+ truncate(path, length, callback) { callback(enosys()); },
54
+ unlink(path, callback) { callback(enosys()); },
55
+ utimes(path, atime, mtime, callback) { callback(enosys()); },
56
+ };
57
+ }
58
+ if (!globalThis.process) {
59
+ globalThis.process = {
60
+ getuid() { return -1; },
61
+ getgid() { return -1; },
62
+ geteuid() { return -1; },
63
+ getegid() { return -1; },
64
+ getgroups() { throw enosys(); },
65
+ pid: -1,
66
+ ppid: -1,
67
+ umask() { throw enosys(); },
68
+ cwd() { throw enosys(); },
69
+ chdir() { throw enosys(); },
70
+ };
71
+ }
72
+ if (!globalThis.crypto) {
73
+ throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)");
74
+ }
75
+ if (!globalThis.performance) {
76
+ throw new Error("globalThis.performance is not available, polyfill required (performance.now only)");
77
+ }
78
+ if (!globalThis.TextEncoder) {
79
+ throw new Error("globalThis.TextEncoder is not available, polyfill required");
80
+ }
81
+ if (!globalThis.TextDecoder) {
82
+ throw new Error("globalThis.TextDecoder is not available, polyfill required");
83
+ }
84
+ const encoder = new TextEncoder("utf-8");
85
+ const decoder = new TextDecoder("utf-8");
86
+ globalThis.Go = class {
87
+ constructor() {
88
+ this.argv = ["js"];
89
+ this.env = {};
90
+ this.exit = (code) => {
91
+ if (code !== 0) {
92
+ console.warn("exit code:", code);
93
+ }
94
+ };
95
+ this._exitPromise = new Promise((resolve) => {
96
+ this._resolveExitPromise = resolve;
97
+ });
98
+ this._pendingEvent = null;
99
+ this._scheduledTimeouts = new Map();
100
+ this._nextCallbackTimeoutID = 1;
101
+ const setInt64 = (addr, v) => {
102
+ this.mem.setUint32(addr + 0, v, true);
103
+ this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
104
+ };
105
+ const setInt32 = (addr, v) => {
106
+ this.mem.setUint32(addr + 0, v, true);
107
+ };
108
+ const getInt64 = (addr) => {
109
+ const low = this.mem.getUint32(addr + 0, true);
110
+ const high = this.mem.getInt32(addr + 4, true);
111
+ return low + high * 4294967296;
112
+ };
113
+ const loadValue = (addr) => {
114
+ const f = this.mem.getFloat64(addr, true);
115
+ if (f === 0) {
116
+ return undefined;
117
+ }
118
+ if (!isNaN(f)) {
119
+ return f;
120
+ }
121
+ const id = this.mem.getUint32(addr, true);
122
+ return this._values[id];
123
+ };
124
+ const storeValue = (addr, v) => {
125
+ const nanHead = 0x7FF80000;
126
+ if (typeof v === "number" && v !== 0) {
127
+ if (isNaN(v)) {
128
+ this.mem.setUint32(addr + 4, nanHead, true);
129
+ this.mem.setUint32(addr, 0, true);
130
+ return;
131
+ }
132
+ this.mem.setFloat64(addr, v, true);
133
+ return;
134
+ }
135
+ if (v === undefined) {
136
+ this.mem.setFloat64(addr, 0, true);
137
+ return;
138
+ }
139
+ let id = this._ids.get(v);
140
+ if (id === undefined) {
141
+ id = this._idPool.pop();
142
+ if (id === undefined) {
143
+ id = this._values.length;
144
+ }
145
+ this._values[id] = v;
146
+ this._goRefCounts[id] = 0;
147
+ this._ids.set(v, id);
148
+ }
149
+ this._goRefCounts[id]++;
150
+ let typeFlag = 0;
151
+ switch (typeof v) {
152
+ case "object":
153
+ if (v !== null) {
154
+ typeFlag = 1;
155
+ }
156
+ break;
157
+ case "string":
158
+ typeFlag = 2;
159
+ break;
160
+ case "symbol":
161
+ typeFlag = 3;
162
+ break;
163
+ case "function":
164
+ typeFlag = 4;
165
+ break;
166
+ }
167
+ this.mem.setUint32(addr + 4, nanHead | typeFlag, true);
168
+ this.mem.setUint32(addr, id, true);
169
+ };
170
+ const loadSlice = (addr) => {
171
+ const array = getInt64(addr + 0);
172
+ const len = getInt64(addr + 8);
173
+ return new Uint8Array(this._inst.exports.mem.buffer, array, len);
174
+ };
175
+ const loadSliceOfValues = (addr) => {
176
+ const array = getInt64(addr + 0);
177
+ const len = getInt64(addr + 8);
178
+ const a = new Array(len);
179
+ for (let i = 0; i < len; i++) {
180
+ a[i] = loadValue(array + i * 8);
181
+ }
182
+ return a;
183
+ };
184
+ const loadString = (addr) => {
185
+ const saddr = getInt64(addr + 0);
186
+ const len = getInt64(addr + 8);
187
+ return decoder.decode(new DataView(this._inst.exports.mem.buffer, saddr, len));
188
+ };
189
+ const timeOrigin = Date.now() - performance.now();
190
+ this.importObject = {
191
+ _gotest: {
192
+ add: (a, b) => a + b,
193
+ },
194
+ gojs: {
195
+ "runtime.wasmExit": (sp) => {
196
+ sp >>>= 0;
197
+ const code = this.mem.getInt32(sp + 8, true);
198
+ this.exited = true;
199
+ delete this._inst;
200
+ delete this._values;
201
+ delete this._goRefCounts;
202
+ delete this._ids;
203
+ delete this._idPool;
204
+ this.exit(code);
205
+ },
206
+ "runtime.wasmWrite": (sp) => {
207
+ sp >>>= 0;
208
+ const fd = getInt64(sp + 8);
209
+ const p = getInt64(sp + 16);
210
+ const n = this.mem.getInt32(sp + 24, true);
211
+ fs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n));
212
+ },
213
+ "runtime.resetMemoryDataView": (sp) => {
214
+ sp >>>= 0;
215
+ this.mem = new DataView(this._inst.exports.mem.buffer);
216
+ },
217
+ "runtime.nanotime1": (sp) => {
218
+ sp >>>= 0;
219
+ setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
220
+ },
221
+ "runtime.walltime": (sp) => {
222
+ sp >>>= 0;
223
+ const msec = (new Date).getTime();
224
+ setInt64(sp + 8, msec / 1000);
225
+ this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true);
226
+ },
227
+ "runtime.scheduleTimeoutEvent": (sp) => {
228
+ sp >>>= 0;
229
+ const id = this._nextCallbackTimeoutID;
230
+ this._nextCallbackTimeoutID++;
231
+ this._scheduledTimeouts.set(id, setTimeout(() => {
232
+ this._resume();
233
+ while (this._scheduledTimeouts.has(id)) {
234
+ console.warn("scheduleTimeoutEvent: missed timeout event");
235
+ this._resume();
236
+ }
237
+ }, getInt64(sp + 8)));
238
+ this.mem.setInt32(sp + 16, id, true);
239
+ },
240
+ "runtime.clearTimeoutEvent": (sp) => {
241
+ sp >>>= 0;
242
+ const id = this.mem.getInt32(sp + 8, true);
243
+ clearTimeout(this._scheduledTimeouts.get(id));
244
+ this._scheduledTimeouts.delete(id);
245
+ },
246
+ "runtime.getRandomData": (sp) => {
247
+ sp >>>= 0;
248
+ crypto.getRandomValues(loadSlice(sp + 8));
249
+ },
250
+ "syscall/js.finalizeRef": (sp) => {
251
+ sp >>>= 0;
252
+ const id = this.mem.getUint32(sp + 8, true);
253
+ this._goRefCounts[id]--;
254
+ if (this._goRefCounts[id] === 0) {
255
+ const v = this._values[id];
256
+ this._values[id] = null;
257
+ this._ids.delete(v);
258
+ this._idPool.push(id);
259
+ }
260
+ },
261
+ "syscall/js.stringVal": (sp) => {
262
+ sp >>>= 0;
263
+ storeValue(sp + 24, loadString(sp + 8));
264
+ },
265
+ "syscall/js.valueGet": (sp) => {
266
+ sp >>>= 0;
267
+ const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));
268
+ sp = this._inst.exports.getsp() >>> 0;
269
+ storeValue(sp + 32, result);
270
+ },
271
+ "syscall/js.valueSet": (sp) => {
272
+ sp >>>= 0;
273
+ Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
274
+ },
275
+ "syscall/js.valueDelete": (sp) => {
276
+ sp >>>= 0;
277
+ Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));
278
+ },
279
+ "syscall/js.valueIndex": (sp) => {
280
+ sp >>>= 0;
281
+ storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
282
+ },
283
+ "syscall/js.valueSetIndex": (sp) => {
284
+ sp >>>= 0;
285
+ Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));
286
+ },
287
+ "syscall/js.valueCall": (sp) => {
288
+ sp >>>= 0;
289
+ try {
290
+ const v = loadValue(sp + 8);
291
+ const m = Reflect.get(v, loadString(sp + 16));
292
+ const args = loadSliceOfValues(sp + 32);
293
+ const result = Reflect.apply(m, v, args);
294
+ sp = this._inst.exports.getsp() >>> 0;
295
+ storeValue(sp + 56, result);
296
+ this.mem.setUint8(sp + 64, 1);
297
+ }
298
+ catch (err) {
299
+ sp = this._inst.exports.getsp() >>> 0;
300
+ storeValue(sp + 56, err);
301
+ this.mem.setUint8(sp + 64, 0);
302
+ }
303
+ },
304
+ "syscall/js.valueInvoke": (sp) => {
305
+ sp >>>= 0;
306
+ try {
307
+ const v = loadValue(sp + 8);
308
+ const args = loadSliceOfValues(sp + 16);
309
+ const result = Reflect.apply(v, undefined, args);
310
+ sp = this._inst.exports.getsp() >>> 0;
311
+ storeValue(sp + 40, result);
312
+ this.mem.setUint8(sp + 48, 1);
313
+ }
314
+ catch (err) {
315
+ sp = this._inst.exports.getsp() >>> 0;
316
+ storeValue(sp + 40, err);
317
+ this.mem.setUint8(sp + 48, 0);
318
+ }
319
+ },
320
+ "syscall/js.valueNew": (sp) => {
321
+ sp >>>= 0;
322
+ try {
323
+ const v = loadValue(sp + 8);
324
+ const args = loadSliceOfValues(sp + 16);
325
+ const result = Reflect.construct(v, args);
326
+ sp = this._inst.exports.getsp() >>> 0;
327
+ storeValue(sp + 40, result);
328
+ this.mem.setUint8(sp + 48, 1);
329
+ }
330
+ catch (err) {
331
+ sp = this._inst.exports.getsp() >>> 0;
332
+ storeValue(sp + 40, err);
333
+ this.mem.setUint8(sp + 48, 0);
334
+ }
335
+ },
336
+ "syscall/js.valueLength": (sp) => {
337
+ sp >>>= 0;
338
+ setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
339
+ },
340
+ "syscall/js.valuePrepareString": (sp) => {
341
+ sp >>>= 0;
342
+ const str = encoder.encode(String(loadValue(sp + 8)));
343
+ storeValue(sp + 16, str);
344
+ setInt64(sp + 24, str.length);
345
+ },
346
+ "syscall/js.valueLoadString": (sp) => {
347
+ sp >>>= 0;
348
+ const str = loadValue(sp + 8);
349
+ loadSlice(sp + 16).set(str);
350
+ },
351
+ "syscall/js.valueInstanceOf": (sp) => {
352
+ sp >>>= 0;
353
+ this.mem.setUint8(sp + 24, (loadValue(sp + 8) instanceof loadValue(sp + 16)) ? 1 : 0);
354
+ },
355
+ "syscall/js.copyBytesToGo": (sp) => {
356
+ sp >>>= 0;
357
+ const dst = loadSlice(sp + 8);
358
+ const src = loadValue(sp + 32);
359
+ if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) {
360
+ this.mem.setUint8(sp + 48, 0);
361
+ return;
362
+ }
363
+ const toCopy = src.subarray(0, dst.length);
364
+ dst.set(toCopy);
365
+ setInt64(sp + 40, toCopy.length);
366
+ this.mem.setUint8(sp + 48, 1);
367
+ },
368
+ "syscall/js.copyBytesToJS": (sp) => {
369
+ sp >>>= 0;
370
+ const dst = loadValue(sp + 8);
371
+ const src = loadSlice(sp + 16);
372
+ if (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) {
373
+ this.mem.setUint8(sp + 48, 0);
374
+ return;
375
+ }
376
+ const toCopy = src.subarray(0, dst.length);
377
+ dst.set(toCopy);
378
+ setInt64(sp + 40, toCopy.length);
379
+ this.mem.setUint8(sp + 48, 1);
380
+ },
381
+ "debug": (value) => {
382
+ console.log(value);
383
+ },
384
+ }
385
+ };
386
+ }
387
+ async run(instance) {
388
+ if (!(instance instanceof WebAssembly.Instance)) {
389
+ throw new Error("Go.run: WebAssembly.Instance expected");
390
+ }
391
+ this._inst = instance;
392
+ this.mem = new DataView(this._inst.exports.mem.buffer);
393
+ this._values = [
394
+ NaN,
395
+ 0,
396
+ null,
397
+ true,
398
+ false,
399
+ globalThis,
400
+ this,
401
+ ];
402
+ this._goRefCounts = new Array(this._values.length).fill(Infinity);
403
+ this._ids = new Map([
404
+ [0, 1],
405
+ [null, 2],
406
+ [true, 3],
407
+ [false, 4],
408
+ [globalThis, 5],
409
+ [this, 6],
410
+ ]);
411
+ this._idPool = [];
412
+ this.exited = false;
413
+ let offset = 4096;
414
+ const strPtr = (str) => {
415
+ const ptr = offset;
416
+ const bytes = encoder.encode(str + "\0");
417
+ new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
418
+ offset += bytes.length;
419
+ if (offset % 8 !== 0) {
420
+ offset += 8 - (offset % 8);
421
+ }
422
+ return ptr;
423
+ };
424
+ const argc = this.argv.length;
425
+ const argvPtrs = [];
426
+ this.argv.forEach((arg) => {
427
+ argvPtrs.push(strPtr(arg));
428
+ });
429
+ argvPtrs.push(0);
430
+ const keys = Object.keys(this.env).sort();
431
+ keys.forEach((key) => {
432
+ argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
433
+ });
434
+ argvPtrs.push(0);
435
+ const argv = offset;
436
+ argvPtrs.forEach((ptr) => {
437
+ this.mem.setUint32(offset, ptr, true);
438
+ this.mem.setUint32(offset + 4, 0, true);
439
+ offset += 8;
440
+ });
441
+ const wasmMinDataAddr = 4096 + 8192;
442
+ if (offset >= wasmMinDataAddr) {
443
+ throw new Error("total length of command line and environment variables exceeds limit");
444
+ }
445
+ this._inst.exports.run(argc, argv);
446
+ if (this.exited) {
447
+ this._resolveExitPromise();
448
+ }
449
+ await this._exitPromise;
450
+ }
451
+ _resume() {
452
+ if (this.exited) {
453
+ throw new Error("Go program has already exited");
454
+ }
455
+ this._inst.exports.resume();
456
+ if (this.exited) {
457
+ this._resolveExitPromise();
458
+ }
459
+ }
460
+ _makeFuncWrapper(id) {
461
+ const go = this;
462
+ return function () {
463
+ const event = { id: id, this: this, args: arguments };
464
+ go._pendingEvent = event;
465
+ go._resume();
466
+ return event.result;
467
+ };
468
+ }
469
+ };
470
+ })();
471
+ return new globalThis.Go();
472
+ }
569
473
  //# sourceMappingURL=wasmExecRuntime.js.map