Skip to content

Commit a5152c9

Browse files
authored
Merge pull request #1309 from Workiva/atomicPntrOverrides
[go1.20] Temporarily overriding generics for go1.20
2 parents 73958a0 + df7a1cb commit a5152c9

File tree

6 files changed

+187
-1
lines changed

6 files changed

+187
-1
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//go:build js
2+
// +build js
3+
4+
package gob
5+
6+
import (
7+
"reflect"
8+
"sync"
9+
)
10+
11+
type typeInfo struct {
12+
id typeId
13+
encInit sync.Mutex
14+
15+
// temporarily replacement of atomic.Pointer[encEngine] for go1.20 without generics.
16+
encoder atomicEncEnginePointer
17+
wire *wireType
18+
}
19+
20+
type atomicEncEnginePointer struct {
21+
v *encEngine
22+
}
23+
24+
func (x *atomicEncEnginePointer) Load() *encEngine { return x.v }
25+
func (x *atomicEncEnginePointer) Store(val *encEngine) { x.v = val }
26+
27+
// temporarily replacement of growSlice[E any] for go1.20 without generics.
28+
func growSlice(v reflect.Value, ps any, length int) {
29+
vps := reflect.ValueOf(ps)
30+
vs := vps.Elem()
31+
zero := reflect.Zero(vs.Elem().Type())
32+
vs.Set(reflect.Append(vs, zero))
33+
cp := vs.Cap()
34+
if cp > length {
35+
cp = length
36+
}
37+
vs.Set(vs.Slice(0, cp))
38+
v.Set(vs)
39+
vps.Set(vs.Addr())
40+
}

compiler/natives/src/go/token/position.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,11 @@ type atomicFilePointer struct {
2020

2121
func (x *atomicFilePointer) Load() *File { return x.v }
2222
func (x *atomicFilePointer) Store(val *File) { x.v = val }
23+
24+
func (x *atomicFilePointer) CompareAndSwap(old, new *File) bool {
25+
if x.v == old {
26+
x.v = new
27+
return true
28+
}
29+
return false
30+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//go:build js
2+
// +build js
3+
4+
package godebug
5+
6+
import (
7+
"sync"
8+
)
9+
10+
type Setting struct {
11+
name string
12+
once sync.Once
13+
14+
// temporarily replacement of atomic.Pointer[string] for go1.20 without generics.
15+
value *atomicStringPointer
16+
}
17+
18+
type atomicStringPointer struct {
19+
v *string
20+
}
21+
22+
func (x *atomicStringPointer) Load() *string { return x.v }
23+
func (x *atomicStringPointer) Store(val *string) { x.v = val }
24+
25+
func (s *Setting) Value() string {
26+
s.once.Do(func() {
27+
v, ok := cache.Load(s.name)
28+
if !ok {
29+
// temporarily replacement of atomic.Pointer[string] for go1.20 without generics.
30+
p := new(atomicStringPointer)
31+
p.Store(&empty)
32+
v, _ = cache.LoadOrStore(s.name, p)
33+
}
34+
// temporarily replacement of atomic.Pointer[string] for go1.20 without generics.
35+
s.value = v.(*atomicStringPointer)
36+
})
37+
return *s.value.Load()
38+
}
39+
40+
func update(def, env string) {
41+
updateMu.Lock()
42+
defer updateMu.Unlock()
43+
44+
did := make(map[string]bool)
45+
parse(did, env)
46+
parse(did, def)
47+
48+
cache.Range(func(name, v any) bool {
49+
if !did[name.(string)] {
50+
// temporarily replacement of atomic.Pointer[string] for go1.20 without generics.
51+
v.(*atomicStringPointer).Store(&empty)
52+
}
53+
return true
54+
})
55+
}
56+
57+
func parse(did map[string]bool, s string) {
58+
end := len(s)
59+
eq := -1
60+
for i := end - 1; i >= -1; i-- {
61+
if i == -1 || s[i] == ',' {
62+
if eq >= 0 {
63+
name, value := s[i+1:eq], s[eq+1:end]
64+
if !did[name] {
65+
did[name] = true
66+
v, ok := cache.Load(name)
67+
if !ok {
68+
// temporarily replacement of atomic.Pointer[string] for go1.20 without generics.
69+
p := new(atomicStringPointer)
70+
p.Store(&empty)
71+
v, _ = cache.LoadOrStore(name, p)
72+
}
73+
// temporarily replacement of atomic.Pointer[string] for go1.20 without generics.
74+
v.(*atomicStringPointer).Store(&value)
75+
}
76+
}
77+
eq = -1
78+
end = i
79+
} else if s[i] == '=' {
80+
eq = i
81+
}
82+
}
83+
}

compiler/natives/src/net/http/http.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ package http
66
import (
77
"bufio"
88
"bytes"
9+
"context"
10+
"crypto/tls"
911
"errors"
1012
"io"
13+
"net"
1114
"net/textproto"
1215
"strconv"
16+
"sync"
17+
"sync/atomic"
1318

1419
"github.com/gopherjs/gopherjs/js"
1520
)
@@ -113,3 +118,30 @@ func (t *XHRTransport) CancelRequest(req *Request) {
113118
xhr.Call("abort")
114119
}
115120
}
121+
122+
type conn struct {
123+
server *Server
124+
cancelCtx context.CancelFunc
125+
rwc net.Conn
126+
remoteAddr string
127+
tlsState *tls.ConnectionState
128+
werr error
129+
r *connReader
130+
bufr *bufio.Reader
131+
bufw *bufio.Writer
132+
lastMethod string
133+
134+
// temporarily replacement of `atomic.Pointer[response]` for go1.20 without generics.
135+
curReq atomicResponsePointer
136+
137+
curState atomic.Uint64
138+
mu sync.Mutex
139+
hijackedv bool
140+
}
141+
142+
type atomicResponsePointer struct {
143+
v *response
144+
}
145+
146+
func (x *atomicResponsePointer) Load() *response { return x.v }
147+
func (x *atomicResponsePointer) Store(val *response) { x.v = val }

compiler/natives/src/sync/atomic/atomic.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,5 +221,19 @@ func sameType(x, y interface{}) bool {
221221
return js.InternalObject(x).Get("constructor") == js.InternalObject(y).Get("constructor")
222222
}
223223

224-
//gopherjs:purge for go1.19 without generics
224+
// Override pointer so that the type check in the source code is satisfied
225+
// but remove the fields and methods for go1.20 without generics.
226+
// See https://cs.opensource.google/go/go/+/refs/tags/go1.20.14:src/sync/atomic/type.go;l=40
225227
type Pointer[T any] struct{}
228+
229+
//gopherjs:purge for go1.20 without generics
230+
func (x *Pointer[T]) Load() *T
231+
232+
//gopherjs:purge for go1.20 without generics
233+
func (x *Pointer[T]) Store(val *T)
234+
235+
//gopherjs:purge for go1.20 without generics
236+
func (x *Pointer[T]) Swap(new *T) (old *T)
237+
238+
//gopherjs:purge for go1.20 without generics
239+
func (x *Pointer[T]) CompareAndSwap(old, new *T) (swapped bool)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//go:build js
2+
// +build js
3+
4+
package time
5+
6+
// replaced `parseRFC3339[string]` for go1.20 temporarily without generics.
7+
var ParseRFC3339 = func(s string, local *Location) (Time, bool) {
8+
return parseRFC3339(s, local)
9+
}

0 commit comments

Comments
 (0)