From ad5be7271ec7bb524b543eec7cc6fa205c457aaf Mon Sep 17 00:00:00 2001 From: Juan Carlos Date: Thu, 24 Dec 2020 12:05:31 -0300 Subject: [PATCH 1/8] Add assertions for jsconsole --- changelog.md | 1 + lib/js/jsconsole.nim | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index a6a865514b57..ee5928b768bc 100644 --- a/changelog.md +++ b/changelog.md @@ -75,6 +75,7 @@ - Added `euclDiv` and `euclMod` to `math`. - Added `httpcore.is1xx` and missing HTTP codes. +- Added `jsconsole.doAssert` for JavaScript target. ## Language changes diff --git a/lib/js/jsconsole.nim b/lib/js/jsconsole.nim index 35afd95ad3a2..511133d53964 100644 --- a/lib/js/jsconsole.nim +++ b/lib/js/jsconsole.nim @@ -10,10 +10,12 @@ ## Wrapper for the `console` object for the `JavaScript backend ## `_. +import std/private/since + 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,15 @@ 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): + # proc nor func can not be used here, because may eval assertion at compile-time, + # writing directly false or true to the JavaScript instead of the expression. + # importjs nor importcpp can not be used with template. + + template doAssert*(console: Console; assertion) = + ## https://developer.mozilla.org/en-US/docs/Web/API/Console/assert + runnableExamples: console.doAssert(42 == 42) + {.emit: "console.assert(" & astToStr(assertion) & ");".} + + var console* {.importc, nodecl.}: Console From 453efd002404524ccf93abb40a7d47b6ebb8b4da Mon Sep 17 00:00:00 2001 From: Juan Carlos Date: Thu, 24 Dec 2020 19:22:21 -0300 Subject: [PATCH 2/8] fixes --- changelog.md | 2 +- lib/js/jsconsole.nim | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/changelog.md b/changelog.md index ee5928b768bc..93d8008588cb 100644 --- a/changelog.md +++ b/changelog.md @@ -75,7 +75,7 @@ - Added `euclDiv` and `euclMod` to `math`. - Added `httpcore.is1xx` and missing HTTP codes. -- Added `jsconsole.doAssert` for JavaScript target. +- Added `jsconsole.jsAssert` for JavaScript target. ## Language changes diff --git a/lib/js/jsconsole.nim b/lib/js/jsconsole.nim index 511133d53964..09c763388ae2 100644 --- a/lib/js/jsconsole.nim +++ b/lib/js/jsconsole.nim @@ -74,10 +74,16 @@ since (1, 5): # writing directly false or true to the JavaScript instead of the expression. # importjs nor importcpp can not be used with template. - template doAssert*(console: Console; assertion) = + template jsAssert*(console: Console; assertion) = + ## JavaScript `console.assert`, + ## assert failure just prints to console and do not quit the program. ## https://developer.mozilla.org/en-US/docs/Web/API/Console/assert - runnableExamples: console.doAssert(42 == 42) - {.emit: "console.assert(" & astToStr(assertion) & ");".} + runnableExamples: + console.jsAssert(42 == 42) # OK + console.jsAssert(42 != 42) # Fail, prints "Assertion failed" and continues + var message = "" + message.addQuoted(astToStr(assertion)) + {.emit: ["console.assert(", astToStr(assertion), ", ", message.cstring, ");"].} var console* {.importc, nodecl.}: Console From c309e9acea92c159ffe61a1d01ba91786ed45d78 Mon Sep 17 00:00:00 2001 From: Juan Carlos Date: Thu, 24 Dec 2020 19:26:25 -0300 Subject: [PATCH 3/8] fixes --- lib/js/jsconsole.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/js/jsconsole.nim b/lib/js/jsconsole.nim index 09c763388ae2..f8692dda2723 100644 --- a/lib/js/jsconsole.nim +++ b/lib/js/jsconsole.nim @@ -75,7 +75,7 @@ since (1, 5): # importjs nor importcpp can not be used with template. template jsAssert*(console: Console; assertion) = - ## JavaScript `console.assert`, + ## JavaScript `console.assert`, for NodeJS this prints to stderr, ## assert failure just prints to console and do not quit the program. ## https://developer.mozilla.org/en-US/docs/Web/API/Console/assert runnableExamples: From fb788a46787e2eefad3bb7096cf47f34d064aae2 Mon Sep 17 00:00:00 2001 From: Juan Carlos Date: Thu, 24 Dec 2020 19:30:26 -0300 Subject: [PATCH 4/8] fixes --- lib/js/jsconsole.nim | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/js/jsconsole.nim b/lib/js/jsconsole.nim index f8692dda2723..34d69cdcbf10 100644 --- a/lib/js/jsconsole.nim +++ b/lib/js/jsconsole.nim @@ -76,7 +76,10 @@ since (1, 5): 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. + ## 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 From 737b53924cb21c5d17c47cb93f739ea6f72453f4 Mon Sep 17 00:00:00 2001 From: Juan Carlos Date: Thu, 24 Dec 2020 20:02:42 -0300 Subject: [PATCH 5/8] Fix it --- lib/js/jsconsole.nim | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/js/jsconsole.nim b/lib/js/jsconsole.nim index 34d69cdcbf10..bfb9106b8922 100644 --- a/lib/js/jsconsole.nim +++ b/lib/js/jsconsole.nim @@ -10,7 +10,7 @@ ## Wrapper for the `console` object for the `JavaScript backend ## `_. -import std/private/since +import std/private/since, std/private/miscdollars when not defined(js) and not defined(Nimdoc): {.error: "This module only works on the JavaScript platform".} @@ -73,6 +73,12 @@ since (1, 5): # proc nor func can not be used here, because may eval assertion at compile-time, # writing directly false or true to the JavaScript instead of the expression. # importjs nor importcpp can not be used with template. + type InstantiationInfo = tuple[filename: string, line: int, column: int] + + proc `$`(info: InstantiationInfo): string = + var temp = "" + temp.toLocation(info.filename, info.line, info.column + 1) + result.addQuoted(temp) template jsAssert*(console: Console; assertion) = ## JavaScript `console.assert`, for NodeJS this prints to stderr, @@ -84,9 +90,16 @@ since (1, 5): runnableExamples: console.jsAssert(42 == 42) # OK console.jsAssert(42 != 42) # Fail, prints "Assertion failed" and continues - var message = "" - message.addQuoted(astToStr(assertion)) - {.emit: ["console.assert(", astToStr(assertion), ", ", message.cstring, ");"].} + console.jsAssert('`' == '\n' and '\t' == '\0') # Message correctly formatted + assert 42 == 42 # Normal assertions keep working + + const + loc = instantiationInfo(fullPaths = compileOption("excessiveStackTrace")) + ploc = cstring($loc) + var msg = "" + msg.addQuoted(astToStr(assertion)) + {.line: loc.}: + {.emit: ["console.assert(", assertion, ", ", ploc, ", ", msg.cstring, ");"].} var console* {.importc, nodecl.}: Console From e5dd4628de015448d6525d7530b47de33e6dacc6 Mon Sep 17 00:00:00 2001 From: Juan Carlos Date: Thu, 24 Dec 2020 20:05:30 -0300 Subject: [PATCH 6/8] Fix it --- lib/js/jsconsole.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/js/jsconsole.nim b/lib/js/jsconsole.nim index bfb9106b8922..e780ae947a8b 100644 --- a/lib/js/jsconsole.nim +++ b/lib/js/jsconsole.nim @@ -10,7 +10,7 @@ ## Wrapper for the `console` object for the `JavaScript backend ## `_. -import std/private/since, std/private/miscdollars +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".} From a4783e3ff6ac28539730d4c3e15f680df0064ae7 Mon Sep 17 00:00:00 2001 From: Juan Carlos Date: Thu, 24 Dec 2020 20:23:54 -0300 Subject: [PATCH 7/8] Fix it --- lib/js/jsconsole.nim | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/js/jsconsole.nim b/lib/js/jsconsole.nim index e780ae947a8b..3f34c73f7392 100644 --- a/lib/js/jsconsole.nim +++ b/lib/js/jsconsole.nim @@ -75,10 +75,12 @@ since (1, 5): # importjs nor importcpp can not be used with template. type InstantiationInfo = tuple[filename: string, line: int, column: int] - proc `$`(info: InstantiationInfo): string = + 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, @@ -87,7 +89,7 @@ since (1, 5): ## 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: + 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 @@ -95,11 +97,9 @@ since (1, 5): const loc = instantiationInfo(fullPaths = compileOption("excessiveStackTrace")) - ploc = cstring($loc) - var msg = "" - msg.addQuoted(astToStr(assertion)) + msg = getMsg(loc, astToStr(assertion)).cstring {.line: loc.}: - {.emit: ["console.assert(", assertion, ", ", ploc, ", ", msg.cstring, ");"].} + {.emit: ["console.assert(", assertion, ", ", msg, ");"].} var console* {.importc, nodecl.}: Console From 2c988e8c39c29e7427ccac179be2c16d33bd6492 Mon Sep 17 00:00:00 2001 From: Juan Carlos Date: Thu, 24 Dec 2020 20:29:37 -0300 Subject: [PATCH 8/8] Fix it --- lib/js/jsconsole.nim | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/js/jsconsole.nim b/lib/js/jsconsole.nim index 3f34c73f7392..5b9893e75b95 100644 --- a/lib/js/jsconsole.nim +++ b/lib/js/jsconsole.nim @@ -70,9 +70,6 @@ proc table*(console: Console) {.importcpp, varargs.} ## https://developer.mozilla.org/docs/Web/API/Console/table since (1, 5): - # proc nor func can not be used here, because may eval assertion at compile-time, - # writing directly false or true to the JavaScript instead of the expression. - # importjs nor importcpp can not be used with template. type InstantiationInfo = tuple[filename: string, line: int, column: int] func getMsg(info: InstantiationInfo; msg: string): string =