[前][次][番号順一覧][スレッド一覧]

ruby-changes:58042

From: =E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3 <ko1@a...>
Date: Mon, 30 Sep 2019 11:43:17 +0900 (JST)
Subject: [ruby-changes:58042] 595b3c4fdd (master): refactor rb_method_definition_create take opts

https://git.ruby-lang.org/ruby.git/commit/?id=595b3c4fdd

From 595b3c4fddc5cde58add2fa2637acb2664694194 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: Mon, 23 Sep 2019 17:42:27 +0900
Subject: 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.

diff --git a/proc.c b/proc.c
index 496baa4..465135d 100644
--- a/proc.c
+++ b/proc.c
@@ -15,7 +15,7 @@ https://github.com/ruby/ruby/blob/trunk/proc.c#L15
 #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) https://github.com/ruby/ruby/blob/trunk/proc.c#L1482
     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 7db8630..0919f6e 100644
--- a/vm_insnhelper.c
+++ b/vm_insnhelper.c
@@ -19,8 +19,8 @@ https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L19
 #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) https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L2590
 	}
 	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 730ec35..bde8c1b 100644
--- a/vm_method.c
+++ b/vm_method.c
@@ -334,10 +334,15 @@ the_method_optimized(const void *p) https://github.com/ruby/ruby/blob/trunk/vm_method.c#L334
 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) https://github.com/ruby/ruby/blob/trunk/vm_method.c#L443
 }
 
 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 https://github.com/ruby/ruby/blob/trunk/vm_method.c#L518
 {
     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 https://github.com/ruby/ruby/blob/trunk/vm_method.c#L525
 	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) https://github.com/ruby/ruby/blob/trunk/vm_method.c#L561
 	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 https://github.com/ruby/ruby/blob/trunk/vm_method.c#L698
     }
 
     /* 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);
 
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]