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.
9 const err = new Error("not implemented");
17 constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused
19 outputBuf += decoder.decode(buf);
20 const nl = outputBuf.lastIndexOf("\n");
22 console.log(outputBuf.substring(0, nl));
23 outputBuf = outputBuf.substring(nl + 1);
27 write(fd, buf, offset, length, position, callback) {
28 if (offset !== 0 || length !== buf.length || position !== null) {
32 const n = this.writeSync(fd, buf);
35 chmod(path, mode, callback) { callback(enosys()); },
36 chown(path, uid, gid, callback) { callback(enosys()); },
37 close(fd, callback) { callback(enosys()); },
38 fchmod(fd, mode, callback) { callback(enosys()); },
39 fchown(fd, uid, gid, callback) { callback(enosys()); },
40 fstat(fd, callback) { callback(enosys()); },
41 fsync(fd, callback) { callback(null); },
42 ftruncate(fd, length, callback) { callback(enosys()); },
43 lchown(path, uid, gid, callback) { callback(enosys()); },
44 link(path, link, callback) { callback(enosys()); },
45 lstat(path, callback) { callback(enosys()); },
46 mkdir(path, perm, callback) { callback(enosys()); },
47 open(path, flags, mode, callback) { callback(enosys()); },
48 read(fd, buffer, offset, length, position, callback) { callback(enosys()); },
49 readdir(path, callback) { callback(enosys()); },
50 readlink(path, callback) { callback(enosys()); },
51 rename(from, to, callback) { callback(enosys()); },
52 rmdir(path, callback) { callback(enosys()); },
53 stat(path, callback) { callback(enosys()); },
54 symlink(path, link, callback) { callback(enosys()); },
55 truncate(path, length, callback) { callback(enosys()); },
56 unlink(path, callback) { callback(enosys()); },
57 utimes(path, atime, mtime, callback) { callback(enosys()); },
61 if (!globalThis.process) {
62 globalThis.process = {
63 getuid() { return -1; },
64 getgid() { return -1; },
65 geteuid() { return -1; },
66 getegid() { return -1; },
67 getgroups() { throw enosys(); },
70 umask() { throw enosys(); },
71 cwd() { throw enosys(); },
72 chdir() { throw enosys(); },
76 if (!globalThis.crypto) {
77 throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)");
80 if (!globalThis.performance) {
81 throw new Error("globalThis.performance is not available, polyfill required (performance.now only)");
84 if (!globalThis.TextEncoder) {
85 throw new Error("globalThis.TextEncoder is not available, polyfill required");
88 if (!globalThis.TextDecoder) {
89 throw new Error("globalThis.TextDecoder is not available, polyfill required");
92 const encoder = new TextEncoder("utf-8");
93 const decoder = new TextDecoder("utf-8");
95 globalThis.Go = class {
99 this.exit = (code) => {
101 console.warn("exit code:", code);
104 this._exitPromise = new Promise((resolve) => {
105 this._resolveExitPromise = resolve;
107 this._pendingEvent = null;
108 this._scheduledTimeouts = new Map();
109 this._nextCallbackTimeoutID = 1;
111 const setInt64 = (addr, v) => {
112 this.mem.setUint32(addr + 0, v, true);
113 this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
116 const setInt32 = (addr, v) => {
117 this.mem.setUint32(addr + 0, v, true);
120 const getInt64 = (addr) => {
121 const low = this.mem.getUint32(addr + 0, true);
122 const high = this.mem.getInt32(addr + 4, true);
123 return low + high * 4294967296;
126 const loadValue = (addr) => {
127 const f = this.mem.getFloat64(addr, true);
135 const id = this.mem.getUint32(addr, true);
136 return this._values[id];
139 const storeValue = (addr, v) => {
140 const nanHead = 0x7FF80000;
142 if (typeof v === "number" && v !== 0) {
144 this.mem.setUint32(addr + 4, nanHead, true);
145 this.mem.setUint32(addr, 0, true);
148 this.mem.setFloat64(addr, v, true);
152 if (v === undefined) {
153 this.mem.setFloat64(addr, 0, true);
157 let id = this._ids.get(v);
158 if (id === undefined) {
159 id = this._idPool.pop();
160 if (id === undefined) {
161 id = this._values.length;
163 this._values[id] = v;
164 this._goRefCounts[id] = 0;
165 this._ids.set(v, id);
167 this._goRefCounts[id]++;
185 this.mem.setUint32(addr + 4, nanHead | typeFlag, true);
186 this.mem.setUint32(addr, id, true);
189 const loadSlice = (addr) => {
190 const array = getInt64(addr + 0);
191 const len = getInt64(addr + 8);
192 return new Uint8Array(this._inst.exports.mem.buffer, array, len);
195 const loadSliceOfValues = (addr) => {
196 const array = getInt64(addr + 0);
197 const len = getInt64(addr + 8);
198 const a = new Array(len);
199 for (let i = 0; i < len; i++) {
200 a[i] = loadValue(array + i * 8);
205 const loadString = (addr) => {
206 const saddr = getInt64(addr + 0);
207 const len = getInt64(addr + 8);
208 return decoder.decode(new DataView(this._inst.exports.mem.buffer, saddr, len));
211 const timeOrigin = Date.now() - performance.now();
212 this.importObject = {
214 add: (a, b) => a + b,
217 // Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
218 // may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
219 // function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
220 // This changes the SP, thus we have to update the SP used by the imported function.
222 // func wasmExit(code int32)
223 "runtime.wasmExit": (sp) => {
225 const code = this.mem.getInt32(sp + 8, true);
229 delete this._goRefCounts;
235 // func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
236 "runtime.wasmWrite": (sp) => {
238 const fd = getInt64(sp + 8);
239 const p = getInt64(sp + 16);
240 const n = this.mem.getInt32(sp + 24, true);
241 fs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n));
244 // func resetMemoryDataView()
245 "runtime.resetMemoryDataView": (sp) => {
247 this.mem = new DataView(this._inst.exports.mem.buffer);
250 // func nanotime1() int64
251 "runtime.nanotime1": (sp) => {
253 setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
256 // func walltime() (sec int64, nsec int32)
257 "runtime.walltime": (sp) => {
259 const msec = (new Date).getTime();
260 setInt64(sp + 8, msec / 1000);
261 this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true);
264 // func scheduleTimeoutEvent(delay int64) int32
265 "runtime.scheduleTimeoutEvent": (sp) => {
267 const id = this._nextCallbackTimeoutID;
268 this._nextCallbackTimeoutID++;
269 this._scheduledTimeouts.set(id, setTimeout(
272 while (this._scheduledTimeouts.has(id)) {
273 // for some reason Go failed to register the timeout event, log and try again
274 // (temporary workaround for https://github.com/golang/go/issues/28975)
275 console.warn("scheduleTimeoutEvent: missed timeout event");
281 this.mem.setInt32(sp + 16, id, true);
284 // func clearTimeoutEvent(id int32)
285 "runtime.clearTimeoutEvent": (sp) => {
287 const id = this.mem.getInt32(sp + 8, true);
288 clearTimeout(this._scheduledTimeouts.get(id));
289 this._scheduledTimeouts.delete(id);
292 // func getRandomData(r []byte)
293 "runtime.getRandomData": (sp) => {
295 crypto.getRandomValues(loadSlice(sp + 8));
298 // func finalizeRef(v ref)
299 "syscall/js.finalizeRef": (sp) => {
301 const id = this.mem.getUint32(sp + 8, true);
302 this._goRefCounts[id]--;
303 if (this._goRefCounts[id] === 0) {
304 const v = this._values[id];
305 this._values[id] = null;
307 this._idPool.push(id);
311 // func stringVal(value string) ref
312 "syscall/js.stringVal": (sp) => {
314 storeValue(sp + 24, loadString(sp + 8));
317 // func valueGet(v ref, p string) ref
318 "syscall/js.valueGet": (sp) => {
320 const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));
321 sp = this._inst.exports.getsp() >>> 0; // see comment above
322 storeValue(sp + 32, result);
325 // func valueSet(v ref, p string, x ref)
326 "syscall/js.valueSet": (sp) => {
328 Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
331 // func valueDelete(v ref, p string)
332 "syscall/js.valueDelete": (sp) => {
334 Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));
337 // func valueIndex(v ref, i int) ref
338 "syscall/js.valueIndex": (sp) => {
340 storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
343 // valueSetIndex(v ref, i int, x ref)
344 "syscall/js.valueSetIndex": (sp) => {
346 Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));
349 // func valueCall(v ref, m string, args []ref) (ref, bool)
350 "syscall/js.valueCall": (sp) => {
353 const v = loadValue(sp + 8);
354 const m = Reflect.get(v, loadString(sp + 16));
355 const args = loadSliceOfValues(sp + 32);
356 const result = Reflect.apply(m, v, args);
357 sp = this._inst.exports.getsp() >>> 0; // see comment above
358 storeValue(sp + 56, result);
359 this.mem.setUint8(sp + 64, 1);
361 sp = this._inst.exports.getsp() >>> 0; // see comment above
362 storeValue(sp + 56, err);
363 this.mem.setUint8(sp + 64, 0);
367 // func valueInvoke(v ref, args []ref) (ref, bool)
368 "syscall/js.valueInvoke": (sp) => {
371 const v = loadValue(sp + 8);
372 const args = loadSliceOfValues(sp + 16);
373 const result = Reflect.apply(v, undefined, args);
374 sp = this._inst.exports.getsp() >>> 0; // see comment above
375 storeValue(sp + 40, result);
376 this.mem.setUint8(sp + 48, 1);
378 sp = this._inst.exports.getsp() >>> 0; // see comment above
379 storeValue(sp + 40, err);
380 this.mem.setUint8(sp + 48, 0);
384 // func valueNew(v ref, args []ref) (ref, bool)
385 "syscall/js.valueNew": (sp) => {
388 const v = loadValue(sp + 8);
389 const args = loadSliceOfValues(sp + 16);
390 const result = Reflect.construct(v, args);
391 sp = this._inst.exports.getsp() >>> 0; // see comment above
392 storeValue(sp + 40, result);
393 this.mem.setUint8(sp + 48, 1);
395 sp = this._inst.exports.getsp() >>> 0; // see comment above
396 storeValue(sp + 40, err);
397 this.mem.setUint8(sp + 48, 0);
401 // func valueLength(v ref) int
402 "syscall/js.valueLength": (sp) => {
404 setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
407 // valuePrepareString(v ref) (ref, int)
408 "syscall/js.valuePrepareString": (sp) => {
410 const str = encoder.encode(String(loadValue(sp + 8)));
411 storeValue(sp + 16, str);
412 setInt64(sp + 24, str.length);
415 // valueLoadString(v ref, b []byte)
416 "syscall/js.valueLoadString": (sp) => {
418 const str = loadValue(sp + 8);
419 loadSlice(sp + 16).set(str);
422 // func valueInstanceOf(v ref, t ref) bool
423 "syscall/js.valueInstanceOf": (sp) => {
425 this.mem.setUint8(sp + 24, (loadValue(sp + 8) instanceof loadValue(sp + 16)) ? 1 : 0);
428 // func copyBytesToGo(dst []byte, src ref) (int, bool)
429 "syscall/js.copyBytesToGo": (sp) => {
431 const dst = loadSlice(sp + 8);
432 const src = loadValue(sp + 32);
433 if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) {
434 this.mem.setUint8(sp + 48, 0);
437 const toCopy = src.subarray(0, dst.length);
439 setInt64(sp + 40, toCopy.length);
440 this.mem.setUint8(sp + 48, 1);
443 // func copyBytesToJS(dst ref, src []byte) (int, bool)
444 "syscall/js.copyBytesToJS": (sp) => {
446 const dst = loadValue(sp + 8);
447 const src = loadSlice(sp + 16);
448 if (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) {
449 this.mem.setUint8(sp + 48, 0);
452 const toCopy = src.subarray(0, dst.length);
454 setInt64(sp + 40, toCopy.length);
455 this.mem.setUint8(sp + 48, 1);
458 "debug": (value) => {
465 async run(instance) {
466 if (!(instance instanceof WebAssembly.Instance)) {
467 throw new Error("Go.run: WebAssembly.Instance expected");
469 this._inst = instance;
470 this.mem = new DataView(this._inst.exports.mem.buffer);
471 this._values = [ // JS values that Go currently has references to, indexed by reference id
480 this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id
481 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
492 // Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
495 const strPtr = (str) => {
497 const bytes = encoder.encode(str + "\0");
498 new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
499 offset += bytes.length;
500 if (offset % 8 !== 0) {
501 offset += 8 - (offset % 8);
506 const argc = this.argv.length;
509 this.argv.forEach((arg) => {
510 argvPtrs.push(strPtr(arg));
514 const keys = Object.keys(this.env).sort();
515 keys.forEach((key) => {
516 argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
521 argvPtrs.forEach((ptr) => {
522 this.mem.setUint32(offset, ptr, true);
523 this.mem.setUint32(offset + 4, 0, true);
527 // The linker guarantees global data starts from at least wasmMinDataAddr.
528 // Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr.
529 const wasmMinDataAddr = 4096 + 8192;
530 if (offset >= wasmMinDataAddr) {
531 throw new Error("total length of command line and environment variables exceeds limit");
534 this._inst.exports.run(argc, argv);
536 this._resolveExitPromise();
538 await this._exitPromise;
543 throw new Error("Go program has already exited");
545 this._inst.exports.resume();
547 this._resolveExitPromise();
551 _makeFuncWrapper(id) {
554 const event = { id: id, this: this, args: arguments };
555 go._pendingEvent = event;