aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--src/os/file.go40
-rw-r--r--src/os/file_unix.go24
-rw-r--r--src/os/file_wasm.go34
-rw-r--r--testdata/stdlib.go12
-rw-r--r--testdata/stdlib.txt3
6 files changed, 114 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index c42eb805f..f61942682 100644
--- a/Makefile
+++ b/Makefile
@@ -82,7 +82,7 @@ clean:
@rm -rf build
fmt:
- @go fmt . ./compiler ./interp ./loader ./ir ./src/device/arm ./src/examples/* ./src/machine ./src/runtime ./src/sync
+ @go fmt . ./compiler ./interp ./loader ./ir ./src/device/arm ./src/examples/* ./src/machine ./src/os ./src/runtime ./src/sync
@go fmt ./testdata/*.go
test:
diff --git a/src/os/file.go b/src/os/file.go
new file mode 100644
index 000000000..c275a39ed
--- /dev/null
+++ b/src/os/file.go
@@ -0,0 +1,40 @@
+// Package os implements a subset of the Go "os" package. See
+// https://godoc.org/os for details.
+//
+// Note that the current implementation is blocking. This limitation should be
+// removed in a future version.
+package os
+
+import (
+ "errors"
+)
+
+// Portable analogs of some common system call errors.
+var (
+ ErrUnsupported = errors.New("operation not supported")
+)
+
+// Stdin, Stdout, and Stderr are open Files pointing to the standard input,
+// standard output, and standard error file descriptors.
+var (
+ Stdin = &File{0, "/dev/stdin"}
+ Stdout = &File{1, "/dev/stdout"}
+ Stderr = &File{2, "/dev/stderr"}
+)
+
+// File represents an open file descriptor.
+type File struct {
+ fd uintptr
+ name string
+}
+
+// NewFile returns a new File with the given file descriptor and name.
+func NewFile(fd uintptr, name string) *File {
+ return &File{fd, name}
+}
+
+// Fd returns the integer Unix file descriptor referencing the open file. The
+// file descriptor is valid only until f.Close is called.
+func (f *File) Fd() uintptr {
+ return f.fd
+}
diff --git a/src/os/file_unix.go b/src/os/file_unix.go
new file mode 100644
index 000000000..1f1cb71c7
--- /dev/null
+++ b/src/os/file_unix.go
@@ -0,0 +1,24 @@
+// +build linux
+
+package os
+
+import (
+ "syscall"
+)
+
+// Read reads up to len(b) bytes from the File. It returns the number of bytes
+// read and any error encountered. At end of file, Read returns 0, io.EOF.
+func (f *File) Read(b []byte) (n int, err error) {
+ return syscall.Read(int(f.fd), b)
+}
+
+// Write writes len(b) bytes to the File. It returns the number of bytes written
+// and an error, if any. Write returns a non-nil error when n != len(b).
+func (f *File) Write(b []byte) (n int, err error) {
+ return syscall.Write(int(f.fd), b)
+}
+
+// Close closes the File, rendering it unusable for I/O.
+func (f *File) Close() error {
+ return syscall.Close(int(f.fd))
+}
diff --git a/src/os/file_wasm.go b/src/os/file_wasm.go
new file mode 100644
index 000000000..995e19e70
--- /dev/null
+++ b/src/os/file_wasm.go
@@ -0,0 +1,34 @@
+// +build wasm
+
+package os
+
+import (
+ _ "unsafe"
+)
+
+// Read is unsupported on this system.
+func (f *File) Read(b []byte) (n int, err error) {
+ return 0, ErrUnsupported
+}
+
+// Write writes len(b) bytes to the output. It returns the number of bytes
+// written or an error if this file is not stdout or stderr.
+func (f *File) Write(b []byte) (n int, err error) {
+ switch f.fd {
+ case Stdout.fd, Stderr.fd:
+ for _, c := range b {
+ putchar(c)
+ }
+ return len(b), nil
+ default:
+ return 0, ErrUnsupported
+ }
+}
+
+// Close is unsupported on this system.
+func (f *File) Close() error {
+ return ErrUnsupported
+}
+
+//go:linkname putchar runtime.putchar
+func putchar(c byte)
diff --git a/testdata/stdlib.go b/testdata/stdlib.go
new file mode 100644
index 000000000..29c9c9e10
--- /dev/null
+++ b/testdata/stdlib.go
@@ -0,0 +1,12 @@
+package main
+
+import (
+ "fmt"
+ "os"
+)
+
+func main() {
+ fmt.Println("stdin: ", os.Stdin.Fd())
+ fmt.Println("stdout:", os.Stdout.Fd())
+ fmt.Println("stderr:", os.Stderr.Fd())
+}
diff --git a/testdata/stdlib.txt b/testdata/stdlib.txt
new file mode 100644
index 000000000..3db2f1874
--- /dev/null
+++ b/testdata/stdlib.txt
@@ -0,0 +1,3 @@
+stdin: 0
+stdout: 1
+stderr: 2