Debian Codegraph @

internal/encoder/vm_indent/util.go

raw
1 package vm_indent
2
3 import (
4 "encoding/json"
5 "fmt"
6 "math"
7 "unsafe"
8
9 "github.com/goccy/go-json/internal/encoder"
10 "github.com/goccy/go-json/internal/runtime"
11 )
12
13 var (
14 appendInt = encoder.AppendInt
15 appendUint = encoder.AppendUint
16 appendFloat64 = encoder.AppendFloat64
17 appendString = encoder.AppendString
18 appendByteSlice = encoder.AppendByteSlice
19 appendNumber = encoder.AppendNumber
20 appendStructEnd = encoder.AppendStructEndIndent
21 appendIndent = encoder.AppendIndent
22 errUnsupportedFloat = encoder.ErrUnsupportedFloat
23 )
24
25 type emptyInterface struct {
26 typ unsafe.Pointer
27 ptr unsafe.Pointer
28 }
29
30 func errUnimplementedOp(op encoder.OpType) error {
31 return fmt.Errorf("encoder (indent): opcode %s has not been implemented", op)
32 }
33
34 // load / store are for the half of a slot for a pointer, and loadInt / storeInt are for the other half.
35 // A pointer is stored as uintptr, which needs no write barrier: see encoder.Slot.
36
37 func load(base unsafe.Pointer, idx uint32) unsafe.Pointer {
38 return *(*unsafe.Pointer)(unsafe.Add(base, idx))
39 }
40
41 func store(base unsafe.Pointer, idx uint32, p unsafe.Pointer) {
42 *(*uintptr)(unsafe.Add(base, idx)) = uintptr(p)
43 }
44
45 func loadInt(base unsafe.Pointer, idx uint32) uintptr {
46 return *(*uintptr)(unsafe.Add(base, idx))
47 }
48
49 func storeInt(base unsafe.Pointer, idx uint32, v uintptr) {
50 *(*uintptr)(unsafe.Add(base, idx)) = v
51 }
52
53 func ptrToUint64(p unsafe.Pointer, bitSize uint8) uint64 {
54 switch bitSize {
55 case 8:
56 return uint64(*(*uint8)(p))
57 case 16:
58 return uint64(*(*uint16)(p))
59 case 32:
60 return uint64(*(*uint32)(p))
61 case 64:
62 return *(*uint64)(p)
63 }
64 return 0
65 }
66 func ptrToFloat32(p unsafe.Pointer) float32 { return *(*float32)(p) }
67
68 // isInfOrNaN is whether the float is not finite, which encoding/json refuses: the exponent is all ones.
69 // math.IsInf is not inlined into Run, which is a big function to the inliner; this is.
70 func isInfOrNaN(v float64) bool {
71 const exponent = 0x7ff << 52
72 return math.Float64bits(v)&exponent == exponent
73 }
74
75 // appendFloat32 appends the float, or returns the error for one which is not finite as encoding/json does.
76 func appendFloat32(ctx *encoder.RuntimeContext, b []byte, v float32) ([]byte, error) {
77 if isInfOrNaN(float64(v)) {
78 return nil, errUnsupportedFloat(float64(v))
79 }
80 return encoder.AppendFloat32(ctx, b, v), nil
81 }
82 func ptrToFloat64(p unsafe.Pointer) float64 { return *(*float64)(p) }
83 func ptrToBool(p unsafe.Pointer) bool { return *(*bool)(p) }
84 func ptrToBytes(p unsafe.Pointer) []byte { return *(*[]byte)(p) }
85 func ptrToNumber(p unsafe.Pointer) json.Number { return *(*json.Number)(p) }
86 func ptrToString(p unsafe.Pointer) string { return *(*string)(p) }
87 func ptrToSlice(p unsafe.Pointer) *runtime.SliceHeader { return (*runtime.SliceHeader)(p) }
88 func ptrToPtr(p unsafe.Pointer) unsafe.Pointer { return *(*unsafe.Pointer)(p) }
89
90 // ptrToNPtr follows the pointer ptrNum times, or up to a nil one. It is written to be inlined into Run,
91 // which is a big function: only a function whose cost is 20 or less is inlined into it.
92 func ptrToNPtr(p unsafe.Pointer, ptrNum uint8) unsafe.Pointer {
93 for ; ptrNum > 0 && p != nil; ptrNum-- {
94 p = *(*unsafe.Pointer)(p)
95 }
96 return p
97 }
98
99 func ptrToInterface(code *encoder.Opcode, p unsafe.Pointer) any {
100 return *(*any)(unsafe.Pointer(&emptyInterface{
101 typ: code.Type,
102 ptr: p,
103 }))
104 }
105
106 func appendBool(_ *encoder.RuntimeContext, b []byte, v bool) []byte {
107 if v {
108 return append(b, "true"...)
109 }
110 return append(b, "false"...)
111 }
112
113 func appendNull(_ *encoder.RuntimeContext, b []byte) []byte {
114 return append(b, "null"...)
115 }
116
117 func appendComma(_ *encoder.RuntimeContext, b []byte) []byte {
118 return append(b, ',', '\n')
119 }
120
121 func appendNullComma(_ *encoder.RuntimeContext, b []byte) []byte {
122 return append(b, "null,\n"...)
123 }
124
125 func appendColon(_ *encoder.RuntimeContext, b []byte) []byte {
126 return append(b[:len(b)-2], ':', ' ')
127 }
128
129 func appendMapKeyValue(ctx *encoder.RuntimeContext, code *encoder.Opcode, b, key, value []byte) []byte {
130 b = appendIndent(ctx, b, code.Indent+1)
131 b = append(b, key...)
132 b[len(b)-2] = ':'
133 b[len(b)-1] = ' '
134 return append(b, value...)
135 }
136
137 func appendMapEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
138 b = b[:len(b)-2]
139 b = append(b, '\n')
140 b = appendIndent(ctx, b, code.Indent)
141 return append(b, '}', ',', '\n')
142 }
143
144 func appendArrayHead(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
145 b = append(b, '[', '\n')
146 return appendIndent(ctx, b, code.Indent+1)
147 }
148
149 func appendArrayEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
150 b = b[:len(b)-2]
151 b = append(b, '\n')
152 b = appendIndent(ctx, b, code.Indent)
153 return append(b, ']', ',', '\n')
154 }
155
156 func appendEmptyArray(_ *encoder.RuntimeContext, b []byte) []byte {
157 return append(b, '[', ']', ',', '\n')
158 }
159
160 func appendEmptyObject(_ *encoder.RuntimeContext, b []byte) []byte {
161 return append(b, '{', '}', ',', '\n')
162 }
163
164 func appendObjectEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
165 last := len(b) - 1
166 // replace comma to newline
167 b[last-1] = '\n'
168 b = appendIndent(ctx, b[:last], code.Indent)
169 return append(b, '}', ',', '\n')
170 }
171
172 func appendMarshalJSON(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, p unsafe.Pointer) ([]byte, error) {
173 return encoder.AppendMarshalJSONIndent(ctx, code, b, p)
174 }
175
176 func appendMarshalText(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, p unsafe.Pointer) ([]byte, error) {
177 return encoder.AppendMarshalTextIndent(ctx, code, b, p)
178 }
179
180 func appendStructHead(_ *encoder.RuntimeContext, b []byte) []byte {
181 return append(b, '{', '\n')
182 }
183
184 func appendStructKey(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
185 b = appendIndent(ctx, b, code.Indent)
186 b = append(b, code.Key...)
187 return append(b, ' ')
188 }
189
190 // appendLongStructKey appends a key of any length: every key is written by appendStructKey here.
191 func appendLongStructKey(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
192 return appendStructKey(ctx, code, b)
193 }
194
195 func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
196 last := len(b) - 1
197 if b[last-1] == '{' {
198 b[last] = '}'
199 } else {
200 if b[last] == '\n' {
201 // to remove ',' and '\n' characters
202 b = b[:len(b)-2]
203 }
204 b = append(b, '\n')
205 b = appendIndent(ctx, b, code.Indent-1)
206 b = append(b, '}')
207 }
208 return appendComma(ctx, b)
209 }
210
211 func appendArrayElemIndent(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
212 return appendIndent(ctx, b, code.Indent+1)
213 }
214
215 // appendMapKeyIndent indents an entry of a map, whose opcode is the one of the map or of its key: one deeper
216 // than the braces of the map, which appendObjectEnd indents by the same opcode.
217 func appendMapKeyIndent(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
218 return appendIndent(ctx, b, code.Indent+1)
219 }
220
221 // appendScalar appends the value at p by the opcode of a scalar, with the comma: what the VM does for the opcode.
222 // It is for a scalar held by an interface value, which is encoded without a frame.
223 //
224 //go:noinline
225 func appendScalar(ctx *encoder.RuntimeContext, b []byte, code *encoder.Opcode, p unsafe.Pointer) ([]byte, error) {
226 switch code.Op {
227 case encoder.OpInt:
228 b = appendInt(ctx, b, p, code)
229 case encoder.OpUint:
230 b = appendUint(ctx, b, p, code)
231 case encoder.OpFloat32:
232 bb, err := appendFloat32(ctx, b, ptrToFloat32(p))
233 if err != nil {
234 return nil, err
235 }
236 b = bb
237 case encoder.OpFloat64:
238 v := ptrToFloat64(p)
239 if isInfOrNaN(v) {
240 return nil, errUnsupportedFloat(v)
241 }
242 b = appendFloat64(ctx, b, v)
243 case encoder.OpString:
244 b = appendString(ctx, b, ptrToString(p))
245 case encoder.OpBool:
246 b = appendBool(ctx, b, ptrToBool(p))
247 case encoder.OpBytes:
248 b = appendByteSlice(ctx, b, ptrToBytes(p))
249 case encoder.OpNumber:
250 bb, err := appendNumber(ctx, b, ptrToNumber(p))
251 if err != nil {
252 return nil, err
253 }
254 b = bb
255 default:
256 return nil, errUnimplementedOp(code.Op)
257 }
258 return appendComma(ctx, b), nil
259 }