Commit 82b0ad9c

82b0ad9c6178Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Kernel#Integer/#Float dispatch on the argument type

Integer()/Float() previously always routed through the strict string parser (sp_str_to_i_strict / atof), so a numeric argument passed a scalar where a const char* was expected. They now pick by arg type: strict parse for a String, a numeric cast for Int/Float, sp_poly_to_* for poly.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
82b0ad9c6178
Parent
dd685456cc2f

src/codegen.cmodifiedView file

@@ -859,8 +859,22 @@
859859 if (recv < 0 && comp_method_index(c, name) < 0) {
860860 int args = nt_ref(nt, id, "arguments");
861861 int ac = 0; const int *av = args >= 0 ? nt_arr(nt, args, "arguments", &ac) : NULL;
862- if (!strcmp(name, "Integer") && ac == 1) { buf_puts(b, "sp_str_to_i_strict("); emit_expr(c, av[0], b); buf_puts(b, ")"); return; }
863- if (!strcmp(name, "Float") && ac == 1) { buf_puts(b, "atof("); emit_expr(c, av[0], b); buf_puts(b, ")"); return; }
862+ if (!strcmp(name, "Integer") && ac == 1) {
863+ TyKind at = comp_ntype(c, av[0]);
864+ if (at == TY_STRING) { buf_puts(b, "sp_str_to_i_strict("); emit_expr(c, av[0], b); buf_puts(b, ")"); }
865+ else if (at == TY_FLOAT) { buf_puts(b, "((mrb_int)("); emit_expr(c, av[0], b); buf_puts(b, "))"); }
866+ else if (at == TY_POLY) { buf_puts(b, "sp_poly_to_i("); emit_expr(c, av[0], b); buf_puts(b, ")"); }
867+ else { buf_puts(b, "("); emit_expr(c, av[0], b); buf_puts(b, ")"); }
868+ return;
869+ }
870+ if (!strcmp(name, "Float") && ac == 1) {
871+ TyKind at = comp_ntype(c, av[0]);
872+ if (at == TY_STRING) { buf_puts(b, "sp_str_to_f_strict("); emit_expr(c, av[0], b); buf_puts(b, ")"); }
873+ else if (at == TY_INT) { buf_puts(b, "((mrb_float)("); emit_expr(c, av[0], b); buf_puts(b, "))"); }
874+ else if (at == TY_POLY) { buf_puts(b, "sp_poly_to_f("); emit_expr(c, av[0], b); buf_puts(b, ")"); }
875+ else { buf_puts(b, "("); emit_expr(c, av[0], b); buf_puts(b, ")"); }
876+ return;
877+ }
864878 }
865879
866880 /* raise */