- Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathjsconsole.nim
125 lines (95 loc) · 4.72 KB
/
jsconsole.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#
#
# Nim's Runtime Library
# (c) Copyright 2012 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
## Wrapper for the `console` object for the `JavaScript backend
## <backends.html#backends-the-javascript-target>`_.
##
## Styled Messages
## ===============
##
## CSS-styled messages in the browser are useful for debugging purposes.
## To use them, prefix the message with one or more `%c`,
## and provide the CSS style as the last argument.
## The amount of `%c`'s must match the amount of CSS-styled strings.
##
runnableExamples("-r:off"):
console.log"%c My Debug Message", "color: red"# Notice the "%c"
console.log"%c My Debug %c Message", "color: red", "font-size: 2em"
import std/private/since, std/private/miscdollars # toLocation
whennotdefined(js):
{.error: "This module only works on the JavaScript platform".}
typeConsole*=refobjectofJsRoot
proclog*(console: Console) {.importcpp, varargs.}
## https://developer.mozilla.org/docs/Web/API/Console/log
procdebug*(console: Console) {.importcpp, varargs.}
## https://developer.mozilla.org/docs/Web/API/Console/debug
procinfo*(console: Console) {.importcpp, varargs.}
## https://developer.mozilla.org/docs/Web/API/Console/info
procerror*(console: Console) {.importcpp, varargs.}
## https://developer.mozilla.org/docs/Web/API/Console/error
templateexception*(console: Console, args: varargs[untyped]) =
## Alias for `console.error()`.
error(console, args)
proctrace*(console: Console) {.importcpp, varargs.}
## https://developer.mozilla.org/docs/Web/API/Console/trace
procwarn*(console: Console) {.importcpp, varargs.}
## https://developer.mozilla.org/docs/Web/API/Console/warn
procclear*(console: Console) {.importcpp, varargs.}
## https://developer.mozilla.org/docs/Web/API/Console/clear
proccount*(console: Console, label ="".cstring) {.importcpp.}
## https://developer.mozilla.org/docs/Web/API/Console/count
proccountReset*(console: Console, label ="".cstring) {.importcpp.}
## https://developer.mozilla.org/docs/Web/API/Console/countReset
procgroup*(console: Console, label ="".cstring) {.importcpp.}
## https://developer.mozilla.org/docs/Web/API/Console/group
procgroupCollapsed*(console: Console, label ="".cstring) {.importcpp.}
## https://developer.mozilla.org/en-US/docs/Web/API/Console/groupCollapsed
procgroupEnd*(console: Console) {.importcpp.}
## https://developer.mozilla.org/docs/Web/API/Console/groupEnd
proctime*(console: Console, label ="".cstring) {.importcpp.}
## https://developer.mozilla.org/docs/Web/API/Console/time
proctimeEnd*(console: Console, label ="".cstring) {.importcpp.}
## https://developer.mozilla.org/docs/Web/API/Console/timeEnd
proctimeLog*(console: Console, label ="".cstring) {.importcpp.}
## https://developer.mozilla.org/docs/Web/API/Console/timeLog
proctable*(console: Console) {.importcpp, varargs.}
## https://developer.mozilla.org/docs/Web/API/Console/table
since (1, 5):
typeInstantiationInfo=tuple[filename: string, line: int, column: int]
funcgetMsg(info: InstantiationInfo; msg: string): string=
var temp =""
temp.toLocation(info.filename, info.line, info.column +1)
result.addQuoted("[jsAssert] "& temp)
result.add','
result.addQuoted(msg)
templatejsAssert*(console: Console; assertion) =
## JavaScript `console.assert`, for NodeJS this prints to stderr,
## assert failure just prints to console and do not quit the program,
## this is not meant to be better or even equal than normal assertions,
## is just for when you need faster performance *and* assertions,
## otherwise use the normal assertions for better user experience.
## https://developer.mozilla.org/en-US/docs/Web/API/Console/assert
runnableExamples:
console.jsAssert(42==42) # OK
console.jsAssert(42!=42) # Fail, prints "Assertion failed" and continues
console.jsAssert('`'=='\n'and'\t'=='\0') # Message correctly formatted
assert42==42# Normal assertions keep working
const
loc =instantiationInfo(fullPaths =compileOption("excessiveStackTrace"))
msg =getMsg(loc, astToStr(assertion)).cstring
{.line: loc.}:
{.emit: ["console.assert(", assertion, ", ", msg, ");"].}
funcdir*(console: Console; obj: auto) {.importcpp.}
## https://developer.mozilla.org/en-US/docs/Web/API/Console/dir
funcdirxml*(console: Console; obj: auto) {.importcpp.}
## https://developer.mozilla.org/en-US/docs/Web/API/Console/dirxml
functimeStamp*(console: Console; label: cstring) {.importcpp.}
## https://developer.mozilla.org/en-US/docs/Web/API/Console/timeStamp
##
## ..warning:: non-standard
var console* {.importc, nodecl.}: Console