Commit 61e94e2a
61e94e2a6898Merged
Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
fix(codegen): forward arguments through poly method dispatch
compile_poly_method_call emitted sp_<Class>_<method>(recv) with
no argument list, dropping any positional arguments at the call
site. The following pattern didn't compile:
class Doubler; def call(x); x * 2; end; end class Tripler; def call(x); x * 3; end; end def apply(a, x); a.call(x); end apply(Doubler.new, 5) # 10 apply(Tripler.new, 5) # 15
Master fails with:
error: too few arguments to function 'sp_Doubler_call'; expected 2, have 1
Stash the receiver in a temp so it isn't re-evaluated for each
cls_id branch, compile the argument list once into arg_strs, and
forward it verbatim into every sp_<Class>_<method>(self, args...)
call.
Adds test/bm_poly_method_args.rb as a small reproducer.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 61e94e2a6898
- Parent
- 36cc6fb9bd66
spinel_codegen.rbmodifiedView file
end ret_ct = c_type(ret_type) ret_def = c_default_val(ret_type) # Stash the receiver in a temp so we don't re-evaluate the # expression for every if-branch below. recv_tmp = new_temp emit(" sp_RbVal " + recv_tmp + " = " + rc + ";") # Compile the call's argument list once. arg_strs = "" args_id = @nd_arguments[nid] if args_id >= 0 aargs = get_args(args_id) k = 0 while k < aargs.length arg_strs = arg_strs + ", " + compile_expr(aargs[k]) k = k + 1 end end tmp = new_temp emit(" " + ret_ct + " " + tmp + " = " + ret_def + ";") emit(" if (" + rc + ".tag == SP_TAG_OBJ) {") emit(" if (" + recv_tmp + ".tag == SP_TAG_OBJ) {") i = 0 while i < @cls_names.length cname = @cls_names[i] midx = cls_find_method_direct(i, mname) if midx >= 0 call_expr = "sp_" + cname + "_" + sanitize_name(mname) + "((sp_" + cname + " *)" + rc + ".v.p)" call_expr = "sp_" + cname + "_" + sanitize_name(mname) + "((sp_" + cname + " *)" + recv_tmp + ".v.p" + arg_strs + ")" rhs = call_expr if is_poly_ret == 1 this_rt = cls_method_return(i, mname) rhs = box_val_to_poly(call_expr, this_rt) end emit(" if (" + rc + ".v.cls_id == " + i.to_s + ") " + tmp + " = " + rhs + ";") emit(" if (" + recv_tmp + ".v.cls_id == " + i.to_s + ") " + tmp + " = " + rhs + ";") end i = i + 1 endtest/bm_poly_method_args.rbaddedView file
Loading diff…