github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/syscall/syscall_nonhosted.go (about)

     1  //go:build baremetal || js || wasm_unknown
     2  
     3  package syscall
     4  
     5  import (
     6  	"internal/itoa"
     7  )
     8  
     9  // Most code here has been copied from the Go sources:
    10  //   https://github.com/golang/go/blob/go1.12/src/syscall/syscall_js.go
    11  // It has the following copyright note:
    12  //
    13  //     Copyright 2018 The Go Authors. All rights reserved.
    14  //     Use of this source code is governed by a BSD-style
    15  //     license that can be found in the LICENSE file.
    16  
    17  // A Signal is a number describing a process signal.
    18  // It implements the os.Signal interface.
    19  type Signal int
    20  
    21  const (
    22  	_ Signal = iota
    23  	SIGCHLD
    24  	SIGINT
    25  	SIGKILL
    26  	SIGTRAP
    27  	SIGQUIT
    28  	SIGTERM
    29  	SIGILL
    30  	SIGABRT
    31  	SIGBUS
    32  	SIGFPE
    33  	SIGSEGV
    34  	SIGPIPE
    35  )
    36  
    37  func (s Signal) Signal() {}
    38  
    39  func (s Signal) String() string {
    40  	if 0 <= s && int(s) < len(signals) {
    41  		str := signals[s]
    42  		if str != "" {
    43  			return str
    44  		}
    45  	}
    46  	return "signal " + itoa.Itoa(int(s))
    47  }
    48  
    49  var signals = [...]string{}
    50  
    51  // File system
    52  
    53  const (
    54  	Stdin  = 0
    55  	Stdout = 1
    56  	Stderr = 2
    57  )
    58  
    59  const (
    60  	O_RDONLY = 0
    61  	O_WRONLY = 1
    62  	O_RDWR   = 2
    63  
    64  	O_CREAT  = 0100
    65  	O_CREATE = O_CREAT
    66  	O_TRUNC  = 01000
    67  	O_APPEND = 02000
    68  	O_EXCL   = 0200
    69  	O_SYNC   = 010000
    70  
    71  	O_CLOEXEC = 0
    72  )
    73  
    74  // Dummy values to allow compiling tests
    75  // Dummy source: https://opensource.apple.com/source/xnu/xnu-7195.81.3/bsd/sys/mman.h.auto.html
    76  const (
    77  	PROT_NONE  = 0x00 // no permissions
    78  	PROT_READ  = 0x01 // pages can be read
    79  	PROT_WRITE = 0x02 // pages can be written
    80  	PROT_EXEC  = 0x04 // pages can be executed
    81  
    82  	MAP_SHARED  = 0x0001 // share changes
    83  	MAP_PRIVATE = 0x0002 // changes are private
    84  
    85  	MAP_FILE      = 0x0000 // map from file (default)
    86  	MAP_ANON      = 0x1000 // allocated from memory, swap space
    87  	MAP_ANONYMOUS = MAP_ANON
    88  )
    89  
    90  func runtime_envs() []string
    91  
    92  func Getenv(key string) (value string, found bool) {
    93  	env := runtime_envs()
    94  	for _, keyval := range env {
    95  		// Split at '=' character.
    96  		var k, v string
    97  		for i := 0; i < len(keyval); i++ {
    98  			if keyval[i] == '=' {
    99  				k = keyval[:i]
   100  				v = keyval[i+1:]
   101  			}
   102  		}
   103  		if k == key {
   104  			return v, true
   105  		}
   106  	}
   107  	return "", false
   108  }
   109  
   110  func Setenv(key, val string) (err error) {
   111  	// stub for now
   112  	return ENOSYS
   113  }
   114  
   115  func Unsetenv(key string) (err error) {
   116  	// stub for now
   117  	return ENOSYS
   118  }
   119  
   120  func Clearenv() (err error) {
   121  	// stub for now
   122  	return ENOSYS
   123  }
   124  
   125  func Environ() []string {
   126  	env := runtime_envs()
   127  	envCopy := make([]string, len(env))
   128  	copy(envCopy, env)
   129  	return envCopy
   130  }
   131  
   132  func Open(path string, mode int, perm uint32) (fd int, err error) {
   133  	return 0, ENOSYS
   134  }
   135  
   136  func Read(fd int, p []byte) (n int, err error) {
   137  	return 0, ENOSYS
   138  }
   139  
   140  func Seek(fd int, offset int64, whence int) (off int64, err error) {
   141  	return 0, ENOSYS
   142  }
   143  
   144  func Close(fd int) (err error) {
   145  	return ENOSYS
   146  }
   147  
   148  // Processes
   149  
   150  type WaitStatus uint32
   151  
   152  func (w WaitStatus) Exited() bool       { return false }
   153  func (w WaitStatus) ExitStatus() int    { return 0 }
   154  func (w WaitStatus) Signaled() bool     { return false }
   155  func (w WaitStatus) Signal() Signal     { return 0 }
   156  func (w WaitStatus) CoreDump() bool     { return false }
   157  func (w WaitStatus) Stopped() bool      { return false }
   158  func (w WaitStatus) Continued() bool    { return false }
   159  func (w WaitStatus) StopSignal() Signal { return 0 }
   160  func (w WaitStatus) TrapCause() int     { return 0 }
   161  
   162  // XXX made up
   163  type Rusage struct {
   164  	Utime Timeval
   165  	Stime Timeval
   166  }
   167  
   168  // XXX made up
   169  type ProcAttr struct {
   170  	Dir   string
   171  	Env   []string
   172  	Files []uintptr
   173  	Sys   *SysProcAttr
   174  }
   175  
   176  type SysProcAttr struct {
   177  }
   178  
   179  func Getgroups() ([]int, error)         { return []int{1}, nil }
   180  func Gettimeofday(tv *Timeval) error    { return ENOSYS }
   181  func Kill(pid int, signum Signal) error { return ENOSYS }
   182  func Pipe2(p []int, flags int) (err error) {
   183  	return ENOSYS // TODO
   184  }
   185  func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
   186  	return 0, ENOSYS
   187  }
   188  func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle uintptr, err error) {
   189  	return 0, 0, ENOSYS
   190  }
   191  func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
   192  	return 0, ENOSYS
   193  }
   194  
   195  func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
   196  	return nil, ENOSYS
   197  }
   198  
   199  func Munmap(b []byte) (err error) {
   200  	return ENOSYS
   201  }
   202  
   203  type Timeval struct {
   204  	Sec  int64
   205  	Usec int64
   206  }
   207  
   208  func Getpagesize() int {
   209  	// There is no right value to return here, but 4096 is a
   210  	// common assumption when pagesize is unknown
   211  	return 4096
   212  }
   213  
   214  type RawSockaddrInet4 struct {
   215  	// stub
   216  }
   217  
   218  type RawSockaddrInet6 struct {
   219  	// stub
   220  }