// Package run drives the encoder: it compiles the opcodes of a value, sets up the runtime context and runs the // VM which the options choose. The v1 and the v2 json packages encode through it.
package run import ( ) func init() { encoder.RunHook = Code } type emptyInterface struct { typ unsafe.Pointer ptr unsafe.Pointer } // Encode appends v to the buffer of ctx as JSON, with a comma after it, by the options of ctx. On an error, it // returns the output written before the error with it, which the v2 json package finds the place of the error by. func ( *encoder.RuntimeContext, any) ([]byte, error) { := .Buf[:0] if == nil { = encoder.AppendNull(, ) = encoder.AppendComma(, ) return , nil } := (*emptyInterface)(unsafe.Pointer(&)) := .typ := uintptr() , := encoder.CompileToGetCodeSet(, ) if != nil { return , } if .Option.Flag&encoder.MarshalFuncsOption != 0 && .ptr != nil { // a pointer at the top level, which the functions of marshaling take as the value it points to. if := encoder.PointeeOf(, ); != nil { = } } := .ValueAddr(, .ptr) if == nil { // only a nil pointer has no address of the value. = encoder.AppendNull(, ) = encoder.AppendComma(, ) return , nil } .Init(, .CodeLength) if .Outer != nil { // the value is where the encoder of the method which called MarshalEncode is ( see ValueDepth ). .BaseIndent = .ValueDepth if := encoder.EnterNested(, .Outer, , ); != nil { return , } } , := Code(, , ) // the VM refers to the value by uintptr. runtime.KeepAlive() if != nil { return , } .Buf = return , nil } // EncodeIndent is Encode with the indentation and the prefix of each line. func ( *encoder.RuntimeContext, any, , string) ([]byte, error) { := .Buf[:0] if == nil { = encoder.AppendNull(, ) = encoder.AppendCommaIndent(, ) return , nil } := (*emptyInterface)(unsafe.Pointer(&)) := .typ := uintptr() , := encoder.CompileToGetCodeSet(, ) if != nil { return , } := .ValueAddr(, .ptr) if == nil { // only a nil pointer has no address of the value. = encoder.AppendNull(, ) = encoder.AppendCommaIndent(, ) return , nil } .Init(, .CodeLength) // a nested run is indented as this one ( see Code ) .Option.Flag |= encoder.IndentOption , := IndentCode(, , , , ) // the VM refers to the value by uintptr. runtime.KeepAlive() if != nil { return , } .Buf = return , nil } // Code runs the opcodes of a value, which ctx is set up for, by the VM of the options of ctx. func ( *encoder.RuntimeContext, []byte, *encoder.OpcodeSet) ([]byte, error) { if .Option.Flag&encoder.IndentOption != 0 { // a run nested in an indented one, with its prefix and its indentation, as of a value which its // MarshalJSONTo declined return indentRun(, , ) } // the escaper of the strings ( see encoder.RuntimeContext.SetEscaper ), without a call for most options. // It is set again only for other options than the ones it was set for: the one of RejectInvalidUTF8Option, // whose record of invalid UTF-8 the caller reads and clears after each run ( see InvalidUTF8Output ), as well. if := .Option.Flag; != .Option.EscaperFlags { if &encoder.RejectInvalidUTF8Option != 0 { .SetStrictEscaper() } else { .Option.Escaper, .Option.EscaperFlags = jsonstring.EscaperOf(uint()), } } if (.Option.Flag & encoder.DebugOption) != 0 { if (.Option.Flag & encoder.ColorizeOption) != 0 { return vm_color.DebugRun(, , ) } return vm.DebugRun(, , ) } if (.Option.Flag & encoder.ColorizeOption) != 0 { return vm_color.Run(, , ) } return vm.Run(, , ) } // IndentCode is Code with the indentation and the prefix of each line. func ( *encoder.RuntimeContext, []byte, *encoder.OpcodeSet, , string) ([]byte, error) { .Prefix = []byte() .IndentStr = []byte() return indentRun(, , ) } // indentRun is IndentCode with the prefix and the indentation which ctx has. func indentRun( *encoder.RuntimeContext, []byte, *encoder.OpcodeSet) ([]byte, error) { .SetEscaper() if (.Option.Flag & encoder.DebugOption) != 0 { if (.Option.Flag & encoder.ColorizeOption) != 0 { return vm_color_indent.DebugRun(, , ) } return vm_indent.DebugRun(, , ) } if (.Option.Flag & encoder.ColorizeOption) != 0 { return vm_color_indent.Run(, , ) } return vm_indent.Run(, , ) }