...

Source file src/syscall/syscall_js.go

     1	// Copyright 2018 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	// +build js,wasm
     6	
     7	package syscall
     8	
     9	import (
    10		"internal/oserror"
    11		"sync"
    12		"unsafe"
    13	)
    14	
    15	const direntSize = 8 + 8 + 2 + 256
    16	
    17	type Dirent struct {
    18		Reclen uint16
    19		Name   [256]byte
    20	}
    21	
    22	func direntIno(buf []byte) (uint64, bool) {
    23		return 1, true
    24	}
    25	
    26	func direntReclen(buf []byte) (uint64, bool) {
    27		return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
    28	}
    29	
    30	func direntNamlen(buf []byte) (uint64, bool) {
    31		reclen, ok := direntReclen(buf)
    32		if !ok {
    33			return 0, false
    34		}
    35		return reclen - uint64(unsafe.Offsetof(Dirent{}.Name)), true
    36	}
    37	
    38	const PathMax = 256
    39	
    40	// An Errno is an unsigned number describing an error condition.
    41	// It implements the error interface. The zero Errno is by convention
    42	// a non-error, so code to convert from Errno to error should use:
    43	//	err = nil
    44	//	if errno != 0 {
    45	//		err = errno
    46	//	}
    47	type Errno uintptr
    48	
    49	func (e Errno) Error() string {
    50		if 0 <= int(e) && int(e) < len(errorstr) {
    51			s := errorstr[e]
    52			if s != "" {
    53				return s
    54			}
    55		}
    56		return "errno " + itoa(int(e))
    57	}
    58	
    59	func (e Errno) Is(target error) bool {
    60		switch target {
    61		case oserror.ErrPermission:
    62			return e == EACCES || e == EPERM
    63		case oserror.ErrExist:
    64			return e == EEXIST || e == ENOTEMPTY
    65		case oserror.ErrNotExist:
    66			return e == ENOENT
    67		}
    68		return false
    69	}
    70	
    71	func (e Errno) Temporary() bool {
    72		return e == EINTR || e == EMFILE || e.Timeout()
    73	}
    74	
    75	func (e Errno) Timeout() bool {
    76		return e == EAGAIN || e == EWOULDBLOCK || e == ETIMEDOUT
    77	}
    78	
    79	// A Signal is a number describing a process signal.
    80	// It implements the os.Signal interface.
    81	type Signal int
    82	
    83	const (
    84		_ Signal = iota
    85		SIGCHLD
    86		SIGINT
    87		SIGKILL
    88		SIGTRAP
    89		SIGQUIT
    90		SIGTERM
    91	)
    92	
    93	func (s Signal) Signal() {}
    94	
    95	func (s Signal) String() string {
    96		if 0 <= s && int(s) < len(signals) {
    97			str := signals[s]
    98			if str != "" {
    99				return str
   100			}
   101		}
   102		return "signal " + itoa(int(s))
   103	}
   104	
   105	var signals = [...]string{}
   106	
   107	// File system
   108	
   109	const (
   110		Stdin  = 0
   111		Stdout = 1
   112		Stderr = 2
   113	)
   114	
   115	const (
   116		O_RDONLY = 0
   117		O_WRONLY = 1
   118		O_RDWR   = 2
   119	
   120		O_CREAT  = 0100
   121		O_CREATE = O_CREAT
   122		O_TRUNC  = 01000
   123		O_APPEND = 02000
   124		O_EXCL   = 0200
   125		O_SYNC   = 010000
   126	
   127		O_CLOEXEC = 0
   128	)
   129	
   130	const (
   131		F_DUPFD   = 0
   132		F_GETFD   = 1
   133		F_SETFD   = 2
   134		F_GETFL   = 3
   135		F_SETFL   = 4
   136		F_GETOWN  = 5
   137		F_SETOWN  = 6
   138		F_GETLK   = 7
   139		F_SETLK   = 8
   140		F_SETLKW  = 9
   141		F_RGETLK  = 10
   142		F_RSETLK  = 11
   143		F_CNVT    = 12
   144		F_RSETLKW = 13
   145	
   146		F_RDLCK   = 1
   147		F_WRLCK   = 2
   148		F_UNLCK   = 3
   149		F_UNLKSYS = 4
   150	)
   151	
   152	const (
   153		S_IFMT        = 0000370000
   154		S_IFSHM_SYSV  = 0000300000
   155		S_IFSEMA      = 0000270000
   156		S_IFCOND      = 0000260000
   157		S_IFMUTEX     = 0000250000
   158		S_IFSHM       = 0000240000
   159		S_IFBOUNDSOCK = 0000230000
   160		S_IFSOCKADDR  = 0000220000
   161		S_IFDSOCK     = 0000210000
   162	
   163		S_IFSOCK = 0000140000
   164		S_IFLNK  = 0000120000
   165		S_IFREG  = 0000100000
   166		S_IFBLK  = 0000060000
   167		S_IFDIR  = 0000040000
   168		S_IFCHR  = 0000020000
   169		S_IFIFO  = 0000010000
   170	
   171		S_UNSUP = 0000370000
   172	
   173		S_ISUID = 0004000
   174		S_ISGID = 0002000
   175		S_ISVTX = 0001000
   176	
   177		S_IREAD  = 0400
   178		S_IWRITE = 0200
   179		S_IEXEC  = 0100
   180	
   181		S_IRWXU = 0700
   182		S_IRUSR = 0400
   183		S_IWUSR = 0200
   184		S_IXUSR = 0100
   185	
   186		S_IRWXG = 070
   187		S_IRGRP = 040
   188		S_IWGRP = 020
   189		S_IXGRP = 010
   190	
   191		S_IRWXO = 07
   192		S_IROTH = 04
   193		S_IWOTH = 02
   194		S_IXOTH = 01
   195	)
   196	
   197	type Stat_t struct {
   198		Dev       int64
   199		Ino       uint64
   200		Mode      uint32
   201		Nlink     uint32
   202		Uid       uint32
   203		Gid       uint32
   204		Rdev      int64
   205		Size      int64
   206		Blksize   int32
   207		Blocks    int32
   208		Atime     int64
   209		AtimeNsec int64
   210		Mtime     int64
   211		MtimeNsec int64
   212		Ctime     int64
   213		CtimeNsec int64
   214	}
   215	
   216	// Processes
   217	// Not supported - just enough for package os.
   218	
   219	var ForkLock sync.RWMutex
   220	
   221	type WaitStatus uint32
   222	
   223	func (w WaitStatus) Exited() bool       { return false }
   224	func (w WaitStatus) ExitStatus() int    { return 0 }
   225	func (w WaitStatus) Signaled() bool     { return false }
   226	func (w WaitStatus) Signal() Signal     { return 0 }
   227	func (w WaitStatus) CoreDump() bool     { return false }
   228	func (w WaitStatus) Stopped() bool      { return false }
   229	func (w WaitStatus) Continued() bool    { return false }
   230	func (w WaitStatus) StopSignal() Signal { return 0 }
   231	func (w WaitStatus) TrapCause() int     { return 0 }
   232	
   233	// XXX made up
   234	type Rusage struct {
   235		Utime Timeval
   236		Stime Timeval
   237	}
   238	
   239	// XXX made up
   240	type ProcAttr struct {
   241		Dir   string
   242		Env   []string
   243		Files []uintptr
   244		Sys   *SysProcAttr
   245	}
   246	
   247	type SysProcAttr struct {
   248	}
   249	
   250	func Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno) {
   251		return 0, 0, ENOSYS
   252	}
   253	
   254	func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) {
   255		return 0, 0, ENOSYS
   256	}
   257	
   258	func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno) {
   259		return 0, 0, ENOSYS
   260	}
   261	
   262	func RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) {
   263		return 0, 0, ENOSYS
   264	}
   265	
   266	func Sysctl(key string) (string, error) {
   267		if key == "kern.hostname" {
   268			return "js", nil
   269		}
   270		return "", ENOSYS
   271	}
   272	
   273	const ImplementsGetwd = true
   274	
   275	func Getwd() (wd string, err error) {
   276		var buf [PathMax]byte
   277		n, err := Getcwd(buf[0:])
   278		if err != nil {
   279			return "", err
   280		}
   281		return string(buf[:n]), nil
   282	}
   283	
   284	func Getuid() int {
   285		return jsProcess.Call("getuid").Int()
   286	}
   287	
   288	func Getgid() int {
   289		return jsProcess.Call("getgid").Int()
   290	}
   291	
   292	func Geteuid() int {
   293		return jsProcess.Call("geteuid").Int()
   294	}
   295	
   296	func Getegid() int {
   297		return jsProcess.Call("getegid").Int()
   298	}
   299	
   300	func Getgroups() ([]int, error) {
   301		array := jsProcess.Call("getgroups")
   302		groups := make([]int, array.Length())
   303		for i := range groups {
   304			groups[i] = array.Index(i).Int()
   305		}
   306		return groups, nil
   307	}
   308	
   309	func Getpid() int {
   310		return jsProcess.Get("pid").Int()
   311	}
   312	
   313	func Getppid() int {
   314		return jsProcess.Get("ppid").Int()
   315	}
   316	
   317	func Umask(mask int) (oldmask int) {
   318		return jsProcess.Call("umask", mask).Int()
   319	}
   320	
   321	func Gettimeofday(tv *Timeval) error { return ENOSYS }
   322	
   323	func Kill(pid int, signum Signal) error { return ENOSYS }
   324	func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
   325		return 0, ENOSYS
   326	}
   327	func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle uintptr, err error) {
   328		return 0, 0, ENOSYS
   329	}
   330	func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
   331		return 0, ENOSYS
   332	}
   333	
   334	type Iovec struct{} // dummy
   335	
   336	type Timespec struct {
   337		Sec  int64
   338		Nsec int64
   339	}
   340	
   341	type Timeval struct {
   342		Sec  int64
   343		Usec int64
   344	}
   345	
   346	func setTimespec(sec, nsec int64) Timespec {
   347		return Timespec{Sec: sec, Nsec: nsec}
   348	}
   349	
   350	func setTimeval(sec, usec int64) Timeval {
   351		return Timeval{Sec: sec, Usec: usec}
   352	}
   353	

View as plain text