aboutsummaryrefslogtreecommitdiff
path: root/core/fmt/fmt_js.odin
blob: ccab15220e9e0ebd68d83ee4bb19924158c27943 (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
75
76
77
78
79
80
81
82
83
84
#+build js
package fmt

foreign import "odin_env"

@(private="file")
foreign odin_env {
	write :: proc "contextless" (fd: u32, p: []byte) ---
}

stdout :: u32(1)
stderr :: u32(2)

@(private="file")
BUF_SIZE :: 1024

@(private="file")
// TODO: Find a way to grow this if necessary
buf: [BUF_SIZE]byte

@(private="file")
get_fd :: proc(f: any, loc := #caller_location) -> (fd: u32) {
	if _fd, _ok := f.(u32); _ok {
		fd = _fd
	}
	if fd != 1 && fd != 2 {
		panic("`fmt.fprint` variant called with invalid file descriptor for JS, only 1 (stdout) and 2 (stderr) are supported", loc)
	}
	return fd
}

// fprint formats using the default print settings and writes to fd
// flush is ignored
fprint :: proc(f: any, args: ..any, sep := " ", flush := true, loc := #caller_location) -> (n: int) {
	fd := get_fd(f)
	s := bprint(buf[:], ..args, sep=sep)
	n = len(s)
	write(fd, transmute([]byte)s)
	return n
}

// fprintln formats using the default print settings and writes to fd, followed by a newline
// flush is ignored
fprintln :: proc(f: any, args: ..any, sep := " ", flush := true, loc := #caller_location) -> (n: int) {
	fd := get_fd(f)
	s := bprintln(buf[:], ..args, sep=sep)
	n = len(s)
	write(fd, transmute([]byte)s)
	return n
}

// fprintf formats according to the specified format string and writes to fd
// flush is ignored
fprintf :: proc(f: any, fmt: string, args: ..any, flush := true, newline := false, loc := #caller_location) -> (n: int) {
	fd := get_fd(f)
	s := bprintf(buf[:], fmt, ..args, newline=newline)
	n = len(s)
	write(fd, transmute([]byte)s)
	return n
}

// fprintfln formats according to the specified format string and writes to fd, followed by a newline.
// flush is ignored
fprintfln :: proc(f: any, fmt: string, args: ..any, flush := true, loc := #caller_location) -> int {
	return fprintf(f, fmt, ..args, flush=flush, newline=true, loc=loc)
}

// print formats using the default print settings and writes to stdout
print   :: proc(args: ..any, sep := " ", flush := true) -> int { return fprint(stdout, ..args, sep=sep, flush=flush) }
// println formats using the default print settings and writes to stdout
println :: proc(args: ..any, sep := " ", flush := true) -> int { return fprintln(stdout, ..args, sep=sep, flush=flush) }
// printf formats according to the specififed format string and writes to stdout
printf  :: proc(fmt: string, args: ..any, flush := true) -> int { return fprintf(stdout, fmt, ..args, flush=flush) }
// printfln formats according to the specified format string and writes to stdout, followed by a newline.
printfln :: proc(fmt: string, args: ..any, flush := true) -> int { return fprintf(stdout, fmt, ..args, flush=flush, newline=true) }

// eprint formats using the default print settings and writes to stderr
eprint   :: proc(args: ..any, sep := " ", flush := true) -> int { return fprint(stderr, ..args, sep=sep, flush=flush) }
// eprintln formats using the default print settings and writes to stderr
eprintln :: proc(args: ..any, sep := " ", flush := true) -> int { return fprintln(stderr, ..args, sep=sep, flush=flush) }
// eprintf formats according to the specififed format string and writes to stderr
eprintf  :: proc(fmt: string, args: ..any, flush := true) -> int { return fprintf(stderr, fmt, ..args, flush=flush) }
// eprintfln formats according to the specified format string and writes to stderr, followed by a newline.
eprintfln :: proc(fmt: string, args: ..any, flush := true) -> int { return fprintf(stderr, fmt, ..args, flush=flush, newline=true) }