decoder.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package jitdec
  2. import (
  3. `unsafe`
  4. `encoding/json`
  5. `reflect`
  6. `runtime`
  7. `github.com/bytedance/sonic/internal/decoder/consts`
  8. `github.com/bytedance/sonic/internal/decoder/errors`
  9. `github.com/bytedance/sonic/internal/rt`
  10. `github.com/bytedance/sonic/utf8`
  11. `github.com/bytedance/sonic/option`
  12. )
  13. type (
  14. MismatchTypeError = errors.MismatchTypeError
  15. SyntaxError = errors.SyntaxError
  16. )
  17. const (
  18. _F_allow_control = consts.F_allow_control
  19. _F_copy_string = consts.F_copy_string
  20. _F_disable_unknown = consts.F_disable_unknown
  21. _F_disable_urc = consts.F_disable_urc
  22. _F_use_int64 = consts.F_use_int64
  23. _F_use_number = consts.F_use_number
  24. _F_no_validate_json = consts.F_no_validate_json
  25. _F_validate_string = consts.F_validate_string
  26. _F_case_sensitive = consts.F_case_sensitive
  27. )
  28. var (
  29. error_wrap = errors.ErrorWrap
  30. error_type = errors.ErrorType
  31. error_field = errors.ErrorField
  32. error_value = errors.ErrorValue
  33. error_mismatch = errors.ErrorMismatch
  34. stackOverflow = errors.StackOverflow
  35. )
  36. // Decode parses the JSON-encoded data from current position and stores the result
  37. // in the value pointed to by val.
  38. func Decode(s *string, i *int, f uint64, val interface{}) error {
  39. /* validate json if needed */
  40. if (f & (1 << _F_validate_string)) != 0 && !utf8.ValidateString(*s){
  41. dbuf := utf8.CorrectWith(nil, rt.Str2Mem(*s), "\ufffd")
  42. *s = rt.Mem2Str(dbuf)
  43. }
  44. vv := rt.UnpackEface(val)
  45. vp := vv.Value
  46. /* check for nil type */
  47. if vv.Type == nil {
  48. return &json.InvalidUnmarshalError{}
  49. }
  50. /* must be a non-nil pointer */
  51. if vp == nil || vv.Type.Kind() != reflect.Ptr {
  52. return &json.InvalidUnmarshalError{Type: vv.Type.Pack()}
  53. }
  54. etp := rt.PtrElem(vv.Type)
  55. /* check the defined pointer type for issue 379 */
  56. if vv.Type.IsNamed() {
  57. newp := vp
  58. etp = vv.Type
  59. vp = unsafe.Pointer(&newp)
  60. }
  61. /* create a new stack, and call the decoder */
  62. sb := newStack()
  63. nb, err := decodeTypedPointer(*s, *i, etp, vp, sb, f)
  64. /* return the stack back */
  65. *i = nb
  66. freeStack(sb)
  67. /* avoid GC ahead */
  68. runtime.KeepAlive(vv)
  69. return err
  70. }
  71. // Pretouch compiles vt ahead-of-time to avoid JIT compilation on-the-fly, in
  72. // order to reduce the first-hit latency.
  73. //
  74. // Opts are the compile options, for example, "option.WithCompileRecursiveDepth" is
  75. // a compile option to set the depth of recursive compile for the nested struct type.
  76. func Pretouch(vt reflect.Type, opts ...option.CompileOption) error {
  77. cfg := option.DefaultCompileOptions()
  78. for _, opt := range opts {
  79. opt(&cfg)
  80. }
  81. return pretouchRec(map[reflect.Type]bool{vt:true}, cfg)
  82. }
  83. func pretouchType(_vt reflect.Type, opts option.CompileOptions) (map[reflect.Type]bool, error) {
  84. /* compile function */
  85. compiler := newCompiler().apply(opts)
  86. decoder := func(vt *rt.GoType, _ ...interface{}) (interface{}, error) {
  87. if pp, err := compiler.compile(_vt); err != nil {
  88. return nil, err
  89. } else {
  90. as := newAssembler(pp)
  91. as.name = _vt.String()
  92. return as.Load(), nil
  93. }
  94. }
  95. /* find or compile */
  96. vt := rt.UnpackType(_vt)
  97. if val := programCache.Get(vt); val != nil {
  98. return nil, nil
  99. } else if _, err := programCache.Compute(vt, decoder); err == nil {
  100. return compiler.rec, nil
  101. } else {
  102. return nil, err
  103. }
  104. }
  105. func pretouchRec(vtm map[reflect.Type]bool, opts option.CompileOptions) error {
  106. if opts.RecursiveDepth < 0 || len(vtm) == 0 {
  107. return nil
  108. }
  109. next := make(map[reflect.Type]bool)
  110. for vt := range(vtm) {
  111. sub, err := pretouchType(vt, opts)
  112. if err != nil {
  113. return err
  114. }
  115. for svt := range(sub) {
  116. next[svt] = true
  117. }
  118. }
  119. opts.RecursiveDepth -= 1
  120. return pretouchRec(next, opts)
  121. }