Commit 54f72fe1
54f72fe18953Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · ~3 modified
Description
Merge pull request #322 from ryanseys/fix/float-coerces-strings
fix(codegen): Float() coerces strings + type-infer as float
Details
- Commit
- 54f72fe18953
- Parent
- b8690def4d92
- Merge parent
- 01c047bd3083
spinel_codegen.rbmodifiedView file
@@ -3007,6 +3007,16 @@
30073007
if mname == "to_f"30083008
return "float"30093009
end3010+
# Kernel coercion methods: Integer(x) / Float(x) return their class.3011+
# Only treat as a Kernel call when there's no explicit receiver — with3012+
# a receiver, "Integer" / "Float" would be ConstantReadNode lookups,3013+
# not method calls, and wouldn't reach this name dispatch anyway.3014+
if recv < 0 && mname == "Integer"3015+
return "int"3016+
end3017+
if recv < 0 && mname == "Float"3018+
return "float"3019+
end30103020
# Float#ceil(n)/floor(n)/round(n)/truncate(n) with n given return30113021
# Float; zero-arg / Integer#ceil etc. return Integer. (truncate's arm30123022
# used to live next to nan?/infinite? — folded in here for one place@@ -21144,6 +21154,29 @@
2114421154
return "(mrb_int)(" + compile_arg0(nid) + ")"2114521155
end2114621156
if mname == "Float"21157+
args_id = @nd_arguments[nid]21158+
if args_id >= 021159+
arg_ids = get_args(args_id)21160+
if arg_ids.length > 021161+
a0 = arg_ids[0]21162+
at = infer_type(a0)21163+
if at == "string" || at == "argv"21164+
return "(mrb_float)strtod(" + compile_expr(a0) + ", NULL)"21165+
end21166+
if at == "int"21167+
return "(mrb_float)(" + compile_expr(a0) + ")"21168+
end21169+
if at == "float"21170+
return compile_expr(a0)21171+
end21172+
if at == "poly"21173+
# Polymorphic — string / int / float branches resolve at21174+
# runtime via sp_poly_to_f, which already handles each tag.21175+
@needs_rb_value = 121176+
return "sp_poly_to_f(" + compile_expr(a0) + ")"21177+
end21178+
end21179+
end2114721180
return "(mrb_float)(" + compile_arg0(nid) + ")"2114821181
end2114921182
if mname == "proc"test/float.rbmodifiedView file
Loading diff…
test/float.rb.expectedmodifiedView file
Loading diff…