Skip to content

Commit 25731b0

Browse files
Some native overrides for go1.20
1 parent 31a8063 commit 25731b0

File tree

12 files changed

+235
-6
lines changed

12 files changed

+235
-6
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//go:build js
2+
// +build js
3+
4+
package slicereader
5+
6+
func toString(b []byte) string {
7+
if len(b) == 0 {
8+
return ``
9+
}
10+
// Overwritten to avoid `unsafe.String`
11+
return string(b)
12+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
// setUpdate is provided by package runtime.
41+
// It calls update(def, env), where def is the default GODEBUG setting
42+
// and env is the current value of the $GODEBUG environment variable.
43+
// After that first call, the runtime calls update(def, env)
44+
// again each time the environment variable changes
45+
// (due to use of os.Setenv, for example).
46+
//
47+
// GOPHERJS: Currently we don't inject a proxy into process.env to watch
48+
// for changes via syscall.runtimeSetenv and syscall.runtimeUnsetenv.
49+
// We may want to look into this in the future.
50+
func setUpdate(update func(string, string)) {}
51+
52+
func update(def, env string) {
53+
updateMu.Lock()
54+
defer updateMu.Unlock()
55+
56+
did := make(map[string]bool)
57+
parse(did, env)
58+
parse(did, def)
59+
60+
cache.Range(func(name, v any) bool {
61+
if !did[name.(string)] {
62+
// temporarily replacement of atomic.Pointer[string] for go1.20 without generics.
63+
v.(*atomicStringPointer).Store(&empty)
64+
}
65+
return true
66+
})
67+
}
68+
69+
func parse(did map[string]bool, s string) {
70+
end := len(s)
71+
eq := -1
72+
for i := end - 1; i >= -1; i-- {
73+
if i == -1 || s[i] == ',' {
74+
if eq >= 0 {
75+
name, value := s[i+1:eq], s[eq+1:end]
76+
if !did[name] {
77+
did[name] = true
78+
v, ok := cache.Load(name)
79+
if !ok {
80+
// temporarily replacement of atomic.Pointer[string] for go1.20 without generics.
81+
p := new(atomicStringPointer)
82+
p.Store(&empty)
83+
v, _ = cache.LoadOrStore(name, p)
84+
}
85+
// temporarily replacement of atomic.Pointer[string] for go1.20 without generics.
86+
v.(*atomicStringPointer).Store(&value)
87+
}
88+
}
89+
eq = -1
90+
end = i
91+
} else if s[i] == '=' {
92+
eq = i
93+
}
94+
}
95+
}
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 rand
5+
6+
import _ "unsafe"
7+
8+
//go:linkname fastrand64 runtime.fastrand64
9+
func fastrand64() uint64

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import (
66
"testing"
77
)
88

9-
func testClientTimeout(t *testing.T, h2 bool) {
9+
func testClientTimeout(t *testing.T, mode testMode) {
1010
// The original test expects Client.Timeout error to be returned, but under
1111
// GopherJS an "i/o timeout" error is frequently returned. Otherwise the test
1212
// seems to be working correctly.
1313
t.Skip("Flaky test under GopherJS.")
1414
}
1515

16-
func testClientTimeout_Headers(t *testing.T, h2 bool) {
16+
func testClientTimeout_Headers(t *testing.T, mode testMode) {
1717
// The original test expects Client.Timeout error to be returned, but under
1818
// GopherJS an "i/o timeout" error is frequently returned. Otherwise the test
1919
// seems to be working correctly.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
"testing"
88
)
99

10-
func testTransportGCRequest(t *testing.T, h2, body bool) {
10+
func testTransportGCRequest(t *testing.T, mode testMode, body bool) {
1111
t.Skip("The test relies on runtime.SetFinalizer(), which is not supported by GopherJS.")
1212
}
1313

14-
func testWriteHeaderAfterWrite(t *testing.T, h2, hijack bool) {
14+
func testWriteHeaderAfterWrite(t *testing.T, mode testMode, hijack bool) {
1515
t.Skip("GopherJS source maps don't preserve original function names in stack traces, which this test relied on.")
1616
}

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/strings/strings_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,9 @@ func TestCompareStrings(t *testing.T) {
1818
}
1919

2020
func TestClone(t *testing.T) {
21-
t.Skip("conversion to reflect.StringHeader is not supported in GopherJS")
21+
t.Skip("conversion to unsafe.StringData is not supported in GopherJS")
22+
}
23+
24+
func TestMap(t *testing.T) {
25+
t.Skip("identity test uses unsafe.StringData is not supported in GopherJS")
2226
}

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)

compiler/natives/src/syscall/syscall_js_wasm.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ func runtime_envs() []string {
2222
return envs
2323
}
2424

25+
func runtimeSetenv(k, v string) {
26+
setenv_c(k, v)
27+
}
28+
29+
func runtimeUnsetenv(k string) {
30+
unsetenv_c(k)
31+
}
32+
2533
func setenv_c(k, v string) {
2634
process := js.Global().Get("process")
2735
if process.IsUndefined() {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//go:build js
2+
// +build js
3+
4+
package time
5+
6+
// replaced for go1.20 temporarily without generics.
7+
var ParseRFC3339 = parseRFC3339

0 commit comments

Comments
 (0)