Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assertions for jsconsole #16460

Merged
merged 8 commits into from
Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@

- Added `euclDiv` and `euclMod` to `math`.
- Added `httpcore.is1xx` and missing HTTP codes.
- Added `jsconsole.jsAssert` for JavaScript target.


## Language changes
Expand Down
34 changes: 33 additions & 1 deletion lib/js/jsconsole.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
juancarlospaco marked this conversation as resolved.
Show resolved Hide resolved
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