v2 / vlib / builtin / wasm / wasi / builtin_notd_no_imports.v
61 lines · 51 sloc · 1.07 KB · d559a62cfe3f3de0a5ca1e59f14e622960e3917b
Raw
1module builtin
2
3// print prints a message to stdout. Unlike `println` stdout is not automatically flushed.
4pub fn print(s string) {
5 elm := CIOVec{
6 buf: s.str
7 len: usize(s.len)
8 }
9
10 WASM.fd_write(1, &elm, 1, 0)
11}
12
13// println prints a message with a line end, to stdout.
14pub fn println(s string) {
15 elm := [CIOVec{
16 buf: s.str
17 len: usize(s.len)
18 }, CIOVec{
19 buf: c'\n'
20 len: 1
21 }]!
22
23 WASM.fd_write(1, &elm[0], 2, 0)
24}
25
26// eprint prints a message to stderr.
27pub fn eprint(s string) {
28 elm := CIOVec{
29 buf: s.str
30 len: usize(s.len)
31 }
32
33 WASM.fd_write(2, &elm, 1, 0)
34}
35
36// eprintln prints a message with a line end, to stderr.
37pub fn eprintln(s string) {
38 elm := [CIOVec{
39 buf: s.str
40 len: usize(s.len)
41 }, CIOVec{
42 buf: c'\n'
43 len: 1
44 }]!
45
46 WASM.fd_write(2, &elm[0], 2, 0)
47}
48
49// exit terminates execution immediately and returns exit `code` to the shell.
50@[noreturn]
51pub fn exit(code int) {
52 WASM.proc_exit(code)
53}
54
55// panic prints a nice error message, then exits the process with exit code of 1.
56@[noreturn]
57pub fn panic(s string) {
58 eprint('V panic: ')
59 eprintln(s)
60 exit(1)
61}
62