diff options
author | Ayke van Laethem <[email protected]> | 2019-02-05 00:48:56 +0100 |
---|---|---|
committer | Ron Evans <[email protected]> | 2019-02-05 17:37:55 +0100 |
commit | 709a2961507c0d28a86384c9867e6faec6b5d078 (patch) | |
tree | c617b870a72a26eec45e865ba2fc46a041f064f0 | |
parent | f7b2a2c977bcca51dfd359894f7116c447a88572 (diff) | |
download | tinygo-709a2961507c0d28a86384c9867e6faec6b5d078.tar.gz tinygo-709a2961507c0d28a86384c9867e6faec6b5d078.zip |
os: add basic OS functionality
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | src/os/file.go | 40 | ||||
-rw-r--r-- | src/os/file_unix.go | 24 | ||||
-rw-r--r-- | src/os/file_wasm.go | 34 | ||||
-rw-r--r-- | testdata/stdlib.go | 12 | ||||
-rw-r--r-- | testdata/stdlib.txt | 3 |
6 files changed, 114 insertions, 1 deletions
@@ -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 |