github.com/mtsmfm/go/src@v0.0.0-20221020090648-44bdcb9f8fde/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 //go:build js && wasm 6 7 package runtime 8 9 import ( 10 "runtime/internal/atomic" 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 *atomic.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 gp := getg() 54 if !canpanic() { 55 throw("unexpected signal during runtime execution") 56 } 57 58 // js only invokes the exception handler for memory faults. 59 gp.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 // 131 //go:nowritebarrier 132 func newosproc(mp *m) { 133 throw("newosproc: not implemented") 134 } 135 136 func setProcessCPUProfiler(hz int32) {} 137 func setThreadCPUProfiler(hz int32) {} 138 func sigdisable(uint32) {} 139 func sigenable(uint32) {} 140 func sigignore(uint32) {} 141 142 //go:linkname os_sigpipe os.sigpipe 143 func os_sigpipe() { 144 throw("too many writes on closed pipe") 145 } 146 147 //go:nosplit 148 func cputicks() int64 { 149 // Currently cputicks() is used in blocking profiler and to seed runtime·fastrand(). 150 // runtime·nanotime() is a poor approximation of CPU ticks that is enough for the profiler. 151 return nanotime() 152 } 153 154 //go:linkname syscall_now syscall.now 155 func syscall_now() (sec int64, nsec int32) { 156 sec, nsec, _ = time_now() 157 return 158 } 159 160 // gsignalStack is unused on js. 161 type gsignalStack struct{} 162 163 const preemptMSupported = false 164 165 func preemptM(mp *m) { 166 // No threads, so nothing to do. 167 }