From 62e996b9bebad22ee885c6ecd62a820f5daf4186 Mon Sep 17 00:00:00 2001 From: syumai Date: Sat, 22 Feb 2025 23:23:43 +0900 Subject: [PATCH 1/4] update wasm_exec_go.js for Go 1.24 --- cmd/workers-assets-gen/assets/wasm_exec_go.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/cmd/workers-assets-gen/assets/wasm_exec_go.js b/cmd/workers-assets-gen/assets/wasm_exec_go.js index 8931314..99585e0 100644 --- a/cmd/workers-assets-gen/assets/wasm_exec_go.js +++ b/cmd/workers-assets-gen/assets/wasm_exec_go.js @@ -14,7 +14,7 @@ if (!globalThis.fs) { let outputBuf = ""; globalThis.fs = { - constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused + constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1, O_DIRECTORY: -1 }, // unused writeSync(fd, buf) { outputBuf += decoder.decode(buf); const nl = outputBuf.lastIndexOf("\n"); @@ -73,6 +73,14 @@ } } + if (!globalThis.path) { + globalThis.path = { + resolve(...pathSegments) { + return pathSegments.join("/"); + } + } + } + if (!globalThis.crypto) { throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)"); } @@ -208,10 +216,16 @@ return decoder.decode(new DataView(this._inst.exports.mem.buffer, saddr, len)); } + const testCallExport = (a, b) => { + this._inst.exports.testExport0(); + return this._inst.exports.testExport(a, b); + } + const timeOrigin = Date.now() - performance.now(); this.importObject = { _gotest: { add: (a, b) => a + b, + callExport: testCallExport, }, gojs: { // Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters) From 0a3add28877283f1197234ca9a9b06845b0e32a6 Mon Sep 17 00:00:00 2001 From: syumai Date: Sat, 22 Feb 2025 23:56:33 +0900 Subject: [PATCH 2/4] update wasm_exec_tinygo.js --- .../assets/wasm_exec_tinygo.js | 67 +++++++++++-------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/cmd/workers-assets-gen/assets/wasm_exec_tinygo.js b/cmd/workers-assets-gen/assets/wasm_exec_tinygo.js index cd1699c..1afa3e9 100644 --- a/cmd/workers-assets-gen/assets/wasm_exec_tinygo.js +++ b/cmd/workers-assets-gen/assets/wasm_exec_tinygo.js @@ -32,7 +32,7 @@ /* if (!global.fs && global.require) { - global.fs = require("fs"); + global.fs = require("node:fs"); } */ @@ -106,7 +106,7 @@ /* if (!global.crypto) { - const nodeCrypto = require("crypto"); + const nodeCrypto = require("node:crypto"); global.crypto = { getRandomValues(b) { nodeCrypto.randomFillSync(b); @@ -126,11 +126,11 @@ /* 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; } */ @@ -140,6 +140,7 @@ const decoder = new TextDecoder("utf-8"); let reinterpretBuf = new DataView(new ArrayBuffer(8)); var logLine = []; + const wasmExit = {}; // thrown to exit via proc_exit (not an error) global.Go = class { constructor() { @@ -278,14 +279,11 @@ fd_close: () => 0, // dummy fd_fdstat_get: () => 0, // dummy fd_seek: () => 0, // dummy - "proc_exit": (code) => { - if (global.process) { - // Node.js - process.exit(code); - } else { - // Can't exit in a browser. - throw 'trying to exit with code ' + code; - } + proc_exit: (code) => { + this.exited = true; + this.exitCode = code; + this._resolveExitPromise(); + throw wasmExit; }, random_get: (bufPtr, bufLen) => { crypto.getRandomValues(loadSlice(bufPtr, bufLen)); @@ -301,7 +299,14 @@ // func sleepTicks(timeout float64) "runtime.sleepTicks": (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) @@ -481,23 +486,25 @@ this._ids = new Map(); // mapping from JS values to reference ids this._idPool = []; // unused ids that have been garbage collected this.exited = false; // whether the Go program has exited + this.exitCode = 0; - 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) { + let exitPromise = new Promise((resolve, reject) => { + this._resolveExitPromise = resolve; }); - this._inst.exports._start(); - if (this.exited) { - break; + + // Run program, but catch the wasmExit exception that's thrown + // to return back here. + try { + this._inst.exports._start(); + } catch (e) { + if (e !== wasmExit) throw e; } - await callbackPromise; + + await exitPromise; + return this.exitCode; + } else { + this._inst.exports._initialize(); } } @@ -505,7 +512,11 @@ if (this.exited) { throw new Error("Go program has already exited"); } - this._inst.exports.resume(); + try { + this._inst.exports.resume(); + } catch (e) { + if (e !== wasmExit) throw e; + } if (this.exited) { this._resolveExitPromise(); } From 106d4af001666b1956ad5050b1648b21c8570251 Mon Sep 17 00:00:00 2001 From: syumai Date: Sat, 22 Feb 2025 23:57:56 +0900 Subject: [PATCH 3/4] update workers-assets-gen version --- _templates/cloudflare/cron-go/Makefile | 2 +- _templates/cloudflare/cron-tinygo/Makefile | 2 +- _templates/cloudflare/pages-tinygo/Makefile | 2 +- _templates/cloudflare/worker-go/Makefile | 2 +- _templates/cloudflare/worker-tinygo/Makefile | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/_templates/cloudflare/cron-go/Makefile b/_templates/cloudflare/cron-go/Makefile index 7f0c8e8..f372706 100644 --- a/_templates/cloudflare/cron-go/Makefile +++ b/_templates/cloudflare/cron-go/Makefile @@ -4,7 +4,7 @@ dev: .PHONY: build build: - go run github.com/syumai/workers/cmd/workers-assets-gen@v0.23.1 -mode=go + go run github.com/syumai/workers/cmd/workers-assets-gen@v0.28.1 -mode=go GOOS=js GOARCH=wasm go build -o ./build/app.wasm . .PHONY: deploy diff --git a/_templates/cloudflare/cron-tinygo/Makefile b/_templates/cloudflare/cron-tinygo/Makefile index 24eeaf0..beb82a0 100644 --- a/_templates/cloudflare/cron-tinygo/Makefile +++ b/_templates/cloudflare/cron-tinygo/Makefile @@ -4,7 +4,7 @@ dev: .PHONY: build build: - go run github.com/syumai/workers/cmd/workers-assets-gen@v0.23.1 + go run github.com/syumai/workers/cmd/workers-assets-gen@v0.28.1 tinygo build -o ./build/app.wasm -target wasm -no-debug ./... .PHONY: deploy diff --git a/_templates/cloudflare/pages-tinygo/Makefile b/_templates/cloudflare/pages-tinygo/Makefile index 9d0c3b0..686c773 100644 --- a/_templates/cloudflare/pages-tinygo/Makefile +++ b/_templates/cloudflare/pages-tinygo/Makefile @@ -4,7 +4,7 @@ dev: .PHONY: build build: - go run github.com/syumai/workers/cmd/workers-assets-gen@v0.23.1 + go run github.com/syumai/workers/cmd/workers-assets-gen@v0.28.1 tinygo build -o ./build/app.wasm -target wasm -no-debug ./... .PHONY: deploy diff --git a/_templates/cloudflare/worker-go/Makefile b/_templates/cloudflare/worker-go/Makefile index 7f0c8e8..f372706 100644 --- a/_templates/cloudflare/worker-go/Makefile +++ b/_templates/cloudflare/worker-go/Makefile @@ -4,7 +4,7 @@ dev: .PHONY: build build: - go run github.com/syumai/workers/cmd/workers-assets-gen@v0.23.1 -mode=go + go run github.com/syumai/workers/cmd/workers-assets-gen@v0.28.1 -mode=go GOOS=js GOARCH=wasm go build -o ./build/app.wasm . .PHONY: deploy diff --git a/_templates/cloudflare/worker-tinygo/Makefile b/_templates/cloudflare/worker-tinygo/Makefile index 24eeaf0..beb82a0 100644 --- a/_templates/cloudflare/worker-tinygo/Makefile +++ b/_templates/cloudflare/worker-tinygo/Makefile @@ -4,7 +4,7 @@ dev: .PHONY: build build: - go run github.com/syumai/workers/cmd/workers-assets-gen@v0.23.1 + go run github.com/syumai/workers/cmd/workers-assets-gen@v0.28.1 tinygo build -o ./build/app.wasm -target wasm -no-debug ./... .PHONY: deploy From ebd280b8fff70fe648fbebbcfee0ab714aa0ed9b Mon Sep 17 00:00:00 2001 From: syumai Date: Sat, 22 Feb 2025 23:59:49 +0900 Subject: [PATCH 4/4] update Go version in Quick Start --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8531f04..d660ae1 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ First, please install the following tools: * Node.js (and npm) * [wrangler](https://developers.cloudflare.com/workers/wrangler/) - You can install it by running `npm install -g wrangler`. -* Go 1.21.3 or later +* Go 1.24.0 or later * [gonew](https://pkg.go.dev/golang.org/x/tools/cmd/gonew) - You can install it by running `go install golang.org/x/tools/cmd/gonew@latest`