Refactor errUnsupportedValue

This commit is contained in:
Masaaki Goshima 2020-09-15 20:48:02 +09:00
parent 2e7a990bbb
commit acee1ef8ba
1 changed files with 12 additions and 8 deletions

View File

@ -21,6 +21,17 @@ func store(base uintptr, idx uintptr, p uintptr) {
*(*uintptr)(unsafe.Pointer(base + idx)) = p *(*uintptr)(unsafe.Pointer(base + idx)) = p
} }
func errUnsupportedValue(code *opcode, ptr uintptr) *UnsupportedValueError {
v := *(*interface{})(unsafe.Pointer(&interfaceHeader{
typ: code.typ,
ptr: unsafe.Pointer(ptr),
}))
return &UnsupportedValueError{
Value: reflect.ValueOf(v),
Str: fmt.Sprintf("encountered a cycle via %s", code.typ),
}
}
func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
recursiveLevel := 0 recursiveLevel := 0
seenPtr := map[uintptr]struct{}{} seenPtr := map[uintptr]struct{}{}
@ -613,14 +624,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
if ptr != 0 { if ptr != 0 {
if recursiveLevel > startDetectingCyclesAfter { if recursiveLevel > startDetectingCyclesAfter {
if _, exists := seenPtr[ptr]; exists { if _, exists := seenPtr[ptr]; exists {
v := *(*interface{})(unsafe.Pointer(&interfaceHeader{ return errUnsupportedValue(code, ptr)
typ: code.typ,
ptr: unsafe.Pointer(ptr),
}))
return &UnsupportedValueError{
Value: reflect.ValueOf(v),
Str: fmt.Sprintf("encountered a cycle via %s", code.typ),
}
} }
} }
} }