Skip to content

Commit

Permalink
update wasm_exec_tinygo.js
Browse files Browse the repository at this point in the history
  • Loading branch information
syumai committed Nov 12, 2024
1 parent 3160475 commit d3a0b9f
Showing 1 changed file with 12 additions and 29 deletions.
41 changes: 12 additions & 29 deletions cmd/workers-assets-gen/assets/wasm_exec_tinygo.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,13 @@
throw new Error("cannot export Go (neither global, window nor self is defined)");
}

/*
if (!global.require && typeof require !== "undefined") {
global.require = require;
}
*/

/*
if (!global.fs && global.require) {
global.fs = require("fs");
global.fs = require("node:fs");
}
*/

const enosys = () => {
const err = new Error("not implemented");
Expand Down Expand Up @@ -104,16 +100,14 @@
}
}

/*
if (!global.crypto) {
const nodeCrypto = require("crypto");
const nodeCrypto = require("node:crypto");
global.crypto = {
getRandomValues(b) {
nodeCrypto.randomFillSync(b);
},
};
}
*/

if (!global.performance) {
global.performance = {
Expand All @@ -124,15 +118,13 @@
};
}

/*
if (!global.TextEncoder) {
global.TextEncoder = require("util").TextEncoder;
global.TextEncoder = require("node:util").TextEncoder;
}

if (!global.TextDecoder) {
global.TextDecoder = require("util").TextDecoder;
global.TextDecoder = require("node:util").TextDecoder;
}
*/

// End of polyfills for common API.

Expand Down Expand Up @@ -412,7 +404,7 @@

// func valueInstanceOf(v ref, t ref) bool
"syscall/js.valueInstanceOf": (v_ref, t_ref) => {
return unboxValue(v_ref) instanceof unboxValue(t_ref);
return unboxValue(v_ref) instanceof unboxValue(t_ref);
},

// func copyBytesToGo(dst []byte, src ref) (int, bool)
Expand Down Expand Up @@ -467,7 +459,7 @@
}
return Reflect.get(...arguments);
}
})
});
this._values = [ // JS values that Go currently has references to, indexed by reference id
NaN,
0,
Expand All @@ -482,22 +474,13 @@
this._idPool = []; // unused ids that have been garbage collected
this.exited = false; // whether the Go program has exited

const mem = new DataView(this._inst.exports.memory.buffer)

while (true) {
const callbackPromise = new Promise((resolve) => {
this._resolveCallbackPromise = () => {
if (this.exited) {
throw new Error("bad callback: Go program has already exited");
}
setTimeout(resolve, 0); // make sure it is asynchronous
};
});
if (this._inst.exports._start) {
this._inst.exports._start();
if (this.exited) {
break;
}
await callbackPromise;

// TODO: wait until the program exists.
await new Promise(() => {});
} else {
this._inst.exports._initialize();
}
}

Expand Down

0 comments on commit d3a0b9f

Please sign in to comment.