ruby-changes:58043
From: =E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3 <ko1@a...>
Date: Mon, 30 Sep 2019 11:43:19 +0900 (JST)
Subject: [ruby-changes:58043] dd883de5ba (master): refactor constify most of rb_method_entry_t
https://git.ruby-lang.org/ruby.git/commit/?id=dd883de5ba From dd883de5ba311c38818d0f638a94b4fbc63f5ee5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?= <shyouhei@r...> Date: Thu, 26 Sep 2019 10:22:01 +0900 Subject: refactor constify most of rb_method_entry_t Now that we have eliminated most destructive operations over the rb_method_entry_t / rb_callable_method_entry_t, let's make them mostly immutabe and mark them const. One exception is rb_export_method(), which destructively modifies visibilities of method entries. I have left that operation as is because I suspect that destructiveness is the nature of that function. diff --git a/class.c b/class.c index 3160444..aaf7541 100644 --- a/class.c +++ b/class.c @@ -965,7 +965,7 @@ static enum rb_id_table_iterator_result https://github.com/ruby/ruby/blob/trunk/class.c#L965 inject_refined_method(ID *key, VALUE *value, void *data, int _) { const tuple *ptr = data; - const rb_method_entry_t *me = *(rb_method_entry_t **) value; + const rb_method_entry_t *me = *(const rb_method_entry_t **) value; const rb_method_entry_t *orig_me = me->def->body.refined.orig_me; const rb_method_entry_t *new_me = rb_method_entry_from_template( @@ -982,8 +982,8 @@ inject_refined_method(ID *key, VALUE *value, void *data, int _) https://github.com/ruby/ruby/blob/trunk/class.c#L982 static enum rb_id_table_iterator_result move_refined_method(ID key, VALUE value, void *data) { - const tuple *ptr = data; - rb_method_entry_t *me = (rb_method_entry_t *) value; + const tuple *ptr = data; + const rb_method_entry_t *me = (const rb_method_entry_t *) value; if (me->def->type == VM_METHOD_TYPE_REFINED) { if (me->def->body.refined.orig_me) { diff --git a/ext/coverage/coverage.c b/ext/coverage/coverage.c index 8503c9d..cb571ca 100644 --- a/ext/coverage/coverage.c +++ b/ext/coverage/coverage.c @@ -123,7 +123,7 @@ method_coverage_i(void *vstart, void *vend, size_t stride, void *data) https://github.com/ruby/ruby/blob/trunk/ext/coverage/coverage.c#L123 for (v = (VALUE)vstart; v != (VALUE)vend; v += stride) { if (RB_TYPE_P(v, T_IMEMO) && imemo_type(v) == imemo_ment) { - const rb_method_entry_t *me = (rb_method_entry_t *) v; + const rb_method_entry_t *me = (const rb_method_entry_t *) v; VALUE path, first_lineno, first_column, last_lineno, last_column; VALUE data[5], ncoverage, methods; VALUE methods_id = ID2SYM(rb_intern("methods")); diff --git a/gc.c b/gc.c index 0caee4c..52b3a66 100644 --- a/gc.c +++ b/gc.c @@ -7829,7 +7829,7 @@ void rb_update_st_references(struct st_table *ht) https://github.com/ruby/ruby/blob/trunk/gc.c#L7829 } static void -gc_ref_update_method_entry(rb_objspace_t *objspace, rb_method_entry_t *me) +gc_ref_update_method_entry(rb_objspace_t *objspace, const rb_method_entry_t *me) { const rb_method_definition_t *def = me->def; diff --git a/method.h b/method.h index 105e4b6..b203f71 100644 --- a/method.h +++ b/method.h @@ -49,53 +49,24 @@ typedef struct rb_cref_struct { https://github.com/ruby/ruby/blob/trunk/method.h#L49 /* method data type */ typedef struct rb_method_entry_struct { - VALUE flags; - VALUE defined_class; + const VALUE flags; + const VALUE defined_class; struct rb_method_definition_struct * const def; - ID called_id; - VALUE owner; + const ID called_id; + const VALUE owner; } rb_method_entry_t; typedef struct rb_callable_method_entry_struct { /* same fields with rb_method_entry_t */ - VALUE flags; + const VALUE flags; const VALUE defined_class; struct rb_method_definition_struct * const def; - ID called_id; + const ID called_id; const VALUE owner; } rb_callable_method_entry_t; #define METHOD_ENTRY_VISI(me) (rb_method_visibility_t)(((me)->flags & (IMEMO_FL_USER0 | IMEMO_FL_USER1)) >> (IMEMO_FL_USHIFT+0)) #define METHOD_ENTRY_BASIC(me) (int) (((me)->flags & (IMEMO_FL_USER2 )) >> (IMEMO_FL_USHIFT+2)) -static inline void -METHOD_ENTRY_VISI_SET(rb_method_entry_t *me, rb_method_visibility_t visi) -{ - VM_ASSERT((int)visi >= 0 && visi <= 3); - me->flags = (me->flags & ~(IMEMO_FL_USER0 | IMEMO_FL_USER1)) | (visi << (IMEMO_FL_USHIFT+0)); -} -static inline void -METHOD_ENTRY_BASIC_SET(rb_method_entry_t *me, unsigned int basic) -{ - VM_ASSERT(basic <= 1); - me->flags = (me->flags & ~(IMEMO_FL_USER2 )) | (basic << (IMEMO_FL_USHIFT+2)); -} -static inline void -METHOD_ENTRY_FLAGS_SET(rb_method_entry_t *me, rb_method_visibility_t visi, unsigned int basic) -{ - VM_ASSERT((int)visi >= 0 && visi <= 3); - VM_ASSERT(basic <= 1); - me->flags = - (me->flags & ~(IMEMO_FL_USER0|IMEMO_FL_USER1|IMEMO_FL_USER2)) | - ((visi << (IMEMO_FL_USHIFT+0)) | (basic << (IMEMO_FL_USHIFT+2))); -} -static inline void -METHOD_ENTRY_FLAGS_COPY(rb_method_entry_t *dst, const rb_method_entry_t *src) -{ - dst->flags = - (dst->flags & ~(IMEMO_FL_USER0|IMEMO_FL_USER1|IMEMO_FL_USER2)) | - (src->flags & (IMEMO_FL_USER0|IMEMO_FL_USER1|IMEMO_FL_USER2)); -} - typedef enum { VM_METHOD_TYPE_ISEQ, /*!< Ruby method */ VM_METHOD_TYPE_CFUNC, /*!< C method */ @@ -190,7 +161,7 @@ void rb_add_method_iseq(VALUE klass, ID mid, const rb_iseq_t *iseq, rb_cref_t *c https://github.com/ruby/ruby/blob/trunk/method.h#L161 void rb_add_refined_method_entry(VALUE refined_class, ID mid); void rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *option, rb_method_visibility_t visi); -rb_method_entry_t *rb_method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *, rb_method_visibility_t noex); +const rb_method_entry_t *rb_method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *, rb_method_visibility_t noex); const rb_method_entry_t *rb_method_entry_from_template(const rb_method_entry_t *template, const void *opts); const rb_method_entry_t *rb_method_entry_for_missing(ID mid, VALUE klass); diff --git a/proc.c b/proc.c index 09b3103..0fd2126 100644 --- a/proc.c +++ b/proc.c @@ -1426,7 +1426,7 @@ bm_compact(void *ptr) https://github.com/ruby/ruby/blob/trunk/proc.c#L1426 UPDATE_REFERENCE(data->recv); UPDATE_REFERENCE(data->klass); UPDATE_REFERENCE(data->iclass); - UPDATE_TYPED_REFERENCE(rb_method_entry_t *, data->me); + UPDATE_TYPED_REFERENCE(const rb_method_entry_t *, data->me); } static size_t @@ -1519,7 +1519,7 @@ mnew_internal(const rb_method_entry_t *me, VALUE klass, VALUE iclass, https://github.com/ruby/ruby/blob/trunk/proc.c#L1519 if (me->defined_class) { VALUE klass = RCLASS_SUPER(RCLASS_ORIGIN(me->defined_class)); id = me->def->original_id; - me = (rb_method_entry_t *)rb_callable_method_entry_with_refinements(klass, id, &iclass); + me = (const rb_method_entry_t *)rb_callable_method_entry_with_refinements(klass, id, &iclass); } else { VALUE klass = RCLASS_SUPER(me->owner); @@ -1557,7 +1557,7 @@ mnew(VALUE klass, VALUE obj, ID id, VALUE mclass, int scope) https://github.com/ruby/ruby/blob/trunk/proc.c#L1557 me = rb_method_entry_with_refinements(klass, id, &iclass); } else { - me = (rb_method_entry_t *)rb_callable_method_entry_with_refinements(klass, id, &iclass); + me = (const rb_method_entry_t *)rb_callable_method_entry_with_refinements(klass, id, &iclass); } return mnew_from_me(me, klass, iclass, obj, id, mclass, scope); } @@ -2947,7 +2947,7 @@ method_super_method(VALUE method) https://github.com/ruby/ruby/blob/trunk/proc.c#L2947 super_class = RCLASS_SUPER(RCLASS_ORIGIN(iclass)); mid = data->me->called_id; if (!super_class) return Qnil; - me = (rb_method_entry_t *)rb_callable_method_entry_with_refinements(super_class, mid, &iclass); + me = (const rb_method_entry_t *)rb_callable_method_entry_with_refinements(super_class, mid, &iclass); if (!me) return Qnil; return mnew_internal(me, me->owner, iclass, data->recv, mid, rb_obj_class(method), FALSE, FALSE); } diff --git a/vm.c b/vm.c index 2483180..16a15c2 100644 --- a/vm.c +++ b/vm.c @@ -1595,7 +1595,7 @@ static enum rb_id_table_iterator_result https://github.com/ruby/ruby/blob/trunk/vm.c#L1595 check_redefined_method(ID mid, VALUE value, void *data) { VALUE klass = (VALUE)data; - const rb_method_entry_t *me = (rb_method_entry_t *)value; + const rb_method_entry_t *me = (const rb_method_entry_t *)value; const rb_method_entry_t *newme = rb_method_entry(klass, mid); if (newme != me) rb_vm_check_redefinition_opt_method(me, me->owner); diff --git a/vm_backtrace.c b/vm_backtrace.c index 44a4ac0..3a8ad91 100644 --- a/vm_backtrace.c +++ b/vm_backtrace.c @@ -1349,7 +1349,7 @@ frame2iseq(VALUE frame) https://github.com/ruby/ruby/blob/trunk/vm_backtrace.c#L1349 return (const rb_iseq_t *)frame; case imemo_ment: { - const rb_callable_method_entry_t *cme = (rb_callable_method_entry_t *)frame; + const rb_callable_method_entry_t *cme = (const rb_callable_method_entry_t *)frame; switch (cme->def->type) { case VM_METHOD_TYPE_ISEQ: return cme->def->body.iseq.iseqptr; @@ -1405,7 +1405,7 @@ frame2klass(VALUE frame) https://github.com/ruby/ruby/blob/trunk/vm_backtrace.c#L1405 if (frame == Qnil) return Qnil; if (RB_TYPE_P(frame, T_IMEMO)) { - const rb_callable_method_entry_t *cme = (rb_callable_method_entry_t *)frame; + const rb_callable_method_entry_t *cme = (const rb_callable_method_entry_t *)frame; if (imemo_type(frame) == imemo_ment) { return cme->defined_class; diff --git a/vm_insnhelper.c b/vm_insnhelper.c index adad1df..197e100 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -578,8 +578,8 @@ vm_getspecial(const rb_execution_context_t *ec, const VALUE *lep, rb_num_t key, https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L578 return val; } -PUREFUNC(static rb_callable_method_entry_t *check_method_entry(VALUE obj, (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/