From 553b1aa3f82cedaf8a33229322f8c81562720beb Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 26 Sep 2025 07:05:26 +0900 Subject: [PATCH] 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 --- mrbgems/mruby-compiler/core/parse.y | 43 ++++++++++++----------------- mrbgems/mruby-compiler/core/y.tab.c | 43 ++++++++++++----------------- 2 files changed, 34 insertions(+), 52 deletions(-) diff --git a/mrbgems/mruby-compiler/core/parse.y b/mrbgems/mruby-compiler/core/parse.y index 0f100cb57..c37f476a5 100644 --- a/mrbgems/mruby-compiler/core/parse.y +++ b/mrbgems/mruby-compiler/core/parse.y @@ -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 diff --git a/mrbgems/mruby-compiler/core/y.tab.c b/mrbgems/mruby-compiler/core/y.tab.c index f53a19bcf..c199e1ee2 100644 --- a/mrbgems/mruby-compiler/core/y.tab.c +++ b/mrbgems/mruby-compiler/core/y.tab.c @@ -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