From bd2e3350632a5aae0e25d9613dc3f6c4e49722f3 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sun, 17 Jan 2021 13:55:40 -0600 Subject: [PATCH] Allow 1 argument call of debug/stacktrace Since fibers now track the last value signaled. --- src/core/debug.c | 14 ++++++++------ src/core/vm.c | 6 +++++- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/core/debug.c b/src/core/debug.c index 722a85da..75aa6f99 100644 --- a/src/core/debug.c +++ b/src/core/debug.c @@ -299,9 +299,11 @@ static Janet cfun_debug_stack(int32_t argc, Janet *argv) { } static Janet cfun_debug_stacktrace(int32_t argc, Janet *argv) { - janet_fixarity(argc, 2); + janet_arity(argc, 1, 2); JanetFiber *fiber = janet_getfiber(argv, 0); - janet_stacktrace(fiber, argv[1]); + Janet x = argc == 1 ? janet_wrap_nil() : argv[1]; + x = janet_checktype(x, JANET_NIL) ? fiber->last_value : x; + janet_stacktrace(fiber, x); return argv[0]; } @@ -377,10 +379,10 @@ static const JanetReg debug_cfuns[] = { }, { "debug/stacktrace", cfun_debug_stacktrace, - JDOC("(debug/stacktrace fiber err)\n\n" - "Prints a nice looking stacktrace for a fiber. The error message " - "err must be passed to the function as fiber's do not keep track of " - "the last error they have thrown. Returns the fiber.") + JDOC("(debug/stacktrace fiber &opt err)\n\n" + "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 default to `(fiber/last-value fiber)`. Returns the fiber.") }, { "debug/lineage", cfun_debug_lineage, diff --git a/src/core/vm.c b/src/core/vm.c index 4f36c6c4..84103e42 100644 --- a/src/core/vm.c +++ b/src/core/vm.c @@ -1065,7 +1065,11 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in) { janet_status_names[sub_status]); } fiber->child = f; - vm_return((int) sub_status, stack[B]); + if (janet_checktype(stack[B], JANET_NIL)) { + vm_return((int) sub_status, f->last_value); + } else { + vm_return((int) sub_status, stack[B]); + } } VM_OP(JOP_CANCEL) {