Source file src/runtime/stackswitch_wasmx_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build (wasm && wasm.stackswitch) || (wasm32 && wasm32.stackswitch)
     6  
     7  package runtime_test
     8  
     9  import (
    10  	"runtime"
    11  	"testing"
    12  )
    13  
    14  func TestWasmContinuationTable(t *testing.T) {
    15  	size := runtime.WasmContTableSize()
    16  	if size <= 0 {
    17  		t.Fatalf("continuation table size = %d, want > 0", size)
    18  	}
    19  	if old := runtime.WasmContTableGrow(1); old != size {
    20  		t.Fatalf("table.grow returned %d, want previous size %d", old, size)
    21  	}
    22  	if got := runtime.WasmContTableSize(); got != size+1 {
    23  		t.Fatalf("continuation table size = %d, want %d", got, size+1)
    24  	}
    25  
    26  	// Keep the goroutines alive together so their slots must be distinct.
    27  	const count = 32
    28  	slots := make(chan int32, count)
    29  	done := make(chan struct{})
    30  	defer close(done)
    31  	for range count {
    32  		go func() {
    33  			slots <- runtime.WasmContSlot()
    34  			<-done
    35  		}()
    36  	}
    37  	seen := make(map[int32]bool)
    38  	for range count {
    39  		slot := <-slots
    40  		if slot <= 0 || slot >= runtime.WasmContTableSize() {
    41  			t.Errorf("goroutine continuation slot %d is outside the table", slot)
    42  		}
    43  		if seen[slot] {
    44  			t.Errorf("goroutines share continuation slot %d", slot)
    45  		}
    46  		seen[slot] = true
    47  	}
    48  }
    49  

View as plain text