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.
This commit is contained in:
parent
c3971bea33
commit
ea817c60fc
Notes:
git
2021-03-18 02:56:05 +09:00
@ -667,12 +667,10 @@ defined
|
|||||||
(VALUE val)
|
(VALUE val)
|
||||||
// attr bool leaf = leafness_of_defined(op_type);
|
// attr bool leaf = leafness_of_defined(op_type);
|
||||||
{
|
{
|
||||||
|
val = Qnil;
|
||||||
if (vm_defined(ec, GET_CFP(), op_type, obj, v)) {
|
if (vm_defined(ec, GET_CFP(), op_type, obj, v)) {
|
||||||
val = needstr;
|
val = needstr;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
val = Qnil;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* check `target' matches `pattern'.
|
/* check `target' matches `pattern'.
|
||||||
|
@ -3969,7 +3969,7 @@ vm_once_clear(VALUE data)
|
|||||||
|
|
||||||
/* defined insn */
|
/* defined insn */
|
||||||
|
|
||||||
static enum defined_type
|
static VALUE
|
||||||
check_respond_to_missing(VALUE obj, VALUE v)
|
check_respond_to_missing(VALUE obj, VALUE v)
|
||||||
{
|
{
|
||||||
VALUE args[2];
|
VALUE args[2];
|
||||||
@ -3978,10 +3978,10 @@ check_respond_to_missing(VALUE obj, VALUE v)
|
|||||||
args[0] = obj; args[1] = Qfalse;
|
args[0] = obj; args[1] = Qfalse;
|
||||||
r = rb_check_funcall(v, idRespond_to_missing, 2, args);
|
r = rb_check_funcall(v, idRespond_to_missing, 2, args);
|
||||||
if (r != Qundef && RTEST(r)) {
|
if (r != Qundef && RTEST(r)) {
|
||||||
return DEFINED_METHOD;
|
return Qtrue;
|
||||||
}
|
}
|
||||||
else {
|
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)
|
vm_defined(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, rb_num_t op_type, VALUE obj, VALUE v)
|
||||||
{
|
{
|
||||||
VALUE klass;
|
VALUE klass;
|
||||||
enum defined_type expr_type = DEFINED_NOT_DEFINED;
|
|
||||||
enum defined_type type = (enum defined_type)op_type;
|
enum defined_type type = (enum defined_type)op_type;
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case DEFINED_IVAR:
|
case DEFINED_IVAR:
|
||||||
if (rb_ivar_defined(GET_SELF(), SYM2ID(obj))) {
|
return rb_ivar_defined(GET_SELF(), SYM2ID(obj));
|
||||||
expr_type = DEFINED_IVAR;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case DEFINED_GVAR:
|
case DEFINED_GVAR:
|
||||||
if (rb_gvar_defined(SYM2ID(obj))) {
|
return rb_gvar_defined(SYM2ID(obj));
|
||||||
expr_type = DEFINED_GVAR;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case DEFINED_CVAR: {
|
case DEFINED_CVAR: {
|
||||||
const rb_cref_t *cref = vm_get_cref(GET_EP());
|
const rb_cref_t *cref = vm_get_cref(GET_EP());
|
||||||
klass = vm_get_cvar_base(cref, GET_CFP(), 0);
|
klass = vm_get_cvar_base(cref, GET_CFP(), 0);
|
||||||
if (rb_cvar_defined(klass, SYM2ID(obj))) {
|
return rb_cvar_defined(klass, SYM2ID(obj));
|
||||||
expr_type = DEFINED_CVAR;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case DEFINED_CONST:
|
case DEFINED_CONST:
|
||||||
case DEFINED_CONST_FROM: {
|
case DEFINED_CONST_FROM: {
|
||||||
bool allow_nil = type == DEFINED_CONST;
|
bool allow_nil = type == DEFINED_CONST;
|
||||||
klass = v;
|
klass = v;
|
||||||
if (vm_get_ev_const(ec, klass, SYM2ID(obj), allow_nil, true)) {
|
return vm_get_ev_const(ec, klass, SYM2ID(obj), allow_nil, true);
|
||||||
expr_type = DEFINED_CONST;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case DEFINED_FUNC:
|
case DEFINED_FUNC:
|
||||||
klass = CLASS_OF(v);
|
klass = CLASS_OF(v);
|
||||||
if (rb_ec_obj_respond_to(ec, v, SYM2ID(obj), TRUE)) {
|
return rb_ec_obj_respond_to(ec, v, SYM2ID(obj), TRUE);
|
||||||
expr_type = DEFINED_METHOD;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case DEFINED_METHOD:{
|
case DEFINED_METHOD:{
|
||||||
VALUE klass = CLASS_OF(v);
|
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;
|
break;
|
||||||
}
|
}
|
||||||
case METHOD_VISI_PUBLIC:
|
case METHOD_VISI_PUBLIC:
|
||||||
expr_type = DEFINED_METHOD;
|
return Qtrue;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
rb_bug("vm_defined: unreachable: %u", (unsigned int)METHOD_ENTRY_VISI(me));
|
rb_bug("vm_defined: unreachable: %u", (unsigned int)METHOD_ENTRY_VISI(me));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
expr_type = check_respond_to_missing(obj, v);
|
return check_respond_to_missing(obj, v);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case DEFINED_YIELD:
|
case DEFINED_YIELD:
|
||||||
if (GET_BLOCK_HANDLER() != VM_BLOCK_HANDLER_NONE) {
|
if (GET_BLOCK_HANDLER() != VM_BLOCK_HANDLER_NONE) {
|
||||||
expr_type = DEFINED_YIELD;
|
return Qtrue;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case DEFINED_ZSUPER:
|
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);
|
VALUE klass = vm_search_normal_superclass(me->defined_class);
|
||||||
ID id = me->def->original_id;
|
ID id = me->def->original_id;
|
||||||
|
|
||||||
if (rb_method_boundp(klass, id, 0)) {
|
return rb_method_boundp(klass, id, 0);
|
||||||
expr_type = DEFINED_ZSUPER;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case DEFINED_REF:{
|
case DEFINED_REF:{
|
||||||
if (vm_getspecial(ec, GET_LEP(), Qfalse, FIX2INT(obj)) != Qnil) {
|
return vm_getspecial(ec, GET_LEP(), Qfalse, FIX2INT(obj)) != Qnil;
|
||||||
expr_type = DEFINED_GVAR;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
@ -4080,12 +4065,7 @@ vm_defined(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, rb_num_t op_
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (expr_type != 0) {
|
|
||||||
return Qtrue;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return Qfalse;
|
return Qfalse;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const VALUE *
|
static const VALUE *
|
||||||
|
Loading…
x
Reference in New Issue
Block a user