1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25129 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

This commit is contained in:
matz 2009-09-28 03:09:16 +00:00
parent 438b28d3f7
commit 102918a3ee
6 changed files with 43 additions and 18 deletions

View file

@ -39,7 +39,8 @@ typedef enum {
VM_METHOD_TYPE_ZSUPER,
VM_METHOD_TYPE_UNDEF,
VM_METHOD_TYPE_NOTIMPLEMENTED,
VM_METHOD_TYPE_OPTIMIZED /* Kernel#send, Proc#call, etc */
VM_METHOD_TYPE_OPTIMIZED, /* Kernel#send, Proc#call, etc */
VM_METHOD_TYPE_MISSING, /* wrapper for method_missing(id) */
} rb_method_type_t;
typedef struct rb_method_cfunc_struct {

37
proc.c
View file

@ -883,18 +883,6 @@ rb_obj_is_method(VALUE m)
return rb_typeddata_is_kind_of(m, &method_data_type);
}
static VALUE
missing_wrap(VALUE dummy, VALUE args, int argc, VALUE *argv)
{
VALUE new_args = rb_ary_new4(argc, argv);
VALUE obj = RARRAY_PTR(args)[0];
VALUE sym = RARRAY_PTR(args)[1];
rb_ary_unshift(new_args, sym);
return rb_funcall2(obj, rb_intern("method_missing"),
check_argc(RARRAY_LEN(new_args)), RARRAY_PTR(new_args));
}
static VALUE
mnew(VALUE klass, VALUE obj, ID id, VALUE mclass, int scope)
{
@ -902,18 +890,30 @@ mnew(VALUE klass, VALUE obj, ID id, VALUE mclass, int scope)
VALUE rclass = klass;
ID rid = id;
struct METHOD *data;
rb_method_entry_t *me;
rb_method_definition_t *def;
rb_method_entry_t *me, meb;
rb_method_definition_t *def = 0;
again:
me = rb_method_entry(klass, id);
me = rb_method_entry(klass, id);
if (UNDEFINED_METHOD_ENTRY_P(me)) {
ID rmiss = rb_intern("respond_to_missing?");
VALUE sym = ID2SYM(id);
if (obj != Qundef && !rb_method_basic_definition_p(klass, rmiss)) {
if (RTEST(rb_funcall(obj, rmiss, 1, sym))) {
return rb_proc_new(missing_wrap, rb_assoc_new(obj, sym));
def = ALLOC(rb_method_definition_t);
def->type = VM_METHOD_TYPE_MISSING;
def->original_id = id;
def->alias_count = 0;
meb.flag = 0;
meb.called_id = id;
meb.klass = klass;
meb.def = def;
me = &meb;
def = 0;
goto gen_method;
}
}
rb_print_undef(klass, id, 0);
@ -922,7 +922,7 @@ mnew(VALUE klass, VALUE obj, ID id, VALUE mclass, int scope)
if (scope && (me->flag & NOEX_MASK) != NOEX_PUBLIC) {
rb_print_undef(rclass, def->original_id, (int)(me->flag & NOEX_MASK));
}
if (def->type == VM_METHOD_TYPE_ZSUPER) {
if (def && def->type == VM_METHOD_TYPE_ZSUPER) {
klass = RCLASS_SUPER(me->klass);
id = def->original_id;
goto again;
@ -939,6 +939,7 @@ mnew(VALUE klass, VALUE obj, ID id, VALUE mclass, int scope)
klass = RBASIC(klass)->klass;
}
gen_method:
method = TypedData_Make_Struct(mclass, struct METHOD, &method_data_type, data);
data->recv = obj;
@ -1552,6 +1553,8 @@ rb_method_entry_arity(const rb_method_entry_t *me)
case VM_METHOD_TYPE_UNDEF:
case VM_METHOD_TYPE_NOTIMPLEMENTED:
return 0;
case VM_METHOD_TYPE_MISSING:
return -1;
case VM_METHOD_TYPE_OPTIMIZED: {
switch (def->body.optimize_type) {
case OPTIMIZED_METHOD_TYPE_SEND:

View file

@ -333,9 +333,12 @@ class TestObject < Test::Unit::TestCase
end
foobar = foo.method(:foobar)
assert_equal(-1, foobar.arity);
assert_equal([:foo], foobar.call);
assert_equal([:foo, 1], foobar.call(1));
assert_equal([:foo, 1, 2, 3, 4, 5], foobar.call(1, 2, 3, 4, 5));
assert_equal(foobar, foo.method(:foobar))
assert_not_equal(foobar, c.new.method(:foobar))
c = Class.new(c)
assert_equal(false, c.method_defined?(:foobar))

View file

@ -110,6 +110,14 @@ vm_call0(rb_thread_t* th, VALUE recv, VALUE id, int argc, const VALUE *argv,
if (!(def = me->def)) return Qnil;
goto again;
}
case VM_METHOD_TYPE_MISSING: {
VALUE new_args = rb_ary_new4(argc, argv);
RB_GC_GUARD(new_args);
rb_ary_unshift(new_args, ID2SYM(id));
return rb_funcall2(recv, rb_intern("method_missing"),
argc+1, RARRAY_PTR(new_args));
}
case VM_METHOD_TYPE_OPTIMIZED: {
switch (def->body.optimize_type) {
case OPTIMIZED_METHOD_TYPE_SEND:

View file

@ -527,6 +527,14 @@ vm_call_method(rb_thread_t *th, rb_control_frame_t *cfp,
cfp->sp -= 1;
break;
}
case VM_METHOD_TYPE_MISSING:{
VALUE *argv = ALLOCA_N(VALUE, num+1);
argv[0] = ID2SYM(me->def->original_id);
MEMCPY(argv+1, cfp->sp - num, VALUE, num);
cfp->sp += - num - 1;
val = rb_funcall2(recv, rb_intern("method_missing"), num+1, argv);
break;
}
case VM_METHOD_TYPE_BMETHOD:{
VALUE *argv = ALLOCA_N(VALUE, num);
MEMCPY(argv, cfp->sp - num, VALUE, num);

View file

@ -810,6 +810,8 @@ rb_method_entry_eq(const rb_method_entry_t *m1, const rb_method_entry_t *m2)
return d1->body.attr_id == d2->body.attr_id;
case VM_METHOD_TYPE_BMETHOD:
return RTEST(rb_equal(d1->body.proc, d2->body.proc));
case VM_METHOD_TYPE_MISSING:
return d1->original_id == d2->original_id;
case VM_METHOD_TYPE_ZSUPER:
case VM_METHOD_TYPE_NOTIMPLEMENTED:
case VM_METHOD_TYPE_UNDEF: