diff --git a/CHANGELOG.md b/CHANGELOG.md index c21568c7..22bef911 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ All notable changes to this project will be documented in this file. ## Unreleased - ??? +- Add `edefer` macro to core. - A struct/table literal/constructor with duplicate keys will use the last value given. Previously, this was inconsistent between tables and structs, literals and constructor functions. - Add debugger to core. The debugger functions are only available diff --git a/src/boot/boot.janet b/src/boot/boot.janet index 8f48151c..89357727 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -301,7 +301,19 @@ ,form (if (= (,fiber/status ,f) :dead) ,r - (propagate ,r ,f))))) + (,propagate ,r ,f))))) + +(defmacro edefer + "Run form after body in the case that body terminates abnormally (an error or user signal 0-4). + Otherwise, return last form in body." + [form & body] + (with-syms [f r] + ~(do + (def ,f (,fiber/new (fn [] ,;body) :ti)) + (def ,r (,resume ,f)) + (if (= (,fiber/status ,f) :dead) + ,r + (do ,form (,propagate ,r ,f)))))) (defmacro prompt "Set up a checkpoint that can be returned to. Tag should be a value @@ -314,7 +326,7 @@ (def [,target ,payload] ,res) (if (,= ,tag ,target) ,payload - (propagate ,res ,fib))))) + (,propagate ,res ,fib))))) (defmacro chr "Convert a string of length 1 to its byte (ascii) value at compile time." diff --git a/src/core/vm.c b/src/core/vm.c index 868a7b96..4355d650 100644 --- a/src/core/vm.c +++ b/src/core/vm.c @@ -107,13 +107,13 @@ JANET_THREAD_LOCAL jmp_buf *janet_vm_jmp_buf = NULL; #define vm_assert_type(X, T) do { \ if (!(janet_checktype((X), (T)))) { \ vm_commit(); \ - janet_panicf("expected %T, got %t", (1 << (T)), (X)); \ + janet_panicf("expected %T, got %v", (1 << (T)), (X)); \ } \ } while (0) #define vm_assert_types(X, TS) do { \ if (!(janet_checktypes((X), (TS)))) { \ vm_commit(); \ - janet_panicf("expected %T, got %t", (TS), (X)); \ + janet_panicf("expected %T, got %v", (TS), (X)); \ } \ } while (0) @@ -910,7 +910,7 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in) { if (janet_indexed_view(stack[D], &vals, &len)) { janet_fiber_pushn(fiber, vals, len); } else { - janet_panicf("expected %T, got %t", JANET_TFLAG_INDEXED, stack[D]); + janet_panicf("expected %T, got %v", JANET_TFLAG_INDEXED, stack[D]); } } stack = fiber->data + fiber->frame;