mirror of
https://github.com/janet-lang/janet
synced 2025-11-08 19:43:41 +00:00
Change name of debugp to description. Use it
the repl for easier inspection of structures.
This commit is contained in:
80
core/stl.c
80
core/stl.c
@@ -832,83 +832,11 @@ int gst_stl_gcollect(Gst *vm) {
|
||||
return GST_RETURN_OK;
|
||||
}
|
||||
|
||||
/* Static debug print helper */
|
||||
static GstInteger gst_stl_debugp_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_stl_debugp_helper(vm, b, seen, data[i], next, depth);
|
||||
if (next == -1)
|
||||
return -1;
|
||||
gst_buffer_push(vm, b, ' ');
|
||||
next = gst_stl_debugp_helper(vm, b, seen, data[i + 1], next, depth);
|
||||
if (next == -1)
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
} else if (gst_seq_view(x, &data, &len)) {
|
||||
for (i = 0; i < len; ++i) {
|
||||
next = gst_stl_debugp_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 */
|
||||
int gst_stl_debugp(Gst *vm) {
|
||||
int gst_stl_description(Gst *vm) {
|
||||
GstValue x = gst_arg(vm, 0);
|
||||
GstBuffer *buf = gst_buffer(vm, 10);
|
||||
GstInteger res = gst_stl_debugp_helper(vm, buf, gst_table(vm, 10), x, 0, 0);
|
||||
if (res == -1) {
|
||||
gst_c_throwc(vm, "recursed too deeply in debugp");
|
||||
} else {
|
||||
gst_c_return(vm, gst_wrap_string(gst_buffer_to_string(vm, buf)));
|
||||
}
|
||||
const uint8_t *buf = gst_description(vm, x);
|
||||
gst_c_return(vm, gst_wrap_string(buf));
|
||||
}
|
||||
|
||||
/***/
|
||||
@@ -1169,7 +1097,7 @@ static const GstModuleItem std_module[] = {
|
||||
{"funcdef", gst_stl_funcdef},
|
||||
{"funcparent", gst_stl_funcparent},
|
||||
{"gcollect", gst_stl_gcollect},
|
||||
{"debugp", gst_stl_debugp},
|
||||
{"description", gst_stl_description},
|
||||
{"global-def", gst_stl_def},
|
||||
{"global-var", gst_stl_var},
|
||||
{NULL, NULL}
|
||||
|
||||
Reference in New Issue
Block a user