-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathevaltypeassertexpr.go
More file actions
36 lines (34 loc) · 940 Bytes
/
Copy pathevaltypeassertexpr.go
File metadata and controls
36 lines (34 loc) · 940 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package eval
import (
"reflect"
)
func evalTypeAssertExpr(assert *TypeAssertExpr, env Env) ([]reflect.Value, error) {
x := assert.X
if vs, err := EvalExpr(x, env); err != nil {
return []reflect.Value{}, err
} else {
v := vs[0]
xT := x.KnownType()[0]
aT := assert.KnownType()[0]
if v.IsNil() {
vs := []reflect.Value{hackedNew(aT).Elem(), reflect.ValueOf(false)}
return vs, PanicInterfaceConversion{aT: aT}
}
dynamic := v.Elem()
dT := dynamic.Type()
if aT.Kind() == reflect.Interface {
if !dT.Implements(aT) {
vs := []reflect.Value{hackedNew(aT).Elem(), reflect.ValueOf(false)}
return vs, PanicInterfaceConversion{xT, aT, nil}
}
} else {
if dT != aT {
vs := []reflect.Value{hackedNew(aT).Elem(), reflect.ValueOf(false)}
return vs, PanicInterfaceConversion{xT, aT, dT}
}
}
r := reflect.New(aT).Elem()
r.Set(dynamic)
return []reflect.Value{r, reflect.ValueOf(true)}, nil
}
}