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