Skip to content

Commit

Permalink
JS unittest stacktrace fix, cleanup js repr and inclrtl includes (#14168
Browse files Browse the repository at this point in the history
)
  • Loading branch information
metagn authored Apr 30, 2020
1 parent 4d799f8 commit 0aefebb
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 46 deletions.
2 changes: 0 additions & 2 deletions lib/pure/asyncdispatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,6 @@
##
## * The effect system (``raises: []``) does not work with async procedures.

include "system/inclrtl"

import os, tables, strutils, times, heapqueue, options, asyncstreams
import options, math, std/monotimes
import asyncfutures except callSoon
Expand Down
2 changes: 0 additions & 2 deletions lib/pure/collections/sharedtables.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import
hashes, math, locks

include "system/inclrtl"

type
KeyValuePair[A, B] = tuple[hcode: Hash, key: A, val: B]
KeyValuePairSeq[A, B] = ptr UncheckedArray[KeyValuePair[A, B]]
Expand Down
17 changes: 9 additions & 8 deletions lib/pure/unittest.nim
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ include "system/inclrtl"
when declared(stdout):
import os

when not defined(ECMAScript):
const useTerminal = not defined(js)

when useTerminal:
import terminal

type
Expand Down Expand Up @@ -224,7 +226,7 @@ proc defaultConsoleFormatter*(): <//>ConsoleOutputFormatter =

method suiteStarted*(formatter: ConsoleOutputFormatter, suiteName: string) =
template rawPrint() = echo("\n[Suite] ", suiteName)
when not defined(ECMAScript):
when useTerminal:
if formatter.colorOutput:
styledEcho styleBright, fgBlue, "\n[Suite] ", resetStyle, suiteName
else: rawPrint()
Expand All @@ -250,7 +252,7 @@ method testEnded*(formatter: ConsoleOutputFormatter, testResult: TestResult) =
let prefix = if testResult.suiteName.len > 0: " " else: ""
template rawPrint() = echo(prefix, "[", $testResult.status, "] ",
testResult.testName)
when not defined(ECMAScript):
when useTerminal:
if formatter.colorOutput:
var color = case testResult.status
of TestStatus.OK: fgGreen
Expand Down Expand Up @@ -515,11 +517,10 @@ template test*(name, body) {.dirty.} =
body

except:
when not defined(js):
let e = getCurrentException()
let eTypeDesc = "[" & exceptionTypeName(e) & "]"
checkpoint("Unhandled exception: " & getCurrentExceptionMsg() & " " & eTypeDesc)
var stackTrace {.inject.} = e.getStackTrace()
let e = getCurrentException()
let eTypeDesc = "[" & exceptionTypeName(e) & "]"
checkpoint("Unhandled exception: " & getCurrentExceptionMsg() & " " & eTypeDesc)
var stackTrace {.inject.} = e.getStackTrace()
fail()

finally:
Expand Down
46 changes: 12 additions & 34 deletions lib/system/reprjs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,25 @@
#
# The generic ``repr`` procedure for the javascript backend.

proc reprInt(x: int64): string {.compilerproc.} = return $x
proc reprFloat(x: float): string {.compilerproc.} =
# Js toString doesn't differentiate between 1.0 and 1,
# but we do.
if $x == $(x.int): $x & ".0"
else: $x
proc reprInt(x: int64): string {.compilerproc.} = $x
proc reprFloat(x: float): string {.compilerproc.} = $x

proc reprPointer(p: pointer): string {.compilerproc.} =
# Do we need to generate the full 8bytes ? In js a pointer is an int anyway
var tmp: int
{. emit: """
if (`p`_Idx == null) {
`tmp` = 0;
} else {
`tmp` = `p`_Idx;
}
""" .}
{.emit: "`tmp` = `p`_Idx || 0;".}
result = $tmp

proc reprBool(x: bool): string {.compilerRtl.} =
if x: result = "true"
else: result = "false"

proc isUndefined[T](x: T): bool {.inline.} = {.emit: "`result` = `x` === undefined;"}

proc reprEnum(e: int, typ: PNimType): string {.compilerRtl.} =
if not typ.node.sons[e].isUndefined:
result = makeNimstrLit(typ.node.sons[e].name)
var tmp: bool
let item = typ.node.sons[e]
{.emit: "`tmp` = `item` !== undefined".}
if tmp:
result = makeNimstrLit(item.name)
else:
result = $e & " (invalid data!)"

Expand All @@ -48,7 +39,7 @@ proc reprChar(x: char): string {.compilerRtl.} =
else: add(result, x)
add(result, "\'")

proc reprStrAux(result: var string, s: cstring, len: int) =
proc reprStrAux(result: var string, s: cstring | string, len: int) =
add(result, "\"")
for i in 0 .. len-1:
let c = s[i]
Expand All @@ -63,17 +54,7 @@ proc reprStrAux(result: var string, s: cstring, len: int) =
add(result, "\"")

proc reprStr(s: string): string {.compilerRtl.} =
result = ""
var sIsNil = false
asm """`sIsNil` = `s` === null"""
if sIsNil: # cast[pointer](s).isNil:
# Handle nil strings here because they don't have a length field in js
# TODO: check for null/undefined before generating call to length in js?
# Also: c backend repr of a nil string is <pointer>"", but repr of an
# array of string that is not initialized is [nil, nil, ...] ??
add(result, "nil")
else:
reprStrAux(result, s, s.len)
reprStrAux(result, s, s.len)

proc addSetElem(result: var string, elem: int, typ: PNimType) =
# Dispatch each set element to the correct repr<Type> proc
Expand Down Expand Up @@ -114,7 +95,6 @@ proc reprSetAux(result: var string, s: int, typ: PNimType) =
add(result, "}")

proc reprSet(e: int, typ: PNimType): string {.compilerRtl.} =
result = ""
reprSetAux(result, e, typ)

type
Expand Down Expand Up @@ -149,7 +129,7 @@ proc reprArray(a: pointer, typ: PNimType,

add(result, "]")

proc isPointedToNil(p: pointer): bool {.inline.}=
proc isPointedToNil(p: pointer): bool =
{. emit: "if (`p` === null) {`result` = true};\n" .}

proc reprRef(result: var string, p: pointer, typ: PNimType,
Expand Down Expand Up @@ -191,7 +171,6 @@ proc reprRecordAux(result: var string, o: pointer, typ: PNimType, cl: var ReprCl
add(result, "]")

proc reprRecord(o: pointer, typ: PNimType, cl: var ReprClosure): string {.compilerRtl.} =
result = ""
reprRecordAux(result, o, typ, cl)


Expand Down Expand Up @@ -257,6 +236,5 @@ proc reprAux(result: var string, p: pointer, typ: PNimType,
proc reprAny(p: pointer, typ: PNimType): string {.compilerRtl.} =
var cl: ReprClosure
initReprClosure(cl)
result = ""
reprAux(result, p, typ, cl)
add(result, "\n")
add(result, "\n")
22 changes: 22 additions & 0 deletions tests/js/tunittest_error.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
discard """
exitcode: 1
outputsub: "[FAILED] with exception"
"""

import unittest

proc ddd() =
raise newException(IOError, "didn't do stuff")

proc ccc() =
ddd()

proc bbb() =
ccc()

proc aaa() =
bbb()

test "with exception":
check 3 == 3
aaa()

0 comments on commit 0aefebb

Please sign in to comment.