2006-12-31 10:02:22 -05:00
|
|
|
/*
|
* common.mk: clean up
- remove blockinlining.$(OBJEXT) to built
- make ENCODING_H_INCLDUES variable (include/ruby/encoding.h)
- make VM_CORE_H_INCLUDES variable (vm_core.h)
- simplify rules.
- make depends rule to output depend status using gcc -MM.
* include/ruby/mvm.h, include/ruby/vm.h: rename mvm.h to vm.h.
* include/ruby.h: ditto.
* load.c: add inclusion explicitly.
* enumerator.c, object.c, parse.y, thread.c, vm_dump.c:
remove useless inclusion.
* eval_intern.h: cleanup inclusion.
* vm_core.h: rb_thread_t should be defined in this file.
* vm_evalbody.c, vm_exec.c: rename vm_evalbody.c to vm_exec.c.
* vm.h, vm_exec.h: rename vm.h to vm_exec.h.
* insnhelper.h, vm_insnhelper.h: rename insnhelper.h to vm_insnhelper.h.
* vm.c, vm_insnhelper.c, vm_insnhelper.h:
- rename vm_eval() to vm_exec_core().
- rename vm_eval_body() to vm_exec().
- cleanup include order.
* vm_method.c: fix comment.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19466 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-22 20:20:28 -04:00
|
|
|
* This file is included by vm.h
|
2006-12-31 10:02:22 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define CACHE_SIZE 0x800
|
|
|
|
#define CACHE_MASK 0x7ff
|
|
|
|
#define EXPR1(c,m) ((((c)>>3)^(m))&CACHE_MASK)
|
|
|
|
|
2009-07-15 10:59:41 -04:00
|
|
|
static void rb_vm_check_redefinition_opt_method(const rb_method_entry_t *me);
|
* eval_method.c: renamed from vm_method.c. "vm_method.c" is included
by "vm.c".
* vm_eval.c: added. Some codes are moved from "eval.c"
* common.mk: fix for above changes.
* compile.c: make a vm_eval(0)
* eval.c, eval_error.c, eval_intern.h, eval_jump.c, proc.c, vm.c,
id.c, id.h, vm_core.h, vm_dump.c, vm_evalbody.c, vm_insnhelper.c,
blockinlining.c: fix for above changes. and do some refactoring.
this changes improve rb_yield() performance.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-24 13:50:17 -04:00
|
|
|
|
2008-12-15 00:15:26 -05:00
|
|
|
static ID object_id;
|
* eval_method.c: renamed from vm_method.c. "vm_method.c" is included
by "vm.c".
* vm_eval.c: added. Some codes are moved from "eval.c"
* common.mk: fix for above changes.
* compile.c: make a vm_eval(0)
* eval.c, eval_error.c, eval_intern.h, eval_jump.c, proc.c, vm.c,
id.c, id.h, vm_core.h, vm_dump.c, vm_evalbody.c, vm_insnhelper.c,
blockinlining.c: fix for above changes. and do some refactoring.
this changes improve rb_yield() performance.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-24 13:50:17 -04:00
|
|
|
static ID removed, singleton_removed, undefined, singleton_undefined;
|
|
|
|
static ID added, singleton_added;
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
struct cache_entry { /* method hash table. */
|
|
|
|
ID mid; /* method's id */
|
|
|
|
VALUE klass; /* receiver's class */
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_method_entry_t *me;
|
2006-12-31 10:02:22 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct cache_entry cache[CACHE_SIZE];
|
* eval_method.c: renamed from vm_method.c. "vm_method.c" is included
by "vm.c".
* vm_eval.c: added. Some codes are moved from "eval.c"
* common.mk: fix for above changes.
* compile.c: make a vm_eval(0)
* eval.c, eval_error.c, eval_intern.h, eval_jump.c, proc.c, vm.c,
id.c, id.h, vm_core.h, vm_dump.c, vm_evalbody.c, vm_insnhelper.c,
blockinlining.c: fix for above changes. and do some refactoring.
this changes improve rb_yield() performance.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-24 13:50:17 -04:00
|
|
|
#define ruby_running (GET_VM()->running)
|
|
|
|
/* int ruby_running = 0; */
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
void
|
|
|
|
rb_clear_cache(void)
|
|
|
|
{
|
|
|
|
struct cache_entry *ent, *end;
|
|
|
|
|
|
|
|
rb_vm_change_state();
|
|
|
|
|
|
|
|
if (!ruby_running)
|
|
|
|
return;
|
|
|
|
ent = cache;
|
|
|
|
end = ent + CACHE_SIZE;
|
|
|
|
while (ent < end) {
|
2009-07-15 19:59:15 -04:00
|
|
|
ent->me = 0;
|
|
|
|
ent->mid = 0;
|
2006-12-31 10:02:22 -05:00
|
|
|
ent++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rb_clear_cache_for_undef(VALUE klass, ID id)
|
|
|
|
{
|
|
|
|
struct cache_entry *ent, *end;
|
|
|
|
|
|
|
|
rb_vm_change_state();
|
|
|
|
|
|
|
|
if (!ruby_running)
|
|
|
|
return;
|
|
|
|
ent = cache;
|
|
|
|
end = ent + CACHE_SIZE;
|
|
|
|
while (ent < end) {
|
2009-07-15 10:59:41 -04:00
|
|
|
if ((ent->me && ent->me->klass == klass) && ent->mid == id) {
|
2009-07-15 19:59:15 -04:00
|
|
|
ent->me = 0;
|
|
|
|
ent->mid = 0;
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
ent++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rb_clear_cache_by_id(ID id)
|
|
|
|
{
|
|
|
|
struct cache_entry *ent, *end;
|
|
|
|
|
|
|
|
rb_vm_change_state();
|
|
|
|
|
|
|
|
if (!ruby_running)
|
|
|
|
return;
|
|
|
|
ent = cache;
|
|
|
|
end = ent + CACHE_SIZE;
|
|
|
|
while (ent < end) {
|
|
|
|
if (ent->mid == id) {
|
2009-07-15 19:59:15 -04:00
|
|
|
ent->me = 0;
|
|
|
|
ent->mid = 0;
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
ent++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_clear_cache_by_class(VALUE klass)
|
|
|
|
{
|
|
|
|
struct cache_entry *ent, *end;
|
|
|
|
|
|
|
|
rb_vm_change_state();
|
|
|
|
|
|
|
|
if (!ruby_running)
|
|
|
|
return;
|
|
|
|
ent = cache;
|
|
|
|
end = ent + CACHE_SIZE;
|
|
|
|
while (ent < end) {
|
2009-07-15 10:59:41 -04:00
|
|
|
if (ent->klass == klass || (ent->me && ent->me->klass == klass)) {
|
2009-07-15 19:59:15 -04:00
|
|
|
ent->me = 0;
|
|
|
|
ent->mid = 0;
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
ent++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-15 10:59:41 -04:00
|
|
|
VALUE rb_f_notimplement(int argc, VALUE *argv, VALUE obj)
|
|
|
|
{
|
|
|
|
rb_notimplement();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rb_define_notimplement_method_id(VALUE mod, ID id, rb_method_flag_t noex)
|
|
|
|
{
|
|
|
|
rb_add_method(mod, id, VM_METHOD_TYPE_NOTIMPLEMENTED, 0, noex);
|
|
|
|
}
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
void
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_add_method_cfunc(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc, rb_method_flag_t noex)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
2009-07-15 10:59:41 -04:00
|
|
|
if (func != rb_f_notimplement) {
|
|
|
|
rb_method_cfunc_t opt = {
|
|
|
|
func, argc,
|
|
|
|
};
|
|
|
|
rb_add_method(klass, mid, VM_METHOD_TYPE_CFUNC, &opt, noex);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_define_notimplement_method_id(klass, mid, noex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_method_entry_t *
|
|
|
|
rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *opts, rb_method_flag_t noex)
|
|
|
|
{
|
|
|
|
rb_method_entry_t *me;
|
|
|
|
st_table *mtbl;
|
|
|
|
st_data_t data;
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
if (NIL_P(klass)) {
|
|
|
|
klass = rb_cObject;
|
|
|
|
}
|
* object.c (rb_obj_untrusted): new method Object#untrusted?.
(rb_obj_untrust): new method Object#untrust.
(rb_obj_trust): new method Object#trust.
* array.c, debug.c, time.c, include/ruby/ruby.h, re.c, variable.c,
string.c, io.c, dir.c, vm_method.c, struct.c, class.c, hash.c,
ruby.c, marshal.c: fixes for Object#untrusted?.
* test/ruby/test_module.rb, test/ruby/test_array.rb,
test/ruby/test_object.rb, test/ruby/test_string.rb,
test/ruby/test_marshal.rb, test/ruby/test_hash.rb: added tests for
Object#untrusted?.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18568 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-08-13 03:25:05 -04:00
|
|
|
if (rb_safe_level() >= 4 &&
|
|
|
|
(klass == rb_cObject || !OBJ_UNTRUSTED(klass))) {
|
2006-12-31 10:02:22 -05:00
|
|
|
rb_raise(rb_eSecurityError, "Insecure: can't define method");
|
|
|
|
}
|
|
|
|
if (!FL_TEST(klass, FL_SINGLETON) &&
|
2009-07-15 10:59:41 -04:00
|
|
|
type != VM_METHOD_TYPE_NOTIMPLEMENTED &&
|
|
|
|
type != VM_METHOD_TYPE_ZSUPER &&
|
2006-12-31 10:02:22 -05:00
|
|
|
(mid == rb_intern("initialize") || mid == rb_intern("initialize_copy"))) {
|
|
|
|
noex = NOEX_PRIVATE | noex;
|
|
|
|
}
|
2009-07-15 10:59:41 -04:00
|
|
|
else if (FL_TEST(klass, FL_SINGLETON) &&
|
|
|
|
type == VM_METHOD_TYPE_CFUNC &&
|
|
|
|
mid == rb_intern("allocate")) {
|
|
|
|
rb_warn("defining %s.allocate is deprecated; use rb_define_alloc_func()",
|
|
|
|
rb_class2name(rb_iv_get(klass, "__attached__")));
|
2006-12-31 10:02:22 -05:00
|
|
|
mid = ID_ALLOCATOR;
|
|
|
|
}
|
|
|
|
if (OBJ_FROZEN(klass)) {
|
|
|
|
rb_error_frozen("class/module");
|
|
|
|
}
|
|
|
|
rb_clear_cache_by_id(mid);
|
|
|
|
|
2009-07-15 10:59:41 -04:00
|
|
|
me = ALLOC(rb_method_entry_t);
|
|
|
|
me->type = type;
|
|
|
|
me->original_id = me->called_id = mid;
|
|
|
|
me->klass = klass;
|
|
|
|
me->flag = NOEX_WITH_SAFE(noex);
|
|
|
|
me->alias_count = 0;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case VM_METHOD_TYPE_ISEQ:
|
|
|
|
me->body.iseq = (rb_iseq_t *)opts;
|
|
|
|
break;
|
|
|
|
case VM_METHOD_TYPE_CFUNC:
|
|
|
|
me->body.cfunc = *(rb_method_cfunc_t *)opts;
|
|
|
|
break;
|
|
|
|
case VM_METHOD_TYPE_ATTRSET:
|
|
|
|
case VM_METHOD_TYPE_IVAR:
|
|
|
|
me->body.attr_id = (ID)opts;
|
|
|
|
break;
|
|
|
|
case VM_METHOD_TYPE_BMETHOD:
|
|
|
|
me->body.proc = (VALUE)opts;
|
|
|
|
break;
|
|
|
|
case VM_METHOD_TYPE_NOTIMPLEMENTED:
|
|
|
|
me->body.cfunc.func = rb_f_notimplement;
|
|
|
|
me->body.cfunc.argc = -1;
|
|
|
|
break;
|
|
|
|
case VM_METHOD_TYPE_OPTIMIZED:
|
|
|
|
me->body.optimize_type = (enum method_optimized_type)opts;
|
|
|
|
break;
|
|
|
|
case VM_METHOD_TYPE_ZSUPER:
|
|
|
|
case VM_METHOD_TYPE_UNDEF:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rb_bug("rb_add_method: unsupported method type (%d)\n", type);
|
|
|
|
}
|
|
|
|
|
|
|
|
mtbl = RCLASS_M_TBL(klass);
|
|
|
|
|
|
|
|
/* check re-definition */
|
|
|
|
if (st_lookup(mtbl, mid, &data)) {
|
|
|
|
rb_method_entry_t *old_me = (rb_method_entry_t *)data;
|
|
|
|
rb_vm_check_redefinition_opt_method(old_me);
|
|
|
|
|
|
|
|
if (RTEST(ruby_verbose) &&
|
|
|
|
old_me->alias_count == 0 &&
|
|
|
|
old_me->type != VM_METHOD_TYPE_UNDEF) {
|
|
|
|
rb_warning("method redefined; discarding old %s", rb_id2name(mid));
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
2009-07-22 09:27:26 -04:00
|
|
|
#if defined(__cplusplus) || (__STDC_VERSION__ >= 199901L)
|
2009-07-15 10:59:41 -04:00
|
|
|
// TODO: free old_me
|
2009-07-22 09:27:26 -04:00
|
|
|
#endif
|
2009-07-15 10:59:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check mid */
|
|
|
|
if (klass == rb_cObject && mid == idInitialize) {
|
|
|
|
rb_warn("redefining Object#initialize may cause infinite loop");
|
|
|
|
}
|
|
|
|
/* check mid */
|
|
|
|
if (mid == object_id || mid == id__send__) {
|
|
|
|
if (type == VM_METHOD_TYPE_ISEQ) {
|
|
|
|
rb_warn("redefining `%s' may cause serious problems", rb_id2name(mid));
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-15 10:59:41 -04:00
|
|
|
st_insert(mtbl, mid, (st_data_t) me);
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2009-07-15 10:59:41 -04:00
|
|
|
if (mid != ID_ALLOCATOR && ruby_running) {
|
2006-12-31 10:02:22 -05:00
|
|
|
if (FL_TEST(klass, FL_SINGLETON)) {
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_funcall(rb_iv_get(klass, "__attached__"), singleton_added, 1, ID2SYM(mid));
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_funcall(klass, added, 1, ID2SYM(mid));
|
|
|
|
}
|
|
|
|
}
|
2009-07-15 10:59:41 -04:00
|
|
|
|
|
|
|
return me;
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-01-12 04:27:43 -05:00
|
|
|
rb_define_alloc_func(VALUE klass, VALUE (*func)(VALUE))
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
|
|
|
Check_Type(klass, T_CLASS);
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_add_method_cfunc(rb_singleton_class(klass), ID_ALLOCATOR,
|
|
|
|
func, 0, NOEX_PRIVATE);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_undef_alloc_func(VALUE klass)
|
|
|
|
{
|
|
|
|
Check_Type(klass, T_CLASS);
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_add_method(rb_singleton_class(klass), ID_ALLOCATOR, VM_METHOD_TYPE_UNDEF, 0, NOEX_UNDEF);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
|
2007-09-08 11:07:18 -04:00
|
|
|
rb_alloc_func_t
|
|
|
|
rb_get_alloc_func(VALUE klass)
|
|
|
|
{
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_method_entry_t *me;
|
2007-09-08 11:07:18 -04:00
|
|
|
Check_Type(klass, T_CLASS);
|
2009-07-15 10:59:41 -04:00
|
|
|
me = rb_method_entry(CLASS_OF(klass), ID_ALLOCATOR);
|
|
|
|
|
|
|
|
if (me && me->type == VM_METHOD_TYPE_CFUNC) {
|
|
|
|
return (rb_alloc_func_t)me->body.cfunc.func;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0;
|
|
|
|
}
|
2007-09-08 11:07:18 -04:00
|
|
|
}
|
|
|
|
|
2009-07-15 10:59:41 -04:00
|
|
|
static rb_method_entry_t*
|
|
|
|
search_method(VALUE klass, ID id)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
2007-09-22 17:16:21 -04:00
|
|
|
st_data_t body;
|
2006-12-31 10:02:22 -05:00
|
|
|
if (!klass) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-28 02:21:46 -04:00
|
|
|
while (!st_lookup(RCLASS_M_TBL(klass), id, &body)) {
|
|
|
|
klass = RCLASS_SUPER(klass);
|
2009-07-15 10:59:41 -04:00
|
|
|
if (!klass) {
|
2006-12-31 10:02:22 -05:00
|
|
|
return 0;
|
2009-07-15 10:59:41 -04:00
|
|
|
}
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
|
2009-07-15 10:59:41 -04:00
|
|
|
return (rb_method_entry_t *)body;
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-07-15 10:59:41 -04:00
|
|
|
* search method entry without method cache.
|
2006-12-31 10:02:22 -05:00
|
|
|
*
|
2009-07-15 10:59:41 -04:00
|
|
|
* if you need method entry with method cache, use
|
|
|
|
* rb_method_entry()
|
2006-12-31 10:02:22 -05:00
|
|
|
*/
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_method_entry_t *
|
|
|
|
rb_get_method_entry(VALUE klass, ID id)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_method_entry_t *me = search_method(klass, id);
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
if (ruby_running) {
|
|
|
|
struct cache_entry *ent;
|
|
|
|
ent = cache + EXPR1(klass, id);
|
|
|
|
ent->klass = klass;
|
|
|
|
|
2009-07-15 10:59:41 -04:00
|
|
|
if (!me || me->type == VM_METHOD_TYPE_UNDEF) {
|
|
|
|
ent->mid = id;
|
|
|
|
ent->me = 0;
|
|
|
|
me = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ent->mid = id;
|
|
|
|
ent->me = me;
|
|
|
|
}
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
|
2009-07-15 10:59:41 -04:00
|
|
|
return me;
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_method_entry_t *
|
|
|
|
rb_method_entry(VALUE klass, ID id)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
|
|
|
struct cache_entry *ent;
|
|
|
|
|
|
|
|
ent = cache + EXPR1(klass, id);
|
2008-08-28 08:08:19 -04:00
|
|
|
if (ent->mid == id && ent->klass == klass) {
|
2009-07-15 10:59:41 -04:00
|
|
|
return ent->me;
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
|
2009-07-15 10:59:41 -04:00
|
|
|
return rb_get_method_entry(klass, id);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
remove_method(VALUE klass, ID mid)
|
|
|
|
{
|
2007-09-22 17:16:21 -04:00
|
|
|
st_data_t data;
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_method_entry_t *me = 0;
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
if (klass == rb_cObject) {
|
|
|
|
rb_secure(4);
|
|
|
|
}
|
* object.c (rb_obj_untrusted): new method Object#untrusted?.
(rb_obj_untrust): new method Object#untrust.
(rb_obj_trust): new method Object#trust.
* array.c, debug.c, time.c, include/ruby/ruby.h, re.c, variable.c,
string.c, io.c, dir.c, vm_method.c, struct.c, class.c, hash.c,
ruby.c, marshal.c: fixes for Object#untrusted?.
* test/ruby/test_module.rb, test/ruby/test_array.rb,
test/ruby/test_object.rb, test/ruby/test_string.rb,
test/ruby/test_marshal.rb, test/ruby/test_hash.rb: added tests for
Object#untrusted?.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18568 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-08-13 03:25:05 -04:00
|
|
|
if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(klass)) {
|
2006-12-31 10:02:22 -05:00
|
|
|
rb_raise(rb_eSecurityError, "Insecure: can't remove method");
|
|
|
|
}
|
|
|
|
if (OBJ_FROZEN(klass))
|
|
|
|
rb_error_frozen("class/module");
|
2008-12-15 00:15:26 -05:00
|
|
|
if (mid == object_id || mid == id__send__ || mid == idInitialize) {
|
2009-07-04 19:14:43 -04:00
|
|
|
rb_warn("removing `%s' may cause serious problems", rb_id2name(mid));
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
2009-07-15 10:59:41 -04:00
|
|
|
|
2007-09-28 02:21:46 -04:00
|
|
|
if (st_lookup(RCLASS_M_TBL(klass), mid, &data)) {
|
2009-07-15 10:59:41 -04:00
|
|
|
me = (rb_method_entry_t *)data;
|
|
|
|
if (!me || me->type == VM_METHOD_TYPE_UNDEF) {
|
|
|
|
me = 0;
|
|
|
|
}
|
2007-09-25 08:41:48 -04:00
|
|
|
else {
|
2007-09-28 02:21:46 -04:00
|
|
|
st_delete(RCLASS_M_TBL(klass), &mid, &data);
|
2007-09-25 08:41:48 -04:00
|
|
|
}
|
|
|
|
}
|
2009-07-15 10:59:41 -04:00
|
|
|
if (!me) {
|
2006-12-31 10:02:22 -05:00
|
|
|
rb_name_error(mid, "method `%s' not defined in %s",
|
|
|
|
rb_id2name(mid), rb_class2name(klass));
|
|
|
|
}
|
2007-05-02 02:21:58 -04:00
|
|
|
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_vm_check_redefinition_opt_method(me);
|
2006-12-31 10:02:22 -05:00
|
|
|
rb_clear_cache_for_undef(klass, mid);
|
2009-07-15 10:59:41 -04:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
if (FL_TEST(klass, FL_SINGLETON)) {
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_funcall(rb_iv_get(klass, "__attached__"), singleton_removed, 1, ID2SYM(mid));
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_funcall(klass, removed, 1, ID2SYM(mid));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_remove_method(VALUE klass, const char *name)
|
|
|
|
{
|
|
|
|
remove_method(klass, rb_intern(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* remove_method(symbol) => self
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 00:25:10 -04:00
|
|
|
*
|
2006-12-31 10:02:22 -05:00
|
|
|
* Removes the method identified by _symbol_ from the current
|
|
|
|
* class. For an example, see <code>Module.undef_method</code>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_mod_remove_method(int argc, VALUE *argv, VALUE mod)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
remove_method(mod, rb_to_id(argv[i]));
|
|
|
|
}
|
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef rb_disable_super
|
|
|
|
#undef rb_enable_super
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_disable_super(VALUE klass, const char *name)
|
|
|
|
{
|
|
|
|
/* obsolete - no use */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_enable_super(VALUE klass, const char *name)
|
|
|
|
{
|
|
|
|
rb_warning("rb_enable_super() is obsolete");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rb_export_method(VALUE klass, ID name, ID noex)
|
|
|
|
{
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_method_entry_t *me;
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
if (klass == rb_cObject) {
|
|
|
|
rb_secure(4);
|
|
|
|
}
|
2009-07-15 10:59:41 -04:00
|
|
|
|
|
|
|
me = search_method(klass, name);
|
|
|
|
if (!me && TYPE(klass) == T_MODULE) {
|
|
|
|
me = search_method(rb_cObject, name);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
2009-07-15 10:59:41 -04:00
|
|
|
|
|
|
|
if (!me || me->type == VM_METHOD_TYPE_UNDEF) {
|
2007-12-17 18:01:50 -05:00
|
|
|
rb_print_undef(klass, name, 0);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
2009-07-15 10:59:41 -04:00
|
|
|
|
|
|
|
if (me->flag != noex) {
|
|
|
|
rb_vm_check_redefinition_opt_method(me);
|
|
|
|
|
|
|
|
if (klass == me->klass) {
|
|
|
|
me->flag = noex;
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
else {
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_add_method(klass, name, VM_METHOD_TYPE_ZSUPER, 0, noex);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_method_boundp(VALUE klass, ID id, int ex)
|
|
|
|
{
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_method_entry_t *me = rb_method_entry(klass, id);
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2009-07-15 10:59:41 -04:00
|
|
|
if (me != 0) {
|
|
|
|
if (ex && (me->flag & NOEX_PRIVATE)) {
|
2006-12-31 10:02:22 -05:00
|
|
|
return Qfalse;
|
|
|
|
}
|
2009-07-15 10:59:41 -04:00
|
|
|
if (me->type == VM_METHOD_TYPE_NOTIMPLEMENTED) {
|
2009-07-04 19:12:04 -04:00
|
|
|
return Qfalse;
|
2009-07-15 10:59:41 -04:00
|
|
|
}
|
2006-12-31 10:02:22 -05:00
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_attr(VALUE klass, ID id, int read, int write, int ex)
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
ID attriv;
|
2009-05-19 11:07:45 -04:00
|
|
|
VALUE aname;
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_method_flag_t noex;
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
if (!ex) {
|
|
|
|
noex = NOEX_PUBLIC;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (SCOPE_TEST(NOEX_PRIVATE)) {
|
|
|
|
noex = NOEX_PRIVATE;
|
|
|
|
rb_warning((SCOPE_CHECK(NOEX_MODFUNC)) ?
|
|
|
|
"attribute accessor as module_function" :
|
|
|
|
"private attribute?");
|
|
|
|
}
|
|
|
|
else if (SCOPE_TEST(NOEX_PROTECTED)) {
|
|
|
|
noex = NOEX_PROTECTED;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
noex = NOEX_PUBLIC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rb_is_local_id(id) && !rb_is_const_id(id)) {
|
|
|
|
rb_name_error(id, "invalid attribute name `%s'", rb_id2name(id));
|
|
|
|
}
|
|
|
|
name = rb_id2name(id);
|
|
|
|
if (!name) {
|
|
|
|
rb_raise(rb_eArgError, "argument needs to be symbol or string");
|
|
|
|
}
|
2009-05-19 11:07:45 -04:00
|
|
|
aname = rb_sprintf("@%s", name);
|
|
|
|
rb_enc_copy(aname, rb_id2str(id));
|
|
|
|
attriv = rb_intern_str(aname);
|
2006-12-31 10:02:22 -05:00
|
|
|
if (read) {
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_add_method(klass, id, VM_METHOD_TYPE_IVAR, (void *)attriv, noex);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
if (write) {
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_add_method(klass, rb_id_attrset(id), VM_METHOD_TYPE_ATTRSET, (void *)attriv, noex);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_undef(VALUE klass, ID id)
|
|
|
|
{
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_method_entry_t *me;
|
2006-12-31 10:02:22 -05:00
|
|
|
|
* vm.c, insns.def, eval.c, vm_insnhelper.c: fix CREF handling.
VM value stack frame of block contains cref information.
(dfp[-1] points CREF)
* compile.c, eval_intern.h, eval_method.c, load.c, proc.c,
vm_dump.h, vm_core.h: ditto.
* include/ruby/ruby.h, gc.c: remove T_VALUES because of above
changes.
* bootstraptest/test_eval.rb, test_knownbug.rb: move solved test.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16468 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-18 23:08:50 -04:00
|
|
|
if (rb_vm_cbase() == rb_cObject && klass == rb_cObject) {
|
2006-12-31 10:02:22 -05:00
|
|
|
rb_secure(4);
|
|
|
|
}
|
* object.c (rb_obj_untrusted): new method Object#untrusted?.
(rb_obj_untrust): new method Object#untrust.
(rb_obj_trust): new method Object#trust.
* array.c, debug.c, time.c, include/ruby/ruby.h, re.c, variable.c,
string.c, io.c, dir.c, vm_method.c, struct.c, class.c, hash.c,
ruby.c, marshal.c: fixes for Object#untrusted?.
* test/ruby/test_module.rb, test/ruby/test_array.rb,
test/ruby/test_object.rb, test/ruby/test_string.rb,
test/ruby/test_marshal.rb, test/ruby/test_hash.rb: added tests for
Object#untrusted?.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18568 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-08-13 03:25:05 -04:00
|
|
|
if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(klass)) {
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_raise(rb_eSecurityError, "Insecure: can't undef `%s'", rb_id2name(id));
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
rb_frozen_class_p(klass);
|
2008-12-15 00:15:26 -05:00
|
|
|
if (id == object_id || id == id__send__ || id == idInitialize) {
|
2009-07-04 19:14:43 -04:00
|
|
|
rb_warn("undefining `%s' may cause serious problems", rb_id2name(id));
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
2009-07-15 10:59:41 -04:00
|
|
|
|
|
|
|
me = search_method(klass, id);
|
|
|
|
|
|
|
|
if (!me || me->type == VM_METHOD_TYPE_UNDEF) {
|
2008-05-31 05:28:20 -04:00
|
|
|
const char *s0 = " class";
|
2006-12-31 10:02:22 -05:00
|
|
|
VALUE c = klass;
|
|
|
|
|
|
|
|
if (FL_TEST(c, FL_SINGLETON)) {
|
|
|
|
VALUE obj = rb_iv_get(klass, "__attached__");
|
|
|
|
|
|
|
|
switch (TYPE(obj)) {
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 00:25:10 -04:00
|
|
|
case T_MODULE:
|
|
|
|
case T_CLASS:
|
2006-12-31 10:02:22 -05:00
|
|
|
c = obj;
|
|
|
|
s0 = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (TYPE(c) == T_MODULE) {
|
|
|
|
s0 = " module";
|
|
|
|
}
|
|
|
|
rb_name_error(id, "undefined method `%s' for%s `%s'",
|
|
|
|
rb_id2name(id), s0, rb_class2name(c));
|
|
|
|
}
|
|
|
|
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_add_method(klass, id, VM_METHOD_TYPE_UNDEF, 0, NOEX_PUBLIC);
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
if (FL_TEST(klass, FL_SINGLETON)) {
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_funcall(rb_iv_get(klass, "__attached__"), singleton_undefined, 1, ID2SYM(id));
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_funcall(klass, undefined, 1, ID2SYM(id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* undef_method(symbol) => self
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 00:25:10 -04:00
|
|
|
*
|
2006-12-31 10:02:22 -05:00
|
|
|
* Prevents the current class from responding to calls to the named
|
|
|
|
* method. Contrast this with <code>remove_method</code>, which deletes
|
|
|
|
* the method from the particular class; Ruby will still search
|
|
|
|
* superclasses and mixed-in modules for a possible receiver.
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 00:25:10 -04:00
|
|
|
*
|
2006-12-31 10:02:22 -05:00
|
|
|
* class Parent
|
|
|
|
* def hello
|
|
|
|
* puts "In parent"
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
* class Child < Parent
|
|
|
|
* def hello
|
|
|
|
* puts "In child"
|
|
|
|
* end
|
|
|
|
* end
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 00:25:10 -04:00
|
|
|
*
|
|
|
|
*
|
2006-12-31 10:02:22 -05:00
|
|
|
* c = Child.new
|
|
|
|
* c.hello
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 00:25:10 -04:00
|
|
|
*
|
|
|
|
*
|
2006-12-31 10:02:22 -05:00
|
|
|
* class Child
|
|
|
|
* remove_method :hello # remove from child, still in parent
|
|
|
|
* end
|
|
|
|
* c.hello
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 00:25:10 -04:00
|
|
|
*
|
|
|
|
*
|
2006-12-31 10:02:22 -05:00
|
|
|
* class Child
|
|
|
|
* undef_method :hello # prevent any calls to 'hello'
|
|
|
|
* end
|
|
|
|
* c.hello
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 00:25:10 -04:00
|
|
|
*
|
2006-12-31 10:02:22 -05:00
|
|
|
* <em>produces:</em>
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 00:25:10 -04:00
|
|
|
*
|
2006-12-31 10:02:22 -05:00
|
|
|
* In child
|
|
|
|
* In parent
|
|
|
|
* prog.rb:23: undefined method `hello' for #<Child:0x401b3bb4> (NoMethodError)
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_mod_undef_method(int argc, VALUE *argv, VALUE mod)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
rb_undef(mod, rb_to_id(argv[i]));
|
|
|
|
}
|
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
|
* eval_method.c: renamed from vm_method.c. "vm_method.c" is included
by "vm.c".
* vm_eval.c: added. Some codes are moved from "eval.c"
* common.mk: fix for above changes.
* compile.c: make a vm_eval(0)
* eval.c, eval_error.c, eval_intern.h, eval_jump.c, proc.c, vm.c,
id.c, id.h, vm_core.h, vm_dump.c, vm_evalbody.c, vm_insnhelper.c,
blockinlining.c: fix for above changes. and do some refactoring.
this changes improve rb_yield() performance.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-24 13:50:17 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* mod.method_defined?(symbol) => true or false
|
|
|
|
*
|
|
|
|
* Returns +true+ if the named method is defined by
|
|
|
|
* _mod_ (or its included modules and, if _mod_ is a class,
|
|
|
|
* its ancestors). Public and protected methods are matched.
|
|
|
|
*
|
|
|
|
* module A
|
|
|
|
* def method1() end
|
|
|
|
* end
|
|
|
|
* class B
|
|
|
|
* def method2() end
|
|
|
|
* end
|
|
|
|
* class C < B
|
|
|
|
* include A
|
|
|
|
* def method3() end
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* A.method_defined? :method1 #=> true
|
|
|
|
* C.method_defined? "method1" #=> true
|
|
|
|
* C.method_defined? "method2" #=> true
|
|
|
|
* C.method_defined? "method3" #=> true
|
|
|
|
* C.method_defined? "method4" #=> false
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_mod_method_defined(VALUE mod, VALUE mid)
|
|
|
|
{
|
|
|
|
return rb_method_boundp(mod, rb_to_id(mid), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define VISI_CHECK(x,f) (((x)&NOEX_MASK) == (f))
|
|
|
|
|
2009-07-15 10:59:41 -04:00
|
|
|
static VALUE
|
|
|
|
check_definition(VALUE mod, VALUE mid, rb_method_flag_t noex)
|
|
|
|
{
|
|
|
|
const rb_method_entry_t *me;
|
|
|
|
me = rb_method_entry(mod, mid);
|
|
|
|
if (me) {
|
|
|
|
if (VISI_CHECK(me->flag, noex))
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
* eval_method.c: renamed from vm_method.c. "vm_method.c" is included
by "vm.c".
* vm_eval.c: added. Some codes are moved from "eval.c"
* common.mk: fix for above changes.
* compile.c: make a vm_eval(0)
* eval.c, eval_error.c, eval_intern.h, eval_jump.c, proc.c, vm.c,
id.c, id.h, vm_core.h, vm_dump.c, vm_evalbody.c, vm_insnhelper.c,
blockinlining.c: fix for above changes. and do some refactoring.
this changes improve rb_yield() performance.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-24 13:50:17 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* mod.public_method_defined?(symbol) => true or false
|
|
|
|
*
|
|
|
|
* Returns +true+ if the named public method is defined by
|
|
|
|
* _mod_ (or its included modules and, if _mod_ is a class,
|
|
|
|
* its ancestors).
|
|
|
|
*
|
|
|
|
* module A
|
|
|
|
* def method1() end
|
|
|
|
* end
|
|
|
|
* class B
|
|
|
|
* protected
|
|
|
|
* def method2() end
|
|
|
|
* end
|
|
|
|
* class C < B
|
|
|
|
* include A
|
|
|
|
* def method3() end
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* A.method_defined? :method1 #=> true
|
|
|
|
* C.public_method_defined? "method1" #=> true
|
|
|
|
* C.public_method_defined? "method2" #=> false
|
|
|
|
* C.method_defined? "method2" #=> true
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_mod_public_method_defined(VALUE mod, VALUE mid)
|
|
|
|
{
|
2009-07-15 10:59:41 -04:00
|
|
|
return check_definition(mod, rb_to_id(mid), NOEX_PUBLIC);
|
* eval_method.c: renamed from vm_method.c. "vm_method.c" is included
by "vm.c".
* vm_eval.c: added. Some codes are moved from "eval.c"
* common.mk: fix for above changes.
* compile.c: make a vm_eval(0)
* eval.c, eval_error.c, eval_intern.h, eval_jump.c, proc.c, vm.c,
id.c, id.h, vm_core.h, vm_dump.c, vm_evalbody.c, vm_insnhelper.c,
blockinlining.c: fix for above changes. and do some refactoring.
this changes improve rb_yield() performance.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-24 13:50:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* mod.private_method_defined?(symbol) => true or false
|
|
|
|
*
|
|
|
|
* Returns +true+ if the named private method is defined by
|
|
|
|
* _ mod_ (or its included modules and, if _mod_ is a class,
|
|
|
|
* its ancestors).
|
|
|
|
*
|
|
|
|
* module A
|
|
|
|
* def method1() end
|
|
|
|
* end
|
|
|
|
* class B
|
|
|
|
* private
|
|
|
|
* def method2() end
|
|
|
|
* end
|
|
|
|
* class C < B
|
|
|
|
* include A
|
|
|
|
* def method3() end
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* A.method_defined? :method1 #=> true
|
|
|
|
* C.private_method_defined? "method1" #=> false
|
|
|
|
* C.private_method_defined? "method2" #=> true
|
|
|
|
* C.method_defined? "method2" #=> false
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_mod_private_method_defined(VALUE mod, VALUE mid)
|
|
|
|
{
|
2009-07-15 10:59:41 -04:00
|
|
|
return check_definition(mod, rb_to_id(mid), NOEX_PRIVATE);
|
* eval_method.c: renamed from vm_method.c. "vm_method.c" is included
by "vm.c".
* vm_eval.c: added. Some codes are moved from "eval.c"
* common.mk: fix for above changes.
* compile.c: make a vm_eval(0)
* eval.c, eval_error.c, eval_intern.h, eval_jump.c, proc.c, vm.c,
id.c, id.h, vm_core.h, vm_dump.c, vm_evalbody.c, vm_insnhelper.c,
blockinlining.c: fix for above changes. and do some refactoring.
this changes improve rb_yield() performance.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-24 13:50:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* mod.protected_method_defined?(symbol) => true or false
|
|
|
|
*
|
|
|
|
* Returns +true+ if the named protected method is defined
|
|
|
|
* by _mod_ (or its included modules and, if _mod_ is a
|
|
|
|
* class, its ancestors).
|
|
|
|
*
|
|
|
|
* module A
|
|
|
|
* def method1() end
|
|
|
|
* end
|
|
|
|
* class B
|
|
|
|
* protected
|
|
|
|
* def method2() end
|
|
|
|
* end
|
|
|
|
* class C < B
|
|
|
|
* include A
|
|
|
|
* def method3() end
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* A.method_defined? :method1 #=> true
|
|
|
|
* C.protected_method_defined? "method1" #=> false
|
|
|
|
* C.protected_method_defined? "method2" #=> true
|
|
|
|
* C.method_defined? "method2" #=> true
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_mod_protected_method_defined(VALUE mod, VALUE mid)
|
|
|
|
{
|
2009-07-15 10:59:41 -04:00
|
|
|
return check_definition(mod, rb_to_id(mid), NOEX_PROTECTED);
|
|
|
|
}
|
* eval_method.c: renamed from vm_method.c. "vm_method.c" is included
by "vm.c".
* vm_eval.c: added. Some codes are moved from "eval.c"
* common.mk: fix for above changes.
* compile.c: make a vm_eval(0)
* eval.c, eval_error.c, eval_intern.h, eval_jump.c, proc.c, vm.c,
id.c, id.h, vm_core.h, vm_dump.c, vm_evalbody.c, vm_insnhelper.c,
blockinlining.c: fix for above changes. and do some refactoring.
this changes improve rb_yield() performance.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-24 13:50:17 -04:00
|
|
|
|
2009-07-15 10:59:41 -04:00
|
|
|
static void *
|
|
|
|
me_opts(const rb_method_entry_t *me)
|
|
|
|
{
|
|
|
|
switch (me->type) {
|
|
|
|
case VM_METHOD_TYPE_ISEQ:
|
|
|
|
return me->body.iseq;
|
|
|
|
case VM_METHOD_TYPE_CFUNC:
|
|
|
|
return (void *)&me->body.cfunc;
|
|
|
|
case VM_METHOD_TYPE_ATTRSET:
|
|
|
|
case VM_METHOD_TYPE_IVAR:
|
|
|
|
return (void *)me->body.attr_id;
|
|
|
|
case VM_METHOD_TYPE_BMETHOD:
|
|
|
|
return (void *)me->body.proc;
|
|
|
|
case VM_METHOD_TYPE_ZSUPER:
|
|
|
|
case VM_METHOD_TYPE_NOTIMPLEMENTED:
|
|
|
|
case VM_METHOD_TYPE_UNDEF:
|
|
|
|
return 0;
|
2009-07-18 22:00:27 -04:00
|
|
|
case VM_METHOD_TYPE_OPTIMIZED:
|
|
|
|
return (void *)me->body.optimize_type;
|
2009-07-15 10:59:41 -04:00
|
|
|
default:
|
|
|
|
rb_bug("rb_add_method: unsupported method type (%d)\n", me->type);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_add_method_me(VALUE klass, ID mid, const rb_method_entry_t *me, rb_method_flag_t noex)
|
|
|
|
{
|
|
|
|
rb_add_method(klass, mid, me->type, me_opts(me), noex);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_method_entry_eq(const rb_method_entry_t *m1, const rb_method_entry_t *m2)
|
|
|
|
{
|
|
|
|
if (m1->type != m2->type) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
switch (m1->type) {
|
|
|
|
case VM_METHOD_TYPE_ISEQ:
|
|
|
|
return m1->body.iseq == m2->body.iseq;
|
|
|
|
case VM_METHOD_TYPE_CFUNC:
|
|
|
|
return
|
|
|
|
m1->body.cfunc.func == m2->body.cfunc.func &&
|
|
|
|
m1->body.cfunc.argc == m2->body.cfunc.argc;
|
|
|
|
case VM_METHOD_TYPE_ATTRSET:
|
|
|
|
case VM_METHOD_TYPE_IVAR:
|
|
|
|
return m1->body.attr_id == m2->body.attr_id;
|
|
|
|
case VM_METHOD_TYPE_BMETHOD:
|
|
|
|
return m1->body.proc == m2->body.proc;
|
|
|
|
case VM_METHOD_TYPE_ZSUPER:
|
|
|
|
case VM_METHOD_TYPE_NOTIMPLEMENTED:
|
|
|
|
case VM_METHOD_TYPE_UNDEF:
|
|
|
|
return 1;
|
2009-08-06 02:44:10 -04:00
|
|
|
case VM_METHOD_TYPE_OPTIMIZED:
|
|
|
|
return m1->body.optimize_type == m2->body.optimize_type;
|
2009-07-15 10:59:41 -04:00
|
|
|
default:
|
|
|
|
rb_bug("rb_add_method: unsupported method type (%d)\n", m1->type);
|
|
|
|
return 0;
|
* eval_method.c: renamed from vm_method.c. "vm_method.c" is included
by "vm.c".
* vm_eval.c: added. Some codes are moved from "eval.c"
* common.mk: fix for above changes.
* compile.c: make a vm_eval(0)
* eval.c, eval_error.c, eval_intern.h, eval_jump.c, proc.c, vm.c,
id.c, id.h, vm_core.h, vm_dump.c, vm_evalbody.c, vm_insnhelper.c,
blockinlining.c: fix for above changes. and do some refactoring.
this changes improve rb_yield() performance.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-24 13:50:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
void
|
|
|
|
rb_alias(VALUE klass, ID name, ID def)
|
|
|
|
{
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_method_entry_t *orig_me, *me;
|
2006-12-31 10:02:22 -05:00
|
|
|
VALUE singleton = 0;
|
|
|
|
|
|
|
|
rb_frozen_class_p(klass);
|
|
|
|
if (klass == rb_cObject) {
|
|
|
|
rb_secure(4);
|
|
|
|
}
|
2009-07-15 10:59:41 -04:00
|
|
|
|
|
|
|
orig_me = search_method(klass, def);
|
|
|
|
|
|
|
|
if (!orig_me || orig_me->type == VM_METHOD_TYPE_UNDEF) {
|
2006-12-31 10:02:22 -05:00
|
|
|
if (TYPE(klass) == T_MODULE) {
|
2009-07-15 10:59:41 -04:00
|
|
|
orig_me = search_method(rb_cObject, def);
|
2009-07-16 04:44:23 -04:00
|
|
|
}
|
|
|
|
if (!orig_me || !orig_me->type == VM_METHOD_TYPE_UNDEF) {
|
|
|
|
rb_print_undef(klass, def, 0);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (FL_TEST(klass, FL_SINGLETON)) {
|
|
|
|
singleton = rb_iv_get(klass, "__attached__");
|
|
|
|
}
|
|
|
|
|
2009-07-15 10:59:41 -04:00
|
|
|
orig_me->alias_count++;
|
|
|
|
me = rb_add_method(klass, name, orig_me->type, me_opts(orig_me), orig_me->flag);
|
|
|
|
me->original_id = def;
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2009-07-15 10:59:41 -04:00
|
|
|
if (!ruby_running) return;
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
rb_clear_cache_by_id(name);
|
|
|
|
|
|
|
|
if (singleton) {
|
|
|
|
rb_funcall(singleton, singleton_added, 1, ID2SYM(name));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_funcall(klass, added, 1, ID2SYM(name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* alias_method(new_name, old_name) => self
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 00:25:10 -04:00
|
|
|
*
|
2006-12-31 10:02:22 -05:00
|
|
|
* Makes <i>new_name</i> a new copy of the method <i>old_name</i>. This can
|
|
|
|
* be used to retain access to methods that are overridden.
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 00:25:10 -04:00
|
|
|
*
|
2006-12-31 10:02:22 -05:00
|
|
|
* module Mod
|
|
|
|
* alias_method :orig_exit, :exit
|
|
|
|
* def exit(code=0)
|
|
|
|
* puts "Exiting with code #{code}"
|
|
|
|
* orig_exit(code)
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
* include Mod
|
|
|
|
* exit(99)
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 00:25:10 -04:00
|
|
|
*
|
2006-12-31 10:02:22 -05:00
|
|
|
* <em>produces:</em>
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 00:25:10 -04:00
|
|
|
*
|
2006-12-31 10:02:22 -05:00
|
|
|
* Exiting with code 99
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_mod_alias_method(VALUE mod, VALUE newname, VALUE oldname)
|
|
|
|
{
|
|
|
|
rb_alias(mod, rb_to_id(newname), rb_to_id(oldname));
|
|
|
|
return mod;
|
|
|
|
}
|
2007-12-24 04:07:20 -05:00
|
|
|
|
|
|
|
static void
|
* eval_method.c: renamed from vm_method.c. "vm_method.c" is included
by "vm.c".
* vm_eval.c: added. Some codes are moved from "eval.c"
* common.mk: fix for above changes.
* compile.c: make a vm_eval(0)
* eval.c, eval_error.c, eval_intern.h, eval_jump.c, proc.c, vm.c,
id.c, id.h, vm_core.h, vm_dump.c, vm_evalbody.c, vm_insnhelper.c,
blockinlining.c: fix for above changes. and do some refactoring.
this changes improve rb_yield() performance.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-24 13:50:17 -04:00
|
|
|
secure_visibility(VALUE self)
|
|
|
|
{
|
* object.c (rb_obj_untrusted): new method Object#untrusted?.
(rb_obj_untrust): new method Object#untrust.
(rb_obj_trust): new method Object#trust.
* array.c, debug.c, time.c, include/ruby/ruby.h, re.c, variable.c,
string.c, io.c, dir.c, vm_method.c, struct.c, class.c, hash.c,
ruby.c, marshal.c: fixes for Object#untrusted?.
* test/ruby/test_module.rb, test/ruby/test_array.rb,
test/ruby/test_object.rb, test/ruby/test_string.rb,
test/ruby/test_marshal.rb, test/ruby/test_hash.rb: added tests for
Object#untrusted?.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18568 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-08-13 03:25:05 -04:00
|
|
|
if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(self)) {
|
* eval_method.c: renamed from vm_method.c. "vm_method.c" is included
by "vm.c".
* vm_eval.c: added. Some codes are moved from "eval.c"
* common.mk: fix for above changes.
* compile.c: make a vm_eval(0)
* eval.c, eval_error.c, eval_intern.h, eval_jump.c, proc.c, vm.c,
id.c, id.h, vm_core.h, vm_dump.c, vm_evalbody.c, vm_insnhelper.c,
blockinlining.c: fix for above changes. and do some refactoring.
this changes improve rb_yield() performance.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-24 13:50:17 -04:00
|
|
|
rb_raise(rb_eSecurityError,
|
|
|
|
"Insecure: can't change method visibility");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_method_visibility(VALUE self, int argc, VALUE *argv, ID ex)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
secure_visibility(self);
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
rb_export_method(self, rb_to_id(argv[i]), ex);
|
|
|
|
}
|
|
|
|
rb_clear_cache_by_class(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* public => self
|
|
|
|
* public(symbol, ...) => self
|
|
|
|
*
|
|
|
|
* With no arguments, sets the default visibility for subsequently
|
|
|
|
* defined methods to public. With arguments, sets the named methods to
|
|
|
|
* have public visibility.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_mod_public(int argc, VALUE *argv, VALUE module)
|
|
|
|
{
|
|
|
|
secure_visibility(module);
|
|
|
|
if (argc == 0) {
|
|
|
|
SCOPE_SET(NOEX_PUBLIC);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
set_method_visibility(module, argc, argv, NOEX_PUBLIC);
|
|
|
|
}
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* protected => self
|
|
|
|
* protected(symbol, ...) => self
|
|
|
|
*
|
|
|
|
* With no arguments, sets the default visibility for subsequently
|
|
|
|
* defined methods to protected. With arguments, sets the named methods
|
|
|
|
* to have protected visibility.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_mod_protected(int argc, VALUE *argv, VALUE module)
|
|
|
|
{
|
|
|
|
secure_visibility(module);
|
|
|
|
if (argc == 0) {
|
|
|
|
SCOPE_SET(NOEX_PROTECTED);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
set_method_visibility(module, argc, argv, NOEX_PROTECTED);
|
|
|
|
}
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* private => self
|
|
|
|
* private(symbol, ...) => self
|
|
|
|
*
|
|
|
|
* With no arguments, sets the default visibility for subsequently
|
|
|
|
* defined methods to private. With arguments, sets the named methods
|
|
|
|
* to have private visibility.
|
|
|
|
*
|
|
|
|
* module Mod
|
|
|
|
* def a() end
|
|
|
|
* def b() end
|
|
|
|
* private
|
|
|
|
* def c() end
|
|
|
|
* private :a
|
|
|
|
* end
|
|
|
|
* Mod.private_instance_methods #=> [:a, :c]
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_mod_private(int argc, VALUE *argv, VALUE module)
|
|
|
|
{
|
|
|
|
secure_visibility(module);
|
|
|
|
if (argc == 0) {
|
|
|
|
SCOPE_SET(NOEX_PRIVATE);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
set_method_visibility(module, argc, argv, NOEX_PRIVATE);
|
|
|
|
}
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* mod.public_class_method(symbol, ...) => mod
|
|
|
|
*
|
|
|
|
* Makes a list of existing class methods public.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_mod_public_method(int argc, VALUE *argv, VALUE obj)
|
|
|
|
{
|
|
|
|
set_method_visibility(CLASS_OF(obj), argc, argv, NOEX_PUBLIC);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* mod.private_class_method(symbol, ...) => mod
|
|
|
|
*
|
|
|
|
* Makes existing class methods private. Often used to hide the default
|
|
|
|
* constructor <code>new</code>.
|
|
|
|
*
|
|
|
|
* class SimpleSingleton # Not thread safe
|
|
|
|
* private_class_method :new
|
|
|
|
* def SimpleSingleton.create(*args, &block)
|
|
|
|
* @me = new(*args, &block) if ! @me
|
|
|
|
* @me
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_mod_private_method(int argc, VALUE *argv, VALUE obj)
|
|
|
|
{
|
|
|
|
set_method_visibility(CLASS_OF(obj), argc, argv, NOEX_PRIVATE);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* public
|
|
|
|
* public(symbol, ...)
|
|
|
|
*
|
|
|
|
* With no arguments, sets the default visibility for subsequently
|
|
|
|
* defined methods to public. With arguments, sets the named methods to
|
|
|
|
* have public visibility.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
top_public(int argc, VALUE *argv)
|
|
|
|
{
|
|
|
|
return rb_mod_public(argc, argv, rb_cObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
top_private(int argc, VALUE *argv)
|
|
|
|
{
|
|
|
|
return rb_mod_private(argc, argv, rb_cObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* module_function(symbol, ...) => self
|
|
|
|
*
|
|
|
|
* Creates module functions for the named methods. These functions may
|
|
|
|
* be called with the module as a receiver, and also become available
|
|
|
|
* as instance methods to classes that mix in the module. Module
|
|
|
|
* functions are copies of the original, and so may be changed
|
|
|
|
* independently. The instance-method versions are made private. If
|
|
|
|
* used with no arguments, subsequently defined methods become module
|
|
|
|
* functions.
|
|
|
|
*
|
|
|
|
* module Mod
|
|
|
|
* def one
|
|
|
|
* "This is one"
|
|
|
|
* end
|
|
|
|
* module_function :one
|
|
|
|
* end
|
|
|
|
* class Cls
|
|
|
|
* include Mod
|
|
|
|
* def callOne
|
|
|
|
* one
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
* Mod.one #=> "This is one"
|
|
|
|
* c = Cls.new
|
|
|
|
* c.callOne #=> "This is one"
|
|
|
|
* module Mod
|
|
|
|
* def one
|
|
|
|
* "This is the new one"
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
* Mod.one #=> "This is one"
|
|
|
|
* c.callOne #=> "This is the new one"
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_mod_modfunc(int argc, VALUE *argv, VALUE module)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
ID id;
|
2009-07-15 10:59:41 -04:00
|
|
|
const rb_method_entry_t *me;
|
* eval_method.c: renamed from vm_method.c. "vm_method.c" is included
by "vm.c".
* vm_eval.c: added. Some codes are moved from "eval.c"
* common.mk: fix for above changes.
* compile.c: make a vm_eval(0)
* eval.c, eval_error.c, eval_intern.h, eval_jump.c, proc.c, vm.c,
id.c, id.h, vm_core.h, vm_dump.c, vm_evalbody.c, vm_insnhelper.c,
blockinlining.c: fix for above changes. and do some refactoring.
this changes improve rb_yield() performance.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-24 13:50:17 -04:00
|
|
|
|
|
|
|
if (TYPE(module) != T_MODULE) {
|
|
|
|
rb_raise(rb_eTypeError, "module_function must be called for modules");
|
|
|
|
}
|
|
|
|
|
|
|
|
secure_visibility(module);
|
|
|
|
if (argc == 0) {
|
|
|
|
SCOPE_SET(NOEX_MODFUNC);
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_method_visibility(module, argc, argv, NOEX_PRIVATE);
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
VALUE m = module;
|
|
|
|
|
|
|
|
id = rb_to_id(argv[i]);
|
|
|
|
for (;;) {
|
2009-07-15 10:59:41 -04:00
|
|
|
me = search_method(m, id);
|
|
|
|
if (me == 0) {
|
|
|
|
me = search_method(rb_cObject, id);
|
* eval_method.c: renamed from vm_method.c. "vm_method.c" is included
by "vm.c".
* vm_eval.c: added. Some codes are moved from "eval.c"
* common.mk: fix for above changes.
* compile.c: make a vm_eval(0)
* eval.c, eval_error.c, eval_intern.h, eval_jump.c, proc.c, vm.c,
id.c, id.h, vm_core.h, vm_dump.c, vm_evalbody.c, vm_insnhelper.c,
blockinlining.c: fix for above changes. and do some refactoring.
this changes improve rb_yield() performance.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-24 13:50:17 -04:00
|
|
|
}
|
2009-07-15 10:59:41 -04:00
|
|
|
if (me == 0 || me->type == VM_METHOD_TYPE_UNDEF) {
|
2008-10-05 10:07:06 -04:00
|
|
|
rb_print_undef(module, id, 0);
|
* eval_method.c: renamed from vm_method.c. "vm_method.c" is included
by "vm.c".
* vm_eval.c: added. Some codes are moved from "eval.c"
* common.mk: fix for above changes.
* compile.c: make a vm_eval(0)
* eval.c, eval_error.c, eval_intern.h, eval_jump.c, proc.c, vm.c,
id.c, id.h, vm_core.h, vm_dump.c, vm_evalbody.c, vm_insnhelper.c,
blockinlining.c: fix for above changes. and do some refactoring.
this changes improve rb_yield() performance.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-24 13:50:17 -04:00
|
|
|
}
|
2009-07-15 10:59:41 -04:00
|
|
|
if (me->type != VM_METHOD_TYPE_ZSUPER) {
|
|
|
|
break; /* normal case: need not to follow 'super' link */
|
* eval_method.c: renamed from vm_method.c. "vm_method.c" is included
by "vm.c".
* vm_eval.c: added. Some codes are moved from "eval.c"
* common.mk: fix for above changes.
* compile.c: make a vm_eval(0)
* eval.c, eval_error.c, eval_intern.h, eval_jump.c, proc.c, vm.c,
id.c, id.h, vm_core.h, vm_dump.c, vm_evalbody.c, vm_insnhelper.c,
blockinlining.c: fix for above changes. and do some refactoring.
this changes improve rb_yield() performance.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-24 13:50:17 -04:00
|
|
|
}
|
|
|
|
m = RCLASS_SUPER(m);
|
|
|
|
if (!m)
|
|
|
|
break;
|
|
|
|
}
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_add_method_me(rb_singleton_class(module), id, me, NOEX_PUBLIC);
|
* eval_method.c: renamed from vm_method.c. "vm_method.c" is included
by "vm.c".
* vm_eval.c: added. Some codes are moved from "eval.c"
* common.mk: fix for above changes.
* compile.c: make a vm_eval(0)
* eval.c, eval_error.c, eval_intern.h, eval_jump.c, proc.c, vm.c,
id.c, id.h, vm_core.h, vm_dump.c, vm_evalbody.c, vm_insnhelper.c,
blockinlining.c: fix for above changes. and do some refactoring.
this changes improve rb_yield() performance.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-24 13:50:17 -04:00
|
|
|
}
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
2008-08-04 14:29:55 -04:00
|
|
|
int
|
|
|
|
rb_method_basic_definition_p(VALUE klass, ID id)
|
|
|
|
{
|
2009-07-15 10:59:41 -04:00
|
|
|
const rb_method_entry_t *me = rb_method_entry(klass, id);
|
|
|
|
if (me && (me->flag & NOEX_BASIC))
|
2008-08-04 14:29:55 -04:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
* eval_method.c: renamed from vm_method.c. "vm_method.c" is included
by "vm.c".
* vm_eval.c: added. Some codes are moved from "eval.c"
* common.mk: fix for above changes.
* compile.c: make a vm_eval(0)
* eval.c, eval_error.c, eval_intern.h, eval_jump.c, proc.c, vm.c,
id.c, id.h, vm_core.h, vm_dump.c, vm_evalbody.c, vm_insnhelper.c,
blockinlining.c: fix for above changes. and do some refactoring.
this changes improve rb_yield() performance.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-24 13:50:17 -04:00
|
|
|
int
|
|
|
|
rb_obj_respond_to(VALUE obj, ID id, int priv)
|
|
|
|
{
|
|
|
|
VALUE klass = CLASS_OF(obj);
|
|
|
|
|
2008-08-04 14:29:55 -04:00
|
|
|
if (rb_method_basic_definition_p(klass, idRespond_to)) {
|
* eval_method.c: renamed from vm_method.c. "vm_method.c" is included
by "vm.c".
* vm_eval.c: added. Some codes are moved from "eval.c"
* common.mk: fix for above changes.
* compile.c: make a vm_eval(0)
* eval.c, eval_error.c, eval_intern.h, eval_jump.c, proc.c, vm.c,
id.c, id.h, vm_core.h, vm_dump.c, vm_evalbody.c, vm_insnhelper.c,
blockinlining.c: fix for above changes. and do some refactoring.
this changes improve rb_yield() performance.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-24 13:50:17 -04:00
|
|
|
return rb_method_boundp(klass, id, !priv);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VALUE args[2];
|
|
|
|
int n = 0;
|
|
|
|
args[n++] = ID2SYM(id);
|
|
|
|
if (priv)
|
|
|
|
args[n++] = Qtrue;
|
|
|
|
return RTEST(rb_funcall2(obj, idRespond_to, n, args));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_respond_to(VALUE obj, ID id)
|
|
|
|
{
|
|
|
|
return rb_obj_respond_to(obj, id, Qfalse);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* obj.respond_to?(symbol, include_private=false) => true or false
|
|
|
|
*
|
2008-12-03 21:39:55 -05:00
|
|
|
* Returns +true+ if _obj_ responds to the given
|
* eval_method.c: renamed from vm_method.c. "vm_method.c" is included
by "vm.c".
* vm_eval.c: added. Some codes are moved from "eval.c"
* common.mk: fix for above changes.
* compile.c: make a vm_eval(0)
* eval.c, eval_error.c, eval_intern.h, eval_jump.c, proc.c, vm.c,
id.c, id.h, vm_core.h, vm_dump.c, vm_evalbody.c, vm_insnhelper.c,
blockinlining.c: fix for above changes. and do some refactoring.
this changes improve rb_yield() performance.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-24 13:50:17 -04:00
|
|
|
* method. Private methods are included in the search only if the
|
|
|
|
* optional second parameter evaluates to +true+.
|
2009-04-21 14:41:07 -04:00
|
|
|
*
|
|
|
|
* If the method is not implemented,
|
|
|
|
* as Process.fork on Windows, File.lchmod on GNU/Linux, etc.,
|
|
|
|
* false is returned.
|
* eval_method.c: renamed from vm_method.c. "vm_method.c" is included
by "vm.c".
* vm_eval.c: added. Some codes are moved from "eval.c"
* common.mk: fix for above changes.
* compile.c: make a vm_eval(0)
* eval.c, eval_error.c, eval_intern.h, eval_jump.c, proc.c, vm.c,
id.c, id.h, vm_core.h, vm_dump.c, vm_evalbody.c, vm_insnhelper.c,
blockinlining.c: fix for above changes. and do some refactoring.
this changes improve rb_yield() performance.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-24 13:50:17 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
obj_respond_to(int argc, VALUE *argv, VALUE obj)
|
|
|
|
{
|
|
|
|
VALUE mid, priv;
|
|
|
|
ID id;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &mid, &priv);
|
|
|
|
id = rb_to_id(mid);
|
|
|
|
if (rb_method_boundp(CLASS_OF(obj), id, !RTEST(priv))) {
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-12-24 04:07:20 -05:00
|
|
|
Init_eval_method(void)
|
|
|
|
{
|
2008-06-09 05:25:32 -04:00
|
|
|
#undef rb_intern
|
2008-08-15 20:20:31 -04:00
|
|
|
#define rb_intern(str) rb_intern_const(str)
|
2008-06-09 05:25:32 -04:00
|
|
|
|
* eval_method.c: renamed from vm_method.c. "vm_method.c" is included
by "vm.c".
* vm_eval.c: added. Some codes are moved from "eval.c"
* common.mk: fix for above changes.
* compile.c: make a vm_eval(0)
* eval.c, eval_error.c, eval_intern.h, eval_jump.c, proc.c, vm.c,
id.c, id.h, vm_core.h, vm_dump.c, vm_evalbody.c, vm_insnhelper.c,
blockinlining.c: fix for above changes. and do some refactoring.
this changes improve rb_yield() performance.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-24 13:50:17 -04:00
|
|
|
rb_define_method(rb_mKernel, "respond_to?", obj_respond_to, -1);
|
|
|
|
|
2007-12-24 04:07:20 -05:00
|
|
|
rb_define_private_method(rb_cModule, "remove_method", rb_mod_remove_method, -1);
|
|
|
|
rb_define_private_method(rb_cModule, "undef_method", rb_mod_undef_method, -1);
|
|
|
|
rb_define_private_method(rb_cModule, "alias_method", rb_mod_alias_method, 2);
|
* eval_method.c: renamed from vm_method.c. "vm_method.c" is included
by "vm.c".
* vm_eval.c: added. Some codes are moved from "eval.c"
* common.mk: fix for above changes.
* compile.c: make a vm_eval(0)
* eval.c, eval_error.c, eval_intern.h, eval_jump.c, proc.c, vm.c,
id.c, id.h, vm_core.h, vm_dump.c, vm_evalbody.c, vm_insnhelper.c,
blockinlining.c: fix for above changes. and do some refactoring.
this changes improve rb_yield() performance.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-24 13:50:17 -04:00
|
|
|
rb_define_private_method(rb_cModule, "public", rb_mod_public, -1);
|
|
|
|
rb_define_private_method(rb_cModule, "protected", rb_mod_protected, -1);
|
|
|
|
rb_define_private_method(rb_cModule, "private", rb_mod_private, -1);
|
|
|
|
rb_define_private_method(rb_cModule, "module_function", rb_mod_modfunc, -1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cModule, "method_defined?", rb_mod_method_defined, 1);
|
|
|
|
rb_define_method(rb_cModule, "public_method_defined?", rb_mod_public_method_defined, 1);
|
|
|
|
rb_define_method(rb_cModule, "private_method_defined?", rb_mod_private_method_defined, 1);
|
|
|
|
rb_define_method(rb_cModule, "protected_method_defined?", rb_mod_protected_method_defined, 1);
|
|
|
|
rb_define_method(rb_cModule, "public_class_method", rb_mod_public_method, -1);
|
|
|
|
rb_define_method(rb_cModule, "private_class_method", rb_mod_private_method, -1);
|
|
|
|
|
|
|
|
rb_define_singleton_method(rb_vm_top_self(), "public", top_public, -1);
|
|
|
|
rb_define_singleton_method(rb_vm_top_self(), "private", top_private, -1);
|
|
|
|
|
|
|
|
object_id = rb_intern("object_id");
|
|
|
|
added = rb_intern("method_added");
|
|
|
|
singleton_added = rb_intern("singleton_method_added");
|
|
|
|
removed = rb_intern("method_removed");
|
|
|
|
singleton_removed = rb_intern("singleton_method_removed");
|
|
|
|
undefined = rb_intern("method_undefined");
|
|
|
|
singleton_undefined = rb_intern("singleton_method_undefined");
|
2007-12-24 04:07:20 -05:00
|
|
|
}
|
* eval_method.c: renamed from vm_method.c. "vm_method.c" is included
by "vm.c".
* vm_eval.c: added. Some codes are moved from "eval.c"
* common.mk: fix for above changes.
* compile.c: make a vm_eval(0)
* eval.c, eval_error.c, eval_intern.h, eval_jump.c, proc.c, vm.c,
id.c, id.h, vm_core.h, vm_dump.c, vm_evalbody.c, vm_insnhelper.c,
blockinlining.c: fix for above changes. and do some refactoring.
this changes improve rb_yield() performance.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-24 13:50:17 -04:00
|
|
|
|