Skip to content

Commit 77e7c39

Browse files
committed
wasm: correctly return from run() in wasm_exec.js
Instead of hanging forever, it should return the exit code from os.Exit.
1 parent 88a6f4e commit 77e7c39

File tree

3 files changed

+81
-13
lines changed

3 files changed

+81
-13
lines changed

main_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,12 +766,56 @@ func TestWasmExportJS(t *testing.T) {
766766
}
767767
}
768768

769+
// Test whether Go.run() (in wasm_exec.js) normally returns and returns the
770+
// right exit code.
771+
func TestWasmExit(t *testing.T) {
772+
t.Parallel()
773+
774+
type testCase struct {
775+
name string
776+
output string
777+
}
778+
779+
tests := []testCase{
780+
{name: "normal", output: "exit code: 0\n"},
781+
{name: "exit-0", output: "exit code: 0\n"},
782+
{name: "exit-0-sleep", output: "slept\nexit code: 0\n"},
783+
{name: "exit-1", output: "exit code: 1\n"},
784+
{name: "exit-1-sleep", output: "slept\nexit code: 1\n"},
785+
}
786+
for _, tc := range tests {
787+
tc := tc
788+
t.Run(tc.name, func(t *testing.T) {
789+
t.Parallel()
790+
options := optionsFromTarget("wasm", sema)
791+
buildConfig, err := builder.NewConfig(&options)
792+
if err != nil {
793+
t.Fatal(err)
794+
}
795+
buildConfig.Target.Emulator = "node testdata/wasmexit.js {}"
796+
output := &bytes.Buffer{}
797+
_, err = buildAndRun("testdata/wasmexit.go", buildConfig, output, []string{tc.name}, nil, time.Minute, func(cmd *exec.Cmd, result builder.BuildResult) error {
798+
return cmd.Run()
799+
})
800+
if err != nil {
801+
t.Error(err)
802+
}
803+
expected := "wasmexit test: " + tc.name + "\n" + tc.output
804+
checkOutputData(t, []byte(expected), output.Bytes())
805+
})
806+
}
807+
}
808+
769809
// Check whether the output of a test equals the expected output.
770810
func checkOutput(t *testing.T, filename string, actual []byte) {
771811
expectedOutput, err := os.ReadFile(filename)
772812
if err != nil {
773813
t.Fatal("could not read output file:", err)
774814
}
815+
checkOutputData(t, expectedOutput, actual)
816+
}
817+
818+
func checkOutputData(t *testing.T, expectedOutput, actual []byte) {
775819
expectedOutput = bytes.ReplaceAll(expectedOutput, []byte("\r\n"), []byte("\n"))
776820
actual = bytes.ReplaceAll(actual, []byte("\r\n"), []byte("\n"))
777821

src/runtime/runtime_wasmentry.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ func wasmEntryCommand() {
2020
run()
2121
if mainExited {
2222
beforeExit()
23+
24+
// To make sure wasm_exec.js knows that we've exited, call proc_exit
25+
// explicitly.
26+
proc_exit(0)
2327
}
2428
}
2529

targets/wasm_exec.js

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
const decoder = new TextDecoder("utf-8");
133133
let reinterpretBuf = new DataView(new ArrayBuffer(8));
134134
var logLine = [];
135+
const wasmExit = {}; // thrown to exit via proc_exit (not an error)
135136

136137
global.Go = class {
137138
constructor() {
@@ -270,14 +271,11 @@
270271
fd_close: () => 0, // dummy
271272
fd_fdstat_get: () => 0, // dummy
272273
fd_seek: () => 0, // dummy
273-
"proc_exit": (code) => {
274-
if (global.process) {
275-
// Node.js
276-
process.exit(code);
277-
} else {
278-
// Can't exit in a browser.
279-
throw 'trying to exit with code ' + code;
280-
}
274+
proc_exit: (code) => {
275+
this.exited = true;
276+
this.exitCode = code;
277+
this._resolveExitPromise();
278+
throw wasmExit;
281279
},
282280
random_get: (bufPtr, bufLen) => {
283281
crypto.getRandomValues(loadSlice(bufPtr, bufLen));
@@ -293,7 +291,14 @@
293291
// func sleepTicks(timeout float64)
294292
"runtime.sleepTicks": (timeout) => {
295293
// Do not sleep, only reactivate scheduler after the given timeout.
296-
setTimeout(this._inst.exports.go_scheduler, timeout);
294+
setTimeout(() => {
295+
if (this.exited) return;
296+
try {
297+
this._inst.exports.go_scheduler();
298+
} catch (e) {
299+
if (e !== wasmExit) throw e;
300+
}
301+
}, timeout);
297302
},
298303

299304
// func finalizeRef(v ref)
@@ -465,12 +470,23 @@
465470
this._ids = new Map(); // mapping from JS values to reference ids
466471
this._idPool = []; // unused ids that have been garbage collected
467472
this.exited = false; // whether the Go program has exited
473+
this.exitCode = 0;
468474

469475
if (this._inst.exports._start) {
470-
this._inst.exports._start();
476+
let exitPromise = new Promise((resolve, reject) => {
477+
this._resolveExitPromise = resolve;
478+
});
479+
480+
// Run program, but catch the wasmExit exception that's thrown
481+
// to return back here.
482+
try {
483+
this._inst.exports._start();
484+
} catch (e) {
485+
if (e !== wasmExit) throw e;
486+
}
471487

472-
// TODO: wait until the program exists.
473-
await new Promise(() => {});
488+
await exitPromise;
489+
return this.exitCode;
474490
} else {
475491
this._inst.exports._initialize();
476492
}
@@ -480,7 +496,11 @@
480496
if (this.exited) {
481497
throw new Error("Go program has already exited");
482498
}
483-
this._inst.exports.resume();
499+
try {
500+
this._inst.exports.resume();
501+
} catch (e) {
502+
if (e !== wasmExit) throw e;
503+
}
484504
if (this.exited) {
485505
this._resolveExitPromise();
486506
}

0 commit comments

Comments
 (0)