Debian Codegraph @

internal/encoder/marshaler.go

raw
1 package encoder
2
3 import (
4 "context"
5 "reflect"
6 "unsafe"
7
8 "github.com/goccy/go-json/internal/runtime"
9 )
10
11 // A method of a marshaler is called directly, by the code of the method, instead of through the interface:
12 // making the interface value, asserting the interface and calling through it cost more than the call.
13 //
14 // A call through an interface passes the data word of the interface value as the receiver: the value itself
15 // if the type is stored directly in an interface value ( a pointer, a map, ... ), or the address of the value.
16 // The opcodes of a marshaler carry that very word. The method which takes the word is found by reflect: the
17 // method of the type itself for a type stored directly, or the method of the pointer to the type otherwise,
18 // which is the method on the pointer or the wrapper of the method on the value. reflect.Value.Pointer gives the
19 // code of a function. A func value is a pointer to a closure whose first word is the code, and the first
20 // argument of a func value is passed as the receiver of a method is, so the code is called as a func value.
21
22 // MarshalerCall is the method of a marshaler, as it is called with the data word of the interface value.
23 type MarshalerCall struct {
24 // fn is the code of the method: it is the first word, so that a pointer to MarshalerCall is a func value.
25 fn uintptr
26 // nilIsNull is whether a nil receiver is encoded as null without a call, which is so for a pointer.
27 nilIsNull bool
28 // trusted is whether the output of MarshalJSON is valid and compact, as the one of a type of the standard
29 // library is, or of a type which has it only by embedding one ( see runtime.IsStdMarshalerType and
30 // runtime.PromotedStdMethod ): it is not checked.
31 trusted bool
32 // recv is the type of the receiver, for an error.
33 recv reflect.Type
34 // appendOutput writes what the method of a type of the standard library returns without calling it, by the
35 // appending method of the type: the output of MarshalJSON, valid and compact ( see stdJSONAppender ), or the
36 // text of MarshalText, by AppendText. It returns false, and writes nothing, when the method is to be called,
37 // as for an error, which is then the one of the method.
38 appendOutput func(b []byte, recv unsafe.Pointer) ([]byte, bool)
39 }
40
41 func (m *MarshalerCall) call(recv unsafe.Pointer) ([]byte, error) {
42 f := *(*func(unsafe.Pointer) ([]byte, error))(unsafe.Pointer(&m))
43 return f(recv)
44 }
45
46 func (m *MarshalerCall) callContext(recv unsafe.Pointer, ctx context.Context) ([]byte, error) {
47 f := *(*func(unsafe.Pointer, context.Context) ([]byte, error))(unsafe.Pointer(&m))
48 return f(recv, ctx)
49 }
50
51 // marshalerInterface is an interface of a marshaler, with the lookup of its method by the name of the method as a
52 // constant ( see runtime.MethodLookup ): a method is never looked up by a name in a variable, so that the linker can
53 // drop the methods which no one calls.
54 type marshalerInterface struct {
55 typ reflect.Type
56 method runtime.MethodLookup
57 }
58
59 var (
60 marshalJSONInterface = &marshalerInterface{typ: marshalJSONType, method: runtime.MarshalJSONMethod}
61 marshalJSONContextInterface = &marshalerInterface{typ: marshalJSONContextType, method: runtime.MarshalJSONMethod}
62 marshalTextInterface = &marshalerInterface{typ: marshalTextType, method: runtime.MarshalTextMethod}
63 appendTextInterface = &marshalerInterface{typ: appendTextType, method: runtime.AppendTextMethod}
64 )
65
66 // newMarshalerCall returns the call of the method of the type of the receiver, which implements the interface.
67 // It is decided when the type is compiled.
68 func newMarshalerCall(recv reflect.Type, iface *marshalerInterface) *MarshalerCall {
69 fn, ok := methodCode(recv, iface)
70 if !ok {
71 return nil
72 }
73 m := &MarshalerCall{fn: fn, nilIsNull: recv.Kind() == reflect.Ptr, recv: recv}
74 if runtime.IsStdMarshalerType(recv) {
75 switch iface {
76 case marshalJSONInterface:
77 m.trusted = true
78 m.appendOutput = stdJSONAppender(recv)
79 case marshalTextInterface:
80 m.appendOutput = textAppenderOf(recv)
81 }
82 } else if origin, offset, inline, ok := runtime.PromotedStdMethod(recv, iface.method); ok {
83 // the method is the one of the embedded value of a type of the standard library, which the wrapper of the
84 // type calls: its output is trusted as that of the type, and written by its appending method from the
85 // embedded value if it is in the value, whose address is then the data word: a pointer, or a type which
86 // is not stored directly in an interface value.
87 switch iface {
88 case marshalJSONInterface:
89 m.trusted = true
90 if inline && (recv.Kind() == reflect.Pointer || runtime.IfaceIndir(recv)) {
91 m.appendOutput = embeddedAppender(stdJSONAppender(origin), offset)
92 }
93 case marshalTextInterface:
94 if inline && (recv.Kind() == reflect.Pointer || runtime.IfaceIndir(recv)) {
95 m.appendOutput = embeddedAppender(textAppenderOf(origin), offset)
96 }
97 }
98 }
99 return m
100 }
101
102 // embeddedAppender returns the appending function of the value embedded at the offset of the receiver, whose data
103 // word is the address of the value which embeds it, or nil if the function is.
104 func embeddedAppender(appendOutput func([]byte, unsafe.Pointer) ([]byte, bool), offset uintptr) func([]byte, unsafe.Pointer) ([]byte, bool) {
105 if appendOutput == nil {
106 return nil
107 }
108 return func(b []byte, recv unsafe.Pointer) ([]byte, bool) {
109 return appendOutput(b, unsafe.Add(recv, offset))
110 }
111 }
112
113 // methodCode returns the code of the method of the interface of the receiver type, which takes the data word of
114 // the interface value.
115 func methodCode(recv reflect.Type, iface *marshalerInterface) (uintptr, bool) {
116 if !recv.Implements(iface.typ) {
117 return 0, false
118 }
119 holder := recv
120 if runtime.IfaceIndir(recv) {
121 // the data word is the address of the value: the method of the pointer takes it.
122 holder = reflect.PointerTo(recv)
123 }
124 method, ok := iface.method(holder)
125 if !ok {
126 return 0, false
127 }
128 return method.Func.Pointer(), true
129 }
130
131 // textAppender is encoding.TextAppender, which is of Go 1.24: the types of the standard library have it from
132 // that release on.
133 type textAppender interface {
134 AppendText(b []byte) ([]byte, error)
135 }
136
137 var appendTextType = reflect.TypeOf((*textAppender)(nil)).Elem()
138
139 // textAppenderOf returns the function which appends the text of a value of the type of the standard library by
140 // AppendText, which appends what MarshalText returns, or nil if the type has no AppendText. AppendText fails
141 // when MarshalText does: MarshalText is then called for its error.
142 func textAppenderOf(recv reflect.Type) func([]byte, unsafe.Pointer) ([]byte, bool) {
143 fn, ok := methodCode(recv, appendTextInterface)
144 if !ok {
145 return nil
146 }
147 code := &fn // a func value is a pointer to its code, as MarshalerCall is
148 appendText := *(*func(unsafe.Pointer, []byte) ([]byte, error))(unsafe.Pointer(&code))
149 return func(b []byte, p unsafe.Pointer) ([]byte, bool) {
150 out, err := appendText(p, b)
151 if err != nil {
152 return b, false
153 }
154 return out, true
155 }
156 }