Commit 98377fca
98377fcacc87Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): FFI :str / :ptr arg extracts .v.s / .v.p from sp_RbVal
Closes #502. An FFI function declared with a :str parameter
takes const char *. When the call site passed an sp_RbVal
value (e.g. an ivar widened to poly via a ternary or branch),
compile_ffi_func_call emitted the raw struct as the arg and
the C compile failed with "passing 'sp_RbVal' to parameter of
incompatible type 'const char *'".
Fix in compile_ffi_func_call's per-arg coercion:
-
spec == "str": when infer_type(arg) is "poly", emit(<arg>).v.s(string payload of the tagged union) instead of the raw expression. -
spec == "ptr": same shape, emit((void *)(<arg>).v.p)for poly args.
Non-poly args keep the original direct-forwarding behavior.
The infer_type() call is inlined at each branch rather than bound to a local because adding fresh locals here trips self-host scan_locals (see feedback_self_host_recursive_fn_arm).
Test fixture uses libc's atoi(const char *) (an always-
available external) to exercise the :str coercion through a
real call; the test asserts the program output under -Werror,
which fails the build if the .v.s extraction regresses.
Local: 475 tests pass.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 98377fcacc87
- Parent
- 8b564e01c786
spinel_codegen.rbmodifiedView file
spec = arg_specs[k] ctype = ffi_c_type_of(spec) # `:str` forwards the raw `const char *` directly — casting would # discard `const` and trigger a warning. # discard `const` and trigger a warning. When the arg's static # type is poly (sp_RbVal), extract `.v.s` (issue #502). The # ternary on infer_type is inlined rather than bound to a local # because adding fresh locals here trips self-host scan_locals; # see feedback_self_host_recursive_fn_arm. if spec == "str" result = result + compile_expr(call_args[k]) if infer_type(call_args[k]) == "poly" @needs_rb_value = 1 result = result + "(" + compile_expr(call_args[k]) + ").v.s" else result = result + compile_expr(call_args[k]) end elsif spec == "ptr" result = result + "((void *)" + compile_expr(call_args[k]) + ")" if infer_type(call_args[k]) == "poly" @needs_rb_value = 1 result = result + "((void *)(" + compile_expr(call_args[k]) + ").v.p)" else result = result + "((void *)" + compile_expr(call_args[k]) + ")" end elsif spec == "float_array" || spec == "int_array" # Zero-copy bulk transfer (#474). The Spinel Array's contiguous # storage lives at `.data`; we hand the raw pointer to C withtest/ffi_str_arg_poly_value.rbaddedView file
Loading diff…
test/ffi_str_arg_poly_value.rb.expectedaddedView file
Loading diff…