mirror of
https://github.com/janet-lang/janet
synced 2025-12-22 16:22:00 +00:00
Change name of debugp to description. Use it
the repl for easier inspection of structures.
This commit is contained in:
75
core/util.c
75
core/util.c
@@ -238,6 +238,81 @@ GstInteger gst_endrange(GstInteger raw, uint32_t len) {
|
||||
return raw;
|
||||
}
|
||||
|
||||
|
||||
/* Static debug print helper */
|
||||
static GstInteger gst_description_helper(Gst *vm, GstBuffer *b, GstTable *seen, GstValue x, GstInteger next, int depth) {
|
||||
GstValue check = gst_table_get(seen, x);
|
||||
const uint8_t *str;
|
||||
/* Prevent a stack overflow */
|
||||
if (depth++ > GST_RECURSION_GUARD)
|
||||
return -1;
|
||||
if (check.type == GST_INTEGER) {
|
||||
str = gst_to_string(vm, check);
|
||||
gst_buffer_append_cstring(vm, b, "<visited ");
|
||||
gst_buffer_append(vm, b, str, gst_string_length(str));
|
||||
gst_buffer_append_cstring(vm, b, ">");
|
||||
} else {
|
||||
uint8_t open, close;
|
||||
uint32_t len, i;
|
||||
const GstValue *data;
|
||||
switch (x.type) {
|
||||
default:
|
||||
str = gst_to_string(vm, x);
|
||||
gst_buffer_append(vm, b, str, gst_string_length(str));
|
||||
return next;
|
||||
case GST_STRUCT:
|
||||
open = '<'; close = '>';
|
||||
break;
|
||||
case GST_TABLE:
|
||||
open = '{'; close = '}';
|
||||
break;
|
||||
case GST_TUPLE:
|
||||
open = '('; close = ')';
|
||||
break;
|
||||
case GST_ARRAY:
|
||||
open = '['; close = ']';
|
||||
break;
|
||||
}
|
||||
gst_table_put(vm, seen, x, gst_wrap_integer(next++));
|
||||
gst_buffer_push(vm, b, open);
|
||||
if (gst_hashtable_view(x, &data, &len)) {
|
||||
int isfirst = 1;
|
||||
for (i = 0; i < len; i += 2) {
|
||||
if (data[i].type != GST_NIL) {
|
||||
if (isfirst)
|
||||
isfirst = 0;
|
||||
else
|
||||
gst_buffer_push(vm, b, ' ');
|
||||
next = gst_description_helper(vm, b, seen, data[i], next, depth);
|
||||
if (next == -1)
|
||||
gst_buffer_append_cstring(vm, b, "...");
|
||||
gst_buffer_push(vm, b, ' ');
|
||||
next = gst_description_helper(vm, b, seen, data[i + 1], next, depth);
|
||||
if (next == -1)
|
||||
gst_buffer_append_cstring(vm, b, "...");
|
||||
}
|
||||
}
|
||||
} else if (gst_seq_view(x, &data, &len)) {
|
||||
for (i = 0; i < len; ++i) {
|
||||
next = gst_description_helper(vm, b, seen, data[i], next, depth);
|
||||
if (next == -1)
|
||||
return -1;
|
||||
if (i != len - 1)
|
||||
gst_buffer_push(vm, b, ' ');
|
||||
}
|
||||
}
|
||||
gst_buffer_push(vm, b, close);
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
/* Debug print. Returns a description of an object as a buffer. */
|
||||
const uint8_t *gst_description(Gst *vm, GstValue x) {
|
||||
GstBuffer *buf = gst_buffer(vm, 10);
|
||||
gst_description_helper(vm, buf, gst_table(vm, 10), x, 0, 0);
|
||||
return gst_buffer_to_string(vm, buf);
|
||||
}
|
||||
|
||||
int gst_callc(Gst *vm, GstCFunction fn, int numargs, ...) {
|
||||
int result, i;
|
||||
va_list args;
|
||||
|
||||
Reference in New Issue
Block a user