diff options
Diffstat (limited to 'vm.c')
-rw-r--r-- | vm.c | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -1655,6 +1655,46 @@ m_core_undef_method(VALUE self, VALUE cbase, VALUE sym) return Qnil; } +static void +vm_define_method(rb_thread_t *th, VALUE obj, ID id, rb_iseq_t *miseq, + rb_num_t is_singleton, NODE *cref) +{ + NODE *newbody; + VALUE klass = cref->nd_clss; + int noex = cref->nd_visi; + + if (NIL_P(klass)) { + rb_raise(rb_eTypeError, "no class/module to add method"); + } + + if (is_singleton) { + if (FIXNUM_P(obj) || SYMBOL_P(obj)) { + rb_raise(rb_eTypeError, + "can't define singleton method \"%s\" for %s", + rb_id2name(id), rb_obj_classname(obj)); + } + + if (OBJ_FROZEN(obj)) { + rb_error_frozen("object"); + } + + klass = rb_singleton_class(obj); + noex = NOEX_PUBLIC; + } + + /* dup */ + COPY_CREF(miseq->cref_stack, cref); + miseq->klass = klass; + miseq->defined_method_id = id; + newbody = NEW_NODE(RUBY_VM_METHOD_NODE, 0, miseq->self, 0); + rb_add_method(klass, id, newbody, noex); + + if (!is_singleton && noex == NOEX_MODFUNC) { + rb_add_method(rb_singleton_class(klass), id, newbody, NOEX_PUBLIC); + } + INC_VM_STATE_VERSION(); +} + static VALUE m_core_define_method(VALUE self, VALUE cbase, VALUE sym, VALUE iseqval) { |