Skip to content

Commit

Permalink
decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Aug 1, 2024
1 parent 4158fff commit 264b8bc
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 26 deletions.
28 changes: 24 additions & 4 deletions internal/decoder/jitdec/assembler_regabi_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -972,11 +972,13 @@ var (

var (
_F_decodeJsonUnmarshaler obj.Addr
_F_decodeJsonUnmarshalerQuoted obj.Addr
_F_decodeTextUnmarshaler obj.Addr
)

func init() {
_F_decodeJsonUnmarshaler = jit.Func(decodeJsonUnmarshaler)
_F_decodeJsonUnmarshalerQuoted = jit.Func(decodeJsonUnmarshalerQuoted)
_F_decodeTextUnmarshaler = jit.Func(decodeTextUnmarshaler)
}

Expand Down Expand Up @@ -1061,14 +1063,15 @@ var (
_F_skip_number = jit.Imm(int64(native.S_skip_number))
)

func (self *_Assembler) unmarshal_json(t reflect.Type, deref bool) {
func (self *_Assembler) unmarshal_json(t reflect.Type, deref bool, f obj.Addr) {
self.call_sf(_F_skip_one) // CALL_SF skip_one
self.Emit("TESTQ", _AX, _AX) // TESTQ AX, AX
self.Sjmp("JS" , _LB_parsing_error_v) // JS _parse_error_v
self.Emit("MOVQ", _IC, _VAR_ic) // store for mismatche error skip
self.slice_from_r(_AX, 0) // SLICE_R AX, $0
self.Emit("MOVQ" , _DI, _ARG_sv_p) // MOVQ DI, sv.p
self.Emit("MOVQ" , _SI, _ARG_sv_n) // MOVQ SI, sv.n
self.unmarshal_func(t, _F_decodeJsonUnmarshaler, deref) // UNMARSHAL json, ${t}, ${deref}
self.unmarshal_func(t, f, deref) // UNMARSHAL json, ${t}, ${deref}
}

func (self *_Assembler) unmarshal_text(t reflect.Type, deref bool) {
Expand Down Expand Up @@ -1102,6 +1105,13 @@ func (self *_Assembler) unmarshal_func(t reflect.Type, fn obj.Addr, deref bool)
self.Emit("MOVQ" , _ARG_sv_p, _CX) // MOVQ sv.p, CX
self.Emit("MOVQ" , _ARG_sv_n, _DI) // MOVQ sv.n, DI
self.call_go(fn) // CALL_GO ${fn}
self.Emit("CMPQ", _ET, _T_miserr) // check if MismatchedError
self.Sjmp("JNE" , "_check_error_{n}")
self.Emit("MOVQ", jit.Type(t), _CX) // store current type
self.Emit("MOVQ", _CX, _VAR_et) // store current type
self.Emit("MOVQ", _VAR_ic, _IC) // recover the pos
self.Emit("XORL", _ET, _ET)
self.Link("_check_error_{n}")
self.Emit("TESTQ", _ET, _ET) // TESTQ ET, ET
self.Sjmp("JNZ" , _LB_error) // JNZ _error
}
Expand Down Expand Up @@ -1774,11 +1784,21 @@ func (self *_Assembler) _asm_OP_struct_field(p *_Instr) {
}

func (self *_Assembler) _asm_OP_unmarshal(p *_Instr) {
self.unmarshal_json(p.vt(), true)
if iv := p.i64(); iv != 0 {
println("unmarshal quoted")
self.unmarshal_json(p.vt(), true, _F_decodeJsonUnmarshalerQuoted)
} else {
self.unmarshal_json(p.vt(), true, _F_decodeJsonUnmarshaler)
}
}

func (self *_Assembler) _asm_OP_unmarshal_p(p *_Instr) {
self.unmarshal_json(p.vt(), false)
if iv := p.i64(); iv != 0 {
println("unmarshal pointer quoted")
self.unmarshal_json(p.vt(), false, _F_decodeJsonUnmarshalerQuoted)
} else {
self.unmarshal_json(p.vt(), false, _F_decodeJsonUnmarshaler)
}
}

func (self *_Assembler) _asm_OP_unmarshal_text(p *_Instr) {
Expand Down
43 changes: 30 additions & 13 deletions internal/decoder/jitdec/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,13 @@ func newInsVt(op _Op, vt reflect.Type) _Instr {
}
}

func newInsVtI(op _Op, vt reflect.Type, iv int) _Instr {
return _Instr {
u: packOp(op) | rt.PackInt(iv),
p: unsafe.Pointer(rt.UnpackType(vt)),
}
}

func newInsVf(op _Op, vf *caching.FieldMap) _Instr {
return _Instr {
u: packOp(op),
Expand Down Expand Up @@ -452,6 +459,10 @@ func (self *_Program) rtt(op _Op, vt reflect.Type) {
*self = append(*self, newInsVt(op, vt))
}

func (self *_Program) rtti(op _Op, vt reflect.Type, iv int) {
*self = append(*self, newInsVtI(op, vt, iv))
}

func (self *_Program) fmv(op _Op, vf *caching.FieldMap) {
*self = append(*self, newInsVf(op, vf))
}
Expand Down Expand Up @@ -527,33 +538,33 @@ func (self *_Compiler) compile(vt reflect.Type) (ret _Program, err error) {
return
}

func (self *_Compiler) checkMarshaler(p *_Program, vt reflect.Type) bool {
func (self *_Compiler) checkMarshaler(p *_Program, vt reflect.Type, iv int) bool {
pt := reflect.PtrTo(vt)

/* check for `json.Unmarshaler` with pointer receiver */
if pt.Implements(jsonUnmarshalerType) {
p.rtt(_OP_unmarshal_p, pt)
p.rtti(_OP_unmarshal_p, pt, iv)
return true
}

/* check for `json.Unmarshaler` */
if vt.Implements(jsonUnmarshalerType) {
p.add(_OP_lspace)
self.compileUnmarshalJson(p, vt)
self.compileUnmarshalJson(p, vt, iv)
return true
}

/* check for `encoding.TextMarshaler` with pointer receiver */
if pt.Implements(encodingTextUnmarshalerType) {
p.add(_OP_lspace)
self.compileUnmarshalTextPtr(p, pt)
self.compileUnmarshalTextPtr(p, pt, iv)
return true
}

/* check for `encoding.TextUnmarshaler` */
if vt.Implements(encodingTextUnmarshalerType) {
p.add(_OP_lspace)
self.compileUnmarshalText(p, vt)
self.compileUnmarshalText(p, vt, iv)
return true
}
return false
Expand All @@ -567,7 +578,7 @@ func (self *_Compiler) compileOne(p *_Program, sp int, vt reflect.Type) {
return
}

if self.checkMarshaler(p, vt) {
if self.checkMarshaler(p, vt, 0) {
return
}

Expand Down Expand Up @@ -690,7 +701,7 @@ func (self *_Compiler) compilePtr(p *_Program, sp int, et reflect.Type) {

/* dereference all the way down */
for et.Kind() == reflect.Ptr {
if self.checkMarshaler(p, et) {
if self.checkMarshaler(p, et, 0) {
return
}
et = et.Elem()
Expand Down Expand Up @@ -939,6 +950,12 @@ end_of_object:
}

func (self *_Compiler) compileStructFieldStr(p *_Program, sp int, vt reflect.Type) {
// according to std, Unmarshaler should be called before stringize
// see https://github.com/bytedance/sonic/issues/670
if self.checkMarshaler(p, vt, 1) {
return
}

n1 := -1
ft := vt
sv := false
Expand Down Expand Up @@ -1106,7 +1123,7 @@ func (self *_Compiler) compileUnmarshalEnd(p *_Program, vt reflect.Type, i int)
p.pin(j)
}

func (self *_Compiler) compileUnmarshalJson(p *_Program, vt reflect.Type) {
func (self *_Compiler) compileUnmarshalJson(p *_Program, vt reflect.Type, iv int) {
i := p.pc()
v := _OP_unmarshal
p.add(_OP_is_null)
Expand All @@ -1117,11 +1134,11 @@ func (self *_Compiler) compileUnmarshalJson(p *_Program, vt reflect.Type) {
}

/* call the unmarshaler */
p.rtt(v, vt)
p.rtti(v, vt, iv)
self.compileUnmarshalEnd(p, vt, i)
}

func (self *_Compiler) compileUnmarshalText(p *_Program, vt reflect.Type) {
func (self *_Compiler) compileUnmarshalText(p *_Program, vt reflect.Type, iv int) {
i := p.pc()
v := _OP_unmarshal_text
p.add(_OP_is_null)
Expand All @@ -1134,15 +1151,15 @@ func (self *_Compiler) compileUnmarshalText(p *_Program, vt reflect.Type) {
}

/* call the unmarshaler */
p.rtt(v, vt)
p.rtti(v, vt, iv)
self.compileUnmarshalEnd(p, vt, i)
}

func (self *_Compiler) compileUnmarshalTextPtr(p *_Program, vt reflect.Type) {
func (self *_Compiler) compileUnmarshalTextPtr(p *_Program, vt reflect.Type, iv int) {
i := p.pc()
p.add(_OP_is_null)
p.chr(_OP_match_char, '"')
p.rtt(_OP_unmarshal_text_p, vt)
p.rtti(_OP_unmarshal_text_p, vt, iv)
p.pin(i)
}

Expand Down
1 change: 1 addition & 0 deletions internal/decoder/jitdec/generic_regabi_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ var (
_T_slice = jit.Type(reflect.TypeOf(([]interface{})(nil)))
_T_string = jit.Type(reflect.TypeOf(""))
_T_number = jit.Type(reflect.TypeOf(json.Number("")))
_T_miserr = jit.Type(reflect.TypeOf(&MismatchTypeError{}))
_T_float64 = jit.Type(reflect.TypeOf(float64(0)))
)

Expand Down
7 changes: 7 additions & 0 deletions internal/decoder/jitdec/primitives.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ func decodeJsonUnmarshaler(vv interface{}, s string) error {
return vv.(json.Unmarshaler).UnmarshalJSON(rt.Str2Mem(s))
}

func decodeJsonUnmarshalerQuoted(vv interface{}, s string) error {
if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' {
return &MismatchTypeError{}
}
return vv.(json.Unmarshaler).UnmarshalJSON(rt.Str2Mem(s[1:len(s)-1]))
}

func decodeTextUnmarshaler(vv interface{}, s string) error {
return vv.(encoding.TextUnmarshaler).UnmarshalText(rt.Str2Mem(s))
}
19 changes: 10 additions & 9 deletions issue_test/xx/xx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ import (
)

func TestX(t *testing.T) {
// var obj = Issue670Case{ D: Date(time.Now().Unix()) }
// so, _ := sonic.MarshalString(obj)
// eo, _ := json.Marshal(obj)
// assert.Equal(t, string(eo), so)
// println(string(eo))
var obj = Issue670Case{ D: Date(time.Now().Unix()) }
so, _ := sonic.MarshalString(obj)
eo, _ := json.Marshal(obj)
assert.Equal(t, string(eo), so)
println(string(eo))

// match
eo := []byte(`{"D":"2021-08-26","E":1}`)
eo = []byte(`{"D":"2021-08-26","E":1}`)
testUnmarshal(t, eo)

// // mismatch
// eo = []byte(`{"D":11,"E":1}`)
// testUnmarshal(t, eo)
// mismatch
eo = []byte(`{"D":11,"E":1}`)
testUnmarshal(t, eo)

// // null
// eo = []byte(`{"D":null,"E":1}`)
Expand All @@ -55,6 +55,7 @@ func testUnmarshal(t *testing.T, eo []byte) {
assert.Equal(t, ee ==nil, es == nil, es)
assert.Equal(t, obj2, obj)
fmt.Printf("error: %v, obj: %#v", ee, obj2)
fmt.Printf("sonic error: %v, obj: %#v", es, obj)
}

type Issue670Case struct {
Expand Down

0 comments on commit 264b8bc

Please sign in to comment.