mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
update wasm_exec_tinygo.js
This commit is contained in:
parent
62e996b9be
commit
0a3add2887
@ -32,7 +32,7 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
if (!global.fs && global.require) {
|
if (!global.fs && global.require) {
|
||||||
global.fs = require("fs");
|
global.fs = require("node:fs");
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -106,7 +106,7 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
if (!global.crypto) {
|
if (!global.crypto) {
|
||||||
const nodeCrypto = require("crypto");
|
const nodeCrypto = require("node:crypto");
|
||||||
global.crypto = {
|
global.crypto = {
|
||||||
getRandomValues(b) {
|
getRandomValues(b) {
|
||||||
nodeCrypto.randomFillSync(b);
|
nodeCrypto.randomFillSync(b);
|
||||||
@ -126,11 +126,11 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
if (!global.TextEncoder) {
|
if (!global.TextEncoder) {
|
||||||
global.TextEncoder = require("util").TextEncoder;
|
global.TextEncoder = require("node:util").TextEncoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!global.TextDecoder) {
|
if (!global.TextDecoder) {
|
||||||
global.TextDecoder = require("util").TextDecoder;
|
global.TextDecoder = require("node:util").TextDecoder;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -140,6 +140,7 @@
|
|||||||
const decoder = new TextDecoder("utf-8");
|
const decoder = new TextDecoder("utf-8");
|
||||||
let reinterpretBuf = new DataView(new ArrayBuffer(8));
|
let reinterpretBuf = new DataView(new ArrayBuffer(8));
|
||||||
var logLine = [];
|
var logLine = [];
|
||||||
|
const wasmExit = {}; // thrown to exit via proc_exit (not an error)
|
||||||
|
|
||||||
global.Go = class {
|
global.Go = class {
|
||||||
constructor() {
|
constructor() {
|
||||||
@ -278,14 +279,11 @@
|
|||||||
fd_close: () => 0, // dummy
|
fd_close: () => 0, // dummy
|
||||||
fd_fdstat_get: () => 0, // dummy
|
fd_fdstat_get: () => 0, // dummy
|
||||||
fd_seek: () => 0, // dummy
|
fd_seek: () => 0, // dummy
|
||||||
"proc_exit": (code) => {
|
proc_exit: (code) => {
|
||||||
if (global.process) {
|
this.exited = true;
|
||||||
// Node.js
|
this.exitCode = code;
|
||||||
process.exit(code);
|
this._resolveExitPromise();
|
||||||
} else {
|
throw wasmExit;
|
||||||
// Can't exit in a browser.
|
|
||||||
throw 'trying to exit with code ' + code;
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
random_get: (bufPtr, bufLen) => {
|
random_get: (bufPtr, bufLen) => {
|
||||||
crypto.getRandomValues(loadSlice(bufPtr, bufLen));
|
crypto.getRandomValues(loadSlice(bufPtr, bufLen));
|
||||||
@ -301,7 +299,14 @@
|
|||||||
// func sleepTicks(timeout float64)
|
// func sleepTicks(timeout float64)
|
||||||
"runtime.sleepTicks": (timeout) => {
|
"runtime.sleepTicks": (timeout) => {
|
||||||
// Do not sleep, only reactivate scheduler after the given timeout.
|
// Do not sleep, only reactivate scheduler after the given timeout.
|
||||||
setTimeout(this._inst.exports.go_scheduler, timeout);
|
setTimeout(() => {
|
||||||
|
if (this.exited) return;
|
||||||
|
try {
|
||||||
|
this._inst.exports.go_scheduler();
|
||||||
|
} catch (e) {
|
||||||
|
if (e !== wasmExit) throw e;
|
||||||
|
}
|
||||||
|
}, timeout);
|
||||||
},
|
},
|
||||||
|
|
||||||
// func finalizeRef(v ref)
|
// func finalizeRef(v ref)
|
||||||
@ -481,23 +486,25 @@
|
|||||||
this._ids = new Map(); // mapping from JS values to reference ids
|
this._ids = new Map(); // mapping from JS values to reference ids
|
||||||
this._idPool = []; // unused ids that have been garbage collected
|
this._idPool = []; // unused ids that have been garbage collected
|
||||||
this.exited = false; // whether the Go program has exited
|
this.exited = false; // whether the Go program has exited
|
||||||
|
this.exitCode = 0;
|
||||||
|
|
||||||
const mem = new DataView(this._inst.exports.memory.buffer)
|
if (this._inst.exports._start) {
|
||||||
|
let exitPromise = new Promise((resolve, reject) => {
|
||||||
while (true) {
|
this._resolveExitPromise = resolve;
|
||||||
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
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Run program, but catch the wasmExit exception that's thrown
|
||||||
|
// to return back here.
|
||||||
|
try {
|
||||||
this._inst.exports._start();
|
this._inst.exports._start();
|
||||||
if (this.exited) {
|
} catch (e) {
|
||||||
break;
|
if (e !== wasmExit) throw e;
|
||||||
}
|
}
|
||||||
await callbackPromise;
|
|
||||||
|
await exitPromise;
|
||||||
|
return this.exitCode;
|
||||||
|
} else {
|
||||||
|
this._inst.exports._initialize();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -505,7 +512,11 @@
|
|||||||
if (this.exited) {
|
if (this.exited) {
|
||||||
throw new Error("Go program has already exited");
|
throw new Error("Go program has already exited");
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
this._inst.exports.resume();
|
this._inst.exports.resume();
|
||||||
|
} catch (e) {
|
||||||
|
if (e !== wasmExit) throw e;
|
||||||
|
}
|
||||||
if (this.exited) {
|
if (this.exited) {
|
||||||
this._resolveExitPromise();
|
this._resolveExitPromise();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user