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
7package runtime
8
9import (
10	"unsafe"
11)
12
13func exit(code int32)
14
15func 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.
24func open(name *byte, mode, perm int32) int32        { panic("not implemented") }
25func closefd(fd int32) int32                         { panic("not implemented") }
26func read(fd int32, p unsafe.Pointer, n int32) int32 { panic("not implemented") }
27
28//go:noescape
29func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
30
31func usleep(usec uint32)
32
33func exitThread(wait *uint32)
34
35type mOS struct{}
36
37func osyield()
38
39const _SIGSEGV = 0xb
40
41func 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
52type 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.
56func mpreinit(mp *m) {
57	mp.gsignal = malg(32 * 1024)
58	mp.gsignal.m = mp
59}
60
61//go:nosplit
62func msigsave(mp *m) {
63}
64
65//go:nosplit
66func msigrestore(sigmask sigset) {
67}
68
69//go:nosplit
70//go:nowritebarrierrec
71func clearSignalHandlers() {
72}
73
74//go:nosplit
75func sigblock() {
76}
77
78// Called to initialize a new m (including the bootstrap m).
79// Called on the new thread, cannot allocate memory.
80func minit() {
81}
82
83// Called from dropm to undo the effect of an minit.
84func unminit() {
85}
86
87func osinit() {
88	ncpu = 1
89	getg().m.procid = 2
90	physPageSize = 64 * 1024
91}
92
93// wasm has no signals
94const _NSIG = 0
95
96func signame(sig uint32) string {
97	return ""
98}
99
100func crash() {
101	*(*int32)(nil) = 0
102}
103
104func getRandomData(r []byte)
105
106func goenvs() {
107	goenvs_unix()
108}
109
110func initsig(preinit bool) {
111}
112
113// May run with m.p==nil, so write barriers are not allowed.
114//go:nowritebarrier
115func newosproc(mp *m) {
116	panic("newosproc: not implemented")
117}
118
119func setProcessCPUProfiler(hz int32) {}
120func setThreadCPUProfiler(hz int32)  {}
121func sigdisable(uint32)              {}
122func sigenable(uint32)               {}
123func sigignore(uint32)               {}
124
125//go:linkname os_sigpipe os.sigpipe
126func os_sigpipe() {
127	throw("too many writes on closed pipe")
128}
129
130//go:nosplit
131func 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	return nanotime()
135}
136
137//go:linkname syscall_now syscall.now
138func syscall_now() (sec int64, nsec int32) {
139	sec, nsec, _ = time_now()
140	return
141}
142
143// gsignalStack is unused on js.
144type gsignalStack struct{}
145
146const preemptMSupported = false
147
148func preemptM(mp *m) {
149	// No threads, so nothing to do.
150}
151