summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2021-04-26 20:05:51 +0200
committerJo-Philipp Wich <jo@mein.io>2021-04-27 12:18:32 +0200
commit9ef693edd6c9572c0ab0b1381fe5e65a8430e8fb (patch)
tree113914d774f7459bd77dbafaf719dd04e33fe15c
parent6def9fcf1cdec117866877aa5e7241e23bdc8c23 (diff)
vm: improve context for early errors
Print "Before start of program" for errors that are raised before entering main(), e.g. on module preloading failure. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r--tests/cram/test_basic.t4
-rw-r--r--vm.c4
2 files changed, 5 insertions, 3 deletions
diff --git a/tests/cram/test_basic.t b/tests/cram/test_basic.t
index 5061d37..982d58a 100644
--- a/tests/cram/test_basic.t
+++ b/tests/cram/test_basic.t
@@ -37,13 +37,13 @@ check that ucode provides proper error messages:
$ ucode -m foo -s ''
Runtime error: No module named 'foo' could be found
- At offset 0
+ At start of program
[1]
$ touch moo; ucode -m foo -i moo
Runtime error: No module named 'foo' could be found
- At offset 0
+ At start of program
[1]
diff --git a/vm.c b/vm.c
index f7d2e09..7d60805 100644
--- a/vm.c
+++ b/vm.c
@@ -842,8 +842,10 @@ uc_vm_get_error_context(uc_vm *vm)
if (offset)
format_error_context(buf, frame->closure->function->source, stacktrace, offset);
+ else if (frame->ip != chunk->entries)
+ ucv_stringbuf_printf(buf, "At instruction %zu", (frame->ip - chunk->entries) - 1);
else
- ucv_stringbuf_printf(buf, "At offset %zu", (frame->ip - chunk->entries) - 1);
+ ucv_stringbuf_append(buf, "At start of program");
ucv_object_add(ucv_array_get(stacktrace, 0), "context", ucv_stringbuf_finish(buf));