From 90b0a771e405eab9bc48e83ca7e4f47d3203d088 Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Sat, 27 Aug 2016 14:11:41 +0300 Subject: Uncaught exceptions in JS now always propagate with better stack trace. --- lib/system/jssys.nim | 66 +++++++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 29 deletions(-) (limited to 'lib/system') diff --git a/lib/system/jssys.nim b/lib/system/jssys.nim index 9c8a18bfe..0fbf4944a 100644 --- a/lib/system/jssys.nim +++ b/lib/system/jssys.nim @@ -7,11 +7,6 @@ # distribution, for details about the copyright. # -when defined(nodejs): - proc alert*(s: cstring) {.importc: "console.log", nodecl.} -else: - proc alert*(s: cstring) {.importc, nodecl.} - proc log*(s: cstring) {.importc: "console.log", varargs, nodecl.} type @@ -101,26 +96,32 @@ proc getStackTrace*(): string = rawWriteStackTrace() proc unhandledException(e: ref Exception) {. compilerproc, asmNoStackFrame.} = - when NimStackTrace: - var buf = rawWriteStackTrace() + var buf = "" + if e.msg != nil and e.msg[0] != '\0': + add(buf, "Error: unhandled exception: ") + add(buf, e.msg) else: - var buf = "" - if e.msg != nil and e.msg[0] != '\0': - add(buf, "Error: unhandled exception: ") - add(buf, e.msg) - else: - add(buf, "Error: unhandled exception") - add(buf, " [") - add(buf, e.name) - add(buf, "]\n") - alert(buf) + add(buf, "Error: unhandled exception") + add(buf, " [") + add(buf, e.name) + add(buf, "]\n") + when NimStackTrace: + add(buf, rawWriteStackTrace()) + let cbuf : cstring = buf + {.emit: """ + if (typeof(Error) !== "undefined") { + throw new Error(`cbuf`); + } + else { + throw `cbuf`; + } + """.} proc raiseException(e: ref Exception, ename: cstring) {. compilerproc, asmNoStackFrame.} = e.name = ename - when not defined(noUnhandledHandler): - if excHandler == 0: - unhandledException(e) + if excHandler == 0: + unhandledException(e) when defined(nimphp): asm """throw new Exception($`e`["message"]);""" else: @@ -130,15 +131,15 @@ proc reraiseException() {.compilerproc, asmNoStackFrame.} = if lastJSError == nil: raise newException(ReraiseError, "no exception to reraise") else: - when not defined(noUnhandledHandler): - if excHandler == 0: - var isNimException: bool - when defined(nimphp): - asm "`isNimException` = isset(`lastJSError`['m_type']);" - else: - asm "`isNimException` = lastJSError.m_type;" - if isNimException: - unhandledException(cast[ref Exception](lastJSError)) + if excHandler == 0: + var isNimException: bool + when defined(nimphp): + asm "`isNimException` = isset(`lastJSError`['m_type']);" + else: + asm "`isNimException` = lastJSError.m_type;" + if isNimException: + unhandledException(cast[ref Exception](lastJSError)) + asm "throw lastJSError;" proc raiseOverflow {.exportc: "raiseOverflow", noreturn.} = @@ -873,3 +874,10 @@ proc nimParseBiggestFloat(s: string, number: var BiggestFloat, start = 0): int { # evaluate sign number = number * sign result = i - start + +when defined(nodejs): + # Deprecated. Use `alert` defined in dom.nim + proc alert*(s: cstring) {.importc: "console.log", nodecl, deprecated.} +else: + # Deprecated. Use `alert` defined in dom.nim + proc alert*(s: cstring) {.importc, nodecl, deprecated.} -- cgit 1.4.1-2-gfad0 From c1dd65c01a7c94764d8a4c255541136b95e643b7 Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Sat, 27 Aug 2016 18:10:13 +0300 Subject: Reset framePtr on unhabdled exception. Dont reset on getStackTrace. --- lib/system/jssys.nim | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'lib/system') diff --git a/lib/system/jssys.nim b/lib/system/jssys.nim index 0fbf4944a..abec44bbb 100644 --- a/lib/system/jssys.nim +++ b/lib/system/jssys.nim @@ -86,9 +86,6 @@ proc auxWriteStackTrace(f: PCallFrame): string = proc rawWriteStackTrace(): string = if framePtr != nil: result = "Traceback (most recent call last)\n" & auxWriteStackTrace(framePtr) - framePtr = nil - elif lastJSError != nil: - result = $lastJSError.stack else: result = "No stack traceback available\n" @@ -108,6 +105,7 @@ proc unhandledException(e: ref Exception) {. when NimStackTrace: add(buf, rawWriteStackTrace()) let cbuf : cstring = buf + framePtr = nil {.emit: """ if (typeof(Error) !== "undefined") { throw new Error(`cbuf`); -- cgit 1.4.1-2-gfad0