Skip to content

Go1.16 support for several more stdlib packages #992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ func importWithSrcDir(bctx build.Context, path string, srcDir string, mode build
switch path {
case "os":
pkg.GoFiles = excludeExecutable(pkg.GoFiles) // Need to exclude executable implementation files, because some of them contain package scope variables that perform (indirectly) syscalls on init.
// Prefer dirent_js.go version, since it targets a similar environment to
// ours. Arguably this file should be excluded by the build tags (see
// https://github.com/gopherjs/gopherjs/issues/693).
pkg.GoFiles = exclude(pkg.GoFiles, "dirent_linux.go")
case "runtime":
pkg.GoFiles = []string{} // Package sources are completely replaced in natives.
case "runtime/internal/sys":
Expand All @@ -183,6 +187,10 @@ func importWithSrcDir(bctx build.Context, path string, srcDir string, mode build
pkg.GoFiles = nil
case "internal/poll":
pkg.GoFiles = exclude(pkg.GoFiles, "fd_poll_runtime.go")
case "sync":
// GopherJS completely replaces sync.Pool implementation with a simpler one,
// since it always executes in a single-threaded environment.
pkg.GoFiles = exclude(pkg.GoFiles, "pool.go")
case "crypto/rand":
pkg.GoFiles = []string{"rand.go", "util.go"}
pkg.TestGoFiles = exclude(pkg.TestGoFiles, "rand_linux_test.go") // Don't want linux-specific tests (since linux-specific package files are excluded too).
Expand Down
2 changes: 1 addition & 1 deletion compiler/natives/src/internal/poll/fd_poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (pd *pollDesc) wait(mode int, isFile bool) error {
if pd.closing {
return errClosing(isFile)
}
return ErrTimeout
return ErrDeadlineExceeded
}

func (pd *pollDesc) waitRead(isFile bool) error { return pd.wait('r', isFile) }
Expand Down
8 changes: 6 additions & 2 deletions compiler/natives/src/internal/syscall/unix/unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ package unix

import "syscall"

const randomTrap = 0
const fstatatTrap = 0
const (
randomTrap uintptr = 0
fstatatTrap uintptr = 0
getrandomTrap uintptr = 0
copyFileRangeTrap uintptr = 0
)

func IsNonblock(fd int) (nonblocking bool, err error) {
return false, nil
Expand Down
8 changes: 4 additions & 4 deletions compiler/natives/src/math/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
)

var math = js.Global.Get("Math")
var zero float64 = 0
var posInf = 1 / zero
var negInf = -1 / zero
var nan = 0 / zero
var _zero float64 = 0
var posInf = 1 / _zero
var negInf = -1 / _zero
var nan = 0 / _zero

func Acos(x float64) float64 {
return math.Call("acos", x).Float()
Expand Down
2 changes: 2 additions & 0 deletions compiler/natives/src/os/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/gopherjs/gopherjs/js"
)

const isBigEndian = false

func runtime_args() []string { // not called on Windows
return Args
}
Expand Down
26 changes: 18 additions & 8 deletions compiler/natives/src/sync/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,25 @@

package sync

import "unsafe"

// A Pool is a set of temporary objects that may be individually saved and
// retrieved.
//
// GopherJS provides a simpler, naive implementation with no synchronization at
// all. This is still correct for the GopherJS runtime because:
//
// 1. JavaScript is single-threaded, so it is impossible for two threads to be
// accessing the pool at the same moment in time.
// 2. GopherJS goroutine implementation uses cooperative multi-tasking model,
// which only allows passing control to other goroutines when the function
// might block.
//
// TODO(nevkontakte): Consider adding a mutex just to be safe if it doesn't
// create a large performance hit.
//
// Note: there is a special handling in the gopherjs/build package that filters
// out all original Pool implementation in order to avoid awkward unused fields
// referenced by dead code.
type Pool struct {
local unsafe.Pointer
localSize uintptr

store []interface{}
New func() interface{}
}
Expand All @@ -30,6 +43,3 @@ func (p *Pool) Put(x interface{}) {
}
p.store = append(p.store, x)
}

func runtime_registerPoolCleanup(cleanup func()) {
}
6 changes: 3 additions & 3 deletions compiler/natives/src/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ var semWaiters = make(map[*uint32][]chan bool)
var semAwoken = make(map[*uint32]uint32)

func runtime_Semacquire(s *uint32) {
runtime_SemacquireMutex(s, false)
runtime_SemacquireMutex(s, false, 1)
}

// SemacquireMutex is like Semacquire, but for profiling contended Mutexes.
// Mutex profiling is not supported, so just use the same implementation as runtime_Semacquire.
// TODO: Investigate this. If it's possible to implement, consider doing so, otherwise remove this comment.
func runtime_SemacquireMutex(s *uint32, lifo bool) {
func runtime_SemacquireMutex(s *uint32, lifo bool, skipframes int) {
if (*s - semAwoken[s]) == 0 {
ch := make(chan bool)
if lifo {
Expand All @@ -41,7 +41,7 @@ func runtime_SemacquireMutex(s *uint32, lifo bool) {
*s--
}

func runtime_Semrelease(s *uint32, handoff bool) {
func runtime_Semrelease(s *uint32, handoff bool, skipframes int) {
// TODO: Use handoff if needed/possible.
*s++

Expand Down
17 changes: 17 additions & 0 deletions compiler/natives/src/time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type runtimeTimer struct {
period int64
f func(interface{}, uintptr)
arg interface{}
seq uintptr
timeout *js.Object
active bool
}
Expand Down Expand Up @@ -79,6 +80,22 @@ func stopTimer(t *runtimeTimer) bool {
return wasActive
}

func modTimer(t *runtimeTimer, when, period int64, f func(interface{}, uintptr), arg interface{}, seq uintptr) {
stopTimer(t)
t.when = when
t.period = period
t.f = f
t.arg = arg
t.seq = seq
startTimer(t)
}

func resetTimer(t *runtimeTimer, when int64) bool {
wasActive := t.active
modTimer(t, when, t.period, t.f, t.arg, t.seq)
return wasActive
}

func forceZipFileForTesting(zipOnly bool) {
}

Expand Down