0001-inherited-flag.patch

Nobuyoshi Nakada, 04/01/2012 09:01 AM

Download (8.69 KB)

View differences:

test/ruby/test_module.rb
12 12
  def assert_method_defined?(klass, mid, message="")
13 13
    message = build_message(message, "#{klass}\##{mid} expected to be defined.")
14 14
    _wrap_assertion do
15
      klass.method_defined?(mid) or
15
      klass.method_defined?(*mid) or
16 16
        raise Test::Unit::AssertionFailedError, message, caller(3)
17 17
    end
18 18
  end
......
20 20
  def assert_method_not_defined?(klass, mid, message="")
21 21
    message = build_message(message, "#{klass}\##{mid} expected to not be defined.")
22 22
    _wrap_assertion do
23
      klass.method_defined?(mid) and
23
      klass.method_defined?(*mid) and
24 24
        raise Test::Unit::AssertionFailedError, message, caller(3)
25 25
    end
26 26
  end
......
289 289
    assert_method_not_defined?(User, :wombat)
290 290
    assert_method_defined?(User, :user)
291 291
    assert_method_defined?(User, :mixin)
292
    assert_method_not_defined?(User, :wombat)
293
    assert_method_defined?(User, :user)
294
    assert_method_defined?(User, :mixin)
292
    assert_method_not_defined?(User, [:wombat, false])
293
    assert_method_defined?(User, [:user, false])
294
    assert_method_not_defined?(User, [:mixin, false])
295 295
  end
296 296

  
297 297
  def module_exec_aux
......
705 705
    assert_equal(false, c.private_method_defined?(:foo))
706 706
    assert_equal(false, c.private_method_defined?(:bar))
707 707
    assert_equal(true, c.private_method_defined?(:baz))
708

  
709
    c = Class.new(c)
710

  
711
    assert_equal(true, c.public_method_defined?(:foo))
712
    assert_equal(false, c.public_method_defined?(:bar))
713
    assert_equal(false, c.public_method_defined?(:baz))
714

  
715
    assert_equal(false, c.protected_method_defined?(:foo))
716
    assert_equal(true, c.protected_method_defined?(:bar))
717
    assert_equal(false, c.protected_method_defined?(:baz))
718

  
719
    assert_equal(false, c.private_method_defined?(:foo))
720
    assert_equal(false, c.private_method_defined?(:bar))
721
    assert_equal(true, c.private_method_defined?(:baz))
722

  
723
    assert_equal(true, c.public_method_defined?(:foo, true))
724
    assert_equal(false, c.public_method_defined?(:bar, true))
725
    assert_equal(false, c.public_method_defined?(:baz, true))
726

  
727
    assert_equal(false, c.protected_method_defined?(:foo, true))
728
    assert_equal(true, c.protected_method_defined?(:bar, true))
729
    assert_equal(false, c.protected_method_defined?(:baz, true))
730

  
731
    assert_equal(false, c.private_method_defined?(:foo, true))
732
    assert_equal(false, c.private_method_defined?(:bar, true))
733
    assert_equal(true, c.private_method_defined?(:baz, true))
734

  
735
    assert_equal(false, c.public_method_defined?(:foo, false))
736
    assert_equal(false, c.public_method_defined?(:bar, false))
737
    assert_equal(false, c.public_method_defined?(:baz, false))
738

  
739
    assert_equal(false, c.protected_method_defined?(:foo, false))
740
    assert_equal(false, c.protected_method_defined?(:bar, false))
741
    assert_equal(false, c.protected_method_defined?(:baz, false))
742

  
743
    assert_equal(false, c.private_method_defined?(:foo, false))
744
    assert_equal(false, c.private_method_defined?(:bar, false))
745
    assert_equal(false, c.private_method_defined?(:baz, false))
708 746
  end
709 747

  
710 748
  def test_change_visibility_under_safe4
vm_method.c
433 433
    return rb_method_entry_get_without_cache(klass, id);
434 434
}
435 435

  
436
rb_method_entry_t *
437
rb_method_entry_at(VALUE klass, ID id)
438
{
439
    st_data_t body;
440
    if (!klass || !st_lookup(RCLASS_M_TBL(klass), id, &body)) {
441
	return 0;
442
    }
443
    return (rb_method_entry_t *)body;
444
}
445

  
436 446
static void
437 447
remove_method(VALUE klass, ID mid)
438 448
{
......
548 558
    }
549 559
}
550 560

  
551
int
552
rb_method_boundp(VALUE klass, ID id, int ex)
561
static int
562
method_accessible_p(rb_method_entry_t *me, int ex)
553 563
{
554
    rb_method_entry_t *me = rb_method_entry(klass, id);
555

  
556 564
    if (me != 0) {
557 565
	if ((ex & ~NOEX_RESPONDS) &&
558 566
	    ((me->flag & NOEX_PRIVATE) ||
......
569 577
    return 0;
570 578
}
571 579

  
580
int
581
rb_method_bound_at(VALUE klass, ID id, int ex)
582
{
583
    return method_accessible_p(rb_method_entry_at(klass, id), ex);
584
}
585

  
586
int
587
rb_method_boundp(VALUE klass, ID id, int ex)
588
{
589
    return method_accessible_p(rb_method_entry(klass, id), ex);
590
}
591

  
592
static int
593
method_boundp(VALUE klass, ID id, int ex, int inherited)
594
{
595
    if (inherited)
596
	return rb_method_boundp(klass, id, ex);
597
    else
598
	return rb_method_bound_at(klass, id, ex);
599
}
600

  
572 601
void
573 602
rb_attr(VALUE klass, ID id, int read, int write, int ex)
574 603
{
......
745 774
 */
746 775

  
747 776
static VALUE
748
rb_mod_method_defined(VALUE mod, VALUE mid)
777
rb_mod_method_defined(int argc, VALUE *argv, VALUE mod)
749 778
{
750
    ID id = rb_check_id(&mid);
751
    if (!id || !rb_method_boundp(mod, id, 1)) {
779
    VALUE mid;
780
    ID id;
781

  
782
    rb_check_arity(argc, 1, 2);
783
    mid = argv[0];
784
    id = rb_check_id(&mid);
785
    if (!id || !method_boundp(mod, id, 1, argc == 1 || RTEST(argv[1]))) {
752 786
	return Qfalse;
753 787
    }
754 788
    return Qtrue;
......
758 792
#define VISI_CHECK(x,f) (((x)&NOEX_MASK) == (f))
759 793

  
760 794
static VALUE
761
check_definition(VALUE mod, ID mid, rb_method_flag_t noex)
795
check_definition(VALUE mod, int argc, VALUE *argv, rb_method_flag_t noex)
762 796
{
763 797
    const rb_method_entry_t *me;
764
    me = rb_method_entry(mod, mid);
798
    VALUE mid;
799
    ID id;
800

  
801
    rb_check_arity(argc, 1, 2);
802
    mid = argv[0];
803
    id = rb_check_id(&mid);
804
    if (!id) return Qfalse;
805
    if (argc == 1 || RTEST(argv[1])) {
806
	me = rb_method_entry(mod, id);
807
    }
808
    else {
809
	me = rb_method_entry_at(mod, id);
810
    }
765 811
    if (me) {
766 812
	if (VISI_CHECK(me->flag, noex))
767 813
	    return Qtrue;
......
796 842
 */
797 843

  
798 844
static VALUE
799
rb_mod_public_method_defined(VALUE mod, VALUE mid)
845
rb_mod_public_method_defined(int argc, VALUE *argv, VALUE mod)
800 846
{
801
    ID id = rb_check_id(&mid);
802
    if (!id) return Qfalse;
803
    return check_definition(mod, id, NOEX_PUBLIC);
847
    return check_definition(mod, argc, argv, NOEX_PUBLIC);
804 848
}
805 849

  
806 850
/*
......
830 874
 */
831 875

  
832 876
static VALUE
833
rb_mod_private_method_defined(VALUE mod, VALUE mid)
877
rb_mod_private_method_defined(int argc, VALUE *argv, VALUE mod)
834 878
{
835
    ID id = rb_check_id(&mid);
836
    if (!id) return Qfalse;
837
    return check_definition(mod, id, NOEX_PRIVATE);
879
    return check_definition(mod, argc, argv, NOEX_PRIVATE);
838 880
}
839 881

  
840 882
/*
......
864 906
 */
865 907

  
866 908
static VALUE
867
rb_mod_protected_method_defined(VALUE mod, VALUE mid)
909
rb_mod_protected_method_defined(int argc, VALUE *argv, VALUE mod)
868 910
{
869
    ID id = rb_check_id(&mid);
870
    if (!id) return Qfalse;
871
    return check_definition(mod, id, NOEX_PROTECTED);
911
    return check_definition(mod, argc, argv, NOEX_PROTECTED);
872 912
}
873 913

  
874 914
int
......
1374 1414
    rb_define_private_method(rb_cModule, "private", rb_mod_private, -1);
1375 1415
    rb_define_private_method(rb_cModule, "module_function", rb_mod_modfunc, -1);
1376 1416

  
1377
    rb_define_method(rb_cModule, "method_defined?", rb_mod_method_defined, 1);
1378
    rb_define_method(rb_cModule, "public_method_defined?", rb_mod_public_method_defined, 1);
1379
    rb_define_method(rb_cModule, "private_method_defined?", rb_mod_private_method_defined, 1);
1380
    rb_define_method(rb_cModule, "protected_method_defined?", rb_mod_protected_method_defined, 1);
1417
    rb_define_method(rb_cModule, "method_defined?", rb_mod_method_defined, -1);
1418
    rb_define_method(rb_cModule, "public_method_defined?", rb_mod_public_method_defined, -1);
1419
    rb_define_method(rb_cModule, "private_method_defined?", rb_mod_private_method_defined, -1);
1420
    rb_define_method(rb_cModule, "protected_method_defined?", rb_mod_protected_method_defined, -1);
1381 1421
    rb_define_method(rb_cModule, "public_class_method", rb_mod_public_method, -1);
1382 1422
    rb_define_method(rb_cModule, "private_class_method", rb_mod_private_method, -1);
1383 1423

  
1384
-