about summary refs log blame commit diff stats
path: root/src/js/console.nim
blob: 712f228c04af40b0bb128189f57a724faf3a1cd1 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
                   
                 


                          
                 





                     
                                                                               







                                     
                                                             

                        


                            
                      





                                         
                                                              

                 
                                                              

                 
                                                             

                 
                                                             

                 
                                                             


                            
                                                             






                                                                 







                                                                  
import io/dynstream
import js/jserror
import js/javascript

type Console* = ref object
  err*: DynStream
  clearFun: proc()
  showFun: proc()
  hideFun: proc()

jsDestructor(Console)

proc newConsole*(err: DynStream; clearFun: proc() = nil; showFun: proc() = nil;
    hideFun: proc() = nil): Console =
  return Console(
    err: err,
    clearFun: clearFun,
    showFun: showFun,
    hideFun: hideFun
  )

proc log*(console: Console; ss: varargs[string]) {.jsfunc.} =
  for i, s in ss:
    console.err.write(s)
    if i != ss.high:
      console.err.write(' ')
  console.err.write('\n')
  console.err.sflush()

proc clear(console: Console) {.jsfunc.} =
  if console.clearFun != nil:
    console.clearFun()

# For now, these are the same as log().
proc debug(console: Console; ss: varargs[string]) {.jsfunc.} =
  console.log(ss)

proc error(console: Console; ss: varargs[string]) {.jsfunc.} =
  console.log(ss)

proc info(console: Console; ss: varargs[string]) {.jsfunc.} =
  console.log(ss)

proc warn(console: Console; ss: varargs[string]) {.jsfunc.} =
  console.log(ss)

proc show(console: Console; ss: varargs[string]) {.jsfunc.} =
  if console.showFun != nil:
    console.showFun()

proc hide(console: Console; ss: varargs[string]) {.jsfunc.} =
  if console.hideFun != nil:
    console.hideFun()

proc addConsoleModule*(ctx: JSContext) =
  #TODO console should not have a prototype
  # "For historical reasons, console is lowercased."
  ctx.registerType(Console, nointerface = true, name = "console")

proc writeException*(ctx: JSContext; s: DynStream) =
  s.write(ctx.getExceptionMsg())
  s.sflush()

proc writeException*(ctx: JSContext; s: DynStream; err: JSError) =
  s.write(ctx.getExceptionMsg(err))
  s.sflush()