JS unittest stacktrace fix, cleanup js repr and inclrtl includes (#14168)

This commit is contained in:
hlaaftana 2020-04-30 19:04:53 +03:00 • committed by GitHub
commit 0aefebb728
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 46 deletions

View file

@ -166,8 +166,6 @@
## ##
## * The effect system (``raises: []``) does not work with async procedures. ## * The effect system (``raises: []``) does not work with async procedures.
include "system/inclrtl"
import os, tables, strutils, times, heapqueue, options, asyncstreams import os, tables, strutils, times, heapqueue, options, asyncstreams
import options, math, std/monotimes import options, math, std/monotimes
import asyncfutures except callSoon import asyncfutures except callSoon

View file

@ -17,8 +17,6 @@
import import
hashes, math, locks hashes, math, locks
include "system/inclrtl"
type type
KeyValuePair[A, B] = tuple[hcode: Hash, key: A, val: B] KeyValuePair[A, B] = tuple[hcode: Hash, key: A, val: B]
KeyValuePairSeq[A, B] = ptr UncheckedArray[KeyValuePair[A, B]] KeyValuePairSeq[A, B] = ptr UncheckedArray[KeyValuePair[A, B]]

View file

@ -101,7 +101,9 @@ include "system/inclrtl"
when declared(stdout): when declared(stdout):
import os import os
when not defined(ECMAScript): const useTerminal = not defined(js)
when useTerminal:
import terminal import terminal
type type
@ -224,7 +226,7 @@ proc defaultConsoleFormatter*(): <//>ConsoleOutputFormatter =
method suiteStarted*(formatter: ConsoleOutputFormatter, suiteName: string) = method suiteStarted*(formatter: ConsoleOutputFormatter, suiteName: string) =
template rawPrint() = echo("\n[Suite] ", suiteName) template rawPrint() = echo("\n[Suite] ", suiteName)
when not defined(ECMAScript): when useTerminal:
if formatter.colorOutput: if formatter.colorOutput:
styledEcho styleBright, fgBlue, "\n[Suite] ", resetStyle, suiteName styledEcho styleBright, fgBlue, "\n[Suite] ", resetStyle, suiteName
else: rawPrint() else: rawPrint()
@ -250,7 +252,7 @@ method testEnded*(formatter: ConsoleOutputFormatter, testResult: TestResult) =
let prefix = if testResult.suiteName.len > 0: " " else: "" let prefix = if testResult.suiteName.len > 0: " " else: ""
template rawPrint() = echo(prefix, "[", $testResult.status, "] ", template rawPrint() = echo(prefix, "[", $testResult.status, "] ",
testResult.testName) testResult.testName)
when not defined(ECMAScript): when useTerminal:
if formatter.colorOutput: if formatter.colorOutput:
var color = case testResult.status var color = case testResult.status
of TestStatus.OK: fgGreen of TestStatus.OK: fgGreen
@ -515,7 +517,6 @@ template test*(name, body) {.dirty.} =
body body
except: except:
when not defined(js):
let e = getCurrentException() let e = getCurrentException()
let eTypeDesc = "[" & exceptionTypeName(e) & "]" let eTypeDesc = "[" & exceptionTypeName(e) & "]"
checkpoint("Unhandled exception: " & getCurrentExceptionMsg() & " " & eTypeDesc) checkpoint("Unhandled exception: " & getCurrentExceptionMsg() & " " & eTypeDesc)

View file

@ -8,34 +8,25 @@
# #
# The generic ``repr`` procedure for the javascript backend. # The generic ``repr`` procedure for the javascript backend.
proc reprInt(x: int64): string {.compilerproc.} = return $x proc reprInt(x: int64): string {.compilerproc.} = $x
proc reprFloat(x: float): string {.compilerproc.} = proc reprFloat(x: float): string {.compilerproc.} = $x
# Js toString doesn't differentiate between 1.0 and 1,
# but we do.
if $x == $(x.int): $x & ".0"
else: $x
proc reprPointer(p: pointer): string {.compilerproc.} = proc reprPointer(p: pointer): string {.compilerproc.} =
# Do we need to generate the full 8bytes ? In js a pointer is an int anyway # Do we need to generate the full 8bytes ? In js a pointer is an int anyway
var tmp: int var tmp: int
{. emit: """ {.emit: "`tmp` = `p`_Idx || 0;".}
if (`p`_Idx == null) {
`tmp` = 0;
} else {
`tmp` = `p`_Idx;
}
""" .}
result = $tmp result = $tmp
proc reprBool(x: bool): string {.compilerRtl.} = proc reprBool(x: bool): string {.compilerRtl.} =
if x: result = "true" if x: result = "true"
else: result = "false" else: result = "false"
proc isUndefined[T](x: T): bool {.inline.} = {.emit: "`result` = `x` === undefined;"}
proc reprEnum(e: int, typ: PNimType): string {.compilerRtl.} = proc reprEnum(e: int, typ: PNimType): string {.compilerRtl.} =
if not typ.node.sons[e].isUndefined: var tmp: bool
result = makeNimstrLit(typ.node.sons[e].name) let item = typ.node.sons[e]
{.emit: "`tmp` = `item` !== undefined".}
if tmp:
result = makeNimstrLit(item.name)
else: else:
result = $e & " (invalid data!)" result = $e & " (invalid data!)"
@ -48,7 +39,7 @@ proc reprChar(x: char): string {.compilerRtl.} =
else: add(result, x) else: add(result, x)
add(result, "\'") add(result, "\'")
proc reprStrAux(result: var string, s: cstring, len: int) = proc reprStrAux(result: var string, s: cstring | string, len: int) =
add(result, "\"") add(result, "\"")
for i in 0 .. len-1: for i in 0 .. len-1:
let c = s[i] let c = s[i]
@ -63,16 +54,6 @@ proc reprStrAux(result: var string, s: cstring, len: int) =
add(result, "\"") add(result, "\"")
proc reprStr(s: string): string {.compilerRtl.} = 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) = proc addSetElem(result: var string, elem: int, typ: PNimType) =
@ -114,7 +95,6 @@ proc reprSetAux(result: var string, s: int, typ: PNimType) =
add(result, "}") add(result, "}")
proc reprSet(e: int, typ: PNimType): string {.compilerRtl.} = proc reprSet(e: int, typ: PNimType): string {.compilerRtl.} =
result = ""
reprSetAux(result, e, typ) reprSetAux(result, e, typ)
type type
@ -149,7 +129,7 @@ proc reprArray(a: pointer, typ: PNimType,
add(result, "]") add(result, "]")
proc isPointedToNil(p: pointer): bool {.inline.}= proc isPointedToNil(p: pointer): bool =
{. emit: "if (`p` === null) {`result` = true};\n" .} {. emit: "if (`p` === null) {`result` = true};\n" .}
proc reprRef(result: var string, p: pointer, typ: PNimType, proc reprRef(result: var string, p: pointer, typ: PNimType,
@ -191,7 +171,6 @@ proc reprRecordAux(result: var string, o: pointer, typ: PNimType, cl: var ReprCl
add(result, "]") add(result, "]")
proc reprRecord(o: pointer, typ: PNimType, cl: var ReprClosure): string {.compilerRtl.} = proc reprRecord(o: pointer, typ: PNimType, cl: var ReprClosure): string {.compilerRtl.} =
result = ""
reprRecordAux(result, o, typ, cl) reprRecordAux(result, o, typ, cl)
@ -257,6 +236,5 @@ proc reprAux(result: var string, p: pointer, typ: PNimType,
proc reprAny(p: pointer, typ: PNimType): string {.compilerRtl.} = proc reprAny(p: pointer, typ: PNimType): string {.compilerRtl.} =
var cl: ReprClosure var cl: ReprClosure
initReprClosure(cl) initReprClosure(cl)
result = ""
reprAux(result, p, typ, cl) reprAux(result, p, typ, cl)
add(result, "\n") add(result, "\n")

View file

@ -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()