Project

General

Profile

Feature #3908 ยป 0003-add-Module-public_constant-and-private_constant.patch

mame (Yusuke Endoh), 10/06/2010 12:19 AM

View differences:

object.c
2656 2656
    rb_define_method(rb_cModule, "class_variable_get", rb_mod_cvar_get, 1);
2657 2657
    rb_define_method(rb_cModule, "class_variable_set", rb_mod_cvar_set, 2);
2658 2658
    rb_define_method(rb_cModule, "class_variable_defined?", rb_mod_cvar_defined, 1);
2659
    rb_define_method(rb_cModule, "public_constant", rb_mod_public_constant, -1);
2660
    rb_define_method(rb_cModule, "private_constant", rb_mod_private_constant, -1);
2659 2661

  
2660 2662
    rb_define_method(rb_cClass, "allocate", rb_obj_alloc, 0);
2661 2663
    rb_define_method(rb_cClass, "new", rb_class_new_instance, -1);
variable.c
1585 1585
    while (RTEST(tmp)) {
1586 1586
	VALUE am = 0;
1587 1587
	while (RCLASS_CONST_TBL(tmp) && st_lookup(RCLASS_CONST_TBL(tmp), (st_data_t)id, &v)) {
1588
	    value = ((rb_const_entry_t*)v)->value;
1588
	    rb_const_entry_t *ce = (rb_const_entry_t *)v;
1589
	    value = ce->value;
1589 1590
	    if (value == Qundef) {
1590 1591
		if (am == tmp) break;
1591 1592
		am = tmp;
......
1596 1597
		rb_warn("toplevel constant %s referenced by %s::%s",
1597 1598
			rb_id2name(id), rb_class2name(klass), rb_id2name(id));
1598 1599
	    }
1600
	    if (ce->flag == CONST_PRIVATE) {
1601
		rb_name_error(id, "private constant %s::%s referenced", rb_class2name(klass), rb_id2name(id));
1602
	    }
1599 1603
	    return value;
1600 1604
	}
1601 1605
	if (!recurse && klass != rb_cObject) break;
......
1887 1891
    rb_define_const(rb_cObject, name, val);
1888 1892
}
1889 1893

  
1894
static void
1895
set_const_visibility(VALUE mod, int argc, VALUE *argv, rb_const_flag_t flag)
1896
{
1897
    int i;
1898
    st_data_t v;
1899
    ID id;
1900

  
1901
    if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(mod)) {
1902
	rb_raise(rb_eSecurityError,
1903
		 "Insecure: can't change method visibility");
1904
    }
1905

  
1906
    for (i = 0; i < argc; i++) {
1907
	id = rb_to_id(argv[i]);
1908
	if (RCLASS_CONST_TBL(mod) && st_lookup(RCLASS_CONST_TBL(mod), (st_data_t)id, &v)) {
1909
	    ((rb_const_entry_t*)v)->flag = flag;
1910
	    return;
1911
	}
1912
	rb_name_error(id, "constant %s::%s not defined", rb_class2name(mod), rb_id2name(id));
1913
    }
1914
    rb_clear_cache_by_class(mod);
1915
}
1916

  
1917
/*
1918
 *  call-seq:
1919
 *     mod.private_constant(symbol, ...)    => mod
1920
 *
1921
 *  Makes a list of existing constants private.
1922
 */
1923

  
1924
VALUE
1925
rb_mod_private_constant(int argc, VALUE *argv, VALUE obj)
1926
{
1927
    set_const_visibility(obj, argc, argv, CONST_PRIVATE);
1928
    return obj;
1929
}
1930

  
1931
/*
1932
 *  call-seq:
1933
 *     mod.public_constant(symbol, ...)    => mod
1934
 *
1935
 *  Makes a list of existing constants public.
1936
 */
1937

  
1938
VALUE
1939
rb_mod_public_constant(int argc, VALUE *argv, VALUE obj)
1940
{
1941
    set_const_visibility(obj, argc, argv, CONST_PUBLIC);
1942
    return obj;
1943
}
1944

  
1890 1945
static VALUE
1891 1946
original_module(VALUE c)
1892 1947
{
1893
-