Skip to content

Commit

Permalink
Test serialization of closure in separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Jun 24, 2024
1 parent cab6ea9 commit 42fefc4
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
6 changes: 6 additions & 0 deletions compiler/coroutine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,12 @@ func TestCoroutineYield(t *testing.T) {
coro: func() { InterfaceEmbedded() },
yields: []int{1, 1, 1},
},

{
name: "closure in separate package",
coro: func() { ClosureInSeparatePackage(3) },
yields: []int{3, 4, 5},
},
}

// This emulates the installation of function type information by the
Expand Down
8 changes: 8 additions & 0 deletions compiler/testdata/coroutine.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"unsafe"

"github.com/dispatchrun/coroutine"
"github.com/dispatchrun/coroutine/compiler/testdata/subpkg"
)

//go:generate coroc
Expand Down Expand Up @@ -741,3 +742,10 @@ func InterfaceEmbedded() {
coroutine.Yield[int, any](x.Value())
coroutine.Yield[int, any](x.Value())
}

func ClosureInSeparatePackage(n int) {
adder := subpkg.Adder(n)
for i := 0; i < n; i++ {
coroutine.Yield[int, any](adder(i))
}
}
58 changes: 58 additions & 0 deletions compiler/testdata/coroutine_durable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package testdata

import (
coroutine "github.com/dispatchrun/coroutine"
subpkg "github.com/dispatchrun/coroutine/compiler/testdata/subpkg"
math "math"
reflect "reflect"
time "time"
Expand Down Expand Up @@ -4123,6 +4124,62 @@ func InterfaceEmbedded() {
coroutine.Yield[int, any](_f0.X3)
}
}

//go:noinline
func ClosureInSeparatePackage(_fn0 int) {
_c := coroutine.LoadContext[int, any]()
var _f0 *struct {
IP int
X0 int
X1 func(int) int
X2 int
X3 int
} = coroutine.Push[struct {
IP int
X0 int
X1 func(int) int
X2 int
X3 int
}](&_c.Stack)
if _f0.IP == 0 {
*_f0 = struct {
IP int
X0 int
X1 func(int) int
X2 int
X3 int
}{X0: _fn0}
}
defer func() {
if !_c.Unwinding() {
coroutine.Pop(&_c.Stack)
}
}()
switch {
case _f0.IP < 2:
_f0.X1 = subpkg.Adder(_f0.X0)
_f0.IP = 2
fallthrough
case _f0.IP < 5:
switch {
case _f0.IP < 3:
_f0.X2 = 0
_f0.IP = 3
fallthrough
case _f0.IP < 5:
for ; _f0.X2 < _f0.X0; _f0.X2, _f0.IP = _f0.X2+1, 3 {
switch {
case _f0.IP < 4:
_f0.X3 = _f0.X1(_f0.X2)
_f0.IP = 4
fallthrough
case _f0.IP < 5:
coroutine.Yield[int, any](_f0.X3)
}
}
}
}
}
func init() {
_types.RegisterFunc[func(_fn1 int) (_ func(int))]("github.com/dispatchrun/coroutine/compiler/testdata.(*Box).Closure")
_types.RegisterClosure[func(_fn0 int), struct {
Expand Down Expand Up @@ -4156,6 +4213,7 @@ func init() {
}]("github.com/dispatchrun/coroutine/compiler/testdata.(*IdentityGenericStruct[go.shape.int]).Closure.func1")
_types.RegisterFunc[func()]("github.com/dispatchrun/coroutine/compiler/testdata.(*IdentityGenericStruct[go.shape.int]).Run")
_types.RegisterFunc[func(_fn1 int)]("github.com/dispatchrun/coroutine/compiler/testdata.(*MethodGeneratorState).MethodGenerator")
_types.RegisterFunc[func(_fn0 int)]("github.com/dispatchrun/coroutine/compiler/testdata.ClosureInSeparatePackage")
_types.RegisterFunc[func(n int)]("github.com/dispatchrun/coroutine/compiler/testdata.Double")
_types.RegisterFunc[func(_fn0 int)]("github.com/dispatchrun/coroutine/compiler/testdata.EllipsisClosure")
_types.RegisterFunc[func(_fn0 int)]("github.com/dispatchrun/coroutine/compiler/testdata.EvenSquareGenerator")
Expand Down
9 changes: 9 additions & 0 deletions compiler/testdata/subpkg/adder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build !durable

package subpkg

func Adder(n int) func(int) int {
return func(x int) int {
return x + n
}
}
18 changes: 18 additions & 0 deletions compiler/testdata/subpkg/adder_durable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//go:build durable

package subpkg

import _types "github.com/dispatchrun/coroutine/types"
//go:noinline
func Adder(n int) func(int) int {
return func(x int) int {
return x + n
}
}
func init() {
_types.RegisterFunc[func(n int) (_ func(int) int)]("github.com/dispatchrun/coroutine/compiler/testdata/subpkg.Adder")
_types.RegisterClosure[func(x int) (_ int), struct {
F uintptr
X0 int
}]("github.com/dispatchrun/coroutine/compiler/testdata/subpkg.Adder.func1")
}

0 comments on commit 42fefc4

Please sign in to comment.