Black Lives Matter. Support the Equal Justice Initiative.

Source file src/runtime/os_js.go

Documentation: runtime

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

View as plain text