diff options
author | Juan Carlos <juancarlospaco@gmail.com> | 2020-12-30 11:10:50 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-30 15:10:50 +0100 |
commit | b8658a3e242f7304f7b0320089ef5aa0d5375c25 (patch) | |
tree | f2dbf6e7f727cc2892da08dac6c4d5d2d3c486e3 | |
parent | 515cd454207ed9066c08ac8ef42407ae5ba62bc8 (diff) | |
download | Nim-b8658a3e242f7304f7b0320089ef5aa0d5375c25.tar.gz |
Add assertions for jsconsole (#16460)
-rw-r--r-- | changelog.md | 1 | ||||
-rw-r--r-- | lib/js/jsconsole.nim | 34 |
2 files changed, 34 insertions, 1 deletions
diff --git a/changelog.md b/changelog.md index bbc83ff25..c6a1d17d9 100644 --- a/changelog.md +++ b/changelog.md @@ -79,6 +79,7 @@ - Added `euclDiv` and `euclMod` to `math`. - Added `httpcore.is1xx` and missing HTTP codes. +- Added `jsconsole.jsAssert` for JavaScript target. ## Language changes diff --git a/lib/js/jsconsole.nim b/lib/js/jsconsole.nim index 35afd95ad..5b9893e75 100644 --- a/lib/js/jsconsole.nim +++ b/lib/js/jsconsole.nim @@ -10,10 +10,12 @@ ## Wrapper for the `console` object for the `JavaScript backend ## <backends.html#backends-the-javascript-target>`_. +import std/private/since, std/private/miscdollars # toLocation + when not defined(js) and not defined(Nimdoc): {.error: "This module only works on the JavaScript platform".} -type Console* = ref object of RootObj +type Console* = ref object of JsRoot proc log*(console: Console) {.importcpp, varargs.} ## https://developer.mozilla.org/docs/Web/API/Console/log @@ -67,4 +69,34 @@ proc timeLog*(console: Console, label = "".cstring) {.importcpp.} proc table*(console: Console) {.importcpp, varargs.} ## https://developer.mozilla.org/docs/Web/API/Console/table +since (1, 5): + type InstantiationInfo = tuple[filename: string, line: int, column: int] + + func getMsg(info: InstantiationInfo; msg: string): string = + var temp = "" + temp.toLocation(info.filename, info.line, info.column + 1) + result.addQuoted(temp) + result.add ',' + result.addQuoted(msg) + + template jsAssert*(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 + assert 42 == 42 # Normal assertions keep working + + const + loc = instantiationInfo(fullPaths = compileOption("excessiveStackTrace")) + msg = getMsg(loc, astToStr(assertion)).cstring + {.line: loc.}: + {.emit: ["console.assert(", assertion, ", ", msg, ");"].} + + var console* {.importc, nodecl.}: Console |