@godexture/js 0.0.1 → 0.0.4
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/main.wasm +0 -0
- package/dist/wasm_exec.js +559 -559
- package/licenses/go.txt +27 -27
- package/package.json +32 -32
package/dist/main.wasm
CHANGED
|
Binary file
|
package/dist/wasm_exec.js
CHANGED
|
@@ -1,559 +1,559 @@
|
|
|
1
|
-
// Copyright 2018 The Go Authors. All rights reserved.
|
|
2
|
-
// Use of this source code is governed by a BSD-style
|
|
3
|
-
// license that can be found in the LICENSE file.
|
|
4
|
-
//
|
|
5
|
-
// This file has been modified for use by the TinyGo compiler.
|
|
6
|
-
|
|
7
|
-
(() => {
|
|
8
|
-
// Map multiple JavaScript environments to a single common API,
|
|
9
|
-
// preferring web standards over Node.js API.
|
|
10
|
-
//
|
|
11
|
-
// Environments considered:
|
|
12
|
-
// - Browsers
|
|
13
|
-
// - Node.js
|
|
14
|
-
// - Electron
|
|
15
|
-
// - Parcel
|
|
16
|
-
|
|
17
|
-
if (typeof global !== "undefined") {
|
|
18
|
-
// global already exists
|
|
19
|
-
} else if (typeof window !== "undefined") {
|
|
20
|
-
window.global = window;
|
|
21
|
-
} else if (typeof self !== "undefined") {
|
|
22
|
-
self.global = self;
|
|
23
|
-
} else {
|
|
24
|
-
throw new Error("cannot export Go (neither global, window nor self is defined)");
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (!global.require && typeof require !== "undefined") {
|
|
28
|
-
global.require = require;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (!global.fs && global.require) {
|
|
32
|
-
global.fs = require("node:fs");
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const enosys = () => {
|
|
36
|
-
const err = new Error("not implemented");
|
|
37
|
-
err.code = "ENOSYS";
|
|
38
|
-
return err;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
if (!global.fs) {
|
|
42
|
-
let outputBuf = "";
|
|
43
|
-
global.fs = {
|
|
44
|
-
constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused
|
|
45
|
-
writeSync(fd, buf) {
|
|
46
|
-
outputBuf += decoder.decode(buf);
|
|
47
|
-
const nl = outputBuf.lastIndexOf("\n");
|
|
48
|
-
if (nl != -1) {
|
|
49
|
-
console.log(outputBuf.substr(0, nl));
|
|
50
|
-
outputBuf = outputBuf.substr(nl + 1);
|
|
51
|
-
}
|
|
52
|
-
return buf.length;
|
|
53
|
-
},
|
|
54
|
-
write(fd, buf, offset, length, position, callback) {
|
|
55
|
-
if (offset !== 0 || length !== buf.length || position !== null) {
|
|
56
|
-
callback(enosys());
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
const n = this.writeSync(fd, buf);
|
|
60
|
-
callback(null, n);
|
|
61
|
-
},
|
|
62
|
-
chmod(path, mode, callback) { callback(enosys()); },
|
|
63
|
-
chown(path, uid, gid, callback) { callback(enosys()); },
|
|
64
|
-
close(fd, callback) { callback(enosys()); },
|
|
65
|
-
fchmod(fd, mode, callback) { callback(enosys()); },
|
|
66
|
-
fchown(fd, uid, gid, callback) { callback(enosys()); },
|
|
67
|
-
fstat(fd, callback) { callback(enosys()); },
|
|
68
|
-
fsync(fd, callback) { callback(null); },
|
|
69
|
-
ftruncate(fd, length, callback) { callback(enosys()); },
|
|
70
|
-
lchown(path, uid, gid, callback) { callback(enosys()); },
|
|
71
|
-
link(path, link, callback) { callback(enosys()); },
|
|
72
|
-
lstat(path, callback) { callback(enosys()); },
|
|
73
|
-
mkdir(path, perm, callback) { callback(enosys()); },
|
|
74
|
-
open(path, flags, mode, callback) { callback(enosys()); },
|
|
75
|
-
read(fd, buffer, offset, length, position, callback) { callback(enosys()); },
|
|
76
|
-
readdir(path, callback) { callback(enosys()); },
|
|
77
|
-
readlink(path, callback) { callback(enosys()); },
|
|
78
|
-
rename(from, to, callback) { callback(enosys()); },
|
|
79
|
-
rmdir(path, callback) { callback(enosys()); },
|
|
80
|
-
stat(path, callback) { callback(enosys()); },
|
|
81
|
-
symlink(path, link, callback) { callback(enosys()); },
|
|
82
|
-
truncate(path, length, callback) { callback(enosys()); },
|
|
83
|
-
unlink(path, callback) { callback(enosys()); },
|
|
84
|
-
utimes(path, atime, mtime, callback) { callback(enosys()); },
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (!global.process) {
|
|
89
|
-
global.process = {
|
|
90
|
-
getuid() { return -1; },
|
|
91
|
-
getgid() { return -1; },
|
|
92
|
-
geteuid() { return -1; },
|
|
93
|
-
getegid() { return -1; },
|
|
94
|
-
getgroups() { throw enosys(); },
|
|
95
|
-
pid: -1,
|
|
96
|
-
ppid: -1,
|
|
97
|
-
umask() { throw enosys(); },
|
|
98
|
-
cwd() { throw enosys(); },
|
|
99
|
-
chdir() { throw enosys(); },
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
if (!global.crypto) {
|
|
104
|
-
const nodeCrypto = require("node:crypto");
|
|
105
|
-
global.crypto = {
|
|
106
|
-
getRandomValues(b) {
|
|
107
|
-
nodeCrypto.randomFillSync(b);
|
|
108
|
-
},
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (!global.performance) {
|
|
113
|
-
global.performance = {
|
|
114
|
-
now() {
|
|
115
|
-
const [sec, nsec] = process.hrtime();
|
|
116
|
-
return sec * 1000 + nsec / 1000000;
|
|
117
|
-
},
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
if (!global.TextEncoder) {
|
|
122
|
-
global.TextEncoder = require("node:util").TextEncoder;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
if (!global.TextDecoder) {
|
|
126
|
-
global.TextDecoder = require("node:util").TextDecoder;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// End of polyfills for common API.
|
|
130
|
-
|
|
131
|
-
const encoder = new TextEncoder("utf-8");
|
|
132
|
-
const decoder = new TextDecoder("utf-8");
|
|
133
|
-
let reinterpretBuf = new DataView(new ArrayBuffer(8));
|
|
134
|
-
var logLine = [];
|
|
135
|
-
const wasmExit = {}; // thrown to exit via proc_exit (not an error)
|
|
136
|
-
|
|
137
|
-
global.Go = class {
|
|
138
|
-
constructor() {
|
|
139
|
-
this._callbackTimeouts = new Map();
|
|
140
|
-
this._nextCallbackTimeoutID = 1;
|
|
141
|
-
|
|
142
|
-
const mem = () => {
|
|
143
|
-
// The buffer may change when requesting more memory.
|
|
144
|
-
return new DataView(this._inst.exports.memory.buffer);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
const unboxValue = (v_ref) => {
|
|
148
|
-
reinterpretBuf.setBigInt64(0, v_ref, true);
|
|
149
|
-
const f = reinterpretBuf.getFloat64(0, true);
|
|
150
|
-
if (f === 0) {
|
|
151
|
-
return undefined;
|
|
152
|
-
}
|
|
153
|
-
if (!isNaN(f)) {
|
|
154
|
-
return f;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const id = v_ref & 0xffffffffn;
|
|
158
|
-
return this._values[id];
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
const loadValue = (addr) => {
|
|
163
|
-
let v_ref = mem().getBigUint64(addr, true);
|
|
164
|
-
return unboxValue(v_ref);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
const boxValue = (v) => {
|
|
168
|
-
const nanHead = 0x7FF80000n;
|
|
169
|
-
|
|
170
|
-
if (typeof v === "number") {
|
|
171
|
-
if (isNaN(v)) {
|
|
172
|
-
return nanHead << 32n;
|
|
173
|
-
}
|
|
174
|
-
if (v === 0) {
|
|
175
|
-
return (nanHead << 32n) | 1n;
|
|
176
|
-
}
|
|
177
|
-
reinterpretBuf.setFloat64(0, v, true);
|
|
178
|
-
return reinterpretBuf.getBigInt64(0, true);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
switch (v) {
|
|
182
|
-
case undefined:
|
|
183
|
-
return 0n;
|
|
184
|
-
case null:
|
|
185
|
-
return (nanHead << 32n) | 2n;
|
|
186
|
-
case true:
|
|
187
|
-
return (nanHead << 32n) | 3n;
|
|
188
|
-
case false:
|
|
189
|
-
return (nanHead << 32n) | 4n;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
let id = this._ids.get(v);
|
|
193
|
-
if (id === undefined) {
|
|
194
|
-
id = this._idPool.pop();
|
|
195
|
-
if (id === undefined) {
|
|
196
|
-
id = BigInt(this._values.length);
|
|
197
|
-
}
|
|
198
|
-
this._values[id] = v;
|
|
199
|
-
this._goRefCounts[id] = 0;
|
|
200
|
-
this._ids.set(v, id);
|
|
201
|
-
}
|
|
202
|
-
this._goRefCounts[id]++;
|
|
203
|
-
let typeFlag = 1n;
|
|
204
|
-
switch (typeof v) {
|
|
205
|
-
case "string":
|
|
206
|
-
typeFlag = 2n;
|
|
207
|
-
break;
|
|
208
|
-
case "symbol":
|
|
209
|
-
typeFlag = 3n;
|
|
210
|
-
break;
|
|
211
|
-
case "function":
|
|
212
|
-
typeFlag = 4n;
|
|
213
|
-
break;
|
|
214
|
-
}
|
|
215
|
-
return id | ((nanHead | typeFlag) << 32n);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
const storeValue = (addr, v) => {
|
|
219
|
-
let v_ref = boxValue(v);
|
|
220
|
-
mem().setBigUint64(addr, v_ref, true);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
const loadSlice = (array, len, cap) => {
|
|
224
|
-
return new Uint8Array(this._inst.exports.memory.buffer, array, len);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
const loadSliceOfValues = (array, len, cap) => {
|
|
228
|
-
const a = new Array(len);
|
|
229
|
-
for (let i = 0; i < len; i++) {
|
|
230
|
-
a[i] = loadValue(array + i * 8);
|
|
231
|
-
}
|
|
232
|
-
return a;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
const loadString = (ptr, len) => {
|
|
236
|
-
return decoder.decode(new DataView(this._inst.exports.memory.buffer, ptr, len));
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
const timeOrigin = Date.now() - performance.now();
|
|
240
|
-
const wasi_EBADF = 8;
|
|
241
|
-
const wasi_ENOSYS = 52;
|
|
242
|
-
this.importObject = {
|
|
243
|
-
wasi_snapshot_preview1: {
|
|
244
|
-
// https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md
|
|
245
|
-
fd_write: function(fd, iovs_ptr, iovs_len, nwritten_ptr) {
|
|
246
|
-
let nwritten = 0;
|
|
247
|
-
if (fd == 1) {
|
|
248
|
-
for (let iovs_i=0; iovs_i<iovs_len;iovs_i++) {
|
|
249
|
-
let iov_ptr = iovs_ptr+iovs_i*8; // assuming wasm32
|
|
250
|
-
let ptr = mem().getUint32(iov_ptr + 0, true);
|
|
251
|
-
let len = mem().getUint32(iov_ptr + 4, true);
|
|
252
|
-
nwritten += len;
|
|
253
|
-
for (let i=0; i<len; i++) {
|
|
254
|
-
let c = mem().getUint8(ptr+i);
|
|
255
|
-
if (c == 13) { // CR
|
|
256
|
-
// ignore
|
|
257
|
-
} else if (c == 10) { // LF
|
|
258
|
-
// write line
|
|
259
|
-
let line = decoder.decode(new Uint8Array(logLine));
|
|
260
|
-
logLine = [];
|
|
261
|
-
console.log(line);
|
|
262
|
-
} else {
|
|
263
|
-
logLine.push(c);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
} else {
|
|
268
|
-
console.error('invalid file descriptor:', fd);
|
|
269
|
-
}
|
|
270
|
-
mem().setUint32(nwritten_ptr, nwritten, true);
|
|
271
|
-
return 0;
|
|
272
|
-
},
|
|
273
|
-
fd_read: () => wasi_ENOSYS,
|
|
274
|
-
fd_close: () => wasi_ENOSYS,
|
|
275
|
-
fd_fdstat_get: () => wasi_ENOSYS,
|
|
276
|
-
fd_prestat_get: () => wasi_EBADF, // wasi-libc relies on this errno value
|
|
277
|
-
fd_prestat_dir_name: () => wasi_ENOSYS,
|
|
278
|
-
fd_seek: () => wasi_ENOSYS,
|
|
279
|
-
path_open: () => wasi_ENOSYS,
|
|
280
|
-
proc_exit: (code) => {
|
|
281
|
-
this.exited = true;
|
|
282
|
-
this.exitCode = code;
|
|
283
|
-
this._resolveExitPromise();
|
|
284
|
-
throw wasmExit;
|
|
285
|
-
},
|
|
286
|
-
random_get: (bufPtr, bufLen) => {
|
|
287
|
-
crypto.getRandomValues(loadSlice(bufPtr, bufLen));
|
|
288
|
-
return 0;
|
|
289
|
-
},
|
|
290
|
-
},
|
|
291
|
-
gojs: {
|
|
292
|
-
// func ticks() int64
|
|
293
|
-
"runtime.ticks": () => {
|
|
294
|
-
return BigInt((timeOrigin + performance.now()) * 1e6);
|
|
295
|
-
},
|
|
296
|
-
|
|
297
|
-
// func sleepTicks(timeout int64)
|
|
298
|
-
"runtime.sleepTicks": (timeout) => {
|
|
299
|
-
// Do not sleep, only reactivate scheduler after the given timeout.
|
|
300
|
-
setTimeout(() => {
|
|
301
|
-
if (this.exited) return;
|
|
302
|
-
try {
|
|
303
|
-
this._inst.exports.go_scheduler();
|
|
304
|
-
} catch (e) {
|
|
305
|
-
if (e !== wasmExit) throw e;
|
|
306
|
-
}
|
|
307
|
-
}, Number(timeout)/1e6);
|
|
308
|
-
},
|
|
309
|
-
|
|
310
|
-
// func finalizeRef(v ref)
|
|
311
|
-
"syscall/js.finalizeRef": (v_ref) => {
|
|
312
|
-
// Note: TinyGo does not support finalizers so this is only called
|
|
313
|
-
// for one specific case, by js.go:jsString. and can/might leak memory.
|
|
314
|
-
const id = v_ref & 0xffffffffn;
|
|
315
|
-
if (this._goRefCounts?.[id] !== undefined) {
|
|
316
|
-
this._goRefCounts[id]--;
|
|
317
|
-
if (this._goRefCounts[id] === 0) {
|
|
318
|
-
const v = this._values[id];
|
|
319
|
-
this._values[id] = null;
|
|
320
|
-
this._ids.delete(v);
|
|
321
|
-
this._idPool.push(id);
|
|
322
|
-
}
|
|
323
|
-
} else {
|
|
324
|
-
console.error("syscall/js.finalizeRef: unknown id", id);
|
|
325
|
-
}
|
|
326
|
-
},
|
|
327
|
-
|
|
328
|
-
// func stringVal(value string) ref
|
|
329
|
-
"syscall/js.stringVal": (value_ptr, value_len) => {
|
|
330
|
-
value_ptr >>>= 0;
|
|
331
|
-
const s = loadString(value_ptr, value_len);
|
|
332
|
-
return boxValue(s);
|
|
333
|
-
},
|
|
334
|
-
|
|
335
|
-
// func valueGet(v ref, p string) ref
|
|
336
|
-
"syscall/js.valueGet": (v_ref, p_ptr, p_len) => {
|
|
337
|
-
let prop = loadString(p_ptr, p_len);
|
|
338
|
-
let v = unboxValue(v_ref);
|
|
339
|
-
let result = Reflect.get(v, prop);
|
|
340
|
-
return boxValue(result);
|
|
341
|
-
},
|
|
342
|
-
|
|
343
|
-
// func valueSet(v ref, p string, x ref)
|
|
344
|
-
"syscall/js.valueSet": (v_ref, p_ptr, p_len, x_ref) => {
|
|
345
|
-
const v = unboxValue(v_ref);
|
|
346
|
-
const p = loadString(p_ptr, p_len);
|
|
347
|
-
const x = unboxValue(x_ref);
|
|
348
|
-
Reflect.set(v, p, x);
|
|
349
|
-
},
|
|
350
|
-
|
|
351
|
-
// func valueDelete(v ref, p string)
|
|
352
|
-
"syscall/js.valueDelete": (v_ref, p_ptr, p_len) => {
|
|
353
|
-
const v = unboxValue(v_ref);
|
|
354
|
-
const p = loadString(p_ptr, p_len);
|
|
355
|
-
Reflect.deleteProperty(v, p);
|
|
356
|
-
},
|
|
357
|
-
|
|
358
|
-
// func valueIndex(v ref, i int) ref
|
|
359
|
-
"syscall/js.valueIndex": (v_ref, i) => {
|
|
360
|
-
return boxValue(Reflect.get(unboxValue(v_ref), i));
|
|
361
|
-
},
|
|
362
|
-
|
|
363
|
-
// valueSetIndex(v ref, i int, x ref)
|
|
364
|
-
"syscall/js.valueSetIndex": (v_ref, i, x_ref) => {
|
|
365
|
-
Reflect.set(unboxValue(v_ref), i, unboxValue(x_ref));
|
|
366
|
-
},
|
|
367
|
-
|
|
368
|
-
// func valueCall(v ref, m string, args []ref) (ref, bool)
|
|
369
|
-
"syscall/js.valueCall": (ret_addr, v_ref, m_ptr, m_len, args_ptr, args_len, args_cap) => {
|
|
370
|
-
const v = unboxValue(v_ref);
|
|
371
|
-
const name = loadString(m_ptr, m_len);
|
|
372
|
-
const args = loadSliceOfValues(args_ptr, args_len, args_cap);
|
|
373
|
-
try {
|
|
374
|
-
const m = Reflect.get(v, name);
|
|
375
|
-
storeValue(ret_addr, Reflect.apply(m, v, args));
|
|
376
|
-
mem().setUint8(ret_addr + 8, 1);
|
|
377
|
-
} catch (err) {
|
|
378
|
-
storeValue(ret_addr, err);
|
|
379
|
-
mem().setUint8(ret_addr + 8, 0);
|
|
380
|
-
}
|
|
381
|
-
},
|
|
382
|
-
|
|
383
|
-
// func valueInvoke(v ref, args []ref) (ref, bool)
|
|
384
|
-
"syscall/js.valueInvoke": (ret_addr, v_ref, args_ptr, args_len, args_cap) => {
|
|
385
|
-
try {
|
|
386
|
-
const v = unboxValue(v_ref);
|
|
387
|
-
const args = loadSliceOfValues(args_ptr, args_len, args_cap);
|
|
388
|
-
storeValue(ret_addr, Reflect.apply(v, undefined, args));
|
|
389
|
-
mem().setUint8(ret_addr + 8, 1);
|
|
390
|
-
} catch (err) {
|
|
391
|
-
storeValue(ret_addr, err);
|
|
392
|
-
mem().setUint8(ret_addr + 8, 0);
|
|
393
|
-
}
|
|
394
|
-
},
|
|
395
|
-
|
|
396
|
-
// func valueNew(v ref, args []ref) (ref, bool)
|
|
397
|
-
"syscall/js.valueNew": (ret_addr, v_ref, args_ptr, args_len, args_cap) => {
|
|
398
|
-
const v = unboxValue(v_ref);
|
|
399
|
-
const args = loadSliceOfValues(args_ptr, args_len, args_cap);
|
|
400
|
-
try {
|
|
401
|
-
storeValue(ret_addr, Reflect.construct(v, args));
|
|
402
|
-
mem().setUint8(ret_addr + 8, 1);
|
|
403
|
-
} catch (err) {
|
|
404
|
-
storeValue(ret_addr, err);
|
|
405
|
-
mem().setUint8(ret_addr+ 8, 0);
|
|
406
|
-
}
|
|
407
|
-
},
|
|
408
|
-
|
|
409
|
-
// func valueLength(v ref) int
|
|
410
|
-
"syscall/js.valueLength": (v_ref) => {
|
|
411
|
-
return unboxValue(v_ref).length;
|
|
412
|
-
},
|
|
413
|
-
|
|
414
|
-
// valuePrepareString(v ref) (ref, int)
|
|
415
|
-
"syscall/js.valuePrepareString": (ret_addr, v_ref) => {
|
|
416
|
-
const s = String(unboxValue(v_ref));
|
|
417
|
-
const str = encoder.encode(s);
|
|
418
|
-
storeValue(ret_addr, str);
|
|
419
|
-
mem().setInt32(ret_addr + 8, str.length, true);
|
|
420
|
-
},
|
|
421
|
-
|
|
422
|
-
// valueLoadString(v ref, b []byte)
|
|
423
|
-
"syscall/js.valueLoadString": (v_ref, slice_ptr, slice_len, slice_cap) => {
|
|
424
|
-
const str = unboxValue(v_ref);
|
|
425
|
-
loadSlice(slice_ptr, slice_len, slice_cap).set(str);
|
|
426
|
-
},
|
|
427
|
-
|
|
428
|
-
// func valueInstanceOf(v ref, t ref) bool
|
|
429
|
-
"syscall/js.valueInstanceOf": (v_ref, t_ref) => {
|
|
430
|
-
return unboxValue(v_ref) instanceof unboxValue(t_ref);
|
|
431
|
-
},
|
|
432
|
-
|
|
433
|
-
// func copyBytesToGo(dst []byte, src ref) (int, bool)
|
|
434
|
-
"syscall/js.copyBytesToGo": (ret_addr, dest_addr, dest_len, dest_cap, src_ref) => {
|
|
435
|
-
let num_bytes_copied_addr = ret_addr;
|
|
436
|
-
let returned_status_addr = ret_addr + 4; // Address of returned boolean status variable
|
|
437
|
-
|
|
438
|
-
const dst = loadSlice(dest_addr, dest_len);
|
|
439
|
-
const src = unboxValue(src_ref);
|
|
440
|
-
if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) {
|
|
441
|
-
mem().setUint8(returned_status_addr, 0); // Return "not ok" status
|
|
442
|
-
return;
|
|
443
|
-
}
|
|
444
|
-
const toCopy = src.subarray(0, dst.length);
|
|
445
|
-
dst.set(toCopy);
|
|
446
|
-
mem().setUint32(num_bytes_copied_addr, toCopy.length, true);
|
|
447
|
-
mem().setUint8(returned_status_addr, 1); // Return "ok" status
|
|
448
|
-
},
|
|
449
|
-
|
|
450
|
-
// copyBytesToJS(dst ref, src []byte) (int, bool)
|
|
451
|
-
// Originally copied from upstream Go project, then modified:
|
|
452
|
-
// https://github.com/golang/go/blob/3f995c3f3b43033013013e6c7ccc93a9b1411ca9/misc/wasm/wasm_exec.js#L404-L416
|
|
453
|
-
"syscall/js.copyBytesToJS": (ret_addr, dst_ref, src_addr, src_len, src_cap) => {
|
|
454
|
-
let num_bytes_copied_addr = ret_addr;
|
|
455
|
-
let returned_status_addr = ret_addr + 4; // Address of returned boolean status variable
|
|
456
|
-
|
|
457
|
-
const dst = unboxValue(dst_ref);
|
|
458
|
-
const src = loadSlice(src_addr, src_len);
|
|
459
|
-
if (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) {
|
|
460
|
-
mem().setUint8(returned_status_addr, 0); // Return "not ok" status
|
|
461
|
-
return;
|
|
462
|
-
}
|
|
463
|
-
const toCopy = src.subarray(0, dst.length);
|
|
464
|
-
dst.set(toCopy);
|
|
465
|
-
mem().setUint32(num_bytes_copied_addr, toCopy.length, true);
|
|
466
|
-
mem().setUint8(returned_status_addr, 1); // Return "ok" status
|
|
467
|
-
},
|
|
468
|
-
}
|
|
469
|
-
};
|
|
470
|
-
|
|
471
|
-
// Go 1.20 uses 'env'. Go 1.21 uses 'gojs'.
|
|
472
|
-
// For compatibility, we use both as long as Go 1.20 is supported.
|
|
473
|
-
this.importObject.env = this.importObject.gojs;
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
async run(instance) {
|
|
477
|
-
this._inst = instance;
|
|
478
|
-
this._values = [ // JS values that Go currently has references to, indexed by reference id
|
|
479
|
-
NaN,
|
|
480
|
-
0,
|
|
481
|
-
null,
|
|
482
|
-
true,
|
|
483
|
-
false,
|
|
484
|
-
global,
|
|
485
|
-
this,
|
|
486
|
-
];
|
|
487
|
-
this._goRefCounts = []; // number of references that Go has to a JS value, indexed by reference id
|
|
488
|
-
this._ids = new Map(); // mapping from JS values to reference ids
|
|
489
|
-
this._idPool = []; // unused ids that have been garbage collected
|
|
490
|
-
this.exited = false; // whether the Go program has exited
|
|
491
|
-
this.exitCode = 0;
|
|
492
|
-
|
|
493
|
-
if (this._inst.exports._start) {
|
|
494
|
-
let exitPromise = new Promise((resolve, reject) => {
|
|
495
|
-
this._resolveExitPromise = resolve;
|
|
496
|
-
});
|
|
497
|
-
|
|
498
|
-
// Run program, but catch the wasmExit exception that's thrown
|
|
499
|
-
// to return back here.
|
|
500
|
-
try {
|
|
501
|
-
this._inst.exports._start();
|
|
502
|
-
} catch (e) {
|
|
503
|
-
if (e !== wasmExit) throw e;
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
await exitPromise;
|
|
507
|
-
return this.exitCode;
|
|
508
|
-
} else {
|
|
509
|
-
this._inst.exports._initialize();
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
_resume() {
|
|
514
|
-
if (this.exited) {
|
|
515
|
-
throw new Error("Go program has already exited");
|
|
516
|
-
}
|
|
517
|
-
try {
|
|
518
|
-
this._inst.exports.resume();
|
|
519
|
-
} catch (e) {
|
|
520
|
-
if (e !== wasmExit) throw e;
|
|
521
|
-
}
|
|
522
|
-
if (this.exited) {
|
|
523
|
-
this._resolveExitPromise();
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
_makeFuncWrapper(id) {
|
|
528
|
-
const go = this;
|
|
529
|
-
return function () {
|
|
530
|
-
const event = { id: id, this: this, args: arguments };
|
|
531
|
-
go._pendingEvent = event;
|
|
532
|
-
go._resume();
|
|
533
|
-
return event.result;
|
|
534
|
-
};
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
if (
|
|
539
|
-
global.require &&
|
|
540
|
-
global.require.main === module &&
|
|
541
|
-
global.process &&
|
|
542
|
-
global.process.versions &&
|
|
543
|
-
!global.process.versions.electron
|
|
544
|
-
) {
|
|
545
|
-
if (process.argv.length != 3) {
|
|
546
|
-
console.error("usage: go_js_wasm_exec [wasm binary] [arguments]");
|
|
547
|
-
process.exit(1);
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
const go = new Go();
|
|
551
|
-
WebAssembly.instantiate(fs.readFileSync(process.argv[2]), go.importObject).then(async (result) => {
|
|
552
|
-
let exitCode = await go.run(result.instance);
|
|
553
|
-
process.exit(exitCode);
|
|
554
|
-
}).catch((err) => {
|
|
555
|
-
console.error(err);
|
|
556
|
-
process.exit(1);
|
|
557
|
-
});
|
|
558
|
-
}
|
|
559
|
-
})();
|
|
1
|
+
// Copyright 2018 The Go Authors. All rights reserved.
|
|
2
|
+
// Use of this source code is governed by a BSD-style
|
|
3
|
+
// license that can be found in the LICENSE file.
|
|
4
|
+
//
|
|
5
|
+
// This file has been modified for use by the TinyGo compiler.
|
|
6
|
+
|
|
7
|
+
(() => {
|
|
8
|
+
// Map multiple JavaScript environments to a single common API,
|
|
9
|
+
// preferring web standards over Node.js API.
|
|
10
|
+
//
|
|
11
|
+
// Environments considered:
|
|
12
|
+
// - Browsers
|
|
13
|
+
// - Node.js
|
|
14
|
+
// - Electron
|
|
15
|
+
// - Parcel
|
|
16
|
+
|
|
17
|
+
if (typeof global !== "undefined") {
|
|
18
|
+
// global already exists
|
|
19
|
+
} else if (typeof window !== "undefined") {
|
|
20
|
+
window.global = window;
|
|
21
|
+
} else if (typeof self !== "undefined") {
|
|
22
|
+
self.global = self;
|
|
23
|
+
} else {
|
|
24
|
+
throw new Error("cannot export Go (neither global, window nor self is defined)");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (!global.require && typeof require !== "undefined") {
|
|
28
|
+
global.require = require;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (!global.fs && global.require) {
|
|
32
|
+
global.fs = require("node:fs");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const enosys = () => {
|
|
36
|
+
const err = new Error("not implemented");
|
|
37
|
+
err.code = "ENOSYS";
|
|
38
|
+
return err;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
if (!global.fs) {
|
|
42
|
+
let outputBuf = "";
|
|
43
|
+
global.fs = {
|
|
44
|
+
constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused
|
|
45
|
+
writeSync(fd, buf) {
|
|
46
|
+
outputBuf += decoder.decode(buf);
|
|
47
|
+
const nl = outputBuf.lastIndexOf("\n");
|
|
48
|
+
if (nl != -1) {
|
|
49
|
+
console.log(outputBuf.substr(0, nl));
|
|
50
|
+
outputBuf = outputBuf.substr(nl + 1);
|
|
51
|
+
}
|
|
52
|
+
return buf.length;
|
|
53
|
+
},
|
|
54
|
+
write(fd, buf, offset, length, position, callback) {
|
|
55
|
+
if (offset !== 0 || length !== buf.length || position !== null) {
|
|
56
|
+
callback(enosys());
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const n = this.writeSync(fd, buf);
|
|
60
|
+
callback(null, n);
|
|
61
|
+
},
|
|
62
|
+
chmod(path, mode, callback) { callback(enosys()); },
|
|
63
|
+
chown(path, uid, gid, callback) { callback(enosys()); },
|
|
64
|
+
close(fd, callback) { callback(enosys()); },
|
|
65
|
+
fchmod(fd, mode, callback) { callback(enosys()); },
|
|
66
|
+
fchown(fd, uid, gid, callback) { callback(enosys()); },
|
|
67
|
+
fstat(fd, callback) { callback(enosys()); },
|
|
68
|
+
fsync(fd, callback) { callback(null); },
|
|
69
|
+
ftruncate(fd, length, callback) { callback(enosys()); },
|
|
70
|
+
lchown(path, uid, gid, callback) { callback(enosys()); },
|
|
71
|
+
link(path, link, callback) { callback(enosys()); },
|
|
72
|
+
lstat(path, callback) { callback(enosys()); },
|
|
73
|
+
mkdir(path, perm, callback) { callback(enosys()); },
|
|
74
|
+
open(path, flags, mode, callback) { callback(enosys()); },
|
|
75
|
+
read(fd, buffer, offset, length, position, callback) { callback(enosys()); },
|
|
76
|
+
readdir(path, callback) { callback(enosys()); },
|
|
77
|
+
readlink(path, callback) { callback(enosys()); },
|
|
78
|
+
rename(from, to, callback) { callback(enosys()); },
|
|
79
|
+
rmdir(path, callback) { callback(enosys()); },
|
|
80
|
+
stat(path, callback) { callback(enosys()); },
|
|
81
|
+
symlink(path, link, callback) { callback(enosys()); },
|
|
82
|
+
truncate(path, length, callback) { callback(enosys()); },
|
|
83
|
+
unlink(path, callback) { callback(enosys()); },
|
|
84
|
+
utimes(path, atime, mtime, callback) { callback(enosys()); },
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (!global.process) {
|
|
89
|
+
global.process = {
|
|
90
|
+
getuid() { return -1; },
|
|
91
|
+
getgid() { return -1; },
|
|
92
|
+
geteuid() { return -1; },
|
|
93
|
+
getegid() { return -1; },
|
|
94
|
+
getgroups() { throw enosys(); },
|
|
95
|
+
pid: -1,
|
|
96
|
+
ppid: -1,
|
|
97
|
+
umask() { throw enosys(); },
|
|
98
|
+
cwd() { throw enosys(); },
|
|
99
|
+
chdir() { throw enosys(); },
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (!global.crypto) {
|
|
104
|
+
const nodeCrypto = require("node:crypto");
|
|
105
|
+
global.crypto = {
|
|
106
|
+
getRandomValues(b) {
|
|
107
|
+
nodeCrypto.randomFillSync(b);
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (!global.performance) {
|
|
113
|
+
global.performance = {
|
|
114
|
+
now() {
|
|
115
|
+
const [sec, nsec] = process.hrtime();
|
|
116
|
+
return sec * 1000 + nsec / 1000000;
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (!global.TextEncoder) {
|
|
122
|
+
global.TextEncoder = require("node:util").TextEncoder;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (!global.TextDecoder) {
|
|
126
|
+
global.TextDecoder = require("node:util").TextDecoder;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// End of polyfills for common API.
|
|
130
|
+
|
|
131
|
+
const encoder = new TextEncoder("utf-8");
|
|
132
|
+
const decoder = new TextDecoder("utf-8");
|
|
133
|
+
let reinterpretBuf = new DataView(new ArrayBuffer(8));
|
|
134
|
+
var logLine = [];
|
|
135
|
+
const wasmExit = {}; // thrown to exit via proc_exit (not an error)
|
|
136
|
+
|
|
137
|
+
global.Go = class {
|
|
138
|
+
constructor() {
|
|
139
|
+
this._callbackTimeouts = new Map();
|
|
140
|
+
this._nextCallbackTimeoutID = 1;
|
|
141
|
+
|
|
142
|
+
const mem = () => {
|
|
143
|
+
// The buffer may change when requesting more memory.
|
|
144
|
+
return new DataView(this._inst.exports.memory.buffer);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const unboxValue = (v_ref) => {
|
|
148
|
+
reinterpretBuf.setBigInt64(0, v_ref, true);
|
|
149
|
+
const f = reinterpretBuf.getFloat64(0, true);
|
|
150
|
+
if (f === 0) {
|
|
151
|
+
return undefined;
|
|
152
|
+
}
|
|
153
|
+
if (!isNaN(f)) {
|
|
154
|
+
return f;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const id = v_ref & 0xffffffffn;
|
|
158
|
+
return this._values[id];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
const loadValue = (addr) => {
|
|
163
|
+
let v_ref = mem().getBigUint64(addr, true);
|
|
164
|
+
return unboxValue(v_ref);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const boxValue = (v) => {
|
|
168
|
+
const nanHead = 0x7FF80000n;
|
|
169
|
+
|
|
170
|
+
if (typeof v === "number") {
|
|
171
|
+
if (isNaN(v)) {
|
|
172
|
+
return nanHead << 32n;
|
|
173
|
+
}
|
|
174
|
+
if (v === 0) {
|
|
175
|
+
return (nanHead << 32n) | 1n;
|
|
176
|
+
}
|
|
177
|
+
reinterpretBuf.setFloat64(0, v, true);
|
|
178
|
+
return reinterpretBuf.getBigInt64(0, true);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
switch (v) {
|
|
182
|
+
case undefined:
|
|
183
|
+
return 0n;
|
|
184
|
+
case null:
|
|
185
|
+
return (nanHead << 32n) | 2n;
|
|
186
|
+
case true:
|
|
187
|
+
return (nanHead << 32n) | 3n;
|
|
188
|
+
case false:
|
|
189
|
+
return (nanHead << 32n) | 4n;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
let id = this._ids.get(v);
|
|
193
|
+
if (id === undefined) {
|
|
194
|
+
id = this._idPool.pop();
|
|
195
|
+
if (id === undefined) {
|
|
196
|
+
id = BigInt(this._values.length);
|
|
197
|
+
}
|
|
198
|
+
this._values[id] = v;
|
|
199
|
+
this._goRefCounts[id] = 0;
|
|
200
|
+
this._ids.set(v, id);
|
|
201
|
+
}
|
|
202
|
+
this._goRefCounts[id]++;
|
|
203
|
+
let typeFlag = 1n;
|
|
204
|
+
switch (typeof v) {
|
|
205
|
+
case "string":
|
|
206
|
+
typeFlag = 2n;
|
|
207
|
+
break;
|
|
208
|
+
case "symbol":
|
|
209
|
+
typeFlag = 3n;
|
|
210
|
+
break;
|
|
211
|
+
case "function":
|
|
212
|
+
typeFlag = 4n;
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
return id | ((nanHead | typeFlag) << 32n);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const storeValue = (addr, v) => {
|
|
219
|
+
let v_ref = boxValue(v);
|
|
220
|
+
mem().setBigUint64(addr, v_ref, true);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const loadSlice = (array, len, cap) => {
|
|
224
|
+
return new Uint8Array(this._inst.exports.memory.buffer, array, len);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const loadSliceOfValues = (array, len, cap) => {
|
|
228
|
+
const a = new Array(len);
|
|
229
|
+
for (let i = 0; i < len; i++) {
|
|
230
|
+
a[i] = loadValue(array + i * 8);
|
|
231
|
+
}
|
|
232
|
+
return a;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const loadString = (ptr, len) => {
|
|
236
|
+
return decoder.decode(new DataView(this._inst.exports.memory.buffer, ptr, len));
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const timeOrigin = Date.now() - performance.now();
|
|
240
|
+
const wasi_EBADF = 8;
|
|
241
|
+
const wasi_ENOSYS = 52;
|
|
242
|
+
this.importObject = {
|
|
243
|
+
wasi_snapshot_preview1: {
|
|
244
|
+
// https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md
|
|
245
|
+
fd_write: function(fd, iovs_ptr, iovs_len, nwritten_ptr) {
|
|
246
|
+
let nwritten = 0;
|
|
247
|
+
if (fd == 1) {
|
|
248
|
+
for (let iovs_i=0; iovs_i<iovs_len;iovs_i++) {
|
|
249
|
+
let iov_ptr = iovs_ptr+iovs_i*8; // assuming wasm32
|
|
250
|
+
let ptr = mem().getUint32(iov_ptr + 0, true);
|
|
251
|
+
let len = mem().getUint32(iov_ptr + 4, true);
|
|
252
|
+
nwritten += len;
|
|
253
|
+
for (let i=0; i<len; i++) {
|
|
254
|
+
let c = mem().getUint8(ptr+i);
|
|
255
|
+
if (c == 13) { // CR
|
|
256
|
+
// ignore
|
|
257
|
+
} else if (c == 10) { // LF
|
|
258
|
+
// write line
|
|
259
|
+
let line = decoder.decode(new Uint8Array(logLine));
|
|
260
|
+
logLine = [];
|
|
261
|
+
console.log(line);
|
|
262
|
+
} else {
|
|
263
|
+
logLine.push(c);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
} else {
|
|
268
|
+
console.error('invalid file descriptor:', fd);
|
|
269
|
+
}
|
|
270
|
+
mem().setUint32(nwritten_ptr, nwritten, true);
|
|
271
|
+
return 0;
|
|
272
|
+
},
|
|
273
|
+
fd_read: () => wasi_ENOSYS,
|
|
274
|
+
fd_close: () => wasi_ENOSYS,
|
|
275
|
+
fd_fdstat_get: () => wasi_ENOSYS,
|
|
276
|
+
fd_prestat_get: () => wasi_EBADF, // wasi-libc relies on this errno value
|
|
277
|
+
fd_prestat_dir_name: () => wasi_ENOSYS,
|
|
278
|
+
fd_seek: () => wasi_ENOSYS,
|
|
279
|
+
path_open: () => wasi_ENOSYS,
|
|
280
|
+
proc_exit: (code) => {
|
|
281
|
+
this.exited = true;
|
|
282
|
+
this.exitCode = code;
|
|
283
|
+
this._resolveExitPromise();
|
|
284
|
+
throw wasmExit;
|
|
285
|
+
},
|
|
286
|
+
random_get: (bufPtr, bufLen) => {
|
|
287
|
+
crypto.getRandomValues(loadSlice(bufPtr, bufLen));
|
|
288
|
+
return 0;
|
|
289
|
+
},
|
|
290
|
+
},
|
|
291
|
+
gojs: {
|
|
292
|
+
// func ticks() int64
|
|
293
|
+
"runtime.ticks": () => {
|
|
294
|
+
return BigInt((timeOrigin + performance.now()) * 1e6);
|
|
295
|
+
},
|
|
296
|
+
|
|
297
|
+
// func sleepTicks(timeout int64)
|
|
298
|
+
"runtime.sleepTicks": (timeout) => {
|
|
299
|
+
// Do not sleep, only reactivate scheduler after the given timeout.
|
|
300
|
+
setTimeout(() => {
|
|
301
|
+
if (this.exited) return;
|
|
302
|
+
try {
|
|
303
|
+
this._inst.exports.go_scheduler();
|
|
304
|
+
} catch (e) {
|
|
305
|
+
if (e !== wasmExit) throw e;
|
|
306
|
+
}
|
|
307
|
+
}, Number(timeout)/1e6);
|
|
308
|
+
},
|
|
309
|
+
|
|
310
|
+
// func finalizeRef(v ref)
|
|
311
|
+
"syscall/js.finalizeRef": (v_ref) => {
|
|
312
|
+
// Note: TinyGo does not support finalizers so this is only called
|
|
313
|
+
// for one specific case, by js.go:jsString. and can/might leak memory.
|
|
314
|
+
const id = v_ref & 0xffffffffn;
|
|
315
|
+
if (this._goRefCounts?.[id] !== undefined) {
|
|
316
|
+
this._goRefCounts[id]--;
|
|
317
|
+
if (this._goRefCounts[id] === 0) {
|
|
318
|
+
const v = this._values[id];
|
|
319
|
+
this._values[id] = null;
|
|
320
|
+
this._ids.delete(v);
|
|
321
|
+
this._idPool.push(id);
|
|
322
|
+
}
|
|
323
|
+
} else {
|
|
324
|
+
console.error("syscall/js.finalizeRef: unknown id", id);
|
|
325
|
+
}
|
|
326
|
+
},
|
|
327
|
+
|
|
328
|
+
// func stringVal(value string) ref
|
|
329
|
+
"syscall/js.stringVal": (value_ptr, value_len) => {
|
|
330
|
+
value_ptr >>>= 0;
|
|
331
|
+
const s = loadString(value_ptr, value_len);
|
|
332
|
+
return boxValue(s);
|
|
333
|
+
},
|
|
334
|
+
|
|
335
|
+
// func valueGet(v ref, p string) ref
|
|
336
|
+
"syscall/js.valueGet": (v_ref, p_ptr, p_len) => {
|
|
337
|
+
let prop = loadString(p_ptr, p_len);
|
|
338
|
+
let v = unboxValue(v_ref);
|
|
339
|
+
let result = Reflect.get(v, prop);
|
|
340
|
+
return boxValue(result);
|
|
341
|
+
},
|
|
342
|
+
|
|
343
|
+
// func valueSet(v ref, p string, x ref)
|
|
344
|
+
"syscall/js.valueSet": (v_ref, p_ptr, p_len, x_ref) => {
|
|
345
|
+
const v = unboxValue(v_ref);
|
|
346
|
+
const p = loadString(p_ptr, p_len);
|
|
347
|
+
const x = unboxValue(x_ref);
|
|
348
|
+
Reflect.set(v, p, x);
|
|
349
|
+
},
|
|
350
|
+
|
|
351
|
+
// func valueDelete(v ref, p string)
|
|
352
|
+
"syscall/js.valueDelete": (v_ref, p_ptr, p_len) => {
|
|
353
|
+
const v = unboxValue(v_ref);
|
|
354
|
+
const p = loadString(p_ptr, p_len);
|
|
355
|
+
Reflect.deleteProperty(v, p);
|
|
356
|
+
},
|
|
357
|
+
|
|
358
|
+
// func valueIndex(v ref, i int) ref
|
|
359
|
+
"syscall/js.valueIndex": (v_ref, i) => {
|
|
360
|
+
return boxValue(Reflect.get(unboxValue(v_ref), i));
|
|
361
|
+
},
|
|
362
|
+
|
|
363
|
+
// valueSetIndex(v ref, i int, x ref)
|
|
364
|
+
"syscall/js.valueSetIndex": (v_ref, i, x_ref) => {
|
|
365
|
+
Reflect.set(unboxValue(v_ref), i, unboxValue(x_ref));
|
|
366
|
+
},
|
|
367
|
+
|
|
368
|
+
// func valueCall(v ref, m string, args []ref) (ref, bool)
|
|
369
|
+
"syscall/js.valueCall": (ret_addr, v_ref, m_ptr, m_len, args_ptr, args_len, args_cap) => {
|
|
370
|
+
const v = unboxValue(v_ref);
|
|
371
|
+
const name = loadString(m_ptr, m_len);
|
|
372
|
+
const args = loadSliceOfValues(args_ptr, args_len, args_cap);
|
|
373
|
+
try {
|
|
374
|
+
const m = Reflect.get(v, name);
|
|
375
|
+
storeValue(ret_addr, Reflect.apply(m, v, args));
|
|
376
|
+
mem().setUint8(ret_addr + 8, 1);
|
|
377
|
+
} catch (err) {
|
|
378
|
+
storeValue(ret_addr, err);
|
|
379
|
+
mem().setUint8(ret_addr + 8, 0);
|
|
380
|
+
}
|
|
381
|
+
},
|
|
382
|
+
|
|
383
|
+
// func valueInvoke(v ref, args []ref) (ref, bool)
|
|
384
|
+
"syscall/js.valueInvoke": (ret_addr, v_ref, args_ptr, args_len, args_cap) => {
|
|
385
|
+
try {
|
|
386
|
+
const v = unboxValue(v_ref);
|
|
387
|
+
const args = loadSliceOfValues(args_ptr, args_len, args_cap);
|
|
388
|
+
storeValue(ret_addr, Reflect.apply(v, undefined, args));
|
|
389
|
+
mem().setUint8(ret_addr + 8, 1);
|
|
390
|
+
} catch (err) {
|
|
391
|
+
storeValue(ret_addr, err);
|
|
392
|
+
mem().setUint8(ret_addr + 8, 0);
|
|
393
|
+
}
|
|
394
|
+
},
|
|
395
|
+
|
|
396
|
+
// func valueNew(v ref, args []ref) (ref, bool)
|
|
397
|
+
"syscall/js.valueNew": (ret_addr, v_ref, args_ptr, args_len, args_cap) => {
|
|
398
|
+
const v = unboxValue(v_ref);
|
|
399
|
+
const args = loadSliceOfValues(args_ptr, args_len, args_cap);
|
|
400
|
+
try {
|
|
401
|
+
storeValue(ret_addr, Reflect.construct(v, args));
|
|
402
|
+
mem().setUint8(ret_addr + 8, 1);
|
|
403
|
+
} catch (err) {
|
|
404
|
+
storeValue(ret_addr, err);
|
|
405
|
+
mem().setUint8(ret_addr+ 8, 0);
|
|
406
|
+
}
|
|
407
|
+
},
|
|
408
|
+
|
|
409
|
+
// func valueLength(v ref) int
|
|
410
|
+
"syscall/js.valueLength": (v_ref) => {
|
|
411
|
+
return unboxValue(v_ref).length;
|
|
412
|
+
},
|
|
413
|
+
|
|
414
|
+
// valuePrepareString(v ref) (ref, int)
|
|
415
|
+
"syscall/js.valuePrepareString": (ret_addr, v_ref) => {
|
|
416
|
+
const s = String(unboxValue(v_ref));
|
|
417
|
+
const str = encoder.encode(s);
|
|
418
|
+
storeValue(ret_addr, str);
|
|
419
|
+
mem().setInt32(ret_addr + 8, str.length, true);
|
|
420
|
+
},
|
|
421
|
+
|
|
422
|
+
// valueLoadString(v ref, b []byte)
|
|
423
|
+
"syscall/js.valueLoadString": (v_ref, slice_ptr, slice_len, slice_cap) => {
|
|
424
|
+
const str = unboxValue(v_ref);
|
|
425
|
+
loadSlice(slice_ptr, slice_len, slice_cap).set(str);
|
|
426
|
+
},
|
|
427
|
+
|
|
428
|
+
// func valueInstanceOf(v ref, t ref) bool
|
|
429
|
+
"syscall/js.valueInstanceOf": (v_ref, t_ref) => {
|
|
430
|
+
return unboxValue(v_ref) instanceof unboxValue(t_ref);
|
|
431
|
+
},
|
|
432
|
+
|
|
433
|
+
// func copyBytesToGo(dst []byte, src ref) (int, bool)
|
|
434
|
+
"syscall/js.copyBytesToGo": (ret_addr, dest_addr, dest_len, dest_cap, src_ref) => {
|
|
435
|
+
let num_bytes_copied_addr = ret_addr;
|
|
436
|
+
let returned_status_addr = ret_addr + 4; // Address of returned boolean status variable
|
|
437
|
+
|
|
438
|
+
const dst = loadSlice(dest_addr, dest_len);
|
|
439
|
+
const src = unboxValue(src_ref);
|
|
440
|
+
if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) {
|
|
441
|
+
mem().setUint8(returned_status_addr, 0); // Return "not ok" status
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
const toCopy = src.subarray(0, dst.length);
|
|
445
|
+
dst.set(toCopy);
|
|
446
|
+
mem().setUint32(num_bytes_copied_addr, toCopy.length, true);
|
|
447
|
+
mem().setUint8(returned_status_addr, 1); // Return "ok" status
|
|
448
|
+
},
|
|
449
|
+
|
|
450
|
+
// copyBytesToJS(dst ref, src []byte) (int, bool)
|
|
451
|
+
// Originally copied from upstream Go project, then modified:
|
|
452
|
+
// https://github.com/golang/go/blob/3f995c3f3b43033013013e6c7ccc93a9b1411ca9/misc/wasm/wasm_exec.js#L404-L416
|
|
453
|
+
"syscall/js.copyBytesToJS": (ret_addr, dst_ref, src_addr, src_len, src_cap) => {
|
|
454
|
+
let num_bytes_copied_addr = ret_addr;
|
|
455
|
+
let returned_status_addr = ret_addr + 4; // Address of returned boolean status variable
|
|
456
|
+
|
|
457
|
+
const dst = unboxValue(dst_ref);
|
|
458
|
+
const src = loadSlice(src_addr, src_len);
|
|
459
|
+
if (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) {
|
|
460
|
+
mem().setUint8(returned_status_addr, 0); // Return "not ok" status
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
const toCopy = src.subarray(0, dst.length);
|
|
464
|
+
dst.set(toCopy);
|
|
465
|
+
mem().setUint32(num_bytes_copied_addr, toCopy.length, true);
|
|
466
|
+
mem().setUint8(returned_status_addr, 1); // Return "ok" status
|
|
467
|
+
},
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
// Go 1.20 uses 'env'. Go 1.21 uses 'gojs'.
|
|
472
|
+
// For compatibility, we use both as long as Go 1.20 is supported.
|
|
473
|
+
this.importObject.env = this.importObject.gojs;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
async run(instance) {
|
|
477
|
+
this._inst = instance;
|
|
478
|
+
this._values = [ // JS values that Go currently has references to, indexed by reference id
|
|
479
|
+
NaN,
|
|
480
|
+
0,
|
|
481
|
+
null,
|
|
482
|
+
true,
|
|
483
|
+
false,
|
|
484
|
+
global,
|
|
485
|
+
this,
|
|
486
|
+
];
|
|
487
|
+
this._goRefCounts = []; // number of references that Go has to a JS value, indexed by reference id
|
|
488
|
+
this._ids = new Map(); // mapping from JS values to reference ids
|
|
489
|
+
this._idPool = []; // unused ids that have been garbage collected
|
|
490
|
+
this.exited = false; // whether the Go program has exited
|
|
491
|
+
this.exitCode = 0;
|
|
492
|
+
|
|
493
|
+
if (this._inst.exports._start) {
|
|
494
|
+
let exitPromise = new Promise((resolve, reject) => {
|
|
495
|
+
this._resolveExitPromise = resolve;
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
// Run program, but catch the wasmExit exception that's thrown
|
|
499
|
+
// to return back here.
|
|
500
|
+
try {
|
|
501
|
+
this._inst.exports._start();
|
|
502
|
+
} catch (e) {
|
|
503
|
+
if (e !== wasmExit) throw e;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
await exitPromise;
|
|
507
|
+
return this.exitCode;
|
|
508
|
+
} else {
|
|
509
|
+
this._inst.exports._initialize();
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
_resume() {
|
|
514
|
+
if (this.exited) {
|
|
515
|
+
throw new Error("Go program has already exited");
|
|
516
|
+
}
|
|
517
|
+
try {
|
|
518
|
+
this._inst.exports.resume();
|
|
519
|
+
} catch (e) {
|
|
520
|
+
if (e !== wasmExit) throw e;
|
|
521
|
+
}
|
|
522
|
+
if (this.exited) {
|
|
523
|
+
this._resolveExitPromise();
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
_makeFuncWrapper(id) {
|
|
528
|
+
const go = this;
|
|
529
|
+
return function () {
|
|
530
|
+
const event = { id: id, this: this, args: arguments };
|
|
531
|
+
go._pendingEvent = event;
|
|
532
|
+
go._resume();
|
|
533
|
+
return event.result;
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
if (
|
|
539
|
+
global.require &&
|
|
540
|
+
global.require.main === module &&
|
|
541
|
+
global.process &&
|
|
542
|
+
global.process.versions &&
|
|
543
|
+
!global.process.versions.electron
|
|
544
|
+
) {
|
|
545
|
+
if (process.argv.length != 3) {
|
|
546
|
+
console.error("usage: go_js_wasm_exec [wasm binary] [arguments]");
|
|
547
|
+
process.exit(1);
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
const go = new Go();
|
|
551
|
+
WebAssembly.instantiate(fs.readFileSync(process.argv[2]), go.importObject).then(async (result) => {
|
|
552
|
+
let exitCode = await go.run(result.instance);
|
|
553
|
+
process.exit(exitCode);
|
|
554
|
+
}).catch((err) => {
|
|
555
|
+
console.error(err);
|
|
556
|
+
process.exit(1);
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
})();
|
package/licenses/go.txt
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
Copyright 2009 The Go Authors.
|
|
2
|
-
|
|
3
|
-
Redistribution and use in source and binary forms, with or without
|
|
4
|
-
modification, are permitted provided that the following conditions are
|
|
5
|
-
met:
|
|
6
|
-
|
|
7
|
-
* Redistributions of source code must retain the above copyright
|
|
8
|
-
notice, this list of conditions and the following disclaimer.
|
|
9
|
-
* Redistributions in binary form must reproduce the above
|
|
10
|
-
copyright notice, this list of conditions and the following disclaimer
|
|
11
|
-
in the documentation and/or other materials provided with the
|
|
12
|
-
distribution.
|
|
13
|
-
* Neither the name of Google LLC nor the names of its
|
|
14
|
-
contributors may be used to endorse or promote products derived from
|
|
15
|
-
this software without specific prior written permission.
|
|
16
|
-
|
|
17
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
18
|
-
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
19
|
-
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
20
|
-
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
21
|
-
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
22
|
-
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
23
|
-
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
24
|
-
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
25
|
-
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
26
|
-
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
27
|
-
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
1
|
+
Copyright 2009 The Go Authors.
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without
|
|
4
|
+
modification, are permitted provided that the following conditions are
|
|
5
|
+
met:
|
|
6
|
+
|
|
7
|
+
* Redistributions of source code must retain the above copyright
|
|
8
|
+
notice, this list of conditions and the following disclaimer.
|
|
9
|
+
* Redistributions in binary form must reproduce the above
|
|
10
|
+
copyright notice, this list of conditions and the following disclaimer
|
|
11
|
+
in the documentation and/or other materials provided with the
|
|
12
|
+
distribution.
|
|
13
|
+
* Neither the name of Google LLC nor the names of its
|
|
14
|
+
contributors may be used to endorse or promote products derived from
|
|
15
|
+
this software without specific prior written permission.
|
|
16
|
+
|
|
17
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
18
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
19
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
20
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
21
|
+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
22
|
+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
23
|
+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
24
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
25
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
26
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
27
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/package.json
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@godexture/js",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "JavaScript/TypeScript bindings for godexture",
|
|
5
|
-
"repository": {
|
|
6
|
-
"type": "git",
|
|
7
|
-
"url": "git+https://github.com/godexture/bindings.git",
|
|
8
|
-
"directory": "js"
|
|
9
|
-
},
|
|
10
|
-
"license": "MIT",
|
|
11
|
-
"main": "dist/index.js",
|
|
12
|
-
"types": "dist/index.d.ts",
|
|
13
|
-
"files": [
|
|
14
|
-
"dist",
|
|
15
|
-
"licenses",
|
|
16
|
-
"LICENSE"
|
|
17
|
-
],
|
|
18
|
-
"scripts": {
|
|
19
|
-
"build": "bun build:wasm && bun build:ts",
|
|
20
|
-
"build:ts": "tsc -p tsconfig.app.json",
|
|
21
|
-
"build:wasm": "bun run scripts/build.ts",
|
|
22
|
-
"test": "bun run ./test"
|
|
23
|
-
},
|
|
24
|
-
"devDependencies": {
|
|
25
|
-
"@types/bun": "1.3.14",
|
|
26
|
-
"@types/node": "26.1.1",
|
|
27
|
-
"typescript": "7.0.2"
|
|
28
|
-
},
|
|
29
|
-
"publishConfig": {
|
|
30
|
-
"access": "public"
|
|
31
|
-
}
|
|
32
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@godexture/js",
|
|
3
|
+
"version": "0.0.4",
|
|
4
|
+
"description": "JavaScript/TypeScript bindings for godexture",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/godexture/bindings.git",
|
|
8
|
+
"directory": "js"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"main": "dist/index.js",
|
|
12
|
+
"types": "dist/index.d.ts",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"licenses",
|
|
16
|
+
"LICENSE"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "bun build:wasm && bun build:ts",
|
|
20
|
+
"build:ts": "tsc -p tsconfig.app.json",
|
|
21
|
+
"build:wasm": "bun run scripts/build.ts",
|
|
22
|
+
"test": "bun run ./test"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/bun": "1.3.14",
|
|
26
|
+
"@types/node": "26.1.1",
|
|
27
|
+
"typescript": "7.0.2"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
}
|
|
32
|
+
}
|