forked from apache/dubbo-go-hessian2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ref.go
163 lines (139 loc) · 3.69 KB
/
ref.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2019 Wongoo, Yincheng Fang
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hessian
import (
"reflect"
"unsafe"
)
import (
perrors "github.com/pkg/errors"
)
// used to ref object,list,map
type _refElem struct {
// record the kind of target, objects are the same only if the address and kind are the same
kind reflect.Kind
// ref index
index int
}
// _refHolder is used to record decode list, the address of which may change when appending more element.
type _refHolder struct {
// destinations
destinations []reflect.Value
value reflect.Value
}
var _refHolderType = reflect.TypeOf(_refHolder{})
// change ref value
func (h *_refHolder) change(v reflect.Value) {
if h.value.CanAddr() && v.CanAddr() && h.value.Pointer() == v.Pointer() {
return
}
h.value = v
}
// notice all destinations ref to the value
func (h *_refHolder) notify() {
for _, dest := range h.destinations {
SetValue(dest, h.value)
}
}
// add destination
func (h *_refHolder) add(dest reflect.Value) {
h.destinations = append(h.destinations, dest)
}
// Add reference
func (d *Decoder) appendRefs(v interface{}) *_refHolder {
var holder *_refHolder
vv := EnsurePackValue(v)
// only slice and array need ref holder , for its address changes when decoding
if vv.Kind() == reflect.Slice || vv.Kind() == reflect.Array {
holder = &_refHolder{
value: vv,
}
// pack holder value
v = reflect.ValueOf(holder)
}
d.refs = append(d.refs, v)
return holder
}
//encRef encode ref index
func encRef(b []byte, index int) []byte {
return encInt32(append(b, BC_REF), int32(index))
}
// return the order number of ref object if found ,
// otherwise, add the object into the encode ref map
func (e *Encoder) checkRefMap(v reflect.Value) (int, bool) {
var (
kind reflect.Kind
addr unsafe.Pointer
)
if v.Kind() == reflect.Ptr {
for v.Elem().Kind() == reflect.Ptr {
v = v.Elem()
}
kind = v.Elem().Kind()
if kind == reflect.Slice || kind == reflect.Map {
addr = unsafe.Pointer(v.Elem().Pointer())
} else {
addr = unsafe.Pointer(v.Pointer())
}
} else {
kind = v.Kind()
switch kind {
case reflect.Slice, reflect.Map:
addr = unsafe.Pointer(v.Pointer())
default:
addr = unsafe.Pointer(PackPtr(v).Pointer())
}
}
if elem, ok := e.refMap[addr]; ok {
// the array addr is equal to the first elem, which must ignore
if elem.kind == kind {
return elem.index, ok
}
return 0, false
}
n := len(e.refMap)
e.refMap[addr] = _refElem{kind, n}
return 0, false
}
/////////////////////////////////////////
// Ref
/////////////////////////////////////////
// # value reference (e.g. circular trees and graphs)
// ref ::= x51 int # reference to nth map/list/object
func (d *Decoder) decRef(flag int32) (interface{}, error) {
var (
err error
tag byte
i int32
)
if flag != TAG_READ {
tag = byte(flag)
} else {
tag, _ = d.readByte()
}
switch {
case tag == BC_REF:
i, err = d.decInt32(TAG_READ)
if err != nil {
return nil, err
}
if len(d.refs) <= int(i) {
return nil, ErrIllegalRefIndex
}
// return the exact ref object, which maybe a _refHolder
return d.refs[i], nil
default:
return nil, perrors.Errorf("decRef illegal ref type tag:%+v", tag)
}
}