-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
354 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
//go:build !race | ||
// +build !race | ||
|
||
/* | ||
* Copyright 2021 ByteDance Inc. | ||
* | ||
* 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 decoder | ||
|
||
import ( | ||
`unsafe` | ||
`encoding/json` | ||
`reflect` | ||
`runtime` | ||
|
||
`github.com/bytedance/sonic/internal/rt` | ||
`github.com/bytedance/sonic/utf8` | ||
) | ||
|
||
// Decode parses the JSON-encoded data from current position and stores the result | ||
// in the value pointed to by val. | ||
func (self *Decoder) Decode(val interface{}) error { | ||
/* validate json if needed */ | ||
if (self.f & (1 << _F_validate_string)) != 0 && !utf8.ValidateString(self.s){ | ||
dbuf := utf8.CorrectWith(nil, rt.Str2Mem(self.s), "\ufffd") | ||
self.s = rt.Mem2Str(dbuf) | ||
} | ||
|
||
vv := rt.UnpackEface(val) | ||
vp := vv.Value | ||
|
||
/* check for nil type */ | ||
if vv.Type == nil { | ||
return &json.InvalidUnmarshalError{} | ||
} | ||
|
||
/* must be a non-nil pointer */ | ||
if vp == nil || vv.Type.Kind() != reflect.Ptr { | ||
return &json.InvalidUnmarshalError{Type: vv.Type.Pack()} | ||
} | ||
|
||
etp := rt.PtrElem(vv.Type) | ||
|
||
/* check the defined pointer type for issue 379 */ | ||
if vv.Type.IsNamed() { | ||
newp := vp | ||
etp = vv.Type | ||
vp = unsafe.Pointer(&newp) | ||
} | ||
|
||
/* create a new stack, and call the decoder */ | ||
sb := newStack() | ||
nb, err := decodeTypedPointer(self.s, self.i, etp, vp, sb, self.f) | ||
/* return the stack back */ | ||
self.i = nb | ||
freeStack(sb) | ||
|
||
/* avoid GC ahead */ | ||
runtime.KeepAlive(vv) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
//go:build race | ||
// +build race | ||
|
||
/* | ||
* Copyright 2021 ByteDance Inc. | ||
* | ||
* 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 decoder | ||
|
||
import ( | ||
`unsafe` | ||
`encoding/json` | ||
`reflect` | ||
`runtime` | ||
|
||
`github.com/bytedance/sonic/internal/rt` | ||
`github.com/bytedance/sonic/utf8` | ||
) | ||
|
||
func helpDetectDataRace(val interface{}) { | ||
_, _ = json.Marshal(val) | ||
} | ||
|
||
// Decode parses the JSON-encoded data from current position and stores the result | ||
// in the value pointed to by val. | ||
func (self *Decoder) Decode(val interface{}) error { | ||
/* validate json if needed */ | ||
if (self.f & (1 << _F_validate_string)) != 0 && !utf8.ValidateString(self.s){ | ||
dbuf := utf8.CorrectWith(nil, rt.Str2Mem(self.s), "\ufffd") | ||
self.s = rt.Mem2Str(dbuf) | ||
} | ||
|
||
vv := rt.UnpackEface(val) | ||
vp := vv.Value | ||
|
||
/* check for nil type */ | ||
if vv.Type == nil { | ||
return &json.InvalidUnmarshalError{} | ||
} | ||
|
||
/* must be a non-nil pointer */ | ||
if vp == nil || vv.Type.Kind() != reflect.Ptr { | ||
return &json.InvalidUnmarshalError{Type: vv.Type.Pack()} | ||
} | ||
|
||
etp := rt.PtrElem(vv.Type) | ||
|
||
/* check the defined pointer type for issue 379 */ | ||
if vv.Type.IsNamed() { | ||
newp := vp | ||
etp = vv.Type | ||
vp = unsafe.Pointer(&newp) | ||
} | ||
|
||
/* create a new stack, and call the decoder */ | ||
sb := newStack() | ||
nb, err := decodeTypedPointer(self.s, self.i, etp, vp, sb, self.f) | ||
/* return the stack back */ | ||
self.i = nb | ||
freeStack(sb) | ||
|
||
/* avoid GC ahead */ | ||
runtime.KeepAlive(vv) | ||
|
||
/* put last to make the panic from sonic will always be caught at first */ | ||
helpDetectDataRace(val) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//go:build !race | ||
// +build !race | ||
|
||
/* | ||
* Copyright 2021 ByteDance Inc. | ||
* | ||
* 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 encoder | ||
|
||
import ( | ||
`runtime` | ||
|
||
`github.com/bytedance/sonic/internal/rt` | ||
) | ||
|
||
|
||
func encodeInto(buf *[]byte, val interface{}, opts Options) error { | ||
stk := newStack() | ||
efv := rt.UnpackEface(val) | ||
err := encodeTypedPointer(buf, efv.Type, &efv.Value, stk, uint64(opts)) | ||
|
||
/* return the stack into pool */ | ||
if err != nil { | ||
resetStack(stk) | ||
} | ||
freeStack(stk) | ||
|
||
/* avoid GC ahead */ | ||
runtime.KeepAlive(buf) | ||
runtime.KeepAlive(efv) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
|
||
//go:build race | ||
// +build race | ||
|
||
/* | ||
* Copyright 2021 ByteDance Inc. | ||
* | ||
* 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 encoder | ||
|
||
import ( | ||
`encoding/json` | ||
`runtime` | ||
|
||
`github.com/bytedance/sonic/internal/rt` | ||
) | ||
|
||
|
||
func helpDetectDataRace(val interface{}) { | ||
_, _ = json.Marshal(val) | ||
} | ||
|
||
func encodeInto(buf *[]byte, val interface{}, opts Options) error { | ||
|
||
|
||
stk := newStack() | ||
efv := rt.UnpackEface(val) | ||
err := encodeTypedPointer(buf, efv.Type, &efv.Value, stk, uint64(opts)) | ||
|
||
/* return the stack into pool */ | ||
if err != nil { | ||
resetStack(stk) | ||
} | ||
freeStack(stk) | ||
|
||
/* avoid GC ahead */ | ||
runtime.KeepAlive(buf) | ||
runtime.KeepAlive(efv) | ||
|
||
/* put last to make the panic from sonic will always be caught at first */ | ||
helpDetectDataRace(val) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
//go:build !race | ||
// +build !race | ||
|
||
/* | ||
* Copyright 2024 ByteDance Inc. | ||
* | ||
|
Oops, something went wrong.