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 end
3010+ # Kernel coercion methods: Integer(x) / Float(x) return their class.
3011+ # Only treat as a Kernel call when there's no explicit receiver — with
3012+ # 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+ end
3017+ if recv < 0 && mname == "Float"
3018+ return "float"
3019+ end
30103020 # Float#ceil(n)/floor(n)/round(n)/truncate(n) with n given return
30113021 # Float; zero-arg / Integer#ceil etc. return Integer. (truncate's arm
30123022 # 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 end
2114621156 if mname == "Float"
21157+ args_id = @nd_arguments[nid]
21158+ if args_id >= 0
21159+ arg_ids = get_args(args_id)
21160+ if arg_ids.length > 0
21161+ 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+ end
21166+ if at == "int"
21167+ return "(mrb_float)(" + compile_expr(a0) + ")"
21168+ end
21169+ if at == "float"
21170+ return compile_expr(a0)
21171+ end
21172+ if at == "poly"
21173+ # Polymorphic — string / int / float branches resolve at
21174+ # runtime via sp_poly_to_f, which already handles each tag.
21175+ @needs_rb_value = 1
21176+ return "sp_poly_to_f(" + compile_expr(a0) + ")"
21177+ end
21178+ end
21179+ end
2114721180 return "(mrb_float)(" + compile_arg0(nid) + ")"
2114821181 end
2114921182 if mname == "proc"