jsconsole.nim 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Wrapper for the `console` object for the `JavaScript backend
  10. ## <backends.html#the-javascript-target>`_.
  11. when not defined(js) and not defined(Nimdoc):
  12. {.error: "This module only works on the JavaScript platform".}
  13. import macros
  14. type Console* {.importc.} = ref object of RootObj
  15. proc convertToConsoleLoggable*[T](v: T): RootRef {.importcpp: "#".}
  16. template convertToConsoleLoggable*(v: string): RootRef = cast[RootRef](cstring(v))
  17. proc logImpl(console: Console) {.importcpp: "log", varargs.}
  18. proc debugImpl(console: Console) {.importcpp: "debug", varargs.}
  19. proc infoImpl(console: Console) {.importcpp: "info", varargs.}
  20. proc errorImpl(console: Console) {.importcpp: "error", varargs.}
  21. proc makeConsoleCall(console: NimNode, procName: NimNode, args: NimNode): NimNode =
  22. result = newCall(procName, console)
  23. for c in args: result.add(c)
  24. macro log*(console: Console, args: varargs[RootRef, convertToConsoleLoggable]): untyped =
  25. makeConsoleCall(console, bindSym "logImpl", args)
  26. macro debug*(console: Console, args: varargs[RootRef, convertToConsoleLoggable]): untyped =
  27. makeConsoleCall(console, bindSym "debugImpl", args)
  28. macro info*(console: Console, args: varargs[RootRef, convertToConsoleLoggable]): untyped =
  29. makeConsoleCall(console, bindSym "infoImpl", args)
  30. macro error*(console: Console, args: varargs[RootRef, convertToConsoleLoggable]): untyped =
  31. makeConsoleCall(console, bindSym "errorImpl", args)
  32. var console* {.importc, nodecl.}: Console