ruby-changes:65561
From: Aaron <ko1@a...>
Date: Thu, 18 Mar 2021 02:55:54 +0900 (JST)
Subject: [ruby-changes:65561] ea817c60fc (master): Refactor vm_defined to return a boolean
https://git.ruby-lang.org/ruby.git/commit/?id=ea817c60fc From ea817c60fcbdc2c4496be045f5bf028b702561ba Mon Sep 17 00:00:00 2001 From: Aaron Patterson <tenderlove@r...> Date: Tue, 16 Mar 2021 15:25:37 -0700 Subject: Refactor vm_defined to return a boolean We just need this function to return whether or not the thing we're looking for is defined. If it's defined, return something true, otherwise false. --- insns.def | 4 +--- vm_insnhelper.c | 48 ++++++++++++++---------------------------------- 2 files changed, 15 insertions(+), 37 deletions(-) diff --git a/insns.def b/insns.def index ca56416..1dc0bb3 100644 --- a/insns.def +++ b/insns.def @@ -667,12 +667,10 @@ defined https://github.com/ruby/ruby/blob/trunk/insns.def#L667 (VALUE val) // attr bool leaf = leafness_of_defined(op_type); { + val = Qnil; if (vm_defined(ec, GET_CFP(), op_type, obj, v)) { val = needstr; } - else { - val = Qnil; - } } /* check `target' matches `pattern'. diff --git a/vm_insnhelper.c b/vm_insnhelper.c index 81ac2eb..f343de0 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -3969,7 +3969,7 @@ vm_once_clear(VALUE data) https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L3969 /* defined insn */ -static enum defined_type +static VALUE check_respond_to_missing(VALUE obj, VALUE v) { VALUE args[2]; @@ -3978,10 +3978,10 @@ check_respond_to_missing(VALUE obj, VALUE v) https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L3978 args[0] = obj; args[1] = Qfalse; r = rb_check_funcall(v, idRespond_to_missing, 2, args); if (r != Qundef && RTEST(r)) { - return DEFINED_METHOD; + return Qtrue; } else { - return DEFINED_NOT_DEFINED; + return Qfalse; } } @@ -3989,42 +3989,31 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L3989 vm_defined(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, rb_num_t op_type, VALUE obj, VALUE v) { VALUE klass; - enum defined_type expr_type = DEFINED_NOT_DEFINED; enum defined_type type = (enum defined_type)op_type; switch (type) { case DEFINED_IVAR: - if (rb_ivar_defined(GET_SELF(), SYM2ID(obj))) { - expr_type = DEFINED_IVAR; - } + return rb_ivar_defined(GET_SELF(), SYM2ID(obj)); break; case DEFINED_GVAR: - if (rb_gvar_defined(SYM2ID(obj))) { - expr_type = DEFINED_GVAR; - } + return rb_gvar_defined(SYM2ID(obj)); break; case DEFINED_CVAR: { const rb_cref_t *cref = vm_get_cref(GET_EP()); klass = vm_get_cvar_base(cref, GET_CFP(), 0); - if (rb_cvar_defined(klass, SYM2ID(obj))) { - expr_type = DEFINED_CVAR; - } + return rb_cvar_defined(klass, SYM2ID(obj)); break; } case DEFINED_CONST: case DEFINED_CONST_FROM: { bool allow_nil = type == DEFINED_CONST; klass = v; - if (vm_get_ev_const(ec, klass, SYM2ID(obj), allow_nil, true)) { - expr_type = DEFINED_CONST; - } + return vm_get_ev_const(ec, klass, SYM2ID(obj), allow_nil, true); break; } case DEFINED_FUNC: klass = CLASS_OF(v); - if (rb_ec_obj_respond_to(ec, v, SYM2ID(obj), TRUE)) { - expr_type = DEFINED_METHOD; - } + return rb_ec_obj_respond_to(ec, v, SYM2ID(obj), TRUE); break; case DEFINED_METHOD:{ VALUE klass = CLASS_OF(v); @@ -4039,20 +4028,20 @@ vm_defined(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, rb_num_t op_ https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L4028 break; } case METHOD_VISI_PUBLIC: - expr_type = DEFINED_METHOD; + return Qtrue; break; default: rb_bug("vm_defined: unreachable: %u", (unsigned int)METHOD_ENTRY_VISI(me)); } } else { - expr_type = check_respond_to_missing(obj, v); + return check_respond_to_missing(obj, v); } break; } case DEFINED_YIELD: if (GET_BLOCK_HANDLER() != VM_BLOCK_HANDLER_NONE) { - expr_type = DEFINED_YIELD; + return Qtrue; } break; case DEFINED_ZSUPER: @@ -4063,16 +4052,12 @@ vm_defined(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, rb_num_t op_ https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L4052 VALUE klass = vm_search_normal_superclass(me->defined_class); ID id = me->def->original_id; - if (rb_method_boundp(klass, id, 0)) { - expr_type = DEFINED_ZSUPER; - } + return rb_method_boundp(klass, id, 0); } } break; case DEFINED_REF:{ - if (vm_getspecial(ec, GET_LEP(), Qfalse, FIX2INT(obj)) != Qnil) { - expr_type = DEFINED_GVAR; - } + return vm_getspecial(ec, GET_LEP(), Qfalse, FIX2INT(obj)) != Qnil; break; } default: @@ -4080,12 +4065,7 @@ vm_defined(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, rb_num_t op_ https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L4065 break; } - if (expr_type != 0) { - return Qtrue; - } - else { - return Qfalse; - } + return Qfalse; } static const VALUE * -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/