diff options
author | Aaron Patterson <tenderlove@ruby-lang.org> | 2021-03-16 15:25:37 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2021-03-17 10:55:37 -0700 |
commit | ea817c60fcbdc2c4496be045f5bf028b702561ba (patch) | |
tree | 8b2ced0f5505268eeb2dd84d843b0301ae3ca959 /vm_insnhelper.c | |
parent | c3971bea3351e014171910402a220770196ed1cc (diff) |
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.
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/4279
Diffstat (limited to 'vm_insnhelper.c')
-rw-r--r-- | vm_insnhelper.c | 48 |
1 files changed, 14 insertions, 34 deletions
diff --git a/vm_insnhelper.c b/vm_insnhelper.c index 81ac2eb273..f343de0be4 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -3969,7 +3969,7 @@ vm_once_clear(VALUE data) /* 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) 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 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_ 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_ 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_ break; } - if (expr_type != 0) { - return Qtrue; - } - else { - return Qfalse; - } + return Qfalse; } static const VALUE * |