Skip to content

Commit

Permalink
Remove workarounds
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Dec 15, 2023
1 parent 5e86534 commit 122c6a2
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 142 deletions.
4 changes: 2 additions & 2 deletions compiler/coroutine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func TestCoroutineYield(t *testing.T) {
{
name: "closure capturing receiver and param",
coro: func() { StructClosure(3) },
yields: []int{-1, 10, 100, 1000, 11, 101, 1000, 12, 102, 1000},
yields: []int{10, 100, 1000, 11, 101, 1000, 12, 102, 1000},
},
{
name: "generic function",
Expand All @@ -241,7 +241,7 @@ func TestCoroutineYield(t *testing.T) {
{
name: "generic struct closure",
coro: func() { IdentityGenericStructClosureInt(11) },
yields: []int{-1, 11, 100, 23, 12, 101, 45},
yields: []int{11, 100, 23, 12, 101, 45},
},
}

Expand Down
7 changes: 0 additions & 7 deletions compiler/testdata/coroutine.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,10 +563,6 @@ type Box struct {
}

func (b *Box) Closure(y int) func(int) {
// Force compilation of this method and the closure within.
// Remove once #84 is fixed.
coroutine.Yield[int, any](-1)

return func(z int) {
coroutine.Yield[int, any](b.x)
coroutine.Yield[int, any](y)
Expand Down Expand Up @@ -622,9 +618,6 @@ func (i *IdentityGenericStruct[T]) Run() {
}

func (i *IdentityGenericStruct[T]) Closure(n T) func(T) {
// Force compilation of this method. Remove once #84 is fixed.
coroutine.Yield[T, any](-1)

return func(x T) {
coroutine.Yield[T, any](i.n)
i.n++
Expand Down
218 changes: 85 additions & 133 deletions compiler/testdata/coroutine_durable.go
Original file line number Diff line number Diff line change
Expand Up @@ -3243,84 +3243,61 @@ type Box struct {

//go:noinline
func (_fn0 *Box) Closure(_fn1 int) (_ func(int)) {
_c := coroutine.LoadContext[int, any]()
var _f1 *struct {
var _f0 *struct {
IP int
X0 *Box
X1 int
} = coroutine.Push[struct {
} = &struct {
IP int
X0 *Box
X1 int
}](&_c.Stack)
if _f1.IP == 0 {
*_f1 = struct {
}{X0: _fn0, X1: _fn1}
return func(_fn0 int) {
_c := coroutine.LoadContext[int, any]()
var _f1 *struct {
IP int
X0 *Box
X1 int
}{X0: _fn0, X1: _fn1}
}
defer func() {
if !_c.Unwinding() {
coroutine.Pop(&_c.Stack)
}
}()
switch {
case _f1.IP < 2:

coroutine.Yield[int, any](-1)
_f1.IP = 2
fallthrough
case _f1.IP < 3:

return func(_fn0 int) {
_c := coroutine.LoadContext[int, any]()
var _f0 *struct {
IP int
X0 int
} = coroutine.Push[struct {
X0 int
} = coroutine.Push[struct {
IP int
X0 int
}](&_c.Stack)
if _f1.IP == 0 {
*_f1 = struct {
IP int
X0 int
}](&_c.Stack)
if _f0.IP == 0 {
*_f0 = struct {
IP int
X0 int
}{X0: _fn0}
}
defer func() {
if !_c.Unwinding() {
coroutine.Pop(&_c.Stack)
}
}()
switch {
case _f0.IP < 2:
coroutine.Yield[int, any](_f1.X0.x)
_f0.IP = 2
fallthrough
case _f0.IP < 3:
coroutine.Yield[int, any](_f1.X1)
_f0.IP = 3
fallthrough
case _f0.IP < 4:
coroutine.Yield[int, any](_f0.X0)
_f0.IP = 4
fallthrough
case _f0.IP < 5:
_f1.X0.
x++
_f0.IP = 5
fallthrough
case _f0.IP < 6:
_f1.X1++
_f0.IP = 6
fallthrough
case _f0.IP < 7:
_f0.X0++
}{X0: _fn0}
}
defer func() {
if !_c.Unwinding() {
coroutine.Pop(&_c.Stack)
}
}()
switch {
case _f1.IP < 2:
coroutine.Yield[int, any](_f0.X0.x)
_f1.IP = 2
fallthrough
case _f1.IP < 3:
coroutine.Yield[int, any](_f0.X1)
_f1.IP = 3
fallthrough
case _f1.IP < 4:
coroutine.Yield[int, any](_f1.X0)
_f1.IP = 4
fallthrough
case _f1.IP < 5:
_f0.X0.
x++
_f1.IP = 5
fallthrough
case _f1.IP < 6:
_f0.X1++
_f1.IP = 6
fallthrough
case _f1.IP < 7:
_f1.X0++
}
}
panic("unreachable")
}

//go:noinline
Expand Down Expand Up @@ -3429,9 +3406,7 @@ func buildClosure[T any](_fn0 T) (_ func()) {
IP int
X0 T
}{X0: _fn0}
return func() {
coroutine.Yield[T, any](_f0.X0)
}
return func() { coroutine.Yield[T, any](_f0.X0) }
}

//go:noinline
Expand All @@ -3450,80 +3425,57 @@ func (i *IdentityGenericStruct[T]) Run() { coroutine.Yield[T, any](i.n) }

//go:noinline
func (_fn0 *IdentityGenericStruct[T]) Closure(_fn1 T) (_ func(T)) {
_c := coroutine.LoadContext[int, any]()
var _f1 *struct {
var _f0 *struct {
IP int
X0 *IdentityGenericStruct[T]
X1 T
} = coroutine.Push[struct {
} = &struct {
IP int
X0 *IdentityGenericStruct[T]
X1 T
}](&_c.Stack)
if _f1.IP == 0 {
*_f1 = struct {
}{X0: _fn0, X1: _fn1}
return func(_fn0 T) {
_c := coroutine.LoadContext[int, any]()
var _f1 *struct {
IP int
X0 *IdentityGenericStruct[T]
X1 T
}{X0: _fn0, X1: _fn1}
}
defer func() {
if !_c.Unwinding() {
coroutine.Pop(&_c.Stack)
}
}()
switch {
case _f1.IP < 2:

coroutine.Yield[T, any](-1)
_f1.IP = 2
fallthrough
case _f1.IP < 3:

return func(_fn0 T) {
_c := coroutine.LoadContext[int, any]()
var _f0 *struct {
IP int
X0 T
} = coroutine.Push[struct {
X0 T
} = coroutine.Push[struct {
IP int
X0 T
}](&_c.Stack)
if _f1.IP == 0 {
*_f1 = struct {
IP int
X0 T
}](&_c.Stack)
if _f0.IP == 0 {
*_f0 = struct {
IP int
X0 T
}{X0: _fn0}
}
defer func() {
if !_c.Unwinding() {
coroutine.Pop(&_c.Stack)
}
}()
switch {
case _f0.IP < 2:
coroutine.Yield[T, any](_f1.X0.n)
_f0.IP = 2
fallthrough
case _f0.IP < 3:
_f1.X0.
n++
_f0.IP = 3
fallthrough
case _f0.IP < 4:
coroutine.Yield[T, any](_f1.X1)
_f0.IP = 4
fallthrough
case _f0.IP < 5:
_f1.X1++
_f0.IP = 5
fallthrough
case _f0.IP < 6:
coroutine.Yield[T, any](_f0.X0)
}{X0: _fn0}
}
defer func() {
if !_c.Unwinding() {
coroutine.Pop(&_c.Stack)
}
}()
switch {
case _f1.IP < 2:
coroutine.Yield[T, any](_f0.X0.n)
_f1.IP = 2
fallthrough
case _f1.IP < 3:
_f0.X0.
n++
_f1.IP = 3
fallthrough
case _f1.IP < 4:
coroutine.Yield[T, any](_f0.X1)
_f1.IP = 4
fallthrough
case _f1.IP < 5:
_f0.X1++
_f1.IP = 5
fallthrough
case _f1.IP < 6:
coroutine.Yield[T, any](_f1.X0)
}
}
panic("unreachable")
}

//go:noinline
Expand Down Expand Up @@ -3575,7 +3527,7 @@ func init() {
X0 *Box
X1 int
}
}]("github.com/stealthrocket/coroutine/compiler/testdata.(*Box).Closure.func2")
}]("github.com/stealthrocket/coroutine/compiler/testdata.(*Box).Closure.func1")
_types.RegisterFunc[func(_fn1 int) (_ func(int))]("github.com/stealthrocket/coroutine/compiler/testdata.(*IdentityGenericStruct[go.shape.int]).Closure")
_types.RegisterClosure[func(_fn0 int), struct {
F uintptr
Expand All @@ -3585,7 +3537,7 @@ func init() {
X1 int
}
D uintptr
}]("github.com/stealthrocket/coroutine/compiler/testdata.(*IdentityGenericStruct[go.shape.int]).Closure.func2")
}]("github.com/stealthrocket/coroutine/compiler/testdata.(*IdentityGenericStruct[go.shape.int]).Closure.func1")
_types.RegisterFunc[func()]("github.com/stealthrocket/coroutine/compiler/testdata.(*IdentityGenericStruct[go.shape.int]).Run")
_types.RegisterFunc[func(_fn1 int)]("github.com/stealthrocket/coroutine/compiler/testdata.(*MethodGeneratorState).MethodGenerator")
_types.RegisterFunc[func(n int)]("github.com/stealthrocket/coroutine/compiler/testdata.Double")
Expand Down

0 comments on commit 122c6a2

Please sign in to comment.