Source file src/runtime/os_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  //go:build js && wasm
     6  
     7  package runtime
     8  
     9  import (
    10  	"unsafe"
    11  )
    12  
    13  func exit(code int32)
    14  
    15  func write1(fd uintptr, p unsafe.Pointer, n int32) int32 {
    16  	if fd > 2 {
    17  		throw("runtime.write to fd > 2 is unsupported")
    18  	}
    19  	wasmWrite(fd, p, n)
    20  	return n
    21  }
    22  
    23  // Stubs so tests can link correctly. These should never be called.
    24  func open(name *byte, mode, perm int32) int32        { panic("not implemented") }
    25  func closefd(fd int32) int32                         { panic("not implemented") }
    26  func read(fd int32, p unsafe.Pointer, n int32) int32 { panic("not implemented") }
    27  
    28  //go:noescape
    29  func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
    30  
    31  func usleep(usec uint32)
    32  
    33  //go:nosplit
    34  func usleep_no_g(usec uint32) {
    35  	usleep(usec)
    36  }
    37  
    38  func exitThread(wait *uint32)
    39  
    40  type mOS struct{}
    41  
    42  func osyield()
    43  
    44  //go:nosplit
    45  func osyield_no_g() {
    46  	osyield()
    47  }
    48  
    49  const _SIGSEGV = 0xb
    50  
    51  func sigpanic() {
    52  	g := getg()
    53  	if !canpanic(g) {
    54  		throw("unexpected signal during runtime execution")
    55  	}
    56  
    57  	// js only invokes the exception handler for memory faults.
    58  	g.sig = _SIGSEGV
    59  	panicmem()
    60  }
    61  
    62  type sigset struct{}
    63  
    64  // Called to initialize a new m (including the bootstrap m).
    65  // Called on the parent thread (main thread in case of bootstrap), can allocate memory.
    66  func mpreinit(mp *m) {
    67  	mp.gsignal = malg(32 * 1024)
    68  	mp.gsignal.m = mp
    69  }
    70  
    71  //go:nosplit
    72  func sigsave(p *sigset) {
    73  }
    74  
    75  //go:nosplit
    76  func msigrestore(sigmask sigset) {
    77  }
    78  
    79  //go:nosplit
    80  //go:nowritebarrierrec
    81  func clearSignalHandlers() {
    82  }
    83  
    84  //go:nosplit
    85  func sigblock(exiting bool) {
    86  }
    87  
    88  // Called to initialize a new m (including the bootstrap m).
    89  // Called on the new thread, cannot allocate memory.
    90  func minit() {
    91  }
    92  
    93  // Called from dropm to undo the effect of an minit.
    94  func unminit() {
    95  }
    96  
    97  // Called from exitm, but not from drop, to undo the effect of thread-owned
    98  // resources in minit, semacreate, or elsewhere. Do not take locks after calling this.
    99  func mdestroy(mp *m) {
   100  }
   101  
   102  func osinit() {
   103  	ncpu = 1
   104  	getg().m.procid = 2
   105  	physPageSize = 64 * 1024
   106  }
   107  
   108  // wasm has no signals
   109  const _NSIG = 0
   110  
   111  func signame(sig uint32) string {
   112  	return ""
   113  }
   114  
   115  func crash() {
   116  	*(*int32)(nil) = 0
   117  }
   118  
   119  func getRandomData(r []byte)
   120  
   121  func goenvs() {
   122  	goenvs_unix()
   123  }
   124  
   125  func initsig(preinit bool) {
   126  }
   127  
   128  // May run with m.p==nil, so write barriers are not allowed.
   129  //go:nowritebarrier
   130  func newosproc(mp *m) {
   131  	panic("newosproc: not implemented")
   132  }
   133  
   134  func setProcessCPUProfiler(hz int32) {}
   135  func setThreadCPUProfiler(hz int32)  {}
   136  func sigdisable(uint32)              {}
   137  func sigenable(uint32)               {}
   138  func sigignore(uint32)               {}
   139  
   140  //go:linkname os_sigpipe os.sigpipe
   141  func os_sigpipe() {
   142  	throw("too many writes on closed pipe")
   143  }
   144  
   145  //go:nosplit
   146  func cputicks() int64 {
   147  	// Currently cputicks() is used in blocking profiler and to seed runtime·fastrand().
   148  	// runtime·nanotime() is a poor approximation of CPU ticks that is enough for the profiler.
   149  	return nanotime()
   150  }
   151  
   152  //go:linkname syscall_now syscall.now
   153  func syscall_now() (sec int64, nsec int32) {
   154  	sec, nsec, _ = time_now()
   155  	return
   156  }
   157  
   158  // gsignalStack is unused on js.
   159  type gsignalStack struct{}
   160  
   161  const preemptMSupported = false
   162  
   163  func preemptM(mp *m) {
   164  	// No threads, so nothing to do.
   165  }
   166  

View as plain text