Debian Codegraph @

internal/encoder/vm_color/util.go

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