fmt_js.odin 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 "c" (fd: u32, p: []byte) ---
  8. }
  9. @(private="file")
  10. write_vtable := &io.Stream_VTable{
  11. impl_write = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) {
  12. fd := u32(uintptr(s.stream_data))
  13. write(fd, p)
  14. return len(p), nil
  15. },
  16. }
  17. @(private="file")
  18. stdout := io.Writer{
  19. stream = {
  20. stream_vtable = write_vtable,
  21. stream_data = rawptr(uintptr(1)),
  22. },
  23. }
  24. @(private="file")
  25. stderr := io.Writer{
  26. stream = {
  27. stream_vtable = write_vtable,
  28. stream_data = rawptr(uintptr(2)),
  29. },
  30. }
  31. // print formats using the default print settings and writes to stdout
  32. print :: proc(args: ..any, sep := " ") -> int { return wprint(w=stdout, args=args, sep=sep) }
  33. // println formats using the default print settings and writes to stdout
  34. println :: proc(args: ..any, sep := " ") -> int { return wprintln(w=stdout, args=args, sep=sep) }
  35. // printf formats according to the specififed format string and writes to stdout
  36. printf :: proc(fmt: string, args: ..any) -> int { return wprintf(stdout, fmt, ..args) }
  37. // eprint formats using the default print settings and writes to stderr
  38. eprint :: proc(args: ..any, sep := " ") -> int { return wprint(w=stderr, args=args, sep=sep) }
  39. // eprintln formats using the default print settings and writes to stderr
  40. eprintln :: proc(args: ..any, sep := " ") -> int { return wprintln(w=stderr, args=args, sep=sep) }
  41. // eprintf formats according to the specififed format string and writes to stderr
  42. eprintf :: proc(fmt: string, args: ..any) -> int { return wprintf(stderr, fmt, ..args) }