blob: 13941716ce3a2591df867f64b888266ef080e520 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001package reflect2
2
3import (
4 "reflect"
5 "unsafe"
6)
7
8type unsafeType struct {
9 safeType
10 rtype unsafe.Pointer
11 ptrRType unsafe.Pointer
12}
13
14func newUnsafeType(cfg *frozenConfig, type1 reflect.Type) *unsafeType {
15 return &unsafeType{
16 safeType: safeType{
17 Type: type1,
18 cfg: cfg,
19 },
20 rtype: unpackEFace(type1).data,
21 ptrRType: unpackEFace(reflect.PtrTo(type1)).data,
22 }
23}
24
25func (type2 *unsafeType) Set(obj interface{}, val interface{}) {
26 objEFace := unpackEFace(obj)
27 assertType("Type.Set argument 1", type2.ptrRType, objEFace.rtype)
28 valEFace := unpackEFace(val)
29 assertType("Type.Set argument 2", type2.ptrRType, valEFace.rtype)
30 type2.UnsafeSet(objEFace.data, valEFace.data)
31}
32
33func (type2 *unsafeType) UnsafeSet(ptr unsafe.Pointer, val unsafe.Pointer) {
34 typedmemmove(type2.rtype, ptr, val)
35}
36
37func (type2 *unsafeType) IsNil(obj interface{}) bool {
38 objEFace := unpackEFace(obj)
39 assertType("Type.IsNil argument 1", type2.ptrRType, objEFace.rtype)
40 return type2.UnsafeIsNil(objEFace.data)
41}
42
43func (type2 *unsafeType) UnsafeIsNil(ptr unsafe.Pointer) bool {
44 return ptr == nil
45}
46
47func (type2 *unsafeType) UnsafeNew() unsafe.Pointer {
48 return unsafe_New(type2.rtype)
49}
50
51func (type2 *unsafeType) New() interface{} {
52 return packEFace(type2.ptrRType, type2.UnsafeNew())
53}
54
55func (type2 *unsafeType) PackEFace(ptr unsafe.Pointer) interface{} {
56 return packEFace(type2.ptrRType, ptr)
57}
58
59func (type2 *unsafeType) RType() uintptr {
60 return uintptr(type2.rtype)
61}
62
63func (type2 *unsafeType) Indirect(obj interface{}) interface{} {
64 objEFace := unpackEFace(obj)
65 assertType("Type.Indirect argument 1", type2.ptrRType, objEFace.rtype)
66 return type2.UnsafeIndirect(objEFace.data)
67}
68
69func (type2 *unsafeType) UnsafeIndirect(obj unsafe.Pointer) interface{} {
70 return packEFace(type2.rtype, obj)
71}
72
73func (type2 *unsafeType) LikePtr() bool {
74 return false
75}
76
77func assertType(where string, expectRType unsafe.Pointer, actualRType unsafe.Pointer) {
78 if expectRType != actualRType {
79 expectType := reflect.TypeOf(0)
80 (*iface)(unsafe.Pointer(&expectType)).data = expectRType
81 actualType := reflect.TypeOf(0)
82 (*iface)(unsafe.Pointer(&actualType)).data = actualRType
83 panic(where + ": expect " + expectType.String() + ", actual " + actualType.String())
84 }
85}