Source file
src/runtime/continuation_wasm32_test.go
1
2
3
4
5
6
7 package runtime_test
8
9 import (
10 "internal/abi"
11 "reflect"
12 "runtime"
13 "strings"
14 "testing"
15 )
16
17 type wasm32Adder interface {
18 Add(int) int
19 }
20
21 type wasm32AddOne struct{}
22
23
24 func (wasm32AddOne) Add(v int) int { return v + 1 }
25
26
27 func wasm32GrowAndYield(depth int, fn func(int) int) int {
28 var frame [256]byte
29 frame[0] = byte(depth)
30 if depth == 0 {
31 runtime.Gosched()
32 result := fn(40)
33 runtime.KeepAlive(&frame)
34 return result
35 }
36 result := wasm32GrowAndYield(depth-1, fn)
37 runtime.KeepAlive(&frame)
38 return result
39 }
40
41
42 func wasm32RecoverAndContinue(fn func() int) (result int) {
43 defer func() {
44 if recover() == nil {
45 result = -1
46 return
47 }
48 runtime.Gosched()
49 result = fn()
50 }()
51 panic("resume")
52 }
53
54
55 func wasm32IndirectTarget(v int) int { return v + 2 }
56
57 func TestWasm32ContinuationState(t *testing.T) {
58 var nilFn func()
59 if got := reflect.ValueOf(nilFn).Pointer(); got != 0 {
60 t.Fatalf("nil function PC = %#x, want 0", got)
61 }
62 if got := reflect.ValueOf(nilFn).UnsafePointer(); got != nil {
63 t.Fatalf("nil function pointer = %p, want nil", got)
64 }
65 fn := wasm32IndirectTarget
66 pc := abi.FuncPCABIInternal(fn)
67 if got := reflect.ValueOf(fn).Pointer(); got != pc {
68 t.Fatalf("reflect function PC = %#x, want %#x", got, pc)
69 }
70 f := runtime.FuncForPC(pc)
71 if f == nil {
72 t.Fatalf("FuncForPC(%#x) returned nil", pc)
73 }
74 if got := f.Name(); !strings.HasSuffix(got, ".wasm32IndirectTarget") {
75 t.Fatalf("FuncForPC(%#x).Name() = %q, want wasm32IndirectTarget", pc, got)
76 }
77
78 done := make(chan int, 1)
79 go func() {
80 var adder wasm32Adder = wasm32AddOne{}
81 result := wasm32GrowAndYield(256, adder.Add)
82 result += wasm32RecoverAndContinue(func() int { return 1 })
83 done <- result
84 }()
85
86 if got := <-done; got != 42 {
87 t.Fatalf("continuation result = %d, want 42", got)
88 }
89 }
90
View as plain text