summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author卜部昌平 <shyouhei@ruby-lang.org>2019-09-23 17:42:27 +0900
committer卜部昌平 <shyouhei@ruby-lang.org>2019-09-30 10:26:38 +0900
commit595b3c4fddc5cde58add2fa2637acb2664694194 (patch)
tree6a77f258999c267804556506a7fef3751ed44af8
parent1521f7cf89fe3cf356a589c45bce4ccb57dce7fd (diff)
refactor rb_method_definition_create take opts
Before this changeset rb_method_definition_create only allocated a memory region and we had to destructively initialize it later. That is not a good design so we change the API to return a complete struct instead.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/2486
-rw-r--r--proc.c4
-rw-r--r--vm_insnhelper.c8
-rw-r--r--vm_method.c76
3 files changed, 40 insertions, 48 deletions
diff --git a/proc.c b/proc.c
index 496baa41f1..465135d505 100644
--- a/proc.c
+++ b/proc.c
@@ -15,7 +15,7 @@
#include "vm_core.h"
#include "iseq.h"
-extern const rb_method_definition_t *rb_method_definition_create(rb_method_type_t type, ID mid);
+extern const rb_method_definition_t *rb_method_definition_create(rb_method_type_t type, ID mid, const void *opts);
/* Proc.new with no block will raise an exception in the future
* versions */
@@ -1482,7 +1482,7 @@ mnew_missing(VALUE klass, VALUE obj, ID id, VALUE mclass)
RB_OBJ_WRITE(method, &data->recv, obj);
RB_OBJ_WRITE(method, &data->klass, klass);
- def = rb_method_definition_create(VM_METHOD_TYPE_MISSING, id);
+ 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);
diff --git a/vm_insnhelper.c b/vm_insnhelper.c
index 7db8630f75..0919f6ebe9 100644
--- a/vm_insnhelper.c
+++ b/vm_insnhelper.c
@@ -19,8 +19,8 @@
#include "ruby/config.h"
#include "debug_counter.h"
-extern const rb_method_definition_t *rb_method_definition_create(rb_method_type_t type, ID mid);
-extern void rb_method_definition_set(const rb_method_entry_t *me, rb_method_definition_t *def, void *opts);
+extern const rb_method_definition_t *rb_method_definition_create(rb_method_type_t type, ID mid, const void *opts);
+extern void rb_method_definition_set(const rb_method_entry_t *me, const rb_method_definition_t *def);
extern int rb_method_definition_eq(const rb_method_definition_t *d1, const rb_method_definition_t *d2);
extern VALUE rb_make_no_method_exception(VALUE exc, VALUE format, VALUE obj,
int argc, const VALUE *argv, int priv);
@@ -2590,8 +2590,8 @@ aliased_callable_method_entry(const rb_callable_method_entry_t *me)
}
else {
const rb_method_definition_t *def =
- rb_method_definition_create(VM_METHOD_TYPE_ALIAS, me->def->original_id);
- rb_method_definition_set((rb_method_entry_t *)me, (void *)def, (void *)cme);
+ rb_method_definition_create(VM_METHOD_TYPE_ALIAS, me->def->original_id, cme);
+ rb_method_definition_set((rb_method_entry_t *)me, def);
}
}
else {
diff --git a/vm_method.c b/vm_method.c
index 730ec35cce..bde8c1bd26 100644
--- a/vm_method.c
+++ b/vm_method.c
@@ -334,10 +334,15 @@ the_method_optimized(const void *p)
static rb_method_refined_t
the_method_refined(const rb_method_refined_t *p)
{
- return (rb_method_refined_t) {
- .orig_me = p->orig_me,
- .owner = p->owner,
- };
+ if (!p) {
+ return (rb_method_refined_t) { 0, };
+ }
+ else {
+ return (rb_method_refined_t) {
+ .orig_me = p->orig_me,
+ .owner = p->owner,
+ };
+ }
}
static rb_method_alias_t
@@ -438,24 +443,16 @@ rb_method_definition_new(rb_method_type_t type, ID mid, const void *opts)
}
MJIT_FUNC_EXPORTED void
-rb_method_definition_set(const rb_method_entry_t *me, rb_method_definition_t *def, void *opts)
+rb_method_definition_set(const rb_method_entry_t *me, const rb_method_definition_t *def)
{
- if (opts) {
- rb_method_definition_t template =
- rb_method_definition_new(def->type, def->original_id, opts);
- memcpy(def, &template, sizeof template);
- }
memcpy((void *)&me->def, &def, sizeof def);
method_definition_reset(me);
}
MJIT_FUNC_EXPORTED const rb_method_definition_t *
-rb_method_definition_create(rb_method_type_t type, ID mid)
+rb_method_definition_create(rb_method_type_t type, ID mid, const void *opts)
{
- rb_method_definition_t template = {
- .type = type,
- .original_id = mid,
- };
+ rb_method_definition_t template = rb_method_definition_new(type, mid, opts);
void *ptr = ALLOC(rb_method_definition_t);
memcpy(ptr, &template, sizeof template);
return ptr;
@@ -521,10 +518,6 @@ rb_method_entry_complement_defined_class(const rb_method_entry_t *src_me, ID cal
{
const rb_method_definition_t *def = src_me->def;
rb_method_entry_t *me;
- struct {
- const struct rb_method_entry_struct *orig_me;
- VALUE owner;
- } refined = {0};
if (!src_me->defined_class &&
def->type == VM_METHOD_TYPE_REFINED &&
@@ -532,19 +525,18 @@ rb_method_entry_complement_defined_class(const rb_method_entry_t *src_me, ID cal
const rb_method_entry_t *orig_me =
rb_method_entry_clone(def->body.refined.orig_me);
RB_OBJ_WRITE((VALUE)orig_me, &orig_me->defined_class, defined_class);
- refined.orig_me = orig_me;
- refined.owner = orig_me->owner;
- def = NULL;
+ def = rb_method_definition_create(
+ VM_METHOD_TYPE_REFINED,
+ called_id,
+ &(rb_method_refined_t) {
+ .orig_me = orig_me,
+ .owner = orig_me->owner});
}
else {
def = method_definition_addref_complement((rb_method_definition_t *)def);
}
me = rb_method_entry_alloc(called_id, src_me->owner, defined_class, def);
METHOD_ENTRY_FLAGS_COPY(me, src_me);
- if (!def) {
- def = rb_method_definition_create(VM_METHOD_TYPE_REFINED, called_id);
- rb_method_definition_set(me, (rb_method_definition_t *)def, &refined);
- }
VM_ASSERT(RB_TYPE_P(me->owner, T_MODULE));
@@ -569,24 +561,23 @@ make_method_entry_refined(VALUE owner, rb_method_entry_t *me)
return;
}
else {
- struct {
- struct rb_method_entry_struct *orig_me;
- VALUE owner;
- } refined;
- const rb_method_definition_t *def;
-
rb_vm_check_redefinition_opt_method(me, me->owner);
-
- refined.orig_me =
+ rb_method_entry_t *orig_me =
rb_method_entry_alloc(me->called_id, me->owner,
me->defined_class ?
me->defined_class : owner,
method_definition_addref(me->def));
- METHOD_ENTRY_FLAGS_COPY(refined.orig_me, me);
- refined.owner = owner;
-
- def = rb_method_definition_create(VM_METHOD_TYPE_REFINED, me->called_id);
- rb_method_definition_set(me, (void *)def, (void *)&refined);
+ METHOD_ENTRY_FLAGS_COPY(orig_me, me);
+ const rb_method_definition_t *def =
+ rb_method_definition_create(
+ VM_METHOD_TYPE_REFINED,
+ me->called_id,
+ &(rb_method_refined_t) {
+ .orig_me = orig_me,
+ .owner = owner,
+ }
+ );
+ rb_method_definition_set(me, def);
METHOD_ENTRY_VISI_SET(me, METHOD_VISI_PUBLIC);
}
}
@@ -707,9 +698,10 @@ rb_method_entry_make(VALUE klass, ID mid, VALUE defined_class, rb_method_visibil
}
/* create method entry */
- me = rb_method_entry_create(mid, defined_class, visi, NULL);
- if (def == NULL) def = rb_method_definition_create(type, original_id);
- rb_method_definition_set(me, (void *)def, opts);
+ if (!def) {
+ def = rb_method_definition_create(type, original_id, opts);
+ }
+ me = rb_method_entry_create(mid, defined_class, visi, def);
rb_clear_method_cache_by_class(klass);