fmt_js.odin 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //+build js
  2. package fmt
  3. import "core:bufio"
  4. import "core:io"
  5. import "core:os"
  6. foreign import "odin_env"
  7. @(private="file")
  8. foreign odin_env {
  9. write :: proc "contextless" (fd: u32, p: []byte) ---
  10. }
  11. @(private="file")
  12. write_stream_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte, offset: i64, whence: io.Seek_From) -> (n: i64, err: io.Error) {
  13. if mode == .Write {
  14. fd := u32(uintptr(stream_data))
  15. write(fd, p)
  16. return i64(len(p)), nil
  17. }
  18. return 0, .Empty
  19. }
  20. @(private="file")
  21. stdout := io.Writer{
  22. procedure = write_stream_proc,
  23. data = rawptr(uintptr(1)),
  24. }
  25. @(private="file")
  26. stderr := io.Writer{
  27. procedure = write_stream_proc,
  28. data = rawptr(uintptr(2)),
  29. }
  30. @(private="file")
  31. fd_to_writer :: proc(fd: os.Handle, loc := #caller_location) -> io.Writer {
  32. switch fd {
  33. case 1: return stdout
  34. case 2: return stderr
  35. case: panic("`fmt.fprint` variant called with invalid file descriptor for JS, only 1 (stdout) and 2 (stderr) are supported", loc)
  36. }
  37. }
  38. // fprint formats using the default print settings and writes to fd
  39. fprint :: proc(fd: os.Handle, args: ..any, sep := " ", flush := true, loc := #caller_location) -> int {
  40. buf: [1024]byte
  41. b: bufio.Writer
  42. defer bufio.writer_flush(&b)
  43. bufio.writer_init_with_buf(&b, fd_to_writer(fd, loc), buf[:])
  44. w := bufio.writer_to_writer(&b)
  45. return wprint(w, ..args, sep=sep, flush=flush)
  46. }
  47. // fprintln formats using the default print settings and writes to fd
  48. fprintln :: proc(fd: os.Handle, args: ..any, sep := " ", flush := true, loc := #caller_location) -> int {
  49. buf: [1024]byte
  50. b: bufio.Writer
  51. defer bufio.writer_flush(&b)
  52. bufio.writer_init_with_buf(&b, fd_to_writer(fd, loc), buf[:])
  53. w := bufio.writer_to_writer(&b)
  54. return wprintln(w, ..args, sep=sep, flush=flush)
  55. }
  56. // fprintf formats according to the specified format string and writes to fd
  57. fprintf :: proc(fd: os.Handle, fmt: string, args: ..any, flush := true, newline := false, loc := #caller_location) -> int {
  58. buf: [1024]byte
  59. b: bufio.Writer
  60. defer bufio.writer_flush(&b)
  61. bufio.writer_init_with_buf(&b, fd_to_writer(fd, loc), buf[:])
  62. w := bufio.writer_to_writer(&b)
  63. return wprintf(w, fmt, ..args, flush=flush, newline=newline)
  64. }
  65. // fprintfln formats according to the specified format string and writes to fd, followed by a newline.
  66. fprintfln :: proc(fd: os.Handle, fmt: string, args: ..any, flush := true, loc := #caller_location) -> int {
  67. return fprintf(fd, fmt, ..args, flush=flush, newline=true, loc=loc)
  68. }
  69. // print formats using the default print settings and writes to stdout
  70. print :: proc(args: ..any, sep := " ", flush := true) -> int { return wprint(w=stdout, args=args, sep=sep, flush=flush) }
  71. // println formats using the default print settings and writes to stdout
  72. println :: proc(args: ..any, sep := " ", flush := true) -> int { return wprintln(w=stdout, args=args, sep=sep, flush=flush) }
  73. // printf formats according to the specififed format string and writes to stdout
  74. printf :: proc(fmt: string, args: ..any, flush := true) -> int { return wprintf(stdout, fmt, ..args, flush=flush) }
  75. // printfln formats according to the specified format string and writes to stdout, followed by a newline.
  76. printfln :: proc(fmt: string, args: ..any, flush := true) -> int { return wprintf(stdout, fmt, ..args, flush=flush, newline=true) }
  77. // eprint formats using the default print settings and writes to stderr
  78. eprint :: proc(args: ..any, sep := " ", flush := true) -> int { return wprint(w=stderr, args=args, sep=sep, flush=flush) }
  79. // eprintln formats using the default print settings and writes to stderr
  80. eprintln :: proc(args: ..any, sep := " ", flush := true) -> int { return wprintln(w=stderr, args=args, sep=sep, flush=flush) }
  81. // eprintf formats according to the specififed format string and writes to stderr
  82. eprintf :: proc(fmt: string, args: ..any, flush := true) -> int { return wprintf(stderr, fmt, ..args, flush=flush) }
  83. // eprintfln formats according to the specified format string and writes to stderr, followed by a newline.
  84. eprintfln :: proc(fmt: string, args: ..any, flush := true) -> int { return wprintf(stdout, fmt, ..args, flush=flush, newline=true) }