fmt_js.odin 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //+build js
  2. package fmt
  3. import "core:io"
  4. foreign import "odin_env"
  5. @(private="file")
  6. foreign odin_env {
  7. write :: proc "contextless" (fd: u32, p: []byte) ---
  8. }
  9. @(private="file")
  10. write_stream_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte, offset: i64, whence: io.Seek_From) -> (n: i64, err: io.Error) {
  11. if mode == .Write {
  12. fd := u32(uintptr(stream_data))
  13. write(fd, p)
  14. return i64(len(p)), nil
  15. }
  16. return 0, .Empty
  17. }
  18. @(private="file")
  19. stdout := io.Writer{
  20. procedure = write_stream_proc,
  21. data = rawptr(uintptr(1)),
  22. }
  23. @(private="file")
  24. stderr := io.Writer{
  25. procedure = write_stream_proc,
  26. data = rawptr(uintptr(2)),
  27. }
  28. // print formats using the default print settings and writes to stdout
  29. print :: proc(args: ..any, sep := " ", flush := true) -> int { return wprint(w=stdout, args=args, sep=sep, flush=flush) }
  30. // println formats using the default print settings and writes to stdout
  31. println :: proc(args: ..any, sep := " ", flush := true) -> int { return wprintln(w=stdout, args=args, sep=sep, flush=flush) }
  32. // printf formats according to the specififed format string and writes to stdout
  33. printf :: proc(fmt: string, args: ..any, flush := true) -> int { return wprintf(stdout, fmt, ..args, flush=flush) }
  34. // eprint formats using the default print settings and writes to stderr
  35. eprint :: proc(args: ..any, sep := " ", flush := true) -> int { return wprint(w=stderr, args=args, sep=sep, flush=flush) }
  36. // eprintln formats using the default print settings and writes to stderr
  37. eprintln :: proc(args: ..any, sep := " ", flush := true) -> int { return wprintln(w=stderr, args=args, sep=sep, flush=flush) }
  38. // eprintf formats according to the specififed format string and writes to stderr
  39. eprintf :: proc(fmt: string, args: ..any, flush := true) -> int { return wprintf(stderr, fmt, ..args, flush=flush) }