Commit c49a10f9 authored by KOBAYASHI Shuji's avatar KOBAYASHI Shuji

Refactor `src/backtrace.c`

- Move calling `mrb_debug_get_filename()` to after `lineno` check.
- Remove unneeded array check in `print_backtrace()`.
- Add a few `const` qualifier.
parent ad87fd22
...@@ -21,7 +21,7 @@ struct backtrace_location { ...@@ -21,7 +21,7 @@ struct backtrace_location {
const char *filename; const char *filename;
}; };
typedef void (*each_backtrace_func)(mrb_state*, struct backtrace_location*, void*); typedef void (*each_backtrace_func)(mrb_state*, const struct backtrace_location*, void*);
static const mrb_data_type bt_type = { "Backtrace", mrb_free }; static const mrb_data_type bt_type = { "Backtrace", mrb_free };
...@@ -57,11 +57,11 @@ each_backtrace(mrb_state *mrb, ptrdiff_t ciidx, mrb_code *pc0, each_backtrace_fu ...@@ -57,11 +57,11 @@ each_backtrace(mrb_state *mrb, ptrdiff_t ciidx, mrb_code *pc0, each_backtrace_fu
else { else {
pc = pc0; pc = pc0;
} }
loc.filename = mrb_debug_get_filename(irep, pc - irep->iseq);
loc.lineno = mrb_debug_get_line(irep, pc - irep->iseq);
loc.lineno = mrb_debug_get_line(irep, pc - irep->iseq);
if (loc.lineno == -1) continue; if (loc.lineno == -1) continue;
loc.filename = mrb_debug_get_filename(irep, pc - irep->iseq);
if (!loc.filename) { if (!loc.filename) {
loc.filename = "(unknown)"; loc.filename = "(unknown)";
} }
...@@ -80,8 +80,6 @@ print_backtrace(mrb_state *mrb, mrb_value backtrace) ...@@ -80,8 +80,6 @@ print_backtrace(mrb_state *mrb, mrb_value backtrace)
mrb_int n; mrb_int n;
FILE *stream = stderr; FILE *stream = stderr;
if (!mrb_array_p(backtrace)) return;
n = RARRAY_LEN(backtrace) - 1; n = RARRAY_LEN(backtrace) - 1;
if (n == 0) return; if (n == 0) return;
...@@ -96,7 +94,7 @@ print_backtrace(mrb_state *mrb, mrb_value backtrace) ...@@ -96,7 +94,7 @@ print_backtrace(mrb_state *mrb, mrb_value backtrace)
} }
static int static int
packed_bt_len(struct backtrace_location *bt, int n) packed_bt_len(const struct backtrace_location *bt, int n)
{ {
int len = 0; int len = 0;
int i; int i;
...@@ -113,7 +111,7 @@ static void ...@@ -113,7 +111,7 @@ static void
print_packed_backtrace(mrb_state *mrb, mrb_value packed) print_packed_backtrace(mrb_state *mrb, mrb_value packed)
{ {
FILE *stream = stderr; FILE *stream = stderr;
struct backtrace_location *bt; const struct backtrace_location *bt;
int n, i; int n, i;
int ai = mrb_gc_arena_save(mrb); int ai = mrb_gc_arena_save(mrb);
...@@ -124,7 +122,7 @@ print_packed_backtrace(mrb_state *mrb, mrb_value packed) ...@@ -124,7 +122,7 @@ print_packed_backtrace(mrb_state *mrb, mrb_value packed)
if (packed_bt_len(bt, n) == 0) return; if (packed_bt_len(bt, n) == 0) return;
fprintf(stream, "trace (most recent call last):\n"); fprintf(stream, "trace (most recent call last):\n");
for (i = 0; i<n; i++) { for (i = 0; i<n; i++) {
struct backtrace_location *entry = &bt[n-i-1]; const struct backtrace_location *entry = &bt[n-i-1];
if (entry->filename == NULL) continue; if (entry->filename == NULL) continue;
fprintf(stream, "\t[%d] %s:%d", i, entry->filename, entry->lineno); fprintf(stream, "\t[%d] %s:%d", i, entry->filename, entry->lineno);
if (entry->method_id != 0) { if (entry->method_id != 0) {
...@@ -172,7 +170,7 @@ mrb_print_backtrace(mrb_state *mrb) ...@@ -172,7 +170,7 @@ mrb_print_backtrace(mrb_state *mrb)
static void static void
count_backtrace_i(mrb_state *mrb, count_backtrace_i(mrb_state *mrb,
struct backtrace_location *loc, const struct backtrace_location *loc,
void *data) void *data)
{ {
int *lenp = (int*)data; int *lenp = (int*)data;
...@@ -183,7 +181,7 @@ count_backtrace_i(mrb_state *mrb, ...@@ -183,7 +181,7 @@ count_backtrace_i(mrb_state *mrb,
static void static void
pack_backtrace_i(mrb_state *mrb, pack_backtrace_i(mrb_state *mrb,
struct backtrace_location *loc, const struct backtrace_location *loc,
void *data) void *data)
{ {
struct backtrace_location **pptr = (struct backtrace_location**)data; struct backtrace_location **pptr = (struct backtrace_location**)data;
...@@ -229,7 +227,7 @@ mrb_keep_backtrace(mrb_state *mrb, mrb_value exc) ...@@ -229,7 +227,7 @@ mrb_keep_backtrace(mrb_state *mrb, mrb_value exc)
mrb_value mrb_value
mrb_unpack_backtrace(mrb_state *mrb, mrb_value backtrace) mrb_unpack_backtrace(mrb_state *mrb, mrb_value backtrace)
{ {
struct backtrace_location *bt; const struct backtrace_location *bt;
mrb_int n, i; mrb_int n, i;
int ai; int ai;
...@@ -244,7 +242,7 @@ mrb_unpack_backtrace(mrb_state *mrb, mrb_value backtrace) ...@@ -244,7 +242,7 @@ mrb_unpack_backtrace(mrb_state *mrb, mrb_value backtrace)
backtrace = mrb_ary_new_capa(mrb, n); backtrace = mrb_ary_new_capa(mrb, n);
ai = mrb_gc_arena_save(mrb); ai = mrb_gc_arena_save(mrb);
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
struct backtrace_location *entry = &bt[i]; const struct backtrace_location *entry = &bt[i];
mrb_value btline; mrb_value btline;
if (entry->filename == NULL) continue; if (entry->filename == NULL) continue;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment