| 1 |
package runtime |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
goruntime "runtime" |
| 6 |
) |
| 7 |
|
| 8 |
// stdMarshalerPackages are the packages of the standard library which have types with marshalers or unmarshalers, |
| 9 |
// by their paths, which a package of another module can't have: the go command resolves the path of a package of |
| 10 |
// the standard library to it, and reports the import as ambiguous when a module has the same path, and GOPATH |
| 11 |
// mode finds the package of GOROOT first. A path is here only from the release of Go which has the package ( see |
| 12 |
// std_package_go127.go ): a program built with an earlier release may have a package of its own at that path. |
| 13 |
// The packages of encoding/json are not here: json.RawMessage and jsontext.Value hold the JSON of the user. |
| 14 |
var stdMarshalerPackages = map[string]bool{ |
| 15 |
"crypto/x509": true, |
| 16 |
"log/slog": true, |
| 17 |
"math/big": true, |
| 18 |
"net": true, |
| 19 |
"net/netip": true, |
| 20 |
"regexp": true, |
| 21 |
"time": true, |
| 22 |
} |
| 23 |
|
| 24 |
// IsStdMarshalerType is whether the type, or the type it points to, is a named type of a package of the standard |
| 25 |
// library whose marshalers are trusted: what MarshalJSON returns is valid and compact, AppendText appends what |
| 26 |
// MarshalText returns, and UnmarshalJSON and UnmarshalText keep nothing of the bytes they are given. |
| 27 |
func IsStdMarshalerType(typ reflect.Type) bool { |
| 28 |
if typ.Kind() == reflect.Pointer && typ.Name() == "" { |
| 29 |
typ = typ.Elem() |
| 30 |
} |
| 31 |
return typ.Name() != "" && stdMarshalerPackages[typ.PkgPath()] |
| 32 |
} |
| 33 |
|
| 34 |
// PromotedStdMethod reports whether the method of the type which the lookup finds, or the one of the type it points to, |
| 35 |
// is the method of a type of the standard library whose marshalers are trusted ( see IsStdMarshalerType ), which the |
| 36 |
// type has by embedding it and doesn't declare itself: a call of the method then runs the method of that type, on the |
| 37 |
// embedded value. It returns that type and, if the embedded value is in the value of the type ( embedded by value at |
| 38 |
// every depth ), its offset in it, and whether it is so ( inline ), and whether the method is such a method. A type |
| 39 |
// which declares the method, or which has it from more than one embedded type at one depth, is not such a type. |
| 40 |
func PromotedStdMethod(typ reflect.Type, method MethodLookup) (reflect.Type, uintptr, bool, bool) { |
| 41 |
if typ.Kind() == reflect.Pointer && typ.Name() == "" { |
| 42 |
typ = typ.Elem() |
| 43 |
} |
| 44 |
if typ.Kind() != reflect.Struct || !isPromotedMethod(typ, method) { |
| 45 |
return nil, 0, false, false |
| 46 |
} |
| 47 |
var field reflect.StructField |
| 48 |
found := false |
| 49 |
for i := 0; i < typ.NumField(); i++ { |
| 50 |
f := typ.Field(i) |
| 51 |
if f.Anonymous && hasMethod(f.Type, method) { |
| 52 |
if found { |
| 53 |
// the depth of the method is not looked for: it is not trusted. |
| 54 |
return nil, 0, false, false |
| 55 |
} |
| 56 |
field, found = f, true |
| 57 |
} |
| 58 |
} |
| 59 |
if !found { |
| 60 |
return nil, 0, false, false |
| 61 |
} |
| 62 |
if IsStdMarshalerType(field.Type) { |
| 63 |
return field.Type, field.Offset, field.Type.Kind() != reflect.Pointer, true |
| 64 |
} |
| 65 |
origin, offset, inline, ok := PromotedStdMethod(field.Type, method) |
| 66 |
return origin, field.Offset + offset, inline && field.Type.Kind() != reflect.Pointer, ok |
| 67 |
} |
| 68 |
|
| 69 |
// hasMethod is whether the type, or the pointer to it, has the method. |
| 70 |
func hasMethod(typ reflect.Type, method MethodLookup) bool { |
| 71 |
if typ.Kind() != reflect.Pointer { |
| 72 |
typ = reflect.PointerTo(typ) |
| 73 |
} |
| 74 |
_, ok := method(typ) |
| 75 |
return ok |
| 76 |
} |
| 77 |
|
| 78 |
// isPromotedMethod is whether the method of the struct type which the lookup finds is promoted from an embedded field: |
| 79 |
// the method of the type and the one of the pointer to it, those which it has, are the wrappers which the compiler |
| 80 |
// makes for a promoted method. A declared method is not a wrapper, although the method of the pointer to the type of a |
| 81 |
// method declared on the value is. |
| 82 |
func isPromotedMethod(typ reflect.Type, method MethodLookup) bool { |
| 83 |
has := false |
| 84 |
for _, holder := range []reflect.Type{typ, reflect.PointerTo(typ)} { |
| 85 |
m, ok := method(holder) |
| 86 |
if !ok { |
| 87 |
continue |
| 88 |
} |
| 89 |
if !isGeneratedCode(m.Func.Pointer()) { |
| 90 |
return false |
| 91 |
} |
| 92 |
has = true |
| 93 |
} |
| 94 |
return has |
| 95 |
} |
| 96 |
|
| 97 |
// generatedFile is the file of the code which the compiler makes, as runtime.Func.FileLine reports it. |
| 98 |
const generatedFile = "<autogenerated>" |
| 99 |
|
| 100 |
// isGeneratedCode is whether the code is made by the compiler, as the wrapper of a promoted method is: the runtime |
| 101 |
// has no other way to tell it than the file of the code, which the compiler names generatedFile ( this is the |
| 102 |
// text of the runtime, not one of this package ). A code in any other file, or of no function, is not: its method |
| 103 |
// is then called and its output checked. |
| 104 |
func isGeneratedCode(pc uintptr) bool { |
| 105 |
f := goruntime.FuncForPC(pc) |
| 106 |
if f == nil { |
| 107 |
return false |
| 108 |
} |
| 109 |
file, _ := f.FileLine(pc) |
| 110 |
return file == generatedFile |
| 111 |
} |