diff --git a/src/boot/boot.janet b/src/boot/boot.janet index 922b759e..1178f642 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -2223,7 +2223,7 @@ col ": compile error: ") (if macrof - (debug/stacktrace macrof msg) + (debug/stacktrace macrof msg "") (eprint msg)) (when ec (print-line-col where line col) @@ -2287,12 +2287,13 @@ (default on-compile-warning warn-compile) (default on-parse-error bad-parse) (default evaluator (fn evaluate [x &] (x))) - (default default-where "") (default guard :ydt) (var where default-where) - (unless (= where "") (put env :current-file where)) + (if where + (put env :current-file where) + (set where "")) # Evaluate 1 source form in a protected manner (def lints @[]) @@ -2659,8 +2660,7 @@ :on-status (fn [f x] (when (not= (fiber/status f) :dead) (when exit - (eprint x) - (debug/stacktrace f) + (debug/stacktrace f x "") (eflush) (os/exit 1)) (put env :exit true) @@ -3167,7 +3167,7 @@ "Print the current fiber stack" [] (print) - (with-dyns [:err-color false] (debug/stacktrace (.fiber) (.signal))) + (with-dyns [:err-color false] (debug/stacktrace (.fiber) (.signal) "")) (print)) (defn .frame @@ -3364,9 +3364,7 @@ (printf (get e :pretty-format "%q") x) (flush)) (do - (def ec (dyn :err-color)) - (eprint (if ec "\e[31m" "") fs ": " x) - (debug/stacktrace f) + (debug/stacktrace f x "") (eflush) (if (e :debug) (enter-debugger f x)))))) @@ -3520,8 +3518,7 @@ (try (dofile path :evaluator flycheck-evaluator ;(kvs kwargs)) ([e f] - (eprint e) - (debug/stacktrace f))) + (debug/stacktrace f e ""))) nil) ### diff --git a/src/core/debug.c b/src/core/debug.c index 81676775..3899ca2c 100644 --- a/src/core/debug.c +++ b/src/core/debug.c @@ -96,15 +96,18 @@ void janet_debug_find( } } +void janet_stacktrace(JanetFiber *fiber, Janet err) { + janet_stacktrace_ext(fiber, err, NULL); +} + /* Error reporting. This can be emulated from within Janet, but for * consitency with the top level code it is defined once. */ -void janet_stacktrace(JanetFiber *fiber, Janet err) { +void janet_stacktrace_ext(JanetFiber *fiber, Janet err, const char *prefix) { + int32_t fi; const char *errstr = (const char *)janet_to_string(err); JanetFiber **fibers = NULL; - - /* Don't print error line if it is nil. */ - int wrote_error = janet_checktype(err, JANET_NIL); + int wrote_error = !prefix; int print_color = janet_truthy(janet_dyn("err-color")); if (print_color) janet_eprintf("\x1b[31m"); @@ -126,9 +129,9 @@ void janet_stacktrace(JanetFiber *fiber, Janet err) { /* Print prelude to stack frame */ if (!wrote_error) { JanetFiberStatus status = janet_fiber_status(fiber); - const char *prefix = status == JANET_STATUS_ERROR ? "" : "status "; + const char *override_prefix = prefix ? prefix : ""; janet_eprintf("%s%s: %s\n", - prefix, + override_prefix, janet_status_names[status], errstr); wrote_error = 1; @@ -361,14 +364,15 @@ JANET_CORE_FN(cfun_debug_stack, } JANET_CORE_FN(cfun_debug_stacktrace, - "(debug/stacktrace fiber &opt err)", + "(debug/stacktrace fiber &opt err prefix)", "Prints a nice looking stacktrace for a fiber. Can optionally provide " "an error value to print the stack trace with. If `err` is nil or not " - "provided, will skip the error line. Returns the fiber.") { - janet_arity(argc, 1, 2); + "provided, and no prefix is given, will skip the error line. Returns the fiber.") { + janet_arity(argc, 1, 3); JanetFiber *fiber = janet_getfiber(argv, 0); Janet x = argc == 1 ? janet_wrap_nil() : argv[1]; - janet_stacktrace(fiber, x); + const char *prefix = janet_optcstring(argv, argc, 2, NULL); + janet_stacktrace_ext(fiber, x, prefix); return argv[0]; } diff --git a/src/include/janet.h b/src/include/janet.h index ee486da0..d128a7c1 100644 --- a/src/include/janet.h +++ b/src/include/janet.h @@ -1763,6 +1763,7 @@ JANET_API JanetSignal janet_step(JanetFiber *fiber, Janet in, Janet *out); JANET_API Janet janet_call(JanetFunction *fun, int32_t argc, const Janet *argv); JANET_API Janet janet_mcall(const char *name, int32_t argc, Janet *argv); JANET_API void janet_stacktrace(JanetFiber *fiber, Janet err); +JANET_API void janet_stacktrace_ext(JanetFiber *fiber, Janet err, const char *prefix); /* Scratch Memory API */ typedef void (*JanetScratchFinalizer)(void *);