forked from mirror/go-json
Remove unnecessary code
This commit is contained in:
parent
88cd2ddd6e
commit
9aa2047766
|
@ -58,11 +58,8 @@ func encodeCompileHead(ctx *encodeCompileContext) (*opcode, error) {
|
|||
switch {
|
||||
case typ.Implements(marshalJSONType):
|
||||
if typ.Kind() != reflect.Ptr || !typ.Elem().Implements(marshalJSONType) {
|
||||
fmt.Println("typ = ", typ)
|
||||
return encodeCompileMarshalJSON(ctx)
|
||||
}
|
||||
// case rtype_ptrTo(typ).Implements(marshalJSONType):
|
||||
// return encodeCompileMarshalJSONPtr(ctx)
|
||||
case typ.Implements(marshalTextType):
|
||||
return encodeCompileMarshalText(ctx)
|
||||
case rtype_ptrTo(typ).Implements(marshalTextType):
|
||||
|
@ -86,8 +83,6 @@ func encodeCompileHead(ctx *encodeCompileContext) (*opcode, error) {
|
|||
return code, nil
|
||||
} else if isPtr && typ.Implements(marshalTextType) {
|
||||
typ = orgType
|
||||
// } else if isPtr && typ.Implements(marshalJSONType) {
|
||||
// typ = orgType
|
||||
}
|
||||
code, err := encodeCompile(ctx.withType(typ), isPtr)
|
||||
if err != nil {
|
||||
|
@ -191,10 +186,7 @@ func encodeOptimizeStructEnd(c *opcode) {
|
|||
func encodeImplementsMarshaler(typ *rtype) bool {
|
||||
switch {
|
||||
case typ.Implements(marshalJSONType):
|
||||
fmt.Println("MarshalJSON", typ)
|
||||
return true
|
||||
// case rtype_ptrTo(typ).Implements(marshalJSONType):
|
||||
// return true
|
||||
case typ.Implements(marshalTextType):
|
||||
return true
|
||||
case rtype_ptrTo(typ).Implements(marshalTextType):
|
||||
|
@ -206,13 +198,8 @@ func encodeImplementsMarshaler(typ *rtype) bool {
|
|||
func encodeCompile(ctx *encodeCompileContext, isPtr bool) (*opcode, error) {
|
||||
typ := ctx.typ
|
||||
switch {
|
||||
//case isPtr && typ.Kind() == reflect.Ptr && typ.Implements(marshalJSONType) && !typ.Elem().Implements(marshalJSONType):
|
||||
// *struct{ field *implementedMarshalJSONType }
|
||||
// return encodeCompileMarshalJSONPtr(ctx)
|
||||
case typ.Implements(marshalJSONType) && (typ.Kind() != reflect.Ptr || !typ.Elem().Implements(marshalJSONType)):
|
||||
return encodeCompileMarshalJSON(ctx)
|
||||
// case rtype_ptrTo(typ).Implements(marshalJSONType):
|
||||
// return encodeCompileMarshalJSONPtr(ctx)
|
||||
case typ.Implements(marshalTextType):
|
||||
return encodeCompileMarshalText(ctx)
|
||||
case rtype_ptrTo(typ).Implements(marshalTextType):
|
||||
|
@ -333,14 +320,12 @@ func encodeCompilePtr(ctx *encodeCompileContext) (*opcode, error) {
|
|||
}
|
||||
|
||||
func encodeCompileMarshalJSON(ctx *encodeCompileContext) (*opcode, error) {
|
||||
fmt.Println("encodeCompileMarshalJSON", ctx.typ)
|
||||
code := newOpCode(ctx, opMarshalJSON)
|
||||
ctx.incIndex()
|
||||
return code, nil
|
||||
}
|
||||
|
||||
func encodeCompileMarshalJSONPtr(ctx *encodeCompileContext) (*opcode, error) {
|
||||
fmt.Println("encodeCompileMarshalJSONPtr")
|
||||
code := newOpCode(ctx.withType(rtype_ptrTo(ctx.typ)), opMarshalJSONPtr)
|
||||
ctx.incIndex()
|
||||
return code, nil
|
||||
|
@ -790,8 +775,6 @@ func encodeTypeToHeaderType(ctx *encodeCompileContext, code *opcode) opType {
|
|||
return opStructFieldHeadArray
|
||||
case opSliceHead:
|
||||
return opStructFieldHeadSlice
|
||||
// case opStructFieldHead:
|
||||
// return opStructFieldHeadStruct
|
||||
case opMarshalJSON:
|
||||
return opStructFieldHeadMarshalJSON
|
||||
case opMarshalJSONPtr:
|
||||
|
@ -1251,22 +1234,10 @@ func encodeCompileStruct(ctx *encodeCompileContext, isPtr bool) (*opcode, error)
|
|||
for i, tag := range tags {
|
||||
field := tag.field
|
||||
fieldType := type2rtype(field.Type)
|
||||
/*
|
||||
if isPtr && i == 0 {
|
||||
// head field of pointer structure at top level
|
||||
// if field type is pointer and implements MarshalJSON or MarshalText,
|
||||
// it need to operation of dereference of pointer.
|
||||
if field.Type.Kind() == reflect.Ptr &&
|
||||
(field.Type.Implements(marshalJSONType) || field.Type.Implements(marshalTextType)) {
|
||||
fieldType = rtype_ptrTo(fieldType)
|
||||
}
|
||||
}
|
||||
*/
|
||||
fieldOpcodeIndex := ctx.opcodeIndex
|
||||
fieldPtrIndex := ctx.ptrIndex
|
||||
ctx.incIndex()
|
||||
nilcheck := true //isPtr || indirect //fieldType.Kind() == reflect.Ptr || isPtr && !indirect
|
||||
fmt.Println("default nilcheck = ", nilcheck, "opcodeIndex = ", ctx.opcodeIndex)
|
||||
nilcheck := true
|
||||
var valueCode *opcode
|
||||
if i == 0 && fieldNum == 1 && isPtr && fieldType.Kind() != reflect.Ptr && rtype_ptrTo(fieldType).Implements(marshalJSONType) && !fieldType.Implements(marshalJSONType) {
|
||||
// *struct{ field implementedMarshalJSONType } => struct { field *implementedMarshalJSONType }
|
||||
|
@ -1291,7 +1262,6 @@ func encodeCompileStruct(ctx *encodeCompileContext, isPtr bool) (*opcode, error)
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//nilcheck = false
|
||||
valueCode = code
|
||||
} else if fieldType.Implements(marshalJSONType) && fieldType.Kind() == reflect.Ptr && !fieldType.Elem().Implements(marshalJSONType) {
|
||||
code, err := encodeCompileMarshalJSON(ctx.withType(fieldType))
|
||||
|
|
63
encode_vm.go
63
encode_vm.go
|
@ -75,7 +75,7 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
ptrOffset := uintptr(0)
|
||||
ctxptr := ctx.ptr()
|
||||
code := codeSet.code
|
||||
fmt.Println(code.dump())
|
||||
//fmt.Println(code.dump())
|
||||
|
||||
for {
|
||||
switch code.op {
|
||||
|
@ -3242,10 +3242,7 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
p = ptrToPtr(p + code.offset)
|
||||
}
|
||||
}
|
||||
fmt.Println("p = ", p, "nilcheck = ", code.nilcheck, "indirect = ", code.indirect)
|
||||
//fmt.Println("type = ", code.typ, "isNilPointer = ", code.typ.Kind() == reflect.Ptr && reflect.ValueOf(iface).IsNil())
|
||||
//fmt.Println("iface = ", iface, "isNil = ", iface == nil)
|
||||
if code.nilcheck && p == 0 /* || code.nilcheck && code.typ.Kind() == reflect.Ptr && ptrToPtr(p) == 0*/ {
|
||||
if code.nilcheck && p == 0 {
|
||||
b = encodeNull(b)
|
||||
} else {
|
||||
iface := ptrToInterface(code, p)
|
||||
|
@ -3278,12 +3275,12 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
break
|
||||
}
|
||||
b = append(b, '{')
|
||||
if code.typ.Kind() == reflect.Ptr && code.indirect {
|
||||
p = ptrToPtr(p + code.offset)
|
||||
} else if code.op == opStructFieldPtrHeadOmitEmptyMarshalJSON && code.typ.Kind() == reflect.Ptr {
|
||||
p = ptrToPtr(p + code.offset)
|
||||
if code.typ.Kind() == reflect.Ptr {
|
||||
if code.indirect || code.op == opStructFieldPtrHeadOmitEmptyMarshalJSON {
|
||||
p = ptrToPtr(p + code.offset)
|
||||
}
|
||||
}
|
||||
if code.nilcheck && p == 0 {
|
||||
if p == 0 && code.nilcheck {
|
||||
code = code.nextField
|
||||
} else {
|
||||
b = append(b, code.key...)
|
||||
|
@ -3330,7 +3327,6 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
b = encodeComma(b)
|
||||
code = code.next
|
||||
case opStructFieldPtrHeadOmitEmptyMarshalJSONPtr:
|
||||
fmt.Println("type = ", code.typ)
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 {
|
||||
b = encodeNull(b)
|
||||
|
@ -3351,7 +3347,6 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
if code.indirect {
|
||||
p = ptrToPtr(p + code.offset)
|
||||
}
|
||||
fmt.Println("p = ", p)
|
||||
b = append(b, '{')
|
||||
if p == 0 {
|
||||
code = code.nextField
|
||||
|
@ -3387,7 +3382,7 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
p = ptrToPtr(p + code.offset)
|
||||
}
|
||||
}
|
||||
if code.nilcheck && p == 0 {
|
||||
if p == 0 && code.nilcheck {
|
||||
b = encodeNull(b)
|
||||
} else {
|
||||
bb, err := encodeMarshalJSON(b, ptrToInterface(code, p+code.offset))
|
||||
|
@ -4114,7 +4109,10 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
p := load(ctxptr, code.headIdx)
|
||||
b = append(b, code.key...)
|
||||
p += code.offset
|
||||
if code.nilcheck && code.typ.Kind() == reflect.Ptr && p != 0 && ptrToPtr(p) == 0 {
|
||||
if code.typ.Kind() == reflect.Ptr {
|
||||
p = ptrToPtr(p)
|
||||
}
|
||||
if p == 0 && code.nilcheck {
|
||||
b = encodeNull(b)
|
||||
} else {
|
||||
v := ptrToInterface(code, p)
|
||||
|
@ -4127,19 +4125,17 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
b = encodeComma(b)
|
||||
code = code.next
|
||||
case opStructFieldOmitEmptyMarshalJSON:
|
||||
ptr := load(ctxptr, code.headIdx)
|
||||
p := ptr + code.offset
|
||||
if code.nilcheck && code.typ.Kind() == reflect.Ptr && p != 0 && ptrToPtr(p) == 0 {
|
||||
code = code.nextField
|
||||
break
|
||||
p := load(ctxptr, code.headIdx)
|
||||
p += code.offset
|
||||
if code.typ.Kind() == reflect.Ptr {
|
||||
p = ptrToPtr(p)
|
||||
}
|
||||
v := ptrToInterface(code, p)
|
||||
if v == nil {
|
||||
if p == 0 && code.nilcheck {
|
||||
code = code.nextField
|
||||
break
|
||||
}
|
||||
b = append(b, code.key...)
|
||||
bb, err := encodeMarshalJSON(b, v)
|
||||
bb, err := encodeMarshalJSON(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -4148,10 +4144,7 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
case opStructFieldMarshalJSONPtr, opStructFieldStringTagMarshalJSONPtr:
|
||||
p := load(ctxptr, code.headIdx)
|
||||
b = append(b, code.key...)
|
||||
p += code.offset
|
||||
if p != 0 {
|
||||
p = ptrToPtr(p)
|
||||
}
|
||||
p = ptrToPtr(p + code.offset)
|
||||
if p == 0 {
|
||||
b = encodeNull(b)
|
||||
} else {
|
||||
|
@ -4165,24 +4158,14 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
code = code.next
|
||||
case opStructFieldOmitEmptyMarshalJSONPtr:
|
||||
p := load(ctxptr, code.headIdx)
|
||||
p += code.offset
|
||||
p = ptrToPtr(p + code.offset)
|
||||
if p != 0 {
|
||||
p = ptrToPtr(p)
|
||||
}
|
||||
v := ptrToInterface(code, p)
|
||||
if v != nil && p != 0 {
|
||||
bb, err := v.(Marshaler).MarshalJSON()
|
||||
if err != nil {
|
||||
return nil, errMarshaler(code, err)
|
||||
}
|
||||
b = append(b, code.key...)
|
||||
buf := bytes.NewBuffer(b)
|
||||
//TODO: we should validate buffer with `compact`
|
||||
if err := compact(buf, bb, false); err != nil {
|
||||
bb, err := encodeMarshalJSON(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b = buf.Bytes()
|
||||
b = encodeComma(b)
|
||||
b = encodeComma(bb)
|
||||
}
|
||||
code = code.next
|
||||
case opStructFieldMarshalText:
|
||||
|
|
Loading…
Reference in New Issue