github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/runtime/os_js.go (about)

     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 runtime
     8  
     9  import (
    10  	"unsafe"
    11  )
    12  
    13  func exit(code int32)
    14  
    15  func write(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  func exitThread(wait *uint32)
    34  
    35  type mOS struct{}
    36  
    37  func osyield()
    38  
    39  const _SIGSEGV = 0xb
    40  
    41  func sigpanic() {
    42  	g := getg()
    43  	if !canpanic(g) {
    44  		throw("unexpected signal during runtime execution")
    45  	}
    46  
    47  	// js only invokes the exception handler for memory faults.
    48  	g.sig = _SIGSEGV
    49  	panicmem()
    50  }
    51  
    52  type sigset struct{}
    53  
    54  // Called to initialize a new m (including the bootstrap m).
    55  // Called on the parent thread (main thread in case of bootstrap), can allocate memory.
    56  func mpreinit(mp *m) {
    57  	mp.gsignal = malg(32 * 1024)
    58  	mp.gsignal.m = mp
    59  }
    60  
    61  //go:nosplit
    62  func msigsave(mp *m) {
    63  }
    64  
    65  //go:nosplit
    66  func msigrestore(sigmask sigset) {
    67  }
    68  
    69  //go:nosplit
    70  //go:nowritebarrierrec
    71  func clearSignalHandlers() {
    72  }
    73  
    74  //go:nosplit
    75  func sigblock() {
    76  }
    77  
    78  // Called to initialize a new m (including the bootstrap m).
    79  // Called on the new thread, cannot allocate memory.
    80  func minit() {
    81  }
    82  
    83  // Called from dropm to undo the effect of an minit.
    84  func unminit() {
    85  }
    86  
    87  func osinit() {
    88  	ncpu = 1
    89  	getg().m.procid = 2
    90  	physPageSize = 64 * 1024
    91  }
    92  
    93  // wasm has no signals
    94  const _NSIG = 0
    95  
    96  func signame(sig uint32) string {
    97  	return ""
    98  }
    99  
   100  func crash() {
   101  	*(*int32)(nil) = 0
   102  }
   103  
   104  func getRandomData(r []byte)
   105  
   106  func goenvs() {
   107  	goenvs_unix()
   108  }
   109  
   110  func initsig(preinit bool) {
   111  }
   112  
   113  // May run with m.p==nil, so write barriers are not allowed.
   114  //go:nowritebarrier
   115  func newosproc(mp *m) {
   116  	panic("newosproc: not implemented")
   117  }
   118  
   119  func setProcessCPUProfiler(hz int32) {}
   120  func setThreadCPUProfiler(hz int32)  {}
   121  func sigdisable(uint32)              {}
   122  func sigenable(uint32)               {}
   123  func sigignore(uint32)               {}
   124  
   125  //go:linkname os_sigpipe os.sigpipe
   126  func os_sigpipe() {
   127  	throw("too many writes on closed pipe")
   128  }
   129  
   130  //go:nosplit
   131  func cputicks() int64 {
   132  	// Currently cputicks() is used in blocking profiler and to seed runtime·fastrand().
   133  	// runtime·nanotime() is a poor approximation of CPU ticks that is enough for the profiler.
   134  	// TODO: need more entropy to better seed fastrand.
   135  	return nanotime()
   136  }
   137  
   138  //go:linkname syscall_now syscall.now
   139  func syscall_now() (sec int64, nsec int32) {
   140  	sec, nsec, _ = time_now()
   141  	return
   142  }
   143  
   144  // gsignalStack is unused on js.
   145  type gsignalStack struct{}