Skip to content

Commit 825ac0f

Browse files
committed
[WIP] Add TP#ivar_name to retrieve ivar's name
1 parent 3038d60 commit 825ac0f

5 files changed

Lines changed: 42 additions & 3 deletions

File tree

include/ruby/debug.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,16 @@ RBIMPL_ATTR_NONNULL((1))
530530
*/
531531
VALUE rb_tracearg_parameters(rb_trace_arg_t *trace_arg);
532532

533+
RBIMPL_ATTR_NONNULL((1))
534+
/**
535+
* Queries the name of the ivar that is set.
536+
*
537+
* @param[in] trace_arg A trace instance.
538+
* @retval RUBY_Qnil There is no ivar.
539+
* @retval otherwise Its ivar name, in Ruby level Symbol.
540+
*/
541+
VALUE rb_tracearg_ivar_name(rb_trace_arg_t *trace_arg);
542+
533543
RBIMPL_ATTR_NONNULL((1))
534544
/**
535545
* Queries the method name of the point where the trace is at.

object.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2950,7 +2950,10 @@ rb_obj_ivar_set_m(VALUE obj, VALUE iv, VALUE val)
29502950
{
29512951
ID id = id_for_var(obj, iv, instance);
29522952
if (!id) id = rb_intern_str(iv);
2953-
EXEC_EVENT_HOOK(GET_EC(), RUBY_EVENT_IVAR_SET, obj, id, 0, 0, val);
2953+
VALUE pair = rb_ary_new_capa(2);
2954+
rb_ary_push(pair, iv);
2955+
rb_ary_push(pair, val);
2956+
EXEC_EVENT_HOOK(GET_EC(), RUBY_EVENT_IVAR_SET, obj, id, 0, 0, pair);
29542957
return rb_ivar_set(obj, id, val);
29552958
}
29562959

trace_point.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,10 @@ def callee_id
338338
Primitive.tracepoint_attr_callee_id
339339
end
340340

341+
def ivar_name
342+
Primitive.tracepoint_attr_ivar_name
343+
end
344+
341345
# Returns the class or module of the method being called.
342346
#
343347
# class C; def foo; end; end

vm_insnhelper.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,11 @@ vm_setinstancevariable(const rb_iseq_t *iseq, VALUE obj, ID id, VALUE val, IVC i
16421642
attr_index_t index;
16431643
vm_ic_atomic_shape_and_index(ic, &dest_shape_id, &index);
16441644

1645-
EXEC_EVENT_HOOK(GET_EC(), RUBY_EVENT_IVAR_SET, obj, id, 0, 0, val);
1645+
VALUE pair = rb_ary_new_capa(2);
1646+
rb_ary_push(pair, rb_id2sym(id));
1647+
rb_ary_push(pair, val);
1648+
1649+
EXEC_EVENT_HOOK(GET_EC(), RUBY_EVENT_IVAR_SET, obj, id, 0, 0, pair);
16461650

16471651
if (UNLIKELY(UNDEF_P(vm_setivar(obj, id, val, dest_shape_id, index)))) {
16481652
switch (BUILTIN_TYPE(obj)) {
@@ -4790,7 +4794,10 @@ vm_call_method_each_type(rb_execution_context_t *ec, rb_control_frame_t *cfp, st
47904794
}
47914795

47924796
ID mid = vm_ci_mid(ci);
4793-
EXEC_EVENT_HOOK(ec, RUBY_EVENT_IVAR_SET, calling->recv, mid, mid, 0, v);
4797+
VALUE pair = rb_ary_new_capa(2);
4798+
rb_ary_push(pair, rb_id2sym(mid));
4799+
rb_ary_push(pair, v);
4800+
EXEC_EVENT_HOOK(ec, RUBY_EVENT_IVAR_SET, calling->recv, mid, mid, 0, pair);
47944801

47954802
return v;
47964803

vm_trace.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,15 @@ rb_tracearg_parameters(rb_trace_arg_t *trace_arg)
959959
return Qnil;
960960
}
961961

962+
VALUE rb_tracearg_ivar_name(rb_trace_arg_t *trace_arg)
963+
{
964+
if (trace_arg->event == RUBY_EVENT_IVAR_SET) {
965+
VALUE pair = trace_arg->data;
966+
return RARRAY_AREF(pair, 0);
967+
}
968+
return Qnil;
969+
}
970+
962971
VALUE
963972
rb_tracearg_method_id(rb_trace_arg_t *trace_arg)
964973
{
@@ -1124,6 +1133,12 @@ tracepoint_attr_parameters(rb_execution_context_t *ec, VALUE tpval)
11241133
return rb_tracearg_parameters(get_trace_arg());
11251134
}
11261135

1136+
static VALUE
1137+
tracepoint_attr_ivar_name(rb_execution_context_t *ec, VALUE tpval)
1138+
{
1139+
return rb_tracearg_ivar_name(get_trace_arg());
1140+
}
1141+
11271142
static VALUE
11281143
tracepoint_attr_method_id(rb_execution_context_t *ec, VALUE tpval)
11291144
{

0 commit comments

Comments
 (0)