Skip to content

Commit e5f8b41

Browse files
maximecbXrXr
authored andcommitted
Implement send with alias method (#23)
* Implement send with alias method * Add alias_method tests
1 parent 0758115 commit e5f8b41

File tree

3 files changed

+106
-44
lines changed

3 files changed

+106
-44
lines changed

bootstraptest/test_yjit.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,52 @@ def foo
130130
foo()
131131
}
132132

133+
# Method aliasing
134+
assert_equal '42', %q{
135+
class Foo
136+
def method_a
137+
42
138+
end
139+
140+
alias method_b method_a
141+
142+
def method_a
143+
:somethingelse
144+
end
145+
end
146+
147+
@obj = Foo.new
148+
149+
def test
150+
@obj.method_b
151+
end
152+
153+
test
154+
test
155+
}
156+
157+
# Method aliasing with method from parent class
158+
assert_equal '777', %q{
159+
class A
160+
def method_a
161+
777
162+
end
163+
end
164+
165+
class B < A
166+
alias method_b method_a
167+
end
168+
169+
@obj = B.new
170+
171+
def test
172+
@obj.method_b
173+
end
174+
175+
test
176+
test
177+
}
178+
133179
# The hash method is a C function and uses the self argument
134180
assert_equal 'true', %q{
135181
def lehashself

vm_insnhelper.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3126,6 +3126,12 @@ aliased_callable_method_entry(const rb_callable_method_entry_t *me)
31263126
return cme;
31273127
}
31283128

3129+
const rb_callable_method_entry_t *
3130+
rb_aliased_callable_method_entry(const rb_callable_method_entry_t *me)
3131+
{
3132+
return aliased_callable_method_entry(me);
3133+
}
3134+
31293135
static VALUE
31303136
vm_call_alias(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct rb_calling_info *calling)
31313137
{

yjit_codegen.c

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,9 @@ gen_send_iseq(jitstate_t *jit, ctx_t *ctx, const struct rb_callinfo *ci, const r
20932093
return true;
20942094
}
20952095

2096+
const rb_callable_method_entry_t *
2097+
rb_aliased_callable_method_entry(const rb_callable_method_entry_t *me);
2098+
20962099
static codegen_status_t
20972100
gen_send_general(jitstate_t *jit, ctx_t *ctx, struct rb_call_data *cd, rb_iseq_t *block)
20982101
{
@@ -2177,54 +2180,61 @@ gen_send_general(jitstate_t *jit, ctx_t *ctx, struct rb_call_data *cd, rb_iseq_t
21772180
// Method calls may corrupt types
21782181
ctx_clear_local_types(ctx);
21792182

2180-
switch (cme->def->type) {
2181-
case VM_METHOD_TYPE_ISEQ:
2182-
return gen_send_iseq(jit, ctx, ci, cme, block, argc);
2183-
case VM_METHOD_TYPE_CFUNC:
2184-
return gen_send_cfunc(jit, ctx, ci, cme, block, argc);
2185-
case VM_METHOD_TYPE_IVAR:
2186-
if (argc != 0) {
2187-
// Argument count mismatch. Getters take no arguments.
2188-
GEN_COUNTER_INC(cb, send_getter_arity);
2183+
// To handle the aliased method case (VM_METHOD_TYPE_ALIAS)
2184+
while (true) {
2185+
// switch on the method type
2186+
switch (cme->def->type) {
2187+
case VM_METHOD_TYPE_ISEQ:
2188+
return gen_send_iseq(jit, ctx, ci, cme, block, argc);
2189+
case VM_METHOD_TYPE_CFUNC:
2190+
return gen_send_cfunc(jit, ctx, ci, cme, block, argc);
2191+
case VM_METHOD_TYPE_IVAR:
2192+
if (argc != 0) {
2193+
// Argument count mismatch. Getters take no arguments.
2194+
GEN_COUNTER_INC(cb, send_getter_arity);
2195+
return YJIT_CANT_COMPILE;
2196+
}
2197+
else {
2198+
mov(cb, REG0, recv);
2199+
2200+
ID ivar_name = cme->def->body.attr.id;
2201+
return gen_get_ivar(jit, ctx, SEND_MAX_DEPTH, comptime_recv, ivar_name, recv_opnd, side_exit);
2202+
}
2203+
case VM_METHOD_TYPE_ATTRSET:
2204+
GEN_COUNTER_INC(cb, send_ivar_set_method);
21892205
return YJIT_CANT_COMPILE;
2206+
case VM_METHOD_TYPE_BMETHOD:
2207+
GEN_COUNTER_INC(cb, send_bmethod);
2208+
return YJIT_CANT_COMPILE;
2209+
case VM_METHOD_TYPE_ZSUPER:
2210+
GEN_COUNTER_INC(cb, send_zsuper_method);
2211+
return YJIT_CANT_COMPILE;
2212+
case VM_METHOD_TYPE_ALIAS: {
2213+
// Retrieve the alised method and re-enter the switch
2214+
cme = rb_aliased_callable_method_entry(cme);
2215+
continue;
21902216
}
2191-
else {
2192-
mov(cb, REG0, recv);
2193-
2194-
ID ivar_name = cme->def->body.attr.id;
2195-
return gen_get_ivar(jit, ctx, SEND_MAX_DEPTH, comptime_recv, ivar_name, recv_opnd, side_exit);
2217+
case VM_METHOD_TYPE_UNDEF:
2218+
GEN_COUNTER_INC(cb, send_undef_method);
2219+
return YJIT_CANT_COMPILE;
2220+
case VM_METHOD_TYPE_NOTIMPLEMENTED:
2221+
GEN_COUNTER_INC(cb, send_not_implemented_method);
2222+
return YJIT_CANT_COMPILE;
2223+
case VM_METHOD_TYPE_OPTIMIZED:
2224+
GEN_COUNTER_INC(cb, send_optimized_method);
2225+
return YJIT_CANT_COMPILE;
2226+
case VM_METHOD_TYPE_MISSING:
2227+
GEN_COUNTER_INC(cb, send_missing_method);
2228+
return YJIT_CANT_COMPILE;
2229+
case VM_METHOD_TYPE_REFINED:
2230+
GEN_COUNTER_INC(cb, send_refined_method);
2231+
return YJIT_CANT_COMPILE;
2232+
// no default case so compiler issues a warning if this is not exhaustive
21962233
}
2197-
case VM_METHOD_TYPE_ATTRSET:
2198-
GEN_COUNTER_INC(cb, send_ivar_set_method);
2199-
return YJIT_CANT_COMPILE;
2200-
case VM_METHOD_TYPE_BMETHOD:
2201-
GEN_COUNTER_INC(cb, send_bmethod);
2202-
return YJIT_CANT_COMPILE;
2203-
case VM_METHOD_TYPE_ZSUPER:
2204-
GEN_COUNTER_INC(cb, send_zsuper_method);
2205-
return YJIT_CANT_COMPILE;
2206-
case VM_METHOD_TYPE_ALIAS:
2207-
GEN_COUNTER_INC(cb, send_alias_method);
2208-
return YJIT_CANT_COMPILE;
2209-
case VM_METHOD_TYPE_UNDEF:
2210-
GEN_COUNTER_INC(cb, send_undef_method);
2211-
return YJIT_CANT_COMPILE;
2212-
case VM_METHOD_TYPE_NOTIMPLEMENTED:
2213-
GEN_COUNTER_INC(cb, send_not_implemented_method);
2214-
return YJIT_CANT_COMPILE;
2215-
case VM_METHOD_TYPE_OPTIMIZED:
2216-
GEN_COUNTER_INC(cb, send_optimized_method);
2217-
return YJIT_CANT_COMPILE;
2218-
case VM_METHOD_TYPE_MISSING:
2219-
GEN_COUNTER_INC(cb, send_missing_method);
2220-
return YJIT_CANT_COMPILE;
2221-
case VM_METHOD_TYPE_REFINED:
2222-
GEN_COUNTER_INC(cb, send_refined_method);
2223-
return YJIT_CANT_COMPILE;
2224-
// no default case so compiler issues a warning if this is not exhaustive
2225-
}
22262234

2227-
return YJIT_CANT_COMPILE;
2235+
// Unreachable
2236+
RUBY_ASSERT(false);
2237+
}
22282238
}
22292239

22302240
static codegen_status_t

0 commit comments

Comments
 (0)