1// This package provides shims over Go 1.2{2,3} APIs
2// which are missing from Go 1.21, and used by the Go 1.24 encoding/json package.
3//
4// Inside the vendored package, all shim code has comments that begin look like
5// // SHIM(...): ...
6package shims
7
8import (
9 "encoding/base64"
10 "reflect"
11 "slices"
12)
13
14type OverflowableType struct{ reflect.Type }
15
16func (t OverflowableType) OverflowInt(x int64) bool {
17 k := t.Kind()
18 switch k {
19 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
20 bitSize := t.Size() * 8
21 trunc := (x << (64 - bitSize)) >> (64 - bitSize)
22 return x != trunc
23 }
24 panic("reflect: OverflowInt of non-int type " + t.String())
25}
26
27func (t OverflowableType) OverflowUint(x uint64) bool {
28 k := t.Kind()
29 switch k {
30 case reflect.Uint, reflect.Uintptr, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
31 bitSize := t.Size() * 8
32 trunc := (x << (64 - bitSize)) >> (64 - bitSize)
33 return x != trunc
34 }
35 panic("reflect: OverflowUint of non-uint type " + t.String())
36}
37
38// Original src code from Go 1.23 go/src/reflect/type.go (taken 1/9/25)
39/*
40
41func (t *rtype) OverflowInt(x int64) bool {
42 k := t.Kind()
43 switch k {
44 case Int, Int8, Int16, Int32, Int64:
45 bitSize := t.Size() * 8
46 trunc := (x << (64 - bitSize)) >> (64 - bitSize)
47 return x != trunc
48 }
49 panic("reflect: OverflowInt of non-int type " + t.String())
50}
51
52func (t *rtype) OverflowUint(x uint64) bool {
53 k := t.Kind()
54 switch k {
55 case Uint, Uintptr, Uint8, Uint16, Uint32, Uint64:
56 bitSize := t.Size() * 8
57 trunc := (x << (64 - bitSize)) >> (64 - bitSize)
58 return x != trunc
59 }
60 panic("reflect: OverflowUint of non-uint type " + t.String())
61}
62
63*/
64
65// TypeFor returns the [Type] that represents the type argument T.
66func TypeFor[T any]() reflect.Type {
67 var v T
68 if t := reflect.TypeOf(v); t != nil {
69 return t // optimize for T being a non-interface kind
70 }
71 return reflect.TypeOf((*T)(nil)).Elem() // only for an interface kind
72}
73
74// Original src code from Go 1.23 go/src/reflect/type.go (taken 1/9/25)
75/*
76
77// TypeFor returns the [Type] that represents the type argument T.
78func TypeFor[T any]() Type {
79 var v T
80 if t := TypeOf(v); t != nil {
81 return t // optimize for T being a non-interface kind
82 }
83 return TypeOf((*T)(nil)).Elem() // only for an interface kind
84}
85
86*/
87
88type AppendableStdEncoding struct{ *base64.Encoding }
89
90// AppendEncode appends the base64 encoded src to dst
91// and returns the extended buffer.
92func (enc AppendableStdEncoding) AppendEncode(dst, src []byte) []byte {
93 n := enc.EncodedLen(len(src))
94 dst = slices.Grow(dst, n)
95 enc.Encode(dst[len(dst):][:n], src)
96 return dst[:len(dst)+n]
97}
98
99// Original src code from Go 1.23.4 go/src/encoding/base64/base64.go (taken 1/9/25)
100/*
101
102// AppendEncode appends the base64 encoded src to dst
103// and returns the extended buffer.
104func (enc *Encoding) AppendEncode(dst, src []byte) []byte {
105 n := enc.EncodedLen(len(src))
106 dst = slices.Grow(dst, n)
107 enc.Encode(dst[len(dst):][:n], src)
108 return dst[:len(dst)+n]
109}
110
111*/