decoder.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package optdec
  2. import (
  3. "reflect"
  4. "unsafe"
  5. "encoding/json"
  6. "github.com/bytedance/sonic/internal/rt"
  7. "github.com/bytedance/sonic/option"
  8. "github.com/bytedance/sonic/internal/decoder/errors"
  9. "github.com/bytedance/sonic/internal/decoder/consts"
  10. )
  11. type (
  12. MismatchTypeError = errors.MismatchTypeError
  13. SyntaxError = errors.SyntaxError
  14. )
  15. const (
  16. _F_allow_control = consts.F_allow_control
  17. _F_copy_string = consts.F_copy_string
  18. _F_disable_unknown = consts.F_disable_unknown
  19. _F_disable_urc = consts.F_disable_urc
  20. _F_use_int64 = consts.F_use_int64
  21. _F_use_number = consts.F_use_number
  22. _F_validate_string = consts.F_validate_string
  23. )
  24. type Options = consts.Options
  25. const (
  26. OptionUseInt64 = consts.OptionUseInt64
  27. OptionUseNumber = consts.OptionUseNumber
  28. OptionUseUnicodeErrors = consts.OptionUseUnicodeErrors
  29. OptionDisableUnknown = consts.OptionDisableUnknown
  30. OptionCopyString = consts.OptionCopyString
  31. OptionValidateString = consts.OptionValidateString
  32. )
  33. func Decode(s *string, i *int, f uint64, val interface{}) error {
  34. vv := rt.UnpackEface(val)
  35. vp := vv.Value
  36. /* check for nil type */
  37. if vv.Type == nil {
  38. return &json.InvalidUnmarshalError{}
  39. }
  40. /* must be a non-nil pointer */
  41. if vp == nil || vv.Type.Kind() != reflect.Ptr {
  42. return &json.InvalidUnmarshalError{Type: vv.Type.Pack()}
  43. }
  44. etp := rt.PtrElem(vv.Type)
  45. /* check the defined pointer type for issue 379 */
  46. if vv.Type.IsNamed() {
  47. newp := vp
  48. etp = vv.Type
  49. vp = unsafe.Pointer(&newp)
  50. }
  51. dec, err := findOrCompile(etp)
  52. if err != nil {
  53. return err
  54. }
  55. /* parse into document */
  56. ctx, err := NewContext(*s, *i, uint64(f), etp)
  57. defer ctx.Delete()
  58. if ctx.Parser.Utf8Inv {
  59. *s = ctx.Parser.Json
  60. }
  61. if err != nil {
  62. goto fix_error;
  63. }
  64. err = dec.FromDom(vp, ctx.Root(), &ctx)
  65. fix_error:
  66. err = fix_error(*s, *i, err)
  67. // update position at last
  68. *i += ctx.Parser.Pos()
  69. return err
  70. }
  71. func fix_error(json string, pos int, err error) error {
  72. if e, ok := err.(SyntaxError); ok {
  73. return SyntaxError{
  74. Pos: int(e.Pos) + pos,
  75. Src: json,
  76. Msg: e.Msg,
  77. }
  78. }
  79. if e, ok := err.(MismatchTypeError); ok {
  80. return &MismatchTypeError {
  81. Pos: int(e.Pos) + pos,
  82. Src: json,
  83. Type: e.Type,
  84. }
  85. }
  86. return err
  87. }
  88. // Pretouch compiles vt ahead-of-time to avoid JIT compilation on-the-fly, in
  89. // order to reduce the first-hit latency.
  90. //
  91. // Opts are the compile options, for example, "option.WithCompileRecursiveDepth" is
  92. // a compile option to set the depth of recursive compile for the nested struct type.
  93. func Pretouch(vt reflect.Type, opts ...option.CompileOption) error {
  94. cfg := option.DefaultCompileOptions()
  95. for _, opt := range opts {
  96. opt(&cfg)
  97. }
  98. return pretouchRec(map[reflect.Type]bool{vt:true}, cfg)
  99. }
  100. func pretouchType(_vt reflect.Type, opts option.CompileOptions) (map[reflect.Type]bool, error) {
  101. /* compile function */
  102. compiler := newCompiler().apply(opts)
  103. decoder := func(vt *rt.GoType, _ ...interface{}) (interface{}, error) {
  104. if f, err := compiler.compileType(_vt); err != nil {
  105. return nil, err
  106. } else {
  107. return f, nil
  108. }
  109. }
  110. /* find or compile */
  111. vt := rt.UnpackType(_vt)
  112. if val := programCache.Get(vt); val != nil {
  113. return nil, nil
  114. } else if _, err := programCache.Compute(vt, decoder); err == nil {
  115. return compiler.visited, nil
  116. } else {
  117. return nil, err
  118. }
  119. }
  120. func pretouchRec(vtm map[reflect.Type]bool, opts option.CompileOptions) error {
  121. if opts.RecursiveDepth < 0 || len(vtm) == 0 {
  122. return nil
  123. }
  124. next := make(map[reflect.Type]bool)
  125. for vt := range(vtm) {
  126. sub, err := pretouchType(vt, opts)
  127. if err != nil {
  128. return err
  129. }
  130. for svt := range(sub) {
  131. next[svt] = true
  132. }
  133. }
  134. opts.RecursiveDepth -= 1
  135. return pretouchRec(next, opts)
  136. }