Commit e56d62c6b7 for golang
commit e56d62c6b751e9fac44725222fa92961f6f1ea54
Author: Carlos Amedee <carlos@golang.org>
Date: Wed Nov 18 15:32:00 2026 -0500
runtime: reuse one continuation per level of host callback
Under GOWASM=stackswitch, when JavaScript calls back into Go
synchronously (a js.FuncOf callback during a syscall/js call), g is
running on an outer continuation, so its slot is null and the callback
runs on a continuation of its own. Previously, that continuation stayed
in g's slot until g's next suspend dropped it. V8 collects dropped
continuations, but Wasmtime does not free them, and the next CL runs
//go:wasmexport calls from an import the same way.
Add wasm_host_loop to wrap wasm_scheduler_loop and track the nesting
depth of host calls into Go, updating the stackswitch entry points in
stackswitch_js_wasm.s and stackswitch_wasip1_wasm.s (leaving the
default port byte-identical). At a nested level (d >= 1), wasm_resume
runs the callback on a continuation from wasmContFree (or creates one
if the pool is empty). When the callback ends, wasm_host_loop moves
that continuation into a null slot of wasmContFree, leaving g with its
now-null slot index for the outer wasm_scheduler_loop. Consecutive
callbacks therefore reuse continuations at any nesting depth.
Unlike dead-goroutine pooling, which returns a goroutine's own slot to
wasmContFree, pooling a callback continuation requires an existing null
slot in wasmContFree. wasmHostEnter ensures at least d null slots are
available at level d >= 1, growing wasmContFree on g0 with
persistentalloc when needed (outside any continuation, it cannot grow
gp's stack or trigger GC). POOL_PUSH and wasm_host_loop check for
overflow via wasmBadPool. Dead-goroutine pooling also moves into
RESUME_AFTER_MCALL since goroutines die only in mcall.
At a nested level, wasm_host_loop throws via wasmHostBadReturn if the
loop returns on a different g than entered it, so a continuation parked
mid-callback is never pooled. (Until runtime.exit stops using PAUSE in
a later CL, os.Exit from another goroutine during a nested callback
hits wasmHostBadReturn after wasmExit.)
For #81963
Cq-Include-Trybots: luci.golang.try:gotip-js-wasm-stackswitch,gotip-wasip1-wasm_wasmtime-stackswitch
Change-Id: Ie328c7dc4b82479d882d1c34bec9e4f0d32cdb24
Reviewed-on: https://go-review.googlesource.com/c/go/+/865882
Auto-Submit: Carlos Amedee <carlos@golang.org>
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/src/cmd/internal/obj/wasm/wasmobj.go b/src/cmd/internal/obj/wasm/wasmobj.go
index 3598f3fb8e..dcd5b5962a 100644
--- a/src/cmd/internal/obj/wasm/wasmobj.go
+++ b/src/cmd/internal/obj/wasm/wasmobj.go
@@ -1210,6 +1210,7 @@ var notUsePC_B = map[string]bool{
"wasm_goentry": true,
"wasm_scheduler_loop": true,
"wasm_resume": true,
+ "wasm_host_loop": true,
}
func assemble(ctxt *obj.Link, s *obj.LSym, newprog obj.ProgAlloc) {
@@ -1273,6 +1274,9 @@ func assemble(ctxt *obj.Link, s *obj.LSym, newprog obj.ProgAlloc) {
varDecls[0].count++
}
useAssemblyRegMap()
+ case "wasm_host_loop":
+ varDecls = []*varDecl{{count: 2, typ: i32}, {count: 1, typ: i64}} // R0, R1, R2
+ useAssemblyRegMap()
case "memchr", "memcmp":
varDecls = []*varDecl{{count: 2, typ: i32}}
useAssemblyRegMap()
diff --git a/src/cmd/link/internal/wasm/asm.go b/src/cmd/link/internal/wasm/asm.go
index d925dad31e..bc5e7a6f24 100644
--- a/src/cmd/link/internal/wasm/asm.go
+++ b/src/cmd/link/internal/wasm/asm.go
@@ -187,6 +187,7 @@ var wasmFuncTypes = map[string]*wasmFuncType{
"memchr": {Params: []byte{I32, I32, I32}, Results: []byte{I32}}, // s, c, len -> index
"wasm_scheduler_loop": {Params: []byte{}}, //
"wasm_resume": {Params: []byte{I32}, Results: []byte{contRef}}, // slot -> cont
+ "wasm_host_loop": {Params: []byte{}}, //
"wasm_goentry": {Params: []byte{}}, //
}
diff --git a/src/runtime/stackswitch_js_wasm.s b/src/runtime/stackswitch_js_wasm.s
index 91a99a917b..75b7e264f4 100644
--- a/src/runtime/stackswitch_js_wasm.s
+++ b/src/runtime/stackswitch_js_wasm.s
@@ -30,18 +30,17 @@ TEXT wasm_export_run(SB),NOSPLIT,$0
I32Const $0 // entry PC_B
Call runtime·rt0_go(SB)
Drop
- Call wasm_scheduler_loop(SB)
+ Call wasm_host_loop(SB)
Return
// wasm_export_resume gets called from JavaScript. It resumes the execution of Go code until it needs to wait for
// an event.
//
-// It runs handleEvent on g, from g's slot (see wasm_resume). If g paused,
-// its continuation is parked in wasm_goentry, which restarts at
-// g.sched.pc. If JavaScript calls back synchronously while g runs, g's
-// slot is initially null, so the host callback runs in a new continuation
-// that stays in g's slot when it pauses (reused by further callbacks until
-// g's next suspend replaces it).
+// It runs handleEvent on g, from g's slot, under wasm_host_loop. If g
+// paused, its continuation is parked in wasm_goentry, which restarts at
+// g.sched.pc. If JavaScript called back into Go synchronously while g runs,
+// g's slot is null, and the callback runs in a pooled or new continuation
+// (see wasm_host_loop).
// Overwriting g.sched.pc is safe in both cases: nothing still needs the PC
// it held. (It would not be safe for a goroutine parked in mcall or
// morestack: RESUME_AFTER_G0 would take the change for a recovered panic.)
@@ -57,5 +56,5 @@ TEXT wasm_export_resume(SB),NOSPLIT,$0
End
MOVD $runtime·handleEvent(SB), g_sched+gobuf_pc(g)
- Call wasm_scheduler_loop(SB)
+ Call wasm_host_loop(SB)
Return
diff --git a/src/runtime/stackswitch_js_wasm_test.go b/src/runtime/stackswitch_js_wasm_test.go
new file mode 100644
index 0000000000..f7c409e650
--- /dev/null
+++ b/src/runtime/stackswitch_js_wasm_test.go
@@ -0,0 +1,74 @@
+// Copyright 2026 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build wasm.stackswitch
+
+package runtime_test
+
+import (
+ "runtime"
+ "syscall/js"
+ "testing"
+)
+
+// wasmContInUse returns the number of continuations not in the pool, which
+// includes any left behind by host calls into Go.
+func wasmContInUse() int32 { return runtime.WasmContCreated() - runtime.WasmContPooled() }
+
+// wasmCallJS calls f through JavaScript, so that f runs in a synchronous
+// callback on a new host level.
+func wasmCallJS(f func()) {
+ cb := js.FuncOf(func(js.Value, []js.Value) any {
+ f()
+ return nil
+ })
+ defer cb.Release()
+ js.Global().Get("Reflect").Call("apply", cb, js.Null(), js.Global().Get("Array").New())
+}
+
+// TestWasmCallbackContinuations checks that synchronous js.FuncOf callbacks
+// reuse a continuation instead of leaving one behind each time.
+func TestWasmCallbackContinuations(t *testing.T) {
+ base := runtime.NumGoroutine()
+ ch := make(chan int)
+ wasmCallJS(func() {}) // warm up the callback path and pool
+ waitGoroutines(t, base)
+ before := wasmContInUse()
+ for i := range 200 {
+ wasmCallJS(func() {})
+ go func() { ch <- i }() // switch goroutines, suspending this one
+ <-ch
+ }
+ waitGoroutines(t, base)
+ if d := wasmContInUse() - before; d > 0 {
+ t.Errorf("continuations in use grew by %d over 200 callbacks, want 0", d)
+ }
+}
+
+// TestWasmCallbackNesting checks that js.FuncOf callbacks can nest past
+// wasmContFree's initial capacity of 64, and that nesting again reuses the
+// continuations.
+func TestWasmCallbackNesting(t *testing.T) {
+ const depth = 100
+ var nest func(n int) int
+ nest = func(n int) int {
+ if n == depth {
+ return n
+ }
+ var r int
+ wasmCallJS(func() { r = nest(n + 1) })
+ return r
+ }
+ base := runtime.NumGoroutine()
+ if got := nest(0); got != depth {
+ t.Fatalf("nest(0) = %d, want %d", got, depth)
+ }
+ waitGoroutines(t, base)
+ before := wasmContInUse()
+ nest(0)
+ waitGoroutines(t, base)
+ if d := wasmContInUse() - before; d > 0 {
+ t.Errorf("continuations in use grew by %d over %d nested callbacks, want 0", d, depth)
+ }
+}
diff --git a/src/runtime/stackswitch_wasip1_wasm.s b/src/runtime/stackswitch_wasip1_wasm.s
index fbe5a0d7a6..7786e2d02f 100644
--- a/src/runtime/stackswitch_wasip1_wasm.s
+++ b/src/runtime/stackswitch_wasip1_wasm.s
@@ -15,5 +15,5 @@ TEXT _rt0_wasm_wasip1(SB),NOSPLIT,$0
I32Const $0 // entry PC_B
Call runtime·rt0_go(SB)
Drop
- Call wasm_scheduler_loop(SB)
+ Call wasm_host_loop(SB)
Return
diff --git a/src/runtime/stackswitch_wasm.s b/src/runtime/stackswitch_wasm.s
index a6885336e2..99fb01283a 100644
--- a/src/runtime/stackswitch_wasm.s
+++ b/src/runtime/stackswitch_wasm.s
@@ -82,13 +82,14 @@ loop:
// wasm_resume resumes the continuation in wasmContTable slot R0 (signature
// (slot i32) -> (ref null $cont)) and returns the continuation that
// suspends on $yield, which the caller stores back into the slot. If the
-// slot is null (a new slot, or on js a synchronous callback), wasm_resume
-// takes a continuation from the pool (see wasmContFree) if non-empty, or
-// creates one starting at wasm_goentry (ContNew); a pooled continuation is
-// parked in RESUME_AFTER_G0 and unwinds to wasm_goentry. If the slot is
-// non-null, wasm_resume clears it: resuming consumes the continuation, so
-// the slot must not keep the reference. Either way, a slot is null while its
-// goroutine runs.
+// slot is null (it is new, or the host called into Go while the goroutine
+// runs on an outer continuation), wasm_resume takes a continuation from the
+// pool (see wasmContFree), or creates one that starts at wasm_goentry
+// (ContNew) if the pool is empty. A pooled continuation is parked in
+// RESUME_AFTER_MCALL (and unwinds to wasm_goentry) or in wasm_goentry. If
+// the slot is non-null, wasm_resume clears it: resuming consumes the
+// continuation, so the slot must not keep the reference. Either way, a slot
+// is null while its goroutine runs.
//
// Resume transfers control to wasm_goentry (for a new continuation) or to
// where the continuation last suspended. When that code suspends on $yield,
@@ -311,6 +312,28 @@ entry:
End
UNDEF
+// POOL_CHECK throws if wasmContFree has no null slot, which wasmHostEnter's
+// reservations make impossible when wasm_host_loop pools a level's
+// continuation.
+#define POOL_CHECK \
+ I32Const $runtime·wasmContPooled(SB); \
+ I32Load $0; \
+ I32Const $runtime·wasmContFree+8(SB); \
+ I32Load $0; \
+ I32GeU; \
+ If; \
+ CALLNORESUME runtime·wasmBadPool(SB); \
+ End
+
+// POOL_INC increments wasmContPooled.
+#define POOL_INC \
+ I32Const $runtime·wasmContPooled(SB); \
+ I32Const $runtime·wasmContPooled(SB); \
+ I32Load $0; \
+ I32Const $1; \
+ I32Add; \
+ I32Store $0
+
// FREE_END pushes the address of wasmContFree[len(wasmContFree)].
#define FREE_END \
I32Const $runtime·wasmContFree(SB); \
@@ -326,8 +349,17 @@ entry:
// null slot there to the end), increments wasmContPooled and
// len(wasmContFree), and resets gp.wasmCont.index to 0. When this
// continuation suspends, wasm_scheduler_loop stores it in gp's old slot,
-// now in the pool.
+// now in the pool. wasmContFree has room because gp's slot is not in it (see
+// wasmContNext); POOL_PUSH throws if wasmContFree is full.
#define POOL_PUSH(gp) \
+ I32Const $runtime·wasmContFree+8(SB); \
+ I32Load $0; \
+ I32Const $runtime·wasmContFree+16(SB); \
+ I32Load $0; \
+ I32GeU; \
+ If; \
+ CALLNORESUME runtime·wasmBadPool(SB); \
+ End; \
FREE_END; \
FREE_SLOT; \
I32Load $0; \
@@ -345,27 +377,22 @@ entry:
I64Const $1; \
I64Add; \
I64Store $0; \
- I32Const $runtime·wasmContPooled(SB); \
- I32Const $runtime·wasmContPooled(SB); \
- I32Load $0; \
- I32Const $1; \
- I32Add; \
- I32Store $0
+ POOL_INC
-// RESUME_AFTER_G0 finishes a switch to g0 whose code unwound back here (via
-// gogo or, on js, runtime.exit). owner is the g that this Wasm stack belongs
-// to; pc and goid are owner.sched.pc and owner.goid saved before switching.
+// RESUME_AFTER_G0 (for morestack) and RESUME_AFTER_MCALL (for mcall) finish
+// a switch to g0 whose code unwound back here (via gogo or, on js,
+// runtime.exit). owner is the g that this Wasm stack belongs to; pc and goid
+// are owner.sched.pc and owner.goid saved before switching.
//
// If PAUSE is set (runtime.exit on js), RESUME_AFTER_G0 returns 1 to keep
// unwinding to wasm_goentry, which yields to wasm_scheduler_loop. Otherwise,
// if gogo switched to another goroutine, it suspends this continuation,
// which wasm_scheduler_loop stores in owner's slot and resumes when owner
-// runs again. If owner is still _Gdead (goexit0's scheduling may already
-// have reused owner for a new goroutine, which then resumes this
-// continuation itself) and wasmContFree has room (which it always does; see
-// wasmContFree), it first pools this continuation with owner's slot
-// (POOL_PUSH) so any goroutine may reuse it; otherwise it stays in owner's
-// slot.
+// runs again. In RESUME_AFTER_MCALL, if owner is still _Gdead, it first
+// pools this continuation with owner's slot (POOL_PUSH) so any goroutine may
+// reuse it (goexit0's scheduling may already have reused owner for a new
+// goroutine, which then resumes this continuation itself). morestack uses
+// RESUME_AFTER_G0, which omits pooling because goroutines die only in mcall.
//
// A goroutine dies here in mcall(goexit0) (from runtime.goexit or
// runtime.Goexit) or mcall(coroswitch_m) (coroutine exit): as on other
@@ -383,6 +410,20 @@ entry:
// garbage collector, which scans only linear memory, can't see it. That is
// safe because a g is never freed: allgs keeps every g reachable.
#define RESUME_AFTER_G0(owner, pc, goid) \
+ Get PAUSE; \
+ If; \
+ I32Const $1; \
+ Return; \
+ End; \
+ Get g; \
+ Get owner; \
+ I64Ne; \
+ If; \
+ Suspend $const_wasmYieldTag; \
+ End; \
+ RESUME_SAME_G(owner, pc, goid)
+
+#define RESUME_AFTER_MCALL(owner, pc, goid) \
Get PAUSE; \
If; \
I32Const $1; \
@@ -395,17 +436,17 @@ entry:
I32Load g_atomicstatus(owner); \
I32Const $const__Gdead; \
I32Eq; \
- I32Const $runtime·wasmContFree+8(SB); \
- I32Load $0; \
- I32Const $runtime·wasmContFree+16(SB); \
- I32Load $0; \
- I32LtU; \
- I32And; \
If; \
POOL_PUSH(owner); \
End; \
Suspend $const_wasmYieldTag; \
End; \
+ RESUME_SAME_G(owner, pc, goid)
+
+// RESUME_SAME_G is the tail shared by RESUME_AFTER_G0 and
+// RESUME_AFTER_MCALL, run once this continuation resumes (on owner or
+// another g from the pool).
+#define RESUME_SAME_G(owner, pc, goid) \
Get pc; \
I64Load g_sched+gobuf_pc(g); \
I64Ne; \
@@ -424,7 +465,7 @@ entry:
// to keep running g.
//
// fn runs on g0's Go stack but on this goroutine's Wasm stack; see
-// RESUME_AFTER_G0.
+// RESUME_AFTER_MCALL.
TEXT runtime·mcall(SB), NOSPLIT, $0-8
MOVD fn+0(FP), CTXT
// R2 = m.g0
@@ -472,7 +513,7 @@ TEXT runtime·mcall(SB), NOSPLIT, $0-8
CALLNORESUME runtime·abort(SB)
End
- RESUME_AFTER_G0(R3, R0, R4)
+ RESUME_AFTER_MCALL(R3, R0, R4)
I32Const $0
Return
@@ -550,3 +591,73 @@ TEXT runtime·morestack(SB), NOSPLIT, $0-0
I32Const $0
Return
+
+// wasm_host_loop runs wasm_scheduler_loop for a call from the host, on a new
+// host level d (see wasmHostEnter). Level 0 is the program's start, or a js
+// event after Go returned to the event loop.
+//
+// At a level d >= 1, the host called back into Go synchronously, while g
+// runs on an outer continuation, so g's slot is null, and the nested loop
+// runs g on a pooled or new continuation (see wasm_resume). When the call
+// ends, that continuation is parked in g's slot; wasm_host_loop moves it
+// into a null slot of wasmContFree (reserved by wasmHostEnter), leaving g's
+// slot null again.
+TEXT wasm_host_loop(SB), NOSPLIT, $0
+ CALLNORESUME runtime·wasmHostEnter(SB)
+
+ // R1 = d, the new level.
+ I32Const $runtime·wasmHostDepth(SB)
+ I32Load $0
+ I32Const $1
+ I32Sub
+ Set R1
+
+ // R0 = g's slot, R2 = g.
+ I32Load (g_wasmCont+wasmContPerG_index)(g)
+ Set R0
+ MOVD g, R2
+
+ Call wasm_scheduler_loop(SB)
+
+ // wasmHostDepth = d.
+ I32Const $runtime·wasmHostDepth(SB)
+ Get R1
+ I32Store $0
+
+ Get R1
+ I32Eqz
+ If
+ // Level 0: g's continuation stays parked in its slot.
+ Return
+ End
+
+ // The loop returns when the call ends, on the goroutine that the host
+ // called in on, whose continuation is then in its slot (R0). If
+ // another goroutine paused instead, R0's continuation is parked in the
+ // middle of the call: it must not be pooled. Compare g's, not slots: a
+ // new goroutine may take the same slot back from the pool. A new
+ // goroutine that reuses R2 after the callback's goroutine exits cannot
+ // end the level: a js callback ends only when the original goroutine
+ // returns from handleEvent, and while its event is pending,
+ // beforeIdle starts no goroutine that pauses.
+ Get R2
+ Get g
+ I64Ne
+ If
+ CALLNORESUME runtime·wasmHostBadReturn(SB)
+ End
+
+ // Pool R0's continuation: table[wasmContFree[wasmContPooled]] =
+ // table[R0]; table[R0] = null; wasmContPooled++. g keeps its slot: the
+ // outer wasm_scheduler_loop stores g's outer continuation there.
+ POOL_CHECK
+ FREE_SLOT
+ I32Load $0
+ Get R0
+ TableGet $const_wasmContTable
+ TableSet $const_wasmContTable
+ Get R0
+ RefNull
+ TableSet $const_wasmContTable
+ POOL_INC
+ Return
diff --git a/src/runtime/stackswitch_wasmx.go b/src/runtime/stackswitch_wasmx.go
index 5a6d65aaba..1b61db69ab 100644
--- a/src/runtime/stackswitch_wasmx.go
+++ b/src/runtime/stackswitch_wasmx.go
@@ -13,9 +13,7 @@
// live g whose goroutine has started owns one slot of it, recorded in
// g.wasmCont.index. A slot holds the g's suspended continuation, or null if
// it has none: the slot is new, or wasm_resume has taken the continuation
-// out to resume it. (On js, a synchronous callback leaves its continuation
-// in the slot of the g that called into JavaScript; see
-// wasm_export_resume.)
+// out to resume it.
//
// When a goroutine exits, its frames are abandoned and its continuation
// stays suspended. It goes to a pool of continuations (wasmContFree), and
@@ -37,7 +35,8 @@
// continuation, which comes with it, or a new one. A dead g pools its
// continuation together with its slot and goes back to index 0. So the table
// holds only unused slot 0, slots owned by live goroutines that have
-// started, and slots in wasmContFree, not one per g. Slots are never freed.
+// started, slots in the pool, and null slots reserved for nested host
+// callbacks (see wasmHostEnter), not one per g. Slots are never freed.
//
// WebAssembly runs a single thread, so the allocation state below needs no locking.
@@ -71,20 +70,28 @@ type wasmContPerG struct {
var (
// wasmContFree holds the slots that no g owns. The first
- // wasmContPooled hold the suspended continuations of dead goroutines
- // (the pool); the others are null. A dead g pools its continuation
- // with its own slot (POOL_PUSH inserts it at
- // wasmContFree[wasmContPooled], moving any null slot there to the end),
- // and a goroutine's first resume takes the most recently pooled slot
- // (wasmContStart). When wasm_resume sees a null slot (a new slot, or on
- // js a synchronous callback), it either decrements wasmContPooled and
- // resumes the continuation from slot wasmContFree[wasmContPooled]
- // (setting that pooled slot to null) if wasmContPooled > 0, or creates
- // one with ContNew; wasm_scheduler_loop stores the yielded continuation
- // in the goroutine's slot. cap(wasmContFree) is at least the number of
- // assigned slots (wasmContLast; see wasmContNext), and each assigned
- // slot is either a g's or in wasmContFree, so POOL_PUSH always has
- // room. Its backing array is not in the GC heap, and assembly reads its
+ // wasmContPooled hold reusable continuations (the pool) from dead
+ // goroutines and ended host callbacks; the rest are null slots.
+ // A dead g pools its continuation with its own slot (POOL_PUSH inserts
+ // it at wasmContFree[wasmContPooled], moving any null slot there to the
+ // end), and a goroutine's first resume takes the most recently pooled
+ // slot (wasmContStart). For a nested host callback (where g's slot is
+ // null because g runs on an outer continuation), wasm_resume decrements
+ // wasmContPooled if non-zero, takes the continuation out of
+ // wasmContFree[wasmContPooled] (leaving that pool slot null), and
+ // resumes it. When the callback ends, wasm_host_loop moves its
+ // continuation from g's slot into the null slot
+ // wasmContFree[wasmContPooled] and increments wasmContPooled.
+ //
+ // Neither pooling path overflows. cap(wasmContFree) is at least the
+ // number of assigned slots (see wasmContNext), and each assigned slot
+ // is either a g's or in wasmContFree, so POOL_PUSH has room.
+ // wasmHostEnter keeps at least d null slots in wasmContFree while host
+ // level d >= 1 is active, one for each active level 1..d to pool its
+ // continuation when it ends: wasm_resume pops only add null slots,
+ // POOL_PUSH and wasmContStart leave the null-slot count unchanged, and
+ // only wasm_host_loop's pooling takes one. wasmContFree's backing array
+ // is not in the GC heap (see wasmContNext), and assembly reads its
// pointer, length, and capacity (and updates its length).
wasmContFree []int32
wasmContPooled int32
@@ -129,9 +136,9 @@ func wasmContStart() {
// wasmContNext returns the next unused slot of the continuation table,
// growing the table if the slot does not fit. It keeps cap(wasmContFree) at
// least the number of assigned slots (wasmContLast), so that pooling never
-// allocates. wasmContFree is allocated with persistentalloc, so that
-// goroutine starts can grow it without triggering a garbage collection. Old
-// arrays are not freed.
+// allocates. wasmContFree is allocated with persistentalloc, so that host
+// calls into Go and goroutine starts can grow it without triggering a
+// garbage collection. Old arrays are not freed.
func wasmContNext() int32 {
slot := wasmContLast + 1
if slot >= wasmContSize {
@@ -171,6 +178,50 @@ func wasmContTableSize() int32
// returns its previous size, or -1 if it cannot grow (table.grow).
func wasmContTableGrow(delta int32) int32
+// Host calls into Go.
+//
+// The host may call back into Go while Go runs a host import, such as a
+// js.FuncOf callback (wasm_export_resume) during syscall/js.valueCall. Each
+// such call runs a nested wasm_host_loop on a new host level, on top of the
+// native frames of the host and of the continuation that called the import.
+
+var (
+ // wasmHostDepth is the number of host calls into Go in progress.
+ // wasm_host_loop restores it on every exit.
+ wasmHostDepth int32
+)
+
+// wasmHostEnter starts a new host call level. Called by wasm_host_loop
+// outside any goroutine continuation, it must not grow gp's stack or switch
+// goroutines. At level d >= 1, it ensures wasmContFree has at least d null
+// slots, one for each active level 1..d to pool its continuation when it
+// ends (see wasmContFree).
+//
+//go:nosplit
+func wasmHostEnter() {
+ d := wasmHostDepth
+ if len(wasmContFree)-int(wasmContPooled) < int(d) {
+ // Grow on g0: wasmHostEnter is nosplit and runs outside any
+ // continuation. systemstack overwrites gp.sched, whose PC the
+ // caller of wasm_host_loop has set.
+ gp := getg()
+ pc, sp := gp.sched.pc, gp.sched.sp
+ systemstack(wasmHostGrow)
+ gp.sched.pc, gp.sched.sp = pc, sp
+ }
+ wasmHostDepth = d + 1
+}
+
+// wasmHostGrow appends a null slot to wasmContFree so the level being
+// entered has a slot to pool its continuation when it ends. It does not
+// allocate from the GC heap.
+func wasmHostGrow() {
+ slot := wasmContNext()
+ n := len(wasmContFree)
+ wasmContFree = wasmContFree[:n+1]
+ wasmContFree[n] = slot
+}
+
// Called from stackswitch_wasm.s.
// wasmBadReturn calls throw when a function returns normally to wasm_goentry
@@ -199,6 +250,23 @@ func wasmBadG() {
throw("wasm: wasm_goentry running on g0")
}
+// wasmHostBadReturn calls throw when a nested host call into Go ends on a
+// goroutine other than the one the host called in on: another goroutine
+// paused instead of the call returning.
+//
+//go:nosplit
+func wasmHostBadReturn() {
+ throw("wasm: host call into Go ended on another goroutine")
+}
+
+// wasmBadPool calls throw when POOL_PUSH or POOL_CHECK finds no room in
+// wasmContFree, which wasmContNext and wasmHostEnter should make impossible.
+//
+//go:nosplit
+func wasmBadPool() {
+ throw("wasm: continuation pool overflow")
+}
+
// wasmBadHostCall calls throw when a host callback enters Go while g is g0,
// which has no continuation (for example, from a host import called on the
// system stack).