summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--compiler/vm.nim12
-rw-r--r--compiler/vmops.nim4
-rw-r--r--tests/vm/texception.nim14
3 files changed, 28 insertions, 2 deletions
diff --git a/compiler/vm.nim b/compiler/vm.nim
index 27b067da2..30ba6f32f 100644
--- a/compiler/vm.nim
+++ b/compiler/vm.nim
@@ -102,7 +102,8 @@ template stackTrace(c: PCtx, tos: PStackFrame, pc: int, msg: string) =
 
 proc bailOut(c: PCtx; tos: PStackFrame) =
   stackTrace(c, tos, c.exceptionInstr, "unhandled exception: " &
-             c.currentExceptionA.sons[3].skipColon.strVal)
+             c.currentExceptionA.sons[3].skipColon.strVal &
+             " [" & c.currentExceptionA.sons[2].skipColon.strVal & "]")
 
 when not defined(nimComputedGoto):
   {.pragma: computedGoto.}
@@ -1199,8 +1200,15 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
           tos = savedFrame
           move(regs, tos.slots)
     of opcRaise:
-      let raised = regs[ra].node
+      let raised =
+        # Empty `raise` statement - reraise current exception
+        if regs[ra].kind == rkNone:
+          c.currentExceptionA
+        else:
+          regs[ra].node
       c.currentExceptionA = raised
+      # Set the `name` field of the exception
+      c.currentExceptionA.sons[2].skipColon.strVal = c.currentExceptionA.typ.sym.name.s
       c.exceptionInstr = pc
 
       var frame = tos
diff --git a/compiler/vmops.nim b/compiler/vmops.nim
index 3bd931b39..5087dabfa 100644
--- a/compiler/vmops.nim
+++ b/compiler/vmops.nim
@@ -79,6 +79,9 @@ proc getCurrentExceptionMsgWrapper(a: VmArgs) {.nimcall.} =
   setResult(a, if a.currentException.isNil: ""
                else: a.currentException.sons[3].skipColon.strVal)
 
+proc getCurrentExceptionWrapper(a: VmArgs) {.nimcall.} =
+  setResult(a, a.currentException)
+
 proc staticWalkDirImpl(path: string, relative: bool): PNode =
   result = newNode(nkBracket)
   for k, f in walkDir(path, relative):
@@ -132,6 +135,7 @@ proc registerAdditionalOps*(c: PCtx) =
     wrap1s(readFile, ioop)
     wrap2si(readLines, ioop)
     systemop getCurrentExceptionMsg
+    systemop getCurrentException
     registerCallback c, "stdlib.*.staticWalkDir", proc (a: VmArgs) {.nimcall.} =
       setResult(a, staticWalkDirImpl(getString(a, 0), getBool(a, 1)))
     systemop gorgeEx
diff --git a/tests/vm/texception.nim b/tests/vm/texception.nim
new file mode 100644
index 000000000..65a781281
--- /dev/null
+++ b/tests/vm/texception.nim
@@ -0,0 +1,14 @@
+proc someFunc() =
+  try:
+    raise newException(ValueError, "message")
+  except ValueError as err:
+    doAssert err.name == "ValueError"
+    doAssert err.msg == "message"
+    raise
+
+static:
+  try:
+    someFunc()
+  except:
+    discard
+  
\ No newline at end of file