1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

refactor add rb_method_entry_from_template

Tired of rb_method_entry_create(..., rb_method_definition_create(
..., &(rb_method_foo_t) {...})) maneuver.  Provide a function that
does the thing to reduce copy&paste.
This commit is contained in:
卜部昌平 2019-09-25 17:00:25 +09:00
parent 167e6b48f1
commit 6c6a25feca
Notes: git 2019-09-30 10:27:04 +09:00
5 changed files with 68 additions and 67 deletions

17
class.c
View file

@ -967,18 +967,11 @@ inject_refined_method(ID *key, VALUE *value, void *data, int _)
const tuple *ptr = data; const tuple *ptr = data;
const rb_method_entry_t *me = *(rb_method_entry_t **) value; const rb_method_entry_t *me = *(rb_method_entry_t **) value;
const rb_method_entry_t *orig_me = me->def->body.refined.orig_me; const rb_method_entry_t *orig_me = me->def->body.refined.orig_me;
rb_method_entry_t *new_me = const rb_method_entry_t *new_me =
rb_method_entry_create( rb_method_entry_from_template(
me->called_id, me, &(rb_method_refined_t) {
me->owner, .orig_me = NULL,
me->defined_class, .owner = me->def->body.refined.owner, });
rb_method_definition_create(
me->def->type,
me->def->original_id,
&(rb_method_refined_t) {
.orig_me = NULL,
.owner = me->def->body.refined.owner, }));
METHOD_ENTRY_FLAGS_COPY(new_me, me);
rb_id_table_insert(RCLASS_M_TBL(ptr->klass), *key, (VALUE)new_me); rb_id_table_insert(RCLASS_M_TBL(ptr->klass), *key, (VALUE)new_me);
RB_OBJ_WRITTEN(ptr->klass, Qundef, new_me); RB_OBJ_WRITTEN(ptr->klass, Qundef, new_me);
*value = (VALUE)rb_method_entry_clone(orig_me); *value = (VALUE)rb_method_entry_clone(orig_me);

View file

@ -185,15 +185,14 @@ STATIC_ASSERT(sizeof_method_def, offsetof(rb_method_definition_t, body)==8);
((def)->type == VM_METHOD_TYPE_REFINED && \ ((def)->type == VM_METHOD_TYPE_REFINED && \
UNDEFINED_METHOD_ENTRY_P((def)->body.refined.orig_me)) UNDEFINED_METHOD_ENTRY_P((def)->body.refined.orig_me))
const rb_method_definition_t *rb_method_definition_create(rb_method_type_t which, ID what, const void *how);
void rb_add_method_cfunc(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc, rb_method_visibility_t visi); void rb_add_method_cfunc(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc, rb_method_visibility_t visi);
void rb_add_method_iseq(VALUE klass, ID mid, const rb_iseq_t *iseq, rb_cref_t *cref, rb_method_visibility_t visi); void rb_add_method_iseq(VALUE klass, ID mid, const rb_iseq_t *iseq, rb_cref_t *cref, rb_method_visibility_t visi);
void rb_add_refined_method_entry(VALUE refined_class, ID mid); 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); 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); rb_method_entry_t *rb_method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *, rb_method_visibility_t noex);
rb_method_entry_t *rb_method_entry_create(ID called_id, VALUE klass, rb_method_visibility_t visi, const rb_method_definition_t *def); 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);
const rb_method_entry_t *rb_method_entry_at(VALUE obj, ID id); const rb_method_entry_t *rb_method_entry_at(VALUE obj, ID id);

9
proc.c
View file

@ -1474,16 +1474,9 @@ mnew_missing(VALUE klass, VALUE obj, ID id, VALUE mclass)
{ {
struct METHOD *data; struct METHOD *data;
VALUE method = TypedData_Make_Struct(mclass, struct METHOD, &method_data_type, data); VALUE method = TypedData_Make_Struct(mclass, struct METHOD, &method_data_type, data);
rb_method_entry_t *me;
const rb_method_definition_t *def;
RB_OBJ_WRITE(method, &data->recv, obj); RB_OBJ_WRITE(method, &data->recv, obj);
RB_OBJ_WRITE(method, &data->klass, klass); RB_OBJ_WRITE(method, &data->klass, klass);
RB_OBJ_WRITE(method, &data->me, rb_method_entry_for_missing(id, klass));
def = rb_method_definition_create(VM_METHOD_TYPE_MISSING, id, NULL);
me = rb_method_entry_create(id, klass, METHOD_VISI_UNDEF, def);
RB_OBJ_WRITE(method, &data->me, me);
OBJ_INFECT(method, klass); OBJ_INFECT(method, klass);

View file

@ -2587,16 +2587,8 @@ aliased_callable_method_entry0(const rb_method_entry_t *me)
VALUE defined_class = find_defined_class_by_owner(me->defined_class, orig_me->owner); VALUE defined_class = find_defined_class_by_owner(me->defined_class, orig_me->owner);
VM_ASSERT(RB_TYPE_P(orig_me->owner, T_MODULE)); VM_ASSERT(RB_TYPE_P(orig_me->owner, T_MODULE));
cme = rb_method_entry_complement_defined_class(orig_me, me->called_id, defined_class); cme = rb_method_entry_complement_defined_class(orig_me, me->called_id, defined_class);
rb_method_entry_t *ret = const rb_method_entry_t *ret =
rb_method_entry_create( rb_method_entry_from_template((const rb_method_entry_t*)me, cme);
me->called_id,
me->owner,
me->defined_class,
rb_method_definition_create(
VM_METHOD_TYPE_ALIAS,
me->def->original_id,
cme));
METHOD_ENTRY_FLAGS_COPY(ret, (const void*)me);
rb_method_entry_spoof(ret); rb_method_entry_spoof(ret);
return ret; return ret;
} }

View file

@ -452,7 +452,7 @@ rb_method_entry_spoof(const rb_method_entry_t *me)
rb_clear_method_cache_by_class(o); rb_clear_method_cache_by_class(o);
} }
MJIT_FUNC_EXPORTED const rb_method_definition_t * static const rb_method_definition_t *
rb_method_definition_create(rb_method_type_t type, ID mid, const void *opts) rb_method_definition_create(rb_method_type_t type, ID mid, const void *opts)
{ {
rb_method_definition_t template = rb_method_definition_new(type, mid, opts); rb_method_definition_t template = rb_method_definition_new(type, mid, opts);
@ -483,6 +483,9 @@ rb_method_entry_alloc(VALUE flags, ID called_id, VALUE owner, VALUE defined_clas
rb_method_entry_t tmp = { flags, }; rb_method_entry_t tmp = { flags, };
rb_method_entry_t *me = (rb_method_entry_t *)rb_imemo_new(imemo_ment, (VALUE)def, (VALUE)called_id, owner, defined_class); rb_method_entry_t *me = (rb_method_entry_t *)rb_imemo_new(imemo_ment, (VALUE)def, (VALUE)called_id, owner, defined_class);
METHOD_ENTRY_FLAGS_COPY(me, &tmp); METHOD_ENTRY_FLAGS_COPY(me, &tmp);
if (def) {
method_definition_reset(me);
}
return me; return me;
} }
@ -500,20 +503,42 @@ filter_defined_class(VALUE klass)
rb_bug("filter_defined_class: %s", rb_obj_info(klass)); rb_bug("filter_defined_class: %s", rb_obj_info(klass));
} }
MJIT_FUNC_EXPORTED rb_method_entry_t * static inline VALUE
rb_method_entry_create(ID called_id, VALUE klass, rb_method_visibility_t visi, const rb_method_definition_t *def) method_entry_flags(rb_method_visibility_t visi)
{ {
rb_method_entry_t tmp = { 0, }; rb_method_entry_t tmp = { 0, };
METHOD_ENTRY_FLAGS_SET(&tmp, visi, !ruby_running); METHOD_ENTRY_FLAGS_SET(&tmp, visi, !ruby_running);
rb_method_entry_t *me = return tmp.flags;
rb_method_entry_alloc( }
tmp.flags,
called_id, MJIT_FUNC_EXPORTED const rb_method_entry_t *
klass, rb_method_entry_from_template(
filter_defined_class(klass), const rb_method_entry_t *me,
def); const void *body)
if (def != NULL) method_definition_reset(me); {
return me; return rb_method_entry_alloc(
me->flags,
me->called_id,
me->owner,
me->defined_class,
rb_method_definition_create(
me->def->type,
me->def->original_id,
body));
}
const rb_method_entry_t *
rb_method_entry_for_missing(ID mid, VALUE klass)
{
return rb_method_entry_alloc(
method_entry_flags(METHOD_VISI_UNDEF),
mid,
klass,
filter_defined_class(klass),
rb_method_definition_create(
VM_METHOD_TYPE_MISSING,
mid,
NULL));
} }
const rb_method_entry_t * const rb_method_entry_t *
@ -569,32 +594,26 @@ make_method_entry_refined(VALUE owner, rb_method_entry_t *me)
} }
else { else {
rb_vm_check_redefinition_opt_method(me, me->owner); rb_vm_check_redefinition_opt_method(me, me->owner);
rb_method_entry_t *orig_me = /* :FIXME: can rb_method_entry_from_template be tweaked to also be
rb_method_entry_alloc( * applicable here? */
me->flags, return rb_method_entry_alloc(
me->called_id, method_entry_flags(METHOD_VISI_PUBLIC),
me->owner, me->called_id,
me->defined_class ? me->owner,
me->defined_class : owner, me->defined_class,
method_definition_addref(me->def));
const rb_method_definition_t *def =
rb_method_definition_create( rb_method_definition_create(
VM_METHOD_TYPE_REFINED, VM_METHOD_TYPE_REFINED,
me->called_id, me->called_id,
&(rb_method_refined_t) { &(rb_method_refined_t) {
.orig_me = orig_me,
.owner = owner, .owner = owner,
} .orig_me =
); rb_method_entry_alloc(
rb_method_entry_t *new_me = me->flags,
rb_method_entry_alloc( me->called_id,
me->flags, me->owner,
me->called_id, me->defined_class ?
me->owner, me->defined_class : owner,
me->defined_class, method_definition_addref(me->def))}));
def);
METHOD_ENTRY_VISI_SET(new_me, METHOD_VISI_PUBLIC);
return new_me;
} }
} }
@ -718,7 +737,12 @@ rb_method_entry_make(VALUE klass, ID mid, VALUE defined_class, rb_method_visibil
if (!def) { if (!def) {
def = rb_method_definition_create(type, original_id, opts); def = rb_method_definition_create(type, original_id, opts);
} }
me = rb_method_entry_create(mid, defined_class, visi, def); me = rb_method_entry_alloc(
method_entry_flags(visi),
mid,
defined_class,
filter_defined_class(defined_class),
def);
rb_clear_method_cache_by_class(klass); rb_clear_method_cache_by_class(klass);