package vm_indent

import (
	
	
	
	

	
	
	
)

var (
	appendInt           = encoder.AppendInt
	appendUint          = encoder.AppendUint
	appendFloat64       = encoder.AppendFloat64
	appendByteSlice     = encoder.AppendByteSlice
	appendNumber        = encoder.AppendNumber
	appendNumberString  = encoder.AppendNumberString
	appendStructEnd     = encoder.AppendStructEndIndent
	appendIndent        = encoder.AppendIndent
	errUnsupportedFloat = encoder.ErrUnsupportedFloat
)

type emptyInterface struct {
	typ unsafe.Pointer
	ptr unsafe.Pointer
}

func errUnimplementedOp( encoder.OpType) error {
	return fmt.Errorf("encoder (indent): opcode %s has not been implemented", )
}

// load / store are for the half of a slot for a pointer, and loadInt / storeInt are for the other half.
// A pointer is stored as uintptr, which needs no write barrier: see encoder.Slot.

func load( unsafe.Pointer,  uint32) unsafe.Pointer {
	return *(*unsafe.Pointer)(unsafe.Add(, ))
}

func store( unsafe.Pointer,  uint32,  unsafe.Pointer) {
	*(*uintptr)(unsafe.Add(, )) = uintptr()
}

func loadInt( unsafe.Pointer,  uint32) uintptr {
	return *(*uintptr)(unsafe.Add(, ))
}

func storeInt( unsafe.Pointer,  uint32,  uintptr) {
	*(*uintptr)(unsafe.Add(, )) = 
}

func ptrToUint64( unsafe.Pointer,  uint8) uint64 {
	switch  {
	case 8:
		return uint64(*(*uint8)())
	case 16:
		return uint64(*(*uint16)())
	case 32:
		return uint64(*(*uint32)())
	case 64:
		return *(*uint64)()
	}
	return 0
}
func ptrToFloat32( unsafe.Pointer) float32 { return *(*float32)() }

// isInfOrNaN is whether the float is not finite, which encoding/json refuses: the exponent is all ones.
// math.IsInf is not inlined into Run, which is a big function to the inliner; this is.
func isInfOrNaN( float64) bool {
	const  = 0x7ff << 52
	return math.Float64bits()& == 
}

// appendFloat32 appends the float, or returns the error for one which is not finite as encoding/json does.
func appendFloat32( *encoder.RuntimeContext,  *encoder.Opcode,  []byte,  float32) ([]byte, error) {
	if isInfOrNaN(float64()) {
		return nil, errUnsupportedFloat(, , float64())
	}
	return encoder.AppendFloat32(, , ), nil
}
func ptrToFloat64( unsafe.Pointer) float64            { return *(*float64)() }
func ptrToBool( unsafe.Pointer) bool                  { return *(*bool)() }
func ptrToBytes( unsafe.Pointer) []byte               { return *(*[]byte)() }
func ptrToNumber( unsafe.Pointer) json.Number         { return *(*json.Number)() }
func ptrToString( unsafe.Pointer) string              { return *(*string)() }
func ptrToSlice( unsafe.Pointer) *runtime.SliceHeader { return (*runtime.SliceHeader)() }
func ptrToPtr( unsafe.Pointer) unsafe.Pointer         { return *(*unsafe.Pointer)() }

// ptrToNPtr follows the pointer ptrNum times, or up to a nil one. It is written to be inlined into Run,
// which is a big function: only a function whose cost is 20 or less is inlined into it.
func ptrToNPtr( unsafe.Pointer,  uint8) unsafe.Pointer {
	for ;  > 0 &&  != nil; -- {
		 = *(*unsafe.Pointer)()
	}
	return 
}

func appendBool( *encoder.RuntimeContext,  []byte,  bool) []byte {
	if  {
		return append(, "true"...)
	}
	return append(, "false"...)
}

func appendNull( *encoder.RuntimeContext,  []byte) []byte {
	return append(, "null"...)
}

func appendComma( *encoder.RuntimeContext,  []byte) []byte {
	return append(, ',', '\n')
}

func appendNullComma( *encoder.RuntimeContext,  []byte) []byte {
	return append(, "null,\n"...)
}

func appendColon( *encoder.RuntimeContext,  []byte) []byte {
	return append([:len()-2], ':', ' ')
}

// mapKeyFooterLen returns the length of the codes of a color which end the names of the keys written by code: none
// without colors.
func mapKeyFooterLen( *encoder.RuntimeContext,  *encoder.Opcode) int { return 0 }

// appendSortedMapKey appends the key of an entry of a sorted map, as it was encoded with a comma after it, and the
// colon after it.
func appendSortedMapKey( *encoder.RuntimeContext,  *encoder.Opcode, ,  []byte) []byte {
	 = appendIndent(, , .Indent+1)
	 = append(, ...)
	[len()-2] = ':'
	[len()-1] = ' '
	return 
}

func appendArrayHead( *encoder.RuntimeContext,  *encoder.Opcode,  []byte) []byte {
	 = append(, '[', '\n')
	return appendIndent(, , .Indent+1)
}

func appendArrayEnd( *encoder.RuntimeContext,  *encoder.Opcode,  []byte) []byte {
	 = [:len()-2]
	 = append(, '\n')
	 = appendIndent(, , .Indent)
	return append(, ']', ',', '\n')
}

func appendEmptyArray( *encoder.RuntimeContext,  []byte) []byte {
	return append(, '[', ']', ',', '\n')
}

func appendEmptyObject( *encoder.RuntimeContext,  []byte) []byte {
	return append(, '{', '}', ',', '\n')
}

func appendObjectEnd( *encoder.RuntimeContext,  *encoder.Opcode,  []byte) []byte {
	 := len() - 1
	// replace comma to newline
	[-1] = '\n'
	 = appendIndent(, [:], .Indent)
	return append(, '}', ',', '\n')
}

func appendMarshalJSON( *encoder.RuntimeContext,  *encoder.Opcode,  []byte,  unsafe.Pointer) ([]byte, error) {
	return encoder.AppendMarshalJSONIndent(, , , )
}

func appendMarshalText( *encoder.RuntimeContext,  *encoder.Opcode,  []byte,  unsafe.Pointer) ([]byte, error) {
	return encoder.AppendMarshalTextIndent(, , , )
}

func appendStructHead( *encoder.RuntimeContext,  []byte) []byte {
	return append(, '{', '\n')
}

func appendStructKey( *encoder.RuntimeContext,  *encoder.Opcode,  []byte) []byte {
	 = appendIndent(, , .Indent)
	 = append(, .Key...)
	return append(, ' ')
}

// appendLongStructKey appends a key of any length: every key is written by appendStructKey here.
func appendLongStructKey( *encoder.RuntimeContext,  *encoder.Opcode,  []byte) []byte {
	return appendStructKey(, , )
}

func appendStructEndSkipLast( *encoder.RuntimeContext,  *encoder.Opcode,  []byte) []byte {
	 := len() - 1
	if [-1] == '{' {
		[] = '}'
	} else {
		if [] == '\n' {
			// to remove ',' and '\n' characters
			 = [:len()-2]
		}
		 = append(, '\n')
		 = appendIndent(, , .Indent-1)
		 = append(, '}')
	}
	return appendComma(, )
}

func appendArrayElemIndent( *encoder.RuntimeContext,  *encoder.Opcode,  []byte) []byte {
	return appendIndent(, , .Indent+1)
}

// appendMapKeyIndent indents an entry of a map, whose opcode is the one of the map or of its key: one deeper
// than the braces of the map, which appendObjectEnd indents by the same opcode.
func appendMapKeyIndent( *encoder.RuntimeContext,  *encoder.Opcode,  []byte) []byte {
	return appendIndent(, , .Indent+1)
}

// appendScalar appends the value at p by the opcode of a scalar, with the comma: what the VM does for the opcode.
// It is for a scalar held by an interface value, which is encoded without a frame.
//
//go:noinline
func appendScalar( *encoder.RuntimeContext,  []byte,  *encoder.Opcode,  unsafe.Pointer) ([]byte, error) {
	switch .Op {
	case encoder.OpInt:
		 = appendInt(, , , )
	case encoder.OpUint:
		 = appendUint(, , , )
	case encoder.OpFloat32:
		,  := appendFloat32(, , , ptrToFloat32())
		if  != nil {
			return nil, 
		}
		 = 
	case encoder.OpFloat64:
		 := ptrToFloat64()
		if isInfOrNaN() {
			return nil, errUnsupportedFloat(, , )
		}
		 = appendFloat64(, , )
	case encoder.OpString:
		, _ = jsonstring.AppendQuoted(encoder.StringEscaper(), , ptrToString())
	case encoder.OpBool:
		 = appendBool(, , ptrToBool())
	case encoder.OpBytes:
		 = appendByteSlice(, , ptrToBytes())
	case encoder.OpNumber:
		,  := appendNumber(, , ptrToNumber())
		if  != nil {
			return nil, 
		}
		 = 
	default:
		return nil, errUnimplementedOp(.Op)
	}
	return appendComma(, ), nil
}