aboutsummaryrefslogtreecommitdiff
path: root/base/runtime/os_specific_wasi.odin
blob: c5e94653a9e587457e616ba9ddf243210983e47b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#+build wasi
#+private
package runtime

foreign import wasi "wasi_snapshot_preview1"

_HAS_RAND_BYTES :: true

@(default_calling_convention="contextless")
foreign wasi {
	fd_write :: proc(
		fd: i32,
		iovs: [][]byte,
		n: ^uint,
	) -> u16 ---

	@(private="file")
	args_sizes_get :: proc(
		num_of_args:  ^uint,
		size_of_args: ^uint,
	) -> u16 ---

	@(private="file")
	args_get :: proc(
		argv:     [^]cstring,
		argv_buf: [^]byte,
	) -> u16 ---

	@(private="file")
	proc_exit :: proc(rval: u32) -> ! ---

	@(private ="file")
	random_get :: proc(buf: []u8) -> u16 ---
}

_stderr_write :: proc "contextless" (data: []byte) -> (int, _OS_Errno) {
	n: uint
	err := fd_write(1, {data}, &n)
	return int(n), _OS_Errno(err)
}

_rand_bytes :: proc "contextless" (dst: []byte) {
	if errno := random_get(dst); errno != 0 {
		panic_contextless("base/runtime: wasi.random_get failed")
	}
}

_wasi_setup_args :: proc() {
	num_of_args, size_of_args: uint
	if errno := args_sizes_get(&num_of_args, &size_of_args); errno != 0 {
		return
	}

	err: Allocator_Error
	if args__, err = make([]cstring, num_of_args); err != nil {
		return
	}

	args_buf: []byte
	if args_buf, err = make([]byte, size_of_args); err != nil {
		delete(args__)
		return
	}

	if errno := args_get(raw_data(args__), raw_data(args_buf)); errno != 0 {
		delete(args__)
		delete(args_buf)
	}
}


_exit :: proc "contextless" (code: int) -> ! {
	proc_exit(u32(code))
}