Commit fdbe84a7
fdbe84a788f9Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 10 files · +6 added · ~4 modified
Description
Bind numbered, auto-splat, and keyword block params for instance_exec
Extends recv.instance_exec(args) { ... } block-param binding beyond
fixed positional params:
- Numbered params (
_1.._N) and the implicitit(Prism normalizesitto_1): the analyze arm now types them from the call-site args (codegen already bound them). - Auto-splat: a single array argument spread across N>=2 params binds each
to the array's element type (analyze) and to
sp_<Kind>Array_get(arr, i)(codegen), evaluating the array once into a temp. - Keyword params (
|k:, j: 5|): matched by name to a trailing call-sitek: vKeywordHashNode; an omitted optional keyword takes its default expr. The hash is excluded from positional binding so|a, k:|withinstance_exec(10, k: 3)works.
Shared helpers ie_call_kwhash / ie_kwhash_value locate the call's keyword hash and a value by name.
Adds instance_exec_numbered / _autosplat / _kwargs tests (with .expected so
they are reference-Ruby-version independent; numbered uses it).
Fixes #1378 Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- fdbe84a788f9
- Parent
- 5a7453ff51ab
src/analyze.cmodifiedView file
@@ -415,6 +415,34 @@
415415
return s->class_id;416416
}417417
418+
/* In a call-site KeywordHashNode (`k: 9, j: 2`), the value node bound to the419+
keyword `name`, or -1. Used to match instance_exec keyword block params. */420+
int ie_kwhash_value(Compiler *c, int kwhash, const char *name) {421+
const NodeTable *nt = c->nt;422+
if (kwhash < 0 || !name) return -1;423+
int en = 0; const int *els = nt_arr(nt, kwhash, "elements", &en);424+
for (int i = 0; i < en; i++) {425+
const char *ety = nt_type(nt, els[i]);426+
if (!ety || strcmp(ety, "AssocNode")) continue;427+
int key = nt_ref(nt, els[i], "key");428+
const char *kty = key >= 0 ? nt_type(nt, key) : NULL;429+
if (!kty || strcmp(kty, "SymbolNode")) continue;430+
const char *kn = nt_str(nt, key, "value");431+
if (kn && !strcmp(kn, name)) return nt_ref(nt, els[i], "value");432+
}433+
return -1;434+
}435+
436+
/* The trailing KeywordHashNode of a call's arguments (`k: 1`), or -1. */437+
int ie_call_kwhash(Compiler *c, int id) {438+
const NodeTable *nt = c->nt;439+
int args = nt_ref(nt, id, "arguments");440+
int ac = 0; const int *av = args >= 0 ? nt_arr(nt, args, "arguments", &ac) : NULL;441+
if (ac <= 0) return -1;442+
const char *lty = nt_type(nt, av[ac - 1]);443+
return (lty && !strcmp(lty, "KeywordHashNode")) ? av[ac - 1] : -1;444+
}445+
418446
/* (Re)build the instance_eval/exec node→class map from current receiver types. */419447
void build_ie_map(Compiler *c) {420448
const NodeTable *nt = c->nt;src/analyze.hmodifiedView file
Loading diff…
src/analyze_pass.cmodifiedView file
Loading diff…
src/codegen_call.cmodifiedView file
Loading diff…
test/instance_exec_autosplat.rbaddedView file
Loading diff…
test/instance_exec_autosplat.rb.expectedaddedView file
Loading diff…
test/instance_exec_kwargs.rbaddedView file
Loading diff…
test/instance_exec_kwargs.rb.expectedaddedView file
Loading diff…
test/instance_exec_numbered.rbaddedView file
Loading diff…
test/instance_exec_numbered.rb.expectedaddedView file
Loading diff…