Commit c2b696d6

c2b696d6466bMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

Merge PR #652: coerce poly args in chained concat and print to const char*

fix(codegen): coerce poly args in chained concat and print to const char*

Details

Commit
c2b696d6466b
Parent
621627dbb4d5
Merge parent
2ecfa55b05ba

spinel_codegen.rbmodifiedView file

@@ -15448,6 +15448,23 @@
1544815448 parts.push("sp_bigint_to_s((sp_Bigint *)" + compile_expr(aargs[0]) + ")")
1544915449 elsif at == "float"
1545015450 parts.push("sp_float_to_s(" + compile_expr(aargs[0]) + ")")
15451+ elsif at == "poly"
15452+ # `"a" + poly_local + "b"` -- the 2-arg sp_str_concat path at
15453+ # the single-`+` site already coerces via sp_poly_to_s; mirror
15454+ # that here for chained concat collapsing into sp_str_concat3 /
15455+ # sp_str_concat_arr. Without this, the chained form silently
15456+ # passed an sp_RbVal into a `const char *` parameter and cc
15457+ # rejected the generated code. Common shape: a local typed
15458+ # through `if x.nil?; ""; else; x; end` when x came from a
15459+ # poly-valued hash lookup -- the union stays sp_RbVal but its
15460+ # string-side payload is always a valid c-string at runtime.
15461+ parts.push("sp_poly_to_s(" + compile_expr(aargs[0]) + ")")
15462+ elsif at == "symbol"
15463+ parts.push("sp_sym_to_s(" + compile_expr(aargs[0]) + ")")
15464+ elsif at == "mutable_str"
15465+ parts.push("(" + compile_expr(aargs[0]) + ")->data")
15466+ elsif at == "bigint"
15467+ parts.push("sp_bigint_to_s(" + compile_expr(aargs[0]) + ")")
1545115468 else
1545215469 parts.push(compile_expr(aargs[0]))
1545315470 end
@@ -32210,16 +32227,25 @@
3221032227 end
3221132228 if at == "int"
3221232229 emit(" printf(\"%lld\", (long long)" + val + ");")
32230+ elsif at == "mutable_str"
32231+ emit(" fputs(" + val + "->data, stdout);")
32232+ elsif at == "string" || at == "string?"
32233+ emit(" if (" + val + ") fputs(" + val + ", stdout);")
32234+ elsif at == "float"
32235+ emit(" fputs(sp_float_to_s(" + val + "), stdout);")
32236+ elsif at == "bool"
32237+ emit(" fputs(" + val + " ? \"true\" : \"false\", stdout);")
32238+ elsif at == "poly"
32239+ # `print poly_local` -- route through sp_poly_to_s rather than
32240+ # falling through to the `printf("%lld", (long long)val)` path,
32241+ # which is a C-level type error (sp_RbVal is a struct; the cast
32242+ # rejects). sp_poly_to_s switches on the tag and returns a
32243+ # valid c-string for every variant, matching CRuby's
32244+ # `print obj` -> `obj.to_s` semantics.
32245+ @needs_rb_value = 1
32246+ emit(" fputs(sp_poly_to_s(" + val + "), stdout);")
3221332247 else
32214- if at == "mutable_str"
32215- emit(" fputs(" + val + "->data, stdout);")
32216- else
32217- if at == "string"
32218- emit(" fputs(" + val + ", stdout);")
32219- else
32220- emit(" printf(\"%lld\", (long long)" + val + ");")
32221- end
32222- end
32248+ emit(" printf(\"%lld\", (long long)" + val + ");")
3222332249 end
3222432250 k = k + 1
3222532251 end