]> Cypherpunks repositories - gostls13.git/commitdiff
net: add js/wasm architecture
authorRichard Musiol <mail@richard-musiol.de>
Sun, 4 Mar 2018 11:18:32 +0000 (12:18 +0100)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 4 Jun 2018 16:01:50 +0000 (16:01 +0000)
This commit adds the js/wasm architecture to the net package.
The net package is not supported by js/wasm, but a simple fake
networking is available so tests of other packages that require
basic TCP sockets can pass. The tests of the net package itself
are mostly disabled.

Updates #18892

Change-Id: Id287200c39f0a3e23d20ef17260ca15ccdcca032
Reviewed-on: https://go-review.googlesource.com/109995
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
63 files changed:
src/internal/poll/hook_unix.go
src/net/conn_test.go
src/net/dial_test.go
src/net/dnsname_test.go
src/net/error_posix.go
src/net/error_test.go
src/net/error_unix.go
src/net/external_test.go
src/net/fd_plan9.go
src/net/fd_unix.go
src/net/fd_windows.go
src/net/file_stub.go
src/net/file_test.go
src/net/hook_unix.go
src/net/http/cgi/host_test.go
src/net/interface_stub.go
src/net/interface_test.go
src/net/internal/socktest/main_test.go
src/net/internal/socktest/main_unix_test.go
src/net/internal/socktest/switch_unix.go
src/net/internal/socktest/sys_unix.go
src/net/ip_test.go
src/net/iprawsock_posix.go
src/net/iprawsock_test.go
src/net/ipsock_posix.go
src/net/listen_test.go
src/net/lookup_fake.go [moved from src/net/lookup_nacl.go with 98% similarity]
src/net/lookup_test.go
src/net/main_conf_test.go
src/net/main_noconf_test.go
src/net/main_posix_test.go
src/net/main_test.go
src/net/mockserver_test.go
src/net/net.go
src/net/net_fake.go [new file with mode: 0644]
src/net/net_test.go
src/net/packetconn_test.go
src/net/port_unix.go
src/net/protoconn_test.go
src/net/rawconn_stub_test.go
src/net/rawconn_test.go
src/net/sendfile_stub.go
src/net/sendfile_test.go
src/net/server_test.go
src/net/smtp/smtp_test.go
src/net/sock_posix.go
src/net/sock_stub.go
src/net/sockaddr_posix.go [new file with mode: 0644]
src/net/sockopt_stub.go
src/net/sockoptip_stub.go
src/net/tcpsock_posix.go
src/net/tcpsock_test.go
src/net/tcpsock_unix_test.go
src/net/tcpsockopt_stub.go
src/net/timeout_test.go
src/net/udpsock_posix.go
src/net/udpsock_test.go
src/net/unixsock_posix.go
src/net/unixsock_test.go
src/net/writev_test.go
src/runtime/netpoll.go
src/runtime/netpoll_fake.go [moved from src/runtime/netpoll_nacl.go with 73% similarity]
src/syscall/net_js.go [new file with mode: 0644]

index 85e102dd73e0fec10b252af3625b39ec82d933d5..c2ad17eb1ad8d63e205b1a21bb35a42fc2e5c19b 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
+// +build darwin dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris
 
 package poll
 
index 16cf69ee169eaf0435b9e0f0ae95a07a0126b160..6854898da2ab09534fa8ecc5176de17dc9314913 100644 (file)
@@ -5,6 +5,8 @@
 // This file implements API tests across platforms and will never have a build
 // tag.
 
+// +build !js
+
 package net
 
 import (
index 3934ad864836056dfc10ce7d4bce9aca87bd9ffd..00a84d17d60599a2aebb4c80481f27f93a3d749e 100644 (file)
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !js
+
 package net
 
 import (
index e0f786dec87ff52319b78f1e576acd41c5b3cd74..806d8756cb5c5f0af4d8359ffb95456992ee7e1e 100644 (file)
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !js
+
 package net
 
 import (
index dd9754c841aa5ac0430d49db0ff45e7db42feb78..0000700809e5cfcaeacc54c09fc8a3c885cc92d2 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
+// +build darwin dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris windows
 
 package net
 
index 9791e6fe4defb61e86367e43e05fb1d923329be2..e09670e5ce0e2f8e126107bd3b95422877231696 100644 (file)
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !js
+
 package net
 
 import (
index 633861e75973e4a4f7655ed6a046b8e3302a0bb0..b5a5829eaa0837aa32e79b2a901beb8bd9287edd 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build darwin dragonfly freebsd linux netbsd openbsd solaris
+// +build darwin dragonfly freebsd js linux netbsd openbsd solaris
 
 package net
 
index 38788efc3d39b7caf5b91c8a875842b1abfb6b8e..f3c69c407f7c1e3f4315d091dd6c602a0c487334 100644 (file)
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !js
+
 package net
 
 import (
index 46ee5d97400f91a7894c171e621f1345127f0ca8..da41bc0c34cac599b0c916b240777084ecc7c402 100644 (file)
@@ -9,6 +9,7 @@ import (
        "io"
        "os"
        "syscall"
+       "time"
 )
 
 // Network file descriptor.
@@ -172,3 +173,15 @@ func setReadBuffer(fd *netFD, bytes int) error {
 func setWriteBuffer(fd *netFD, bytes int) error {
        return syscall.EPLAN9
 }
+
+func (fd *netFD) SetDeadline(t time.Time) error {
+       return fd.pfd.SetDeadline(t)
+}
+
+func (fd *netFD) SetReadDeadline(t time.Time) error {
+       return fd.pfd.SetReadDeadline(t)
+}
+
+func (fd *netFD) SetWriteDeadline(t time.Time) error {
+       return fd.pfd.SetWriteDeadline(t)
+}
index 3dcbeb51c7402bf2e22738f2a7ad2be840e1662b..84613c778c1573d826f0fd488926a9a439445fb5 100644 (file)
@@ -13,6 +13,7 @@ import (
        "runtime"
        "sync/atomic"
        "syscall"
+       "time"
 )
 
 // Network file descriptor.
@@ -298,3 +299,15 @@ func (fd *netFD) dup() (f *os.File, err error) {
 
        return os.NewFile(uintptr(ns), fd.name()), nil
 }
+
+func (fd *netFD) SetDeadline(t time.Time) error {
+       return fd.pfd.SetDeadline(t)
+}
+
+func (fd *netFD) SetReadDeadline(t time.Time) error {
+       return fd.pfd.SetReadDeadline(t)
+}
+
+func (fd *netFD) SetWriteDeadline(t time.Time) error {
+       return fd.pfd.SetWriteDeadline(t)
+}
index e5f8da156a24c5bb8c79a1ab98eb42bc99330391..8a91138a42ca8ad99e1b1ede401bccb18a75f8aa 100644 (file)
@@ -10,6 +10,7 @@ import (
        "os"
        "runtime"
        "syscall"
+       "time"
        "unsafe"
 )
 
@@ -241,3 +242,15 @@ func (fd *netFD) dup() (*os.File, error) {
        // TODO: Implement this
        return nil, syscall.EWINDOWS
 }
+
+func (fd *netFD) SetDeadline(t time.Time) error {
+       return fd.pfd.SetDeadline(t)
+}
+
+func (fd *netFD) SetReadDeadline(t time.Time) error {
+       return fd.pfd.SetReadDeadline(t)
+}
+
+func (fd *netFD) SetWriteDeadline(t time.Time) error {
+       return fd.pfd.SetWriteDeadline(t)
+}
index 0f7460c757974d7cb05a2aaaa0ba18552c3e16d3..22566083658a85e2d199f897afe486519ab87674 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build nacl
+// +build nacl js,wasm
 
 package net
 
index abf8b3a6995f9d92797d180ce072e104495a95ee..9fb5f2fd262671485aab698e08038bcd23604e4a 100644 (file)
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !js
+
 package net
 
 import (
index fee62a972f8c7ed9add7d3b2904e95a2470344b4..d672bd01b03f43d44276677e68dc67987919e95c 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
+// +build darwin dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris
 
 package net
 
index ed8b919fb33c65596e8038d5c42efb10cf326362..1790d5de982fb75ea014c4e571ba9ccae5e9c95f 100644 (file)
@@ -502,6 +502,7 @@ func TestDirWindows(t *testing.T) {
 }
 
 func TestEnvOverride(t *testing.T) {
+       check(t)
        cgifile, _ := filepath.Abs("testdata/test.cgi")
 
        var perl string
index 3b0a1aeacf26d075155cce3c76173e5f09cbcd65..0afaa80d632cc28fd984d05de9bd76485b903412 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build nacl
+// +build nacl js,wasm
 
 package net
 
index 13b94857971a961c81c2f884da3efc9f82917316..5d183c56384dfc91e2f8ed2d1fb19c68d6135e5a 100644 (file)
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !js
+
 package net
 
 import (
index 60e581f463c27779af13d49d6183a860317e5b3a..3b0a48aef4d986f9ea6ce0ac76c83f0ff70c970c 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build !plan9
+// +build !js,!plan9
 
 package socktest_test
 
index b8eebc2aa426eb0a8a0fe1c47aa3f4375eebe5c5..4d9d4143564864ce1b90ad42503f112f23cc5cda 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build !plan9,!windows
+// +build !js,!plan9,!windows
 
 package socktest_test
 
index 14c0c228a2f1e277c1cf428bc1dddb99744e4ce8..d518f7b06fda769ce1c952bffba72db9fd6d4771 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
+// +build darwin dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris
 
 package socktest
 
index 9fe86b55cfde746f666ef43c137d9bdbfd8fba8b..139fc3ece49854f4ea58e8c9c16d89ea90341423 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
+// +build darwin dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris
 
 package socktest
 
index 95dd109e28ca604597804784378153a90ca0e31d..a5fc5e644a2b2717ab16b1005c586be8bb65d2d6 100644 (file)
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !js
+
 package net
 
 import (
index b2f57916433eb807a21efe5c5f00c0a4221600ad..b8293687267ec9813ab393272ef9a204c8584a87 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
+// +build darwin dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris windows
 
 package net
 
index 8972051f5d5e21f58c2f56fa5aaf1e2fab78f901..8e3543dfc7fd71c733e1d674d1d0e53d1d9ed835 100644 (file)
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !js
+
 package net
 
 import (
index eddd4118fa231ad5eee87759c87a5ffebfb5c4db..cb7483cd1f5b7323927760a47b59e9c3a11b27db 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
+// +build darwin dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris windows
 
 package net
 
index ffd38d79506d65935eaf73aedf5ae0963fe23330..ffce8e22ec92067f58454e74496372155e7493e4 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build !plan9
+// +build !js,!plan9
 
 package net
 
similarity index 98%
rename from src/net/lookup_nacl.go
rename to src/net/lookup_fake.go
index 43cebad76028c93d24a9e8c3013a6d4d775d9817..90c6d4718315357a7fbbd8ff38a86490027773b2 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build nacl
+// +build nacl js,wasm
 
 package net
 
index 75fb303d388186069aafc25bd5e56157e0f3d4d6..02fbcd8bac124720986a32fa01db8e72244d0501 100644 (file)
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !js
+
 package net
 
 import (
index 9875ceaf1fb95f09974b038efe14dcbe86fc652a..b535046bda14ddde8927d442b6b5b83b2de04061 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build !nacl,!plan9,!windows
+// +build !js,!nacl,!plan9,!windows
 
 package net
 
index 489477bc97ccb77a6a9487946f105b03bd3cfdd1..55e37708354eec2886d4ae6becd3baf3bd973436 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build nacl plan9 windows
+// +build js,wasm nacl plan9 windows
 
 package net
 
index ead311c3cddb7d8862586ac36570ab6f855291d3..f2484f306db91dce5a64855f0c14494b2bf08b88 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build !plan9
+// +build !js,!plan9
 
 package net
 
index 3e7a85ad2d9fc461c031d7c13af14f90f952b689..85a269d0f4de103b8283f6bf5978a5bff32f837e 100644 (file)
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !js
+
 package net
 
 import (
index 44581d90498d863b2714d02dda9d27e55c1d5962..530293578a08e67b8649a4cff691127b0216a2fc 100644 (file)
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !js
+
 package net
 
 import (
index 94561dd7492d0489c22d568b566175bf76e587b4..48c500167052c238c32ca4f9484e4fcc858248a3 100644 (file)
@@ -229,7 +229,7 @@ func (c *conn) SetDeadline(t time.Time) error {
        if !c.ok() {
                return syscall.EINVAL
        }
-       if err := c.fd.pfd.SetDeadline(t); err != nil {
+       if err := c.fd.SetDeadline(t); err != nil {
                return &OpError{Op: "set", Net: c.fd.net, Source: nil, Addr: c.fd.laddr, Err: err}
        }
        return nil
@@ -240,7 +240,7 @@ func (c *conn) SetReadDeadline(t time.Time) error {
        if !c.ok() {
                return syscall.EINVAL
        }
-       if err := c.fd.pfd.SetReadDeadline(t); err != nil {
+       if err := c.fd.SetReadDeadline(t); err != nil {
                return &OpError{Op: "set", Net: c.fd.net, Source: nil, Addr: c.fd.laddr, Err: err}
        }
        return nil
@@ -251,7 +251,7 @@ func (c *conn) SetWriteDeadline(t time.Time) error {
        if !c.ok() {
                return syscall.EINVAL
        }
-       if err := c.fd.pfd.SetWriteDeadline(t); err != nil {
+       if err := c.fd.SetWriteDeadline(t); err != nil {
                return &OpError{Op: "set", Net: c.fd.net, Source: nil, Addr: c.fd.laddr, Err: err}
        }
        return nil
diff --git a/src/net/net_fake.go b/src/net/net_fake.go
new file mode 100644 (file)
index 0000000..f7595d9
--- /dev/null
@@ -0,0 +1,284 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Fake networking for js/wasm. It is intended to allow tests of other package to pass.
+
+// +build js,wasm
+
+package net
+
+import (
+       "context"
+       "internal/poll"
+       "io"
+       "os"
+       "sync"
+       "syscall"
+       "time"
+)
+
+var listenersMu sync.Mutex
+var listeners = make(map[string]*netFD)
+
+var portCounterMu sync.Mutex
+var portCounter = 0
+
+func nextPort() int {
+       portCounterMu.Lock()
+       defer portCounterMu.Unlock()
+       portCounter++
+       return portCounter
+}
+
+// Network file descriptor.
+type netFD struct {
+       r        *bufferedPipe
+       w        *bufferedPipe
+       incoming chan *netFD
+
+       closedMu sync.Mutex
+       closed   bool
+
+       // immutable until Close
+       listener bool
+       family   int
+       sotype   int
+       net      string
+       laddr    Addr
+       raddr    Addr
+
+       // unused
+       pfd         poll.FD
+       isConnected bool
+}
+
+// socket returns a network file descriptor that is ready for
+// asynchronous I/O using the network poller.
+func socket(ctx context.Context, net string, family, sotype, proto int, ipv6only bool, laddr, raddr sockaddr, ctrlFn func(string, string, syscall.RawConn) error) (*netFD, error) {
+       fd := &netFD{family: family, sotype: sotype, net: net}
+
+       if laddr != nil && raddr == nil { // listener
+               l := laddr.(*TCPAddr)
+               fd.laddr = &TCPAddr{
+                       IP:   l.IP,
+                       Port: nextPort(),
+                       Zone: l.Zone,
+               }
+               fd.listener = true
+               fd.incoming = make(chan *netFD, 1024)
+               listenersMu.Lock()
+               listeners[fd.laddr.(*TCPAddr).String()] = fd
+               listenersMu.Unlock()
+               return fd, nil
+       }
+
+       fd.laddr = &TCPAddr{
+               IP:   IPv4(127, 0, 0, 1),
+               Port: nextPort(),
+       }
+       fd.raddr = raddr
+       fd.r = newBufferedPipe(65536)
+       fd.w = newBufferedPipe(65536)
+
+       fd2 := &netFD{family: fd.family, sotype: sotype, net: net}
+       fd2.laddr = fd.raddr
+       fd2.raddr = fd.laddr
+       fd2.r = fd.w
+       fd2.w = fd.r
+       listenersMu.Lock()
+       l, ok := listeners[fd.raddr.(*TCPAddr).String()]
+       if !ok {
+               listenersMu.Unlock()
+               return nil, syscall.ECONNREFUSED
+       }
+       l.incoming <- fd2
+       listenersMu.Unlock()
+
+       return fd, nil
+}
+
+func (fd *netFD) Read(p []byte) (n int, err error) {
+       return fd.r.Read(p)
+}
+
+func (fd *netFD) Write(p []byte) (nn int, err error) {
+       return fd.w.Write(p)
+}
+
+func (fd *netFD) Close() error {
+       fd.closedMu.Lock()
+       if fd.closed {
+               fd.closedMu.Unlock()
+               return nil
+       }
+       fd.closed = true
+       fd.closedMu.Unlock()
+
+       if fd.listener {
+               listenersMu.Lock()
+               delete(listeners, fd.laddr.String())
+               close(fd.incoming)
+               fd.listener = false
+               listenersMu.Unlock()
+               return nil
+       }
+
+       fd.r.Close()
+       fd.w.Close()
+       return nil
+}
+
+func (fd *netFD) closeRead() error {
+       fd.r.Close()
+       return nil
+}
+
+func (fd *netFD) closeWrite() error {
+       fd.w.Close()
+       return nil
+}
+
+func (fd *netFD) accept() (*netFD, error) {
+       c, ok := <-fd.incoming
+       if !ok {
+               return nil, syscall.EINVAL
+       }
+       return c, nil
+}
+
+func (fd *netFD) SetDeadline(t time.Time) error {
+       fd.r.SetReadDeadline(t)
+       fd.w.SetWriteDeadline(t)
+       return nil
+}
+
+func (fd *netFD) SetReadDeadline(t time.Time) error {
+       fd.r.SetReadDeadline(t)
+       return nil
+}
+
+func (fd *netFD) SetWriteDeadline(t time.Time) error {
+       fd.w.SetWriteDeadline(t)
+       return nil
+}
+
+func newBufferedPipe(softLimit int) *bufferedPipe {
+       p := &bufferedPipe{softLimit: softLimit}
+       p.rCond.L = &p.mu
+       p.wCond.L = &p.mu
+       return p
+}
+
+type bufferedPipe struct {
+       softLimit int
+       mu        sync.Mutex
+       buf       []byte
+       closed    bool
+       rCond     sync.Cond
+       wCond     sync.Cond
+       rDeadline time.Time
+       wDeadline time.Time
+}
+
+func (p *bufferedPipe) Read(b []byte) (int, error) {
+       p.mu.Lock()
+       defer p.mu.Unlock()
+
+       for {
+               if p.closed && len(p.buf) == 0 {
+                       return 0, io.EOF
+               }
+               if !p.rDeadline.IsZero() {
+                       d := time.Until(p.rDeadline)
+                       if d <= 0 {
+                               return 0, syscall.EAGAIN
+                       }
+                       time.AfterFunc(d, p.rCond.Broadcast)
+               }
+               if len(p.buf) > 0 {
+                       break
+               }
+               p.rCond.Wait()
+       }
+
+       n := copy(b, p.buf)
+       p.buf = p.buf[n:]
+       p.wCond.Broadcast()
+       return n, nil
+}
+
+func (p *bufferedPipe) Write(b []byte) (int, error) {
+       p.mu.Lock()
+       defer p.mu.Unlock()
+
+       for {
+               if p.closed {
+                       return 0, syscall.ENOTCONN
+               }
+               if !p.wDeadline.IsZero() {
+                       d := time.Until(p.wDeadline)
+                       if d <= 0 {
+                               return 0, syscall.EAGAIN
+                       }
+                       time.AfterFunc(d, p.wCond.Broadcast)
+               }
+               if len(p.buf) <= p.softLimit {
+                       break
+               }
+               p.wCond.Wait()
+       }
+
+       p.buf = append(p.buf, b...)
+       p.rCond.Broadcast()
+       return len(b), nil
+}
+
+func (p *bufferedPipe) Close() {
+       p.mu.Lock()
+       defer p.mu.Unlock()
+
+       p.closed = true
+       p.rCond.Broadcast()
+       p.wCond.Broadcast()
+}
+
+func (p *bufferedPipe) SetReadDeadline(t time.Time) {
+       p.mu.Lock()
+       defer p.mu.Unlock()
+
+       p.rDeadline = t
+       p.rCond.Broadcast()
+}
+
+func (p *bufferedPipe) SetWriteDeadline(t time.Time) {
+       p.mu.Lock()
+       defer p.mu.Unlock()
+
+       p.wDeadline = t
+       p.wCond.Broadcast()
+}
+
+func sysSocket(family, sotype, proto int) (int, error) {
+       return 0, syscall.ENOSYS
+}
+
+func (fd *netFD) readFrom(p []byte) (n int, sa syscall.Sockaddr, err error) {
+       return 0, nil, syscall.ENOSYS
+}
+
+func (fd *netFD) readMsg(p []byte, oob []byte) (n, oobn, flags int, sa syscall.Sockaddr, err error) {
+       return 0, 0, 0, nil, syscall.ENOSYS
+}
+
+func (fd *netFD) writeTo(p []byte, sa syscall.Sockaddr) (n int, err error) {
+       return 0, syscall.ENOSYS
+}
+
+func (fd *netFD) writeMsg(p []byte, oob []byte, sa syscall.Sockaddr) (n int, oobn int, err error) {
+       return 0, 0, syscall.ENOSYS
+}
+
+func (fd *netFD) dup() (f *os.File, err error) {
+       return nil, syscall.ENOSYS
+}
index c82aa070a2d34970f2a3486d9b0886afd0f37620..facafcf12c99d2971ab7fd99e997ecb09f5a8b27 100644 (file)
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !js
+
 package net
 
 import (
index 7d504890217baf5c62fc9ca95aa34e0019a8a544..a377d333d6afe312e66d32c9c6d506baecbc9927 100644 (file)
@@ -5,6 +5,8 @@
 // This file implements API tests across platforms and will never have a build
 // tag.
 
+// +build !js
+
 package net
 
 import (
index 829f51fcf0f438260a96440d286ed8dd2acf7f8a..64c7f575c7f4d236cf16d16aa196d4896488cffb 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build darwin dragonfly freebsd linux netbsd openbsd solaris nacl
+// +build darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris nacl
 
 // Read system port mappings from /etc/services
 
index 05c45d02b9a0f193f2679fda7962e1147333694c..9f6772c7d15e08ab0d10f61d5bb9aaeea878442b 100644 (file)
@@ -5,6 +5,8 @@
 // This file implements API tests across platforms and will never have a build
 // tag.
 
+// +build !js
+
 package net
 
 import (
index 3e3b6bf5b2a1aa6260791d042621791031a4fb82..0a033c12fc5bb8e35824764a7c60e7e30cc07230 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build nacl plan9
+// +build js,wasm nacl plan9
 
 package net
 
index ecd9457c741cf026db849b201b1276cfd882d37f..11900dff8dd0811524f94202480b861a4e2c9c1d 100644 (file)
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !js
+
 package net
 
 import (
index 905f1d6cef27a33b3653918df86a7eb1d584f3fd..d5b8755af3e4e0f2d435b0dc0fbdbba3d226860c 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build darwin nacl netbsd openbsd
+// +build darwin js,wasm nacl netbsd openbsd
 
 package net
 
index 2255e7c47854a67ae3c02a8d8bd0751a42cf0203..75d4b4e9bf2469fef51c36831ff1c0165c34d64d 100644 (file)
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !js
+
 package net
 
 import (
index 2e998e23a8a0aba1bb3f3687d7fc0d9bfa6235db..1608bebb00caa979f5b9760baba6093f16d0ed6d 100644 (file)
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !js
+
 package net
 
 import (
index e97aaa44863ee381055d4e8487653610eb740c85..000cac4fcbbfee07b8017d4259743ace92f78e96 100644 (file)
@@ -725,7 +725,7 @@ QUIT
 `
 
 func TestTLSClient(t *testing.T) {
-       if runtime.GOOS == "freebsd" && runtime.GOARCH == "amd64" {
+       if (runtime.GOOS == "freebsd" && runtime.GOARCH == "amd64") || runtime.GOOS == "js" {
                testenv.SkipFlaky(t, 19229)
        }
        ln := newLocalListener(t)
index 00ff3fd39394ce902a55031d38a04e35efd393b2..677e423ffaff2b353abb458f906fe86217832175 100644 (file)
@@ -13,29 +13,6 @@ import (
        "syscall"
 )
 
-// A sockaddr represents a TCP, UDP, IP or Unix network endpoint
-// address that can be converted into a syscall.Sockaddr.
-type sockaddr interface {
-       Addr
-
-       // family returns the platform-dependent address family
-       // identifier.
-       family() int
-
-       // isWildcard reports whether the address is a wildcard
-       // address.
-       isWildcard() bool
-
-       // sockaddr returns the address converted into a syscall
-       // sockaddr type that implements syscall.Sockaddr
-       // interface. It returns a nil interface when the address is
-       // nil.
-       sockaddr(family int) (syscall.Sockaddr, error)
-
-       // toLocal maps the zero address to a local system address (127.0.0.1 or ::1)
-       toLocal(net string) sockaddr
-}
-
 // socket returns a network file descriptor that is ready for
 // asynchronous I/O using the network poller.
 func socket(ctx context.Context, net string, family, sotype, proto int, ipv6only bool, laddr, raddr sockaddr, ctrlFn func(string, string, syscall.RawConn) error) (fd *netFD, err error) {
index 5ac1e864f7ffb4cf3c608231c384a98cf4d4f79e..38fc819199ccb8f55f6b1486195f757c6f145922 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build nacl solaris
+// +build nacl js,wasm solaris
 
 package net
 
diff --git a/src/net/sockaddr_posix.go b/src/net/sockaddr_posix.go
new file mode 100644 (file)
index 0000000..4b8699d
--- /dev/null
@@ -0,0 +1,34 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build darwin dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris windows
+
+package net
+
+import (
+       "syscall"
+)
+
+// A sockaddr represents a TCP, UDP, IP or Unix network endpoint
+// address that can be converted into a syscall.Sockaddr.
+type sockaddr interface {
+       Addr
+
+       // family returns the platform-dependent address family
+       // identifier.
+       family() int
+
+       // isWildcard reports whether the address is a wildcard
+       // address.
+       isWildcard() bool
+
+       // sockaddr returns the address converted into a syscall
+       // sockaddr type that implements syscall.Sockaddr
+       // interface. It returns a nil interface when the address is
+       // nil.
+       sockaddr(family int) (syscall.Sockaddr, error)
+
+       // toLocal maps the zero address to a local system address (127.0.0.1 or ::1)
+       toLocal(net string) sockaddr
+}
index 7e9e560e059e11c957be8f57114fed5c6f74493e..bc0667504affccef4d851c2c83a6815d6e603adc 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build nacl
+// +build nacl js,wasm
 
 package net
 
index fc20a9fc33139ccd2fb8351fe75ecb459af06524..3297969314e911a3a9d0a18923d5f3a8f9d0837b 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build nacl
+// +build nacl js,wasm
 
 package net
 
index e6e7144f203b89e030f183be806fbceb8ead5fdf..936a255b88d4c2a26afda7a25d8326ab4d9dde5e 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
+// +build darwin dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris windows
 
 package net
 
index b85ffa62e18ed34876cfe021fbc5d6adc44b8662..c2f26b1770765d53b61cfd471f7bf0a35dd97819 100644 (file)
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !js
+
 package net
 
 import (
index 8d8e4e8eb8b2bad2b0d4b94c83742076677d59b0..2bd591b5944f227b841dd609477bb1026d7b7dd5 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build !plan9,!windows
+// +build !js,!plan9,!windows
 
 package net
 
index 19c83e6a5fa60dc1d02aeb7b74a60fa3aa5c0f66..fd7f57923ba9b7094cf82f3463cf29787f67d62e 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build nacl
+// +build nacl js,wasm
 
 package net
 
index 0ecf5a6d251400ad7bdc8e18257fc935a308fb11..7c7d0c89938c96acf10907f936240ce341eb8395 100644 (file)
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !js
+
 package net
 
 import (
index 8f4b71c01ec8023e49fd98cb4938a34c09337ce6..b0adf9585bd3289ff5e4a9903033b4f59ed743d0 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
+// +build darwin dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris windows
 
 package net
 
index d34c5450966f64ab9d69f3dca11de0de0dd859ed..494064444ec15f7c1b7ea892e33cbb38e15aece3 100644 (file)
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !js
+
 package net
 
 import (
index 2495da1d253fa675fe9315c4374083886ffd7289..b31ad49f25f58e986638ab6dc313317854dc3c5b 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
+// +build darwin dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris windows
 
 package net
 
index 3e5c8bc3769c6b25e2ffadf760019ccd5ee0c3eb..482899031077d1bba723add62832da1c6c931daf 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build !nacl,!plan9,!windows
+// +build !js,!nacl,!plan9,!windows
 
 package net
 
index 4c05be473d9ecd681ebbdde2d0de4d9932660759..c43be844189f7af7254b4e68409372139cade1e9 100644 (file)
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !js
+
 package net
 
 import (
index efcd2b855c15ad3887938eba46f8f56b53396c67..c8fb95d3aaf5db07fa440ba835ee49e194fed6a8 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
+// +build darwin dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris windows
 
 package runtime
 
similarity index 73%
rename from src/runtime/netpoll_nacl.go
rename to src/runtime/netpoll_fake.go
index dc5a55ec8469822a9c51d8a3d466f616a041cd48..aab18dc8468cd5e4345f3e5be5d3e5a781617672 100644 (file)
@@ -2,8 +2,10 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// Fake network poller for NaCl.
-// Should never be used, because NaCl network connections do not honor "SetNonblock".
+// Fake network poller for NaCl and wasm/js.
+// Should never be used, because NaCl and wasm/js network connections do not honor "SetNonblock".
+
+// +build nacl js,wasm
 
 package runtime
 
diff --git a/src/syscall/net_js.go b/src/syscall/net_js.go
new file mode 100644 (file)
index 0000000..0149e2b
--- /dev/null
@@ -0,0 +1,121 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// js/wasm uses fake networking directly implemented in the net package.
+// This file only exists to make the compiler happy.
+
+// +build js,wasm
+
+package syscall
+
+const (
+       AF_UNSPEC = iota
+       AF_UNIX
+       AF_INET
+       AF_INET6
+)
+
+const (
+       SOCK_STREAM = 1 + iota
+       SOCK_DGRAM
+       SOCK_RAW
+       SOCK_SEQPACKET
+)
+
+const (
+       IPPROTO_IP   = 0
+       IPPROTO_IPV4 = 4
+       IPPROTO_IPV6 = 0x29
+       IPPROTO_TCP  = 6
+       IPPROTO_UDP  = 0x11
+)
+
+const (
+       _ = iota
+       IPV6_V6ONLY
+       SOMAXCONN
+       SO_ERROR
+)
+
+type Sockaddr interface {
+}
+
+type SockaddrInet4 struct {
+       Port int
+       Addr [4]byte
+}
+
+type SockaddrInet6 struct {
+       Port   int
+       ZoneId uint32
+       Addr   [16]byte
+}
+
+type SockaddrUnix struct {
+       Name string
+}
+
+func Socket(proto, sotype, unused int) (fd int, err error) {
+       return 0, ENOSYS
+}
+
+func Bind(fd int, sa Sockaddr) error {
+       return ENOSYS
+}
+
+func StopIO(fd int) error {
+       return ENOSYS
+}
+
+func Listen(fd int, backlog int) error {
+       return ENOSYS
+}
+
+func Accept(fd int) (newfd int, sa Sockaddr, err error) {
+       return 0, nil, ENOSYS
+}
+
+func Connect(fd int, sa Sockaddr) error {
+       return ENOSYS
+}
+
+func Recvfrom(fd int, p []byte, flags int) (n int, from Sockaddr, err error) {
+       return 0, nil, ENOSYS
+}
+
+func Sendto(fd int, p []byte, flags int, to Sockaddr) error {
+       return ENOSYS
+}
+
+func Recvmsg(fd int, p, oob []byte, flags int) (n, oobn, recvflags int, from Sockaddr, err error) {
+       return 0, 0, 0, nil, ENOSYS
+}
+
+func SendmsgN(fd int, p, oob []byte, to Sockaddr, flags int) (n int, err error) {
+       return 0, ENOSYS
+}
+
+func GetsockoptInt(fd, level, opt int) (value int, err error) {
+       return 0, ENOSYS
+}
+
+func SetsockoptInt(fd, level, opt int, value int) error {
+       return nil
+}
+
+func SetReadDeadline(fd int, t int64) error {
+       return ENOSYS
+}
+
+func SetWriteDeadline(fd int, t int64) error {
+       return ENOSYS
+}
+
+func Shutdown(fd int, how int) error {
+       return ENOSYS
+}
+
+func SetNonblock(fd int, nonblocking bool) error {
+       return nil
+}