From 78d22d1accce74191bbfd68dce9e6ac11bc04fcc Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Mon, 9 Jun 2025 12:19:56 +0100 Subject: [PATCH] Tweak display of stack traces Tweak the frames to display and try to harden the code to crash less. --- knots.scm | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/knots.scm b/knots.scm index 42f2af7..05b2a1a 100644 --- a/knots.scm +++ b/knots.scm @@ -75,6 +75,8 @@ 0 (and prompt-tag 1))) (_ (make-stack #t)))) + (stack-len + (stack-length stack)) (error-string (call-with-output-string (lambda (port) @@ -83,30 +85,46 @@ (filter knots-exception? (simple-exceptions exn))))) - (let ((stack-vec - (stack->vector stack))) + (let* ((stack-vec + (stack->vector stack)) + (stack-vec-length + (vector-length stack-vec))) (print-frames (list->vector (drop (vector->list stack-vec) - 6)) + (if (< stack-vec-length 5) + 0 + 4))) port #:count (stack-length stack))) (for-each (lambda (stack) - (let ((stack-vec - (stack->vector stack))) + (let* ((stack-vec + (stack->vector stack)) + (stack-vec-length + (vector-length stack-vec))) (print-frames (list->vector (drop (vector->list stack-vec) - 3)) + (if (< stack-vec-length 4) + 0 + 3))) port #:count (stack-length stack)))) knots-stacks) (print-exception port (if (null? knots-stacks) - (stack-ref stack 1) - (stack-ref (last knots-stacks) 3)) + (stack-ref stack + (if (< stack-len 4) + stack-len + 4)) + (let* ((stack (last knots-stacks)) + (stack-len (stack-length stack))) + (stack-ref stack + (if (< stack-len 3) + stack-len + 3)))) '%exception (list exn))))))) (display error-string port)))