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

protected methods

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/v1_1r@140 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 1998-03-26 10:22:14 +00:00
parent fce5e4b1bc
commit c88c96361d
7 changed files with 74 additions and 39 deletions

View file

@ -1,5 +1,7 @@
Thu Mar 26 11:51:09 1998 Yukihiro Matsumoto <matz@netlab.co.jp> Thu Mar 26 11:51:09 1998 Yukihiro Matsumoto <matz@netlab.co.jp>
* eval.c (rb_call): new feature: `protected' methods.
* string.c (str_dump): new method. * string.c (str_dump): new method.
* eval.c (block_pass): block argument can be nil, which means no * eval.c (block_pass): block argument can be nil, which means no

85
eval.c
View file

@ -45,8 +45,12 @@ static VALUE block_pass _((VALUE,NODE*));
static VALUE cMethod; static VALUE cMethod;
static VALUE method_proc _((VALUE)); static VALUE method_proc _((VALUE));
#define SCOPE_PUBLIC 0
#define SCOPE_PRIVATE FL_USER4 #define SCOPE_PRIVATE FL_USER4
#define SCOPE_MODFUNC FL_USER5 #define SCOPE_PROTECTED FL_USER5
#define SCOPE_MODFUNC (FL_USER4|FL_USER4)
#define SCOPE_MASK (FL_USER4|FL_USER4)
#define SCOPE_SET(x,f) do {FL_UNSET(x,SCOPE_MASK);FL_SET(x,(f))} while(0)
#define CACHE_SIZE 0x200 #define CACHE_SIZE 0x200
#define CACHE_MASK 0x1ff #define CACHE_MASK 0x1ff
@ -755,13 +759,13 @@ ruby_init()
the_scope->local_tbl = 0; the_scope->local_tbl = 0;
top_scope = the_scope; top_scope = the_scope;
/* default visibility is private at toplevel */ /* default visibility is private at toplevel */
FL_SET(top_scope, SCOPE_PRIVATE); SCOPE_SET(top_scope, SCOPE_PRIVATE);
FL_UNSET(top_scope, SCOPE_MODFUNC);
PUSH_TAG(PROT_NONE) PUSH_TAG(PROT_NONE)
if ((state = EXEC_TAG()) == 0) { if ((state = EXEC_TAG()) == 0) {
rb_call_inits(); rb_call_inits();
the_class = cObject; the_class = cObject;
the_frame->self = TopSelf;
the_frame->cbase = (VALUE)node_newnode(NODE_CREF,cObject,0,0); the_frame->cbase = (VALUE)node_newnode(NODE_CREF,cObject,0,0);
rb_define_global_const("TOPLEVEL_BINDING", f_binding(TopSelf)); rb_define_global_const("TOPLEVEL_BINDING", f_binding(TopSelf));
ruby_prog_init(); ruby_prog_init();
@ -2202,6 +2206,9 @@ rb_eval(self, node)
if (FL_TEST(the_scope, SCOPE_PRIVATE) || node->nd_mid == init) { if (FL_TEST(the_scope, SCOPE_PRIVATE) || node->nd_mid == init) {
noex = NOEX_PRIVATE; noex = NOEX_PRIVATE;
} }
else if (FL_TEST(the_scope, SCOPE_PROTECTED)) {
noex = NOEX_PROTECTED;
}
else { else {
noex = NOEX_PUBLIC; noex = NOEX_PUBLIC;
} }
@ -2209,7 +2216,7 @@ rb_eval(self, node)
noex |= NOEX_UNDEF; noex |= NOEX_UNDEF;
} }
rb_add_method(the_class, node->nd_mid, node->nd_defn, noex); rb_add_method(the_class, node->nd_mid, node->nd_defn, noex);
if (FL_TEST(the_scope,SCOPE_MODFUNC)) { if (FL_TEST(the_scope,SCOPE_MODFUNC) == SCOPE_MODFUNC) {
rb_add_method(rb_singleton_class(the_class), rb_add_method(rb_singleton_class(the_class),
node->nd_mid, node->nd_defn, NOEX_PUBLIC); node->nd_mid, node->nd_defn, NOEX_PUBLIC);
rb_funcall(the_class, rb_intern("singleton_method_added"), rb_funcall(the_class, rb_intern("singleton_method_added"),
@ -3023,8 +3030,10 @@ rb_ensure(b_proc, data1, e_proc, data2)
} }
static int last_call_status; static int last_call_status;
#define CSTAT_NOEX 1
#define CSTAT_VCALL 2 #define CSTAT_PRIV 1
#define CSTAT_PROT 2
#define CSTAT_VCALL 4
static VALUE static VALUE
f_missing(argc, argv, obj) f_missing(argc, argv, obj)
@ -3059,9 +3068,12 @@ f_missing(argc, argv, obj)
break; break;
} }
if (desc) { if (desc) {
if (last_call_status & CSTAT_NOEX) { if (last_call_status & CSTAT_PRIV) {
format = "private method `%s' called for %s"; format = "private method `%s' called for %s";
} }
if (last_call_status & CSTAT_PROT) {
format = "protected method `%s' called for %s";
}
else if (iterator_p()) { else if (iterator_p()) {
format = "undefined iterator `%s' for %s"; format = "undefined iterator `%s' for %s";
} }
@ -3455,8 +3467,12 @@ rb_call(klass, recv, mid, argc, argv, scope)
} }
/* receiver specified form for private method */ /* receiver specified form for private method */
if (noex & NOEX_PRIVATE && scope == 0) if ((noex & NOEX_PRIVATE) && scope == 0)
return rb_undefined(recv, mid, argc, argv, CSTAT_NOEX); return rb_undefined(recv, mid, argc, argv, CSTAT_PRIV);
/* self must be kind of a specified form for private method */
if ((noex & NOEX_PROTECTED) && !obj_is_kind_of(the_frame->self, klass))
return rb_undefined(recv, mid, argc, argv, CSTAT_PROT);
return rb_call0(klass, recv, id, argc, argv, body, noex & NOEX_UNDEF); return rb_call0(klass, recv, id, argc, argv, body, noex & NOEX_UNDEF);
} }
@ -3802,24 +3818,16 @@ mod_module_eval(mod, src)
VALUE mod, src; VALUE mod, src;
{ {
int state; int state;
int private, modfunc; int mode;
VALUE result = Qnil; VALUE result = Qnil;
private = FL_TEST(the_scope, SCOPE_PRIVATE); mode = FL_TEST(the_scope, SCOPE_MASK);
modfunc = FL_TEST(the_scope, SCOPE_MODFUNC);
FL_UNSET(the_scope, SCOPE_PRIVATE);
FL_UNSET(the_scope, SCOPE_MODFUNC);
PUSH_TAG(PROT_NONE) PUSH_TAG(PROT_NONE)
if ((state = EXEC_TAG()) == 0) { if ((state = EXEC_TAG()) == 0) {
result = eval_under(mod, mod, src); result = eval_under(mod, mod, src);
} }
POP_TAG(); POP_TAG();
if (private) { SCOPE_SET(the_scope, mode);
FL_TEST(the_scope, SCOPE_PRIVATE);
}
if (modfunc) {
FL_TEST(the_scope, SCOPE_MODFUNC);
}
if (state) JUMP_TAG(state); if (state) JUMP_TAG(state);
return result; return result;
@ -3896,8 +3904,7 @@ f_load(obj, fname)
the_scope->local_vars = vars; the_scope->local_vars = vars;
} }
/* default visibility is private at loading toplevel */ /* default visibility is private at loading toplevel */
FL_SET(the_scope, SCOPE_PRIVATE); SCOPE_SET(the_scope, SCOPE_PRIVATE);
FL_UNSET(top_scope, SCOPE_MODFUNC);
state = EXEC_TAG(); state = EXEC_TAG();
last_func = the_frame->last_func; last_func = the_frame->last_func;
@ -4089,8 +4096,7 @@ mod_public(argc, argv, module)
VALUE module; VALUE module;
{ {
if (argc == 0) { if (argc == 0) {
FL_UNSET(the_scope, SCOPE_PRIVATE); SCOPE_SET(the_scope, SCOPE_PUBLIC);
FL_UNSET(top_scope, SCOPE_MODFUNC);
} }
else { else {
set_method_visibility(module, argc, argv, NOEX_PUBLIC); set_method_visibility(module, argc, argv, NOEX_PUBLIC);
@ -4098,6 +4104,21 @@ mod_public(argc, argv, module)
return module; return module;
} }
static VALUE
mod_protected(argc, argv, module)
int argc;
VALUE *argv;
VALUE module;
{
if (argc == 0) {
SCOPE_SET(the_scope, SCOPE_PROTECTED);
}
else {
set_method_visibility(module, argc, argv, NOEX_PROTECTED);
}
return module;
}
static VALUE static VALUE
mod_private(argc, argv, module) mod_private(argc, argv, module)
int argc; int argc;
@ -4105,8 +4126,7 @@ mod_private(argc, argv, module)
VALUE module; VALUE module;
{ {
if (argc == 0) { if (argc == 0) {
FL_SET(the_scope, SCOPE_PRIVATE); SCOPE_SET(the_scope, SCOPE_PRIVATE);
FL_UNSET(top_scope, SCOPE_MODFUNC);
} }
else { else {
set_method_visibility(module, argc, argv, NOEX_PRIVATE); set_method_visibility(module, argc, argv, NOEX_PRIVATE);
@ -4142,6 +4162,14 @@ top_public(argc, argv)
return mod_public(argc, argv, cObject); return mod_public(argc, argv, cObject);
} }
static VALUE
top_protected(argc, argv)
int argc;
VALUE *argv;
{
return mod_protected(argc, argv, cObject);
}
static VALUE static VALUE
top_private(argc, argv) top_private(argc, argv)
int argc; int argc;
@ -4161,8 +4189,7 @@ mod_modfunc(argc, argv, module)
NODE *body; NODE *body;
if (argc == 0) { if (argc == 0) {
FL_SET(the_scope, SCOPE_PRIVATE); SCOPE_SET(the_scope, SCOPE_MODFUNC);
FL_SET(the_scope, SCOPE_MODFUNC);
return module; return module;
} }
@ -4392,6 +4419,7 @@ Init_eval()
rb_define_private_method(cModule, "extend_object", mod_extend_object, 1); rb_define_private_method(cModule, "extend_object", mod_extend_object, 1);
rb_define_private_method(cModule, "include", mod_include, -1); rb_define_private_method(cModule, "include", mod_include, -1);
rb_define_private_method(cModule, "public", mod_public, -1); rb_define_private_method(cModule, "public", mod_public, -1);
rb_define_private_method(cModule, "protected", mod_protected, -1);
rb_define_private_method(cModule, "private", mod_private, -1); rb_define_private_method(cModule, "private", mod_private, -1);
rb_define_private_method(cModule, "module_function", mod_modfunc, -1); rb_define_private_method(cModule, "module_function", mod_modfunc, -1);
rb_define_method(cModule, "method_defined?", mod_method_defined, 1); rb_define_method(cModule, "method_defined?", mod_method_defined, 1);
@ -4408,6 +4436,7 @@ Init_eval()
rb_define_singleton_method(TopSelf, "include", top_include, -1); rb_define_singleton_method(TopSelf, "include", top_include, -1);
rb_define_singleton_method(TopSelf, "public", top_public, -1); rb_define_singleton_method(TopSelf, "public", top_public, -1);
rb_define_singleton_method(TopSelf, "protected", top_protected, -1);
rb_define_singleton_method(TopSelf, "private", top_private, -1); rb_define_singleton_method(TopSelf, "private", top_private, -1);
rb_define_method(mKernel, "extend", obj_extend, -1); rb_define_method(mKernel, "extend", obj_extend, -1);

View file

@ -1,4 +1,5 @@
#!./miniruby #!./miniruby -I.
require "rbconfig.rb" require "rbconfig.rb"
include Config include Config

View file

@ -15,7 +15,8 @@ class Mail
break if /^$/ # end of header break if /^$/ # end of header
if /^(\S+):\s*(.*)/ if /^(\S+):\s*(.*)/
@header[attr = $1.capitalize!] = $2 (attr = $1).capitalize!
@header[attr] = $2
elsif attr elsif attr
sub!(/^\s*/, '') sub!(/^\s*/, '')
@header[attr] += "\n" + $_ @header[attr] += "\n" + $_

7
node.h
View file

@ -306,9 +306,10 @@ NODE *node_newnode();
VALUE rb_method_booundp(); VALUE rb_method_booundp();
#define NOEX_PUBLIC 0 #define NOEX_PUBLIC 0
#define NOEX_PRIVATE 1 #define NOEX_UNDEF 1
#define NOEX_UNDEF 2 #define NOEX_CFUNC 1
#define NOEX_CFUNC 2 #define NOEX_PRIVATE 2
#define NOEX_PROTECTED 4
NODE *compile_string _((char *, char *, int)); NODE *compile_string _((char *, char *, int));
NODE *compile_file _((char *, VALUE, int)); NODE *compile_file _((char *, VALUE, int));

View file

@ -1,4 +1,5 @@
#! ./miniruby #! ./miniruby -I.
require 'rbconfig' require 'rbconfig'
include Config include Config

View file

@ -1,2 +1,2 @@
#define RUBY_VERSION "1.1b9_04" #define RUBY_VERSION "1.1b9_05"
#define VERSION_DATE "98/03/19" #define VERSION_DATE "98/03/26"