Commit 35a20a7e
35a20a7e458eMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #1429 from ryanseys/rs-c-method-arity
fix(codegen): emit Method#arity
Details
- Commit
- 35a20a7e458e
- Parent
- 18a3b76e5669
- Merge parent
- 7621f3bed428
src/codegen_call.cmodifiedView file
@@ -641,6 +641,56 @@
641641
buf_puts(b, "(const char *)("); emit_expr(c, recv, b); buf_puts(b, ")->name");642642
return;643643
}644+
/* <method>.arity -> a compile-time constant from the target method's param645+
shape, read straight off the DefNode's parameters node (the Scope counts646+
fold keyword and post-rest params into nparams/nrequired, so they cannot647+
reconstruct the arity). Per Ruby: a method is variadic (arity -(req + 1))648+
if it has an optional positional, a rest `*`, a forwarding `...`, or a649+
keyword block that is not mandatory; otherwise it reports its required650+
count. Required positionals, post-splat requireds, and a *mandatory*651+
keyword block (a required keyword, which counts as one fixed argument) all652+
contribute to that required count. */653+
if (recv >= 0 && comp_ntype(c, recv) == TY_METHOD && argc == 0 && !strcmp(name, "arity")) {654+
int mn = method_recv_node(c, recv);655+
int target = mn >= 0 ? method_obj_target_mi(c, mn) : -1;656+
if (target >= 0 && c->scopes[target].def_node >= 0) {657+
int pn = nt_ref(c->nt, c->scopes[target].def_node, "parameters");658+
int ok = 1;659+
int n_req = 0, n_opt = 0, n_post = 0;660+
int has_rest = 0, has_forward = 0, kw_block = 0, has_req_kw = 0;661+
if (pn >= 0) {662+
nt_arr(c->nt, pn, "requireds", &n_req);663+
nt_arr(c->nt, pn, "optionals", &n_opt);664+
nt_arr(c->nt, pn, "posts", &n_post);665+
int rp = nt_ref(c->nt, pn, "rest");666+
if (rp >= 0) {667+
const char *rty = nt_type(c->nt, rp);668+
if (rty && !strcmp(rty, "RestParameterNode")) has_rest = 1;669+
else ok = 0; /* e.g. ImplicitRestNode: leave unsupported */670+
}671+
int kn = 0;672+
const int *kws = nt_arr(c->nt, pn, "keywords", &kn);673+
if (kn > 0) kw_block = 1;674+
for (int i = 0; i < kn; i++) {675+
const char *kty = nt_type(c->nt, kws[i]);676+
if (kty && !strcmp(kty, "RequiredKeywordParameterNode")) has_req_kw = 1;677+
}678+
int kwrp = nt_ref(c->nt, pn, "keyword_rest");679+
if (kwrp >= 0) {680+
const char *kty = nt_type(c->nt, kwrp);681+
if (kty && !strcmp(kty, "KeywordRestParameterNode")) kw_block = 1;682+
else if (kty && !strcmp(kty, "ForwardingParameterNode")) has_forward = 1;683+
}684+
}685+
if (ok) {686+
int req = n_req + n_post + (has_req_kw ? 1 : 0);687+
int variadic = n_opt > 0 || has_rest || has_forward || (kw_block && !has_req_kw);688+
int arity = variadic ? -(req + 1) : req;689+
buf_printf(b, "%d", arity);690+
return;691+
}692+
}693+
}644694
/* <method>.call(args) / [] -> invoke the bound function. A top-level645695
method ref calls its function directly; an object-bound Method casts646696
fn through the (void *self, mrb_int...) ABI, evaluating recv once. */test/method_arity.rbaddedView file
Loading diff…
test/method_arity.rb.expectedaddedView file
Loading diff…