Skip to content

Commit

Permalink
Test reflect.Type serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Jun 17, 2024
1 parent 0f722c3 commit bac9b47
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 2 deletions.
10 changes: 10 additions & 0 deletions compiler/coroutine_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package compiler

import (
"math"
"reflect"
"slices"
"testing"

Expand Down Expand Up @@ -260,6 +262,14 @@ func TestCoroutineYield(t *testing.T) {
coro: func() { RangeOverInt(3) },
yields: []int{0, 1, 2},
},

{
name: "reflect type",
coro: func() {
ReflectType(reflect.TypeFor[uint8](), reflect.TypeFor[uint16]())
},
yields: []int{math.MaxUint8, math.MaxUint16},
},
}

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

import (
"math"
"reflect"
"time"
"unsafe"

Expand Down Expand Up @@ -690,3 +692,14 @@ func RangeOverInt(n int) {
coroutine.Yield[int, any](i)
}
}

func ReflectType(types ...reflect.Type) {
for _, t := range types {
v := reflect.New(t).Elem()
if !v.CanUint() {
panic("expected uint type")
}
v.SetUint(math.MaxUint64)
coroutine.Yield[int, any](int(v.Uint()))
}
}
119 changes: 118 additions & 1 deletion compiler/testdata/coroutine_durable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package testdata

import (
coroutine "github.com/dispatchrun/coroutine"
math "math"
reflect "reflect"
time "time"
unsafe "unsafe"
)
Expand Down Expand Up @@ -3514,7 +3516,7 @@ func IdentityGenericInt(n int) { IdentityGeneric[int](n) }

//go:noinline
func IdentityGenericClosure[T any](_fn0 T) {
_c := coroutine.LoadContext[T, any]()
_c := coroutine.LoadContext[int, any]()
var _f0 *struct {
IP int
X0 T
Expand Down Expand Up @@ -3802,6 +3804,120 @@ func RangeOverInt(_fn0 int) {
}
}
}

//go:noinline
func ReflectType(_fn0 ...reflect.Type) {
_c := coroutine.LoadContext[int, any]()
var _f0 *struct {
IP int
X0 []reflect.Type
X1 []reflect.Type
X2 int
X3 reflect.Type
X4 reflect.Value
X5 reflect.Value
X6 bool
X7 bool
X8 uint64
X9 int
} = coroutine.Push[struct {
IP int
X0 []reflect.Type
X1 []reflect.Type
X2 int
X3 reflect.Type
X4 reflect.Value
X5 reflect.Value
X6 bool
X7 bool
X8 uint64
X9 int
}](&_c.Stack)
if _f0.IP == 0 {
*_f0 = struct {
IP int
X0 []reflect.Type
X1 []reflect.Type
X2 int
X3 reflect.Type
X4 reflect.Value
X5 reflect.Value
X6 bool
X7 bool
X8 uint64
X9 int
}{X0: _fn0}
}
defer func() {
if !_c.Unwinding() {
coroutine.Pop(&_c.Stack)
}
}()
switch {
case _f0.IP < 2:
_f0.X1 = _f0.X0
_f0.IP = 2
fallthrough
case _f0.IP < 13:
switch {
case _f0.IP < 3:
_f0.X2 = 0
_f0.IP = 3
fallthrough
case _f0.IP < 13:
for ; _f0.X2 < len(_f0.X1); _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:
_f0.X4 = reflect.New(_f0.X3)
_f0.IP = 5
fallthrough
case _f0.IP < 6:
_f0.X5 = _f0.X4.Elem()
_f0.IP = 6
fallthrough
case _f0.IP < 9:
switch {
case _f0.IP < 7:
_f0.X6 = _f0.X5.
CanUint()
_f0.IP = 7
fallthrough
case _f0.IP < 8:
_f0.X7 = !_f0.X6
_f0.IP = 8
fallthrough
case _f0.IP < 9:
if _f0.X7 {
panic("expected uint type")
}
}
_f0.IP = 9
fallthrough
case _f0.IP < 10:
_f0.X5.
SetUint(math.MaxUint64)
_f0.IP = 10
fallthrough
case _f0.IP < 11:
_f0.X8 = _f0.X5.
Uint()
_f0.IP = 11
fallthrough
case _f0.IP < 12:
_f0.X9 = int(_f0.X8)
_f0.IP = 12
fallthrough
case _f0.IP < 13:
coroutine.Yield[int, any](_f0.X9)
}
}
}
}
}
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 @@ -3937,6 +4053,7 @@ func init() {
_types.RegisterFunc[func(_fn0 int)]("github.com/dispatchrun/coroutine/compiler/testdata.RangeTripleFuncValue")
_types.RegisterFunc[func(i int)]("github.com/dispatchrun/coroutine/compiler/testdata.RangeTripleFuncValue.func2")
_types.RegisterFunc[func(_fn0 int)]("github.com/dispatchrun/coroutine/compiler/testdata.RangeYieldAndDeferAssign")
_types.RegisterFunc[func(_fn0 ...reflect.Type)]("github.com/dispatchrun/coroutine/compiler/testdata.ReflectType")
_types.RegisterFunc[func() (_fn0 int)]("github.com/dispatchrun/coroutine/compiler/testdata.ReturnNamedValue")
_types.RegisterFunc[func(_fn0 int)]("github.com/dispatchrun/coroutine/compiler/testdata.Select")
_types.RegisterFunc[func(_ int)]("github.com/dispatchrun/coroutine/compiler/testdata.Shadowing")
Expand Down
2 changes: 1 addition & 1 deletion types/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func serializeAny(s *Serializer, t reflect.Type, p unsafe.Pointer) {

switch t {
case reflectTypeType:
serializeType(s, t)
serializeType(s, *(*reflect.Type)(p))
return
case reflectValueType:
v := *(*reflect.Value)(p)
Expand Down

0 comments on commit bac9b47

Please sign in to comment.