forked from mirror/go-json
Refactor errUnsupportedValue
This commit is contained in:
parent
2e7a990bbb
commit
acee1ef8ba
20
encode_vm.go
20
encode_vm.go
|
@ -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),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue