mruby-compiler: enable str_dump for better string representation in parser dump

Move str_dump function from commented section to active code and update
dump_str to use proper string dumping with escape sequence handling.
Remove obsolete commented str_dump implementation.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-09-26 07:05:26 +09:00
parent e73212c57f
commit 553b1aa3f8
2 changed files with 34 additions and 52 deletions
+17 -26
View File
@@ -7538,6 +7538,22 @@ dump_cpath(mrb_state *mrb, node *tree, int offset, uint16_t lineno)
printf("name: %s\n", mrb_sym_dump(mrb, node_to_sym(tree->cdr)));
}
/*
* This function restores the GC arena on return.
* For this reason, if a process that further generates an object is
* performed at the caller, the string pointer returned as the return
* value may become invalid.
*/
static const char*
str_dump(mrb_state *mrb, const char *str, int len)
{
int ai = mrb_gc_arena_save(mrb);
mrb_value s = mrb_str_new(mrb, str, (mrb_int)len);
s = mrb_str_dump(mrb, s);
mrb_gc_arena_restore(mrb, ai);
return RSTRING_PTR(s);
}
static void
dump_str(mrb_state *mrb, node *n, int offset, uint16_t lineno)
{
@@ -7545,7 +7561,7 @@ dump_str(mrb_state *mrb, node *n, int offset, uint16_t lineno)
dump_prefix(offset, lineno);
int len = node_to_int(n->car->car);
if (len >= 0) {
printf("str: \"%.*s\"\n", len, (char*)n->car->cdr);
printf("str: %s\n", str_dump(mrb, (char*)n->car->cdr, node_to_int(n->car->car)));
}
else {
printf("interpolation:\n");
@@ -7661,31 +7677,6 @@ dump_callargs(mrb_state *mrb, node *n, int offset, uint16_t lineno)
}
}
/*
* This function restores the GC arena on return.
* For this reason, if a process that further generates an object is
* performed at the caller, the string pointer returned as the return
* value may become invalid.
*/
#if 0
static const char*
str_dump(mrb_state *mrb, const char *str, int len)
{
int ai = mrb_gc_arena_save(mrb);
mrb_value s;
# if INT_MAX > MRB_INT_MAX / 4
/* check maximum length with "\xNN" character */
if (len > MRB_INT_MAX / 4) {
len = MRB_INT_MAX / 4;
}
# endif
s = mrb_str_new(mrb, str, (mrb_int)len);
s = mrb_str_dump(mrb, s);
mrb_gc_arena_restore(mrb, ai);
return RSTRING_PTR(s);
}
#endif
#endif
void
+17 -26
View File
@@ -14367,6 +14367,22 @@ dump_cpath(mrb_state *mrb, node *tree, int offset, uint16_t lineno)
printf("name: %s\n", mrb_sym_dump(mrb, node_to_sym(tree->cdr)));
}
/*
* This function restores the GC arena on return.
* For this reason, if a process that further generates an object is
* performed at the caller, the string pointer returned as the return
* value may become invalid.
*/
static const char*
str_dump(mrb_state *mrb, const char *str, int len)
{
int ai = mrb_gc_arena_save(mrb);
mrb_value s = mrb_str_new(mrb, str, (mrb_int)len);
s = mrb_str_dump(mrb, s);
mrb_gc_arena_restore(mrb, ai);
return RSTRING_PTR(s);
}
static void
dump_str(mrb_state *mrb, node *n, int offset, uint16_t lineno)
{
@@ -14374,7 +14390,7 @@ dump_str(mrb_state *mrb, node *n, int offset, uint16_t lineno)
dump_prefix(offset, lineno);
int len = node_to_int(n->car->car);
if (len >= 0) {
printf("str: \"%.*s\"\n", len, (char*)n->car->cdr);
printf("str: %s\n", str_dump(mrb, (char*)n->car->cdr, node_to_int(n->car->car)));
}
else {
printf("interpolation:\n");
@@ -14490,31 +14506,6 @@ dump_callargs(mrb_state *mrb, node *n, int offset, uint16_t lineno)
}
}
/*
* This function restores the GC arena on return.
* For this reason, if a process that further generates an object is
* performed at the caller, the string pointer returned as the return
* value may become invalid.
*/
#if 0
static const char*
str_dump(mrb_state *mrb, const char *str, int len)
{
int ai = mrb_gc_arena_save(mrb);
mrb_value s;
# if INT_MAX > MRB_INT_MAX / 4
/* check maximum length with "\xNN" character */
if (len > MRB_INT_MAX / 4) {
len = MRB_INT_MAX / 4;
}
# endif
s = mrb_str_new(mrb, str, (mrb_int)len);
s = mrb_str_dump(mrb, s);
mrb_gc_arena_restore(mrb, ai);
return RSTRING_PTR(s);
}
#endif
#endif
void