* eval.c (rb_f_send): allow send/__send__ to call methods of all

visibility again.  we no longer provide __send, __send!.

* eval.c (rb_invoke_method): new method to honor private
  visibility.  if it's invoked in a function call style, it calls
  private methods as well (previous 1.9 send behavior).

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13824 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2007-11-04 20:36:20 +00:00
Родитель ed823523e3
Коммит 1eee78b876
28 изменённых файлов: 92 добавлений и 101 удалений

Просмотреть файл

@ -17,6 +17,15 @@ Mon Nov 5 01:02:56 2007 Minero Aoki <aamine@loveruby.net>
User-Agent to fix 500 error on some corrupted HTTP servers.
[ruby-core:13135]
Mon Nov 5 00:32:32 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
* eval.c (rb_f_send): allow send/__send__ to call methods of all
visibility again. we no longer provide __send, __send!.
* eval.c (rb_invoke_method): new method to honor private
visibility. if it's invoked in a function call style, it calls
private methods as well (previous 1.9 send behavior).
Mon Nov 5 00:24:24 2007 NAKAMURA Usaku <usa@ruby-lang.org>
* win32/Makefile.sub: vendor_ruby support.

Просмотреть файл

@ -89,12 +89,12 @@ assert_equal %q{1}, %q{
eval('a')
}
assert_equal %q{ok}, %q{
__send! :eval, %{
__send__ :eval, %{
:ok
}
}
assert_equal %q{ok}, %q{
1.__send! :instance_eval, %{
1.__send__ :instance_eval, %{
:ok
}
}

Просмотреть файл

@ -314,9 +314,9 @@ assert_equal '1', %q( class C; def m(x,a=7) a end end;
assert_equal '[1, 2]', %q( class C; def m(*a) a end end;
C.new.send(:m,1,2).inspect )
assert_equal '1', %q( class C; def m() 7 end; private :m end
begin C.new.send(:m); rescue NoMethodError; 1 end )
begin C.new.invoke_method(:m); rescue NoMethodError; 1 end )
assert_equal '1', %q( class C; def m() 1 end; private :m end
C.new.send!(:m) )
C.new.send(:m) )
# with block
assert_equal '[[:ok1, :foo], [:ok2, :foo, :bar]]',
@ -867,8 +867,6 @@ assert_equal %q{[:ok, :ok, :ok, :ok, :ok, :ok, :ng, :ng]}, %q{
end
end
alias send! send unless defined? send!
c1 = c2 = nil
lambda{
@ -899,8 +897,8 @@ assert_equal %q{[:ok, :ok, :ok, :ok, :ok, :ok, :ng, :ng]}, %q{
test{o2.mm}
test{o1.send :m}
test{o2.send :mm}
test{o1.send! :m}
test{o2.send! :mm}
test{o1.invoke_method :m}
test{o2.invoke_method :mm}
test{o1.method(:m).call}
test{o2.method(:mm).call}
$ans

Просмотреть файл

@ -1483,11 +1483,11 @@ iseq_specialized_instruction(rb_iseq_t *iseq, INSN *iobj)
}
if (mid == idSend || mid == id__send ||
mid == idSendBang || mid == id__send_bang ||
mid == idSendBang ||
mid == id__send__ ) {
OPERAND_AT(iobj, 3) |= INT2FIX(VM_CALL_SEND_BIT);
}
if (mid == idSendBang || mid == id__send_bang) {
if (mid == idSendBang) {
OPERAND_AT(iobj, 3) |= INT2FIX(VM_CALL_SEND_BANG_BIT);
}
}

48
eval.c
Просмотреть файл

@ -25,7 +25,7 @@ static VALUE rb_frame_self(void);
static ID removed, singleton_removed, undefined, singleton_undefined;
static ID init, eqq, each, aref, aset, match, missing;
static ID added, singleton_added;
static ID object_id, __send, __send_bang, respond_to;
static ID object_id, __send__, respond_to;
VALUE rb_eLocalJumpError;
VALUE rb_eSysStackError;
@ -1471,7 +1471,7 @@ send_internal(int argc, VALUE *argv, VALUE recv, int scope)
/*
* call-seq:
* obj.send(symbol [, args...]) => obj
* obj.__send__(symbol [, args...]) => obj
* obj.__send__(symbol [, args...]) => obj
*
* Invokes the method identified by _symbol_, passing it any
* arguments specified. You can use <code>__send__</code> if the name
@ -1488,6 +1488,24 @@ send_internal(int argc, VALUE *argv, VALUE recv, int scope)
VALUE
rb_f_send(int argc, VALUE *argv, VALUE recv)
{
return send_internal(argc, argv, recv, NOEX_NOSUPER | NOEX_PRIVATE);
}
/*
* call-seq:
* obj.invoke_method(symbol [, args...]) => obj
*
* Invokes the method identified by _symbol_, passing it any
* 1arguments specified. Unlike send, invoke_method calls public
* methods only, unless it's invoked in a function call style.
*
* 1.invoke_method(:puts, "hello") # causes NoMethodError
*/
VALUE
rb_invoke_method(int argc, VALUE *argv, VALUE recv)
{
int scope = NOEX_PUBLIC;
rb_thread_t *th = GET_THREAD();
@ -1500,25 +1518,6 @@ rb_f_send(int argc, VALUE *argv, VALUE recv)
return send_internal(argc, argv, recv, scope);
}
/*
* call-seq:
* obj.send!(symbol [, args...]) => obj
* obj.__send!(symbol [, args...]) => obj
*
* Invokes the method identified by _symbol_, passing it any
* arguments specified. Unlike send, which calls public methods only
* when it is invoked in function call style, send! always aware of
* private methods.
*
* 1.send!(:puts, "hello") # prints "foo"
*/
VALUE
rb_f_send_bang(int argc, VALUE *argv, VALUE recv)
{
return send_internal(argc, argv, recv, NOEX_NOSUPER | NOEX_PRIVATE);
}
VALUE
rb_funcall(VALUE recv, ID mid, int n, ...)
{
@ -2718,8 +2717,7 @@ Init_eval(void)
singleton_undefined = rb_intern("singleton_method_undefined");
object_id = rb_intern("object_id");
__send = rb_intern("__send");
__send_bang = rb_intern("__send!");
__send__ = rb_intern("__send__");
rb_define_virtual_variable("$@", errat_getter, errat_setter);
rb_define_virtual_variable("$!", errinfo_getter, 0);
@ -2748,9 +2746,7 @@ Init_eval(void)
rb_define_method(rb_cBasicObject, "send", rb_f_send, -1);
rb_define_method(rb_cBasicObject, "__send__", rb_f_send, -1);
rb_define_method(rb_cBasicObject, "__send", rb_f_send, -1);
rb_define_method(rb_cBasicObject, "send!", rb_f_send_bang, -1);
rb_define_method(rb_cBasicObject, "__send!", rb_f_send_bang, -1);
rb_define_method(rb_mKernel, "invoke_method", rb_invoke_method, -1);
rb_define_method(rb_mKernel, "instance_eval", rb_obj_instance_eval, -1);
rb_define_method(rb_mKernel, "instance_exec", rb_obj_instance_exec, -1);

Просмотреть файл

@ -158,7 +158,7 @@ rb_add_method(VALUE klass, ID mid, NODE * node, int noex)
}
}
if (mid == object_id || mid == __send || mid == __send_bang) {
if (mid == object_id || mid == __send__) {
if (node && nd_type(node) == RUBY_VM_METHOD_NODE) {
rb_warn("redefining `%s' may cause serious problem",
rb_id2name(mid));
@ -302,7 +302,7 @@ remove_method(VALUE klass, ID mid)
}
if (OBJ_FROZEN(klass))
rb_error_frozen("class/module");
if (mid == object_id || mid == __send || mid == __send_bang || mid == init) {
if (mid == object_id || mid == __send__ || mid == init) {
rb_warn("removing `%s' may cause serious problem", rb_id2name(mid));
}
if (st_lookup(RCLASS_M_TBL(klass), mid, &data)) {
@ -466,7 +466,7 @@ rb_undef(VALUE klass, ID id)
rb_id2name(id));
}
rb_frozen_class_p(klass);
if (id == object_id || id == __send || id == __send_bang || id == init) {
if (id == object_id || id == __send__ || id == init) {
rb_warn("undefining `%s' may cause serious problem", rb_id2name(id));
}
body = search_method(klass, id, &origin);

2
id.c
Просмотреть файл

@ -62,7 +62,5 @@ Init_id(void)
idSend = rb_intern("send");
idSendBang = rb_intern("send!");
id__send = rb_intern("__send");
id__send_bang = rb_intern("__send!");
id__send__ = rb_intern("__send__");
}

Просмотреть файл

@ -588,10 +588,9 @@ vm_send_optimize(rb_control_frame_t *reg_cfp,
{
if (*mn && nd_type((*mn)->nd_body) == NODE_CFUNC) {
NODE *node = (*mn)->nd_body;
extern VALUE rb_f_send_bang(int argc, VALUE *argv, VALUE recv);
extern VALUE rb_f_send(int argc, VALUE *argv, VALUE recv);
if (node->nd_cfnc == rb_f_send_bang || node->nd_cfnc == rb_f_send) {
if (node->nd_cfnc == rb_f_send) {
int i = *num - 1;
VALUE sym = TOPN(i);
*id = SYMBOL_P(sym) ? SYM2ID(sym) : rb_to_id(sym);
@ -604,9 +603,6 @@ vm_send_optimize(rb_control_frame_t *reg_cfp,
*mn = rb_method_node(klass, *id);
*num -= 1;
DEC_SP(1);
}
if (node->nd_cfnc == rb_f_send_bang) {
*flag |= VM_CALL_FCALL_BIT;
}
}

Просмотреть файл

@ -877,14 +877,14 @@ class Date
when :civil
g[1].each do |e|
break if elem[e]
elem[e] = d.__send!(e)
elem[e] = d.__send__(e)
end
elem[:mon] ||= 1
elem[:mday] ||= 1
when :commercial
g[1].each do |e|
break if elem[e]
elem[e] = d.__send!(e)
elem[e] = d.__send__(e)
end
elem[:cweek] ||= 1
elem[:cwday] ||= 1
@ -893,14 +893,14 @@ class Date
when :wnum0
g[1].each do |e|
break if elem[e]
elem[e] = d.__send!(e)
elem[e] = d.__send__(e)
end
elem[:wnum0] ||= 0
elem[:wday] ||= 0
when :wnum1
g[1].each do |e|
break if elem[e]
elem[e] = d.__send!(e)
elem[e] = d.__send__(e)
end
elem[:wnum1] ||= 0
elem[:wday] ||= 0
@ -1727,16 +1727,16 @@ class Time
def to_time() getlocal end
def to_date
jd = Date.__send!(:civil_to_jd, year, mon, mday, Date::ITALY)
Date.new!(Date.__send!(:jd_to_ajd, jd, 0, 0), 0, Date::ITALY)
jd = Date.__send__(:civil_to_jd, year, mon, mday, Date::ITALY)
Date.new!(Date.__send__(:jd_to_ajd, jd, 0, 0), 0, Date::ITALY)
end
def to_datetime
jd = DateTime.__send!(:civil_to_jd, year, mon, mday, DateTime::ITALY)
fr = DateTime.__send!(:time_to_day_fraction, hour, min, [sec, 59].min) +
jd = DateTime.__send__(:civil_to_jd, year, mon, mday, DateTime::ITALY)
fr = DateTime.__send__(:time_to_day_fraction, hour, min, [sec, 59].min) +
usec.to_r/86400_000_000
of = utc_offset.to_r/86400
DateTime.new!(DateTime.__send!(:jd_to_ajd, jd, fr, of),
DateTime.new!(DateTime.__send__(:jd_to_ajd, jd, fr, of),
of, DateTime::ITALY)
end

Просмотреть файл

@ -115,7 +115,7 @@
# implementation, see SimpleDelegator.
#
class Delegator
preserved = [:__id__, :object_id, :__send__, :__send, :__send!, :respond_to?, :send, :send!]
preserved = [:__id__, :object_id, :__send__, :invoke_method, :respond_to?, :send]
instance_methods.each do |m|
next if preserved.include?(m)
undef_method m
@ -137,7 +137,7 @@ class Delegator
unless target.respond_to?(m)
super(m, *args, &block)
else
target.__send(m, *args, &block)
target.__send__(m, *args, &block)
end
rescue Exception
$@.delete_if{|s| /^#{__FILE__}:\d+:in `method_missing'$/ =~ s} #`
@ -262,7 +262,7 @@ def DelegateClass(superclass)
klass = Class.new
methods = superclass.public_instance_methods(true)
methods -= [
:__id__, :object_id, :__send__, :__send, :__send!, :respond_to?, :send, :send!,
:__id__, :object_id, :__send__, :invoke_method, :respond_to?, :send,
:==, :equal?, :initialize, :method_missing, :__getobj__, :__setobj__,
:clone, :dup, :marshal_dump, :marshal_load,
]
@ -281,7 +281,7 @@ def DelegateClass(superclass)
klass.module_eval <<-EOS, __FILE__, __LINE__+1
def #{method}(*args, &block)
begin
@delegate_dc_obj.__send(:#{method}, *args, &block)
@delegate_dc_obj.__send__(:#{method}, *args, &block)
rescue
raise $!, $@[2..-1]
end

Просмотреть файл

@ -1553,7 +1553,7 @@ module DRb
end
ary.collect(&@obj)[0]
else
@obj.send!(@msg_id, *@argv)
@obj.send(@msg_id, *@argv)
end
end

Просмотреть файл

@ -246,7 +246,7 @@ class Matrix
# use to general users.
#
def initialize(init_method, *argv)
self.send!(init_method, *argv)
self.send(init_method, *argv)
end
def init_rows(rows, copy)

Просмотреть файл

@ -91,13 +91,13 @@ module MonitorMixin
if timeout
raise NotImplementedError, "timeout is not implemented yet"
end
@monitor.send!(:mon_check_owner)
count = @monitor.send!(:mon_exit_for_cond)
@monitor.send(:mon_check_owner)
count = @monitor.send(:mon_exit_for_cond)
begin
@cond.wait(@monitor.instance_variable_get("@mon_mutex"))
return true
ensure
@monitor.send!(:mon_enter_for_cond, count)
@monitor.send(:mon_enter_for_cond, count)
end
end
@ -114,12 +114,12 @@ module MonitorMixin
end
def signal
@monitor.send!(:mon_check_owner)
@monitor.send(:mon_check_owner)
@cond.signal
end
def broadcast
@monitor.send!(:mon_check_owner)
@monitor.send(:mon_check_owner)
@cond.broadcast
end
@ -137,7 +137,7 @@ module MonitorMixin
def self.extend_object(obj)
super(obj)
obj.send!(:mon_initialize)
obj.send(:mon_initialize)
end
#

Просмотреть файл

@ -1229,7 +1229,7 @@ module Net
class RawData # :nodoc:
def send_data(imap)
imap.send!(:put_string, @data)
imap.send(:put_string, @data)
end
private
@ -1241,7 +1241,7 @@ module Net
class Atom # :nodoc:
def send_data(imap)
imap.send!(:put_string, @data)
imap.send(:put_string, @data)
end
private
@ -1253,7 +1253,7 @@ module Net
class QuotedString # :nodoc:
def send_data(imap)
imap.send!(:send_quoted_string, @data)
imap.send(:send_quoted_string, @data)
end
private
@ -1265,7 +1265,7 @@ module Net
class Literal # :nodoc:
def send_data(imap)
imap.send!(:send_literal, @data)
imap.send(:send_literal, @data)
end
private
@ -1277,7 +1277,7 @@ module Net
class MessageSet # :nodoc:
def send_data(imap)
imap.send!(:put_string, format_internal(@data))
imap.send(:put_string, format_internal(@data))
end
private

Просмотреть файл

@ -722,7 +722,7 @@ module Net
def authenticate(user, secret, authtype = DEFAULT_AUTH_TYPE)
check_auth_method authtype
check_auth_args user, secret
send! auth_method(authtype), user, secret
send auth_method(authtype), user, secret
end
def auth_plain(user, secret)

Просмотреть файл

@ -9,12 +9,6 @@ require 'enumerator'
class TestPathname < Test::Unit::TestCase
if RUBY_VERSION < "1.9"
FUNCALL = :__send__
else
FUNCALL = :send!
end
def self.define_assertion(name, &block)
@defassert_num ||= {}
@defassert_num[name] ||= 0
@ -123,7 +117,7 @@ class TestPathname < Test::Unit::TestCase
# has_trailing_separator?(path) -> bool
def has_trailing_separator?(path)
Pathname.allocate.send(FUNCALL, :has_trailing_separator?, path)
Pathname.allocate.__send__(:has_trailing_separator?, path)
end
defassert(:has_trailing_separator?, false, "/")
@ -132,11 +126,11 @@ class TestPathname < Test::Unit::TestCase
defassert(:has_trailing_separator?, true, "a/")
def add_trailing_separator(path)
Pathname.allocate.send(FUNCALL, :add_trailing_separator, path)
Pathname.allocate.__send__(:add_trailing_separator, path)
end
def del_trailing_separator(path)
Pathname.allocate.send(FUNCALL, :del_trailing_separator, path)
Pathname.allocate.send(__send__, :del_trailing_separator, path)
end
defassert(:del_trailing_separator, "/", "/")

Просмотреть файл

@ -85,7 +85,7 @@ EOR
excepted = make_element("#{@prefix}:#{name}", {}, value)
meth = "#{RSS::CONTENT_PREFIX}_#{name}_element"
[@rss10, @rss20].each do |rss|
assert_equal(excepted, rss.items.last.__send!(meth))
assert_equal(excepted, rss.items.last.__send__(meth))
end
end

Просмотреть файл

@ -245,7 +245,7 @@ EOR
excepted = "<#{DC_PREFIX}:#{name}>#{value}</#{DC_PREFIX}:#{name}>"
parents.each do |parent_readers|
parent = chain_reader(feed, parent_readers)
assert_equal(excepted, parent.__send!("dc_#{name}_elements"))
assert_equal(excepted, parent.__send__("dc_#{name}_elements"))
end
plural_suffix = dc_plural_suffix(name, check_backward_compatibility)
@ -257,7 +257,7 @@ EOR
klass_name = "DublinCore#{Utils.to_class_name(name.to_s)}"
klass = DublinCoreModel.const_get(klass_name)
elems << klass.new(parent.__send__("dc_#{name}"))
assert_equal(excepted, parent.__send!("dc_#{name}_elements"))
assert_equal(excepted, parent.__send__("dc_#{name}_elements"))
end
end

Просмотреть файл

@ -107,7 +107,7 @@ EOR
excepted = "<#{@prefix}:#{name}>#{value}</#{@prefix}:#{name}>"
@parents.each do |parent|
assert_equal(excepted,
@rss.__send__(parent).__send!("sy_#{name}_element"))
@rss.__send__(parent).__send__("sy_#{name}_element"))
end
end

Просмотреть файл

@ -140,7 +140,7 @@ EOR
def test_to_s
@topics_parents.each do |parent|
meth = "taxo_topics_element"
assert_equal(@topics_node, @rss.__send__(parent).__send!(meth))
assert_equal(@topics_node, @rss.__send__(parent).__send__(meth))
end
@topic_nodes.each_with_index do |node, i|

Просмотреть файл

@ -114,7 +114,7 @@ EOR
@parents.each do |parent|
meth = "#{RSS::TRACKBACK_PREFIX}_#{name}_element"
meth << "s" if name == :about
assert_equal(excepted, @rss.__send__(parent).__send!(meth))
assert_equal(excepted, @rss.__send__(parent).__send__(meth))
end
end

Просмотреть файл

@ -56,7 +56,7 @@ class TestAlias < Test::Unit::TestCase
d = lambda {
$SAFE = 4
dclass = Class.new(C)
dclass.send!(:alias_method, :mm, :m)
dclass.send(:alias_method, :mm, :m)
dclass.new
}.call
assert_raise(SecurityError) { d.mm }

Просмотреть файл

@ -652,7 +652,7 @@ class TestAssignmentGen < Test::Unit::TestCase
assign = assign.to_s
code1 = "#{assign}; [#{vars.join(",")}]"
assign.gsub!(/\bv\d+\b/, "o.a")
code2 = "o=[];class << o; self end.send!(:define_method,:a=){|v|self << v};#{assign};o"
code2 = "o=[];class << o; self end.send(:define_method,:a=){|v|self << v};#{assign};o"
begin
vals1 = eval(code1)
rescue Exception

Просмотреть файл

@ -124,7 +124,7 @@ class TestEval < Test::Unit::TestCase
obj.class.class_variable_set :@@cvar, 13
# Use same value with env. See also test_instance_variable_cvar.
obj.class.const_set :Const, 15 unless obj.class.const_defined?(:Const)
send! mid, obj
send mid, obj
end
end
@ -364,7 +364,7 @@ class TestEval < Test::Unit::TestCase
assert_nothing_raised {
def temporally_method_for_test_eval_and_define_method(&block)
lambda {
class << Object.new; self end.send!(:define_method, :zzz, &block)
class << Object.new; self end.send(:define_method, :zzz, &block)
}
end
v = eval("temporally_method_for_test_eval_and_define_method {}")

Просмотреть файл

@ -50,7 +50,7 @@ class TestRubyPrimitive < Test::Unit::TestCase
assert_equal 3, A::B::C::Const
assert_equal 3, A::B::C.new.const
assert_equal 1, ::TestRubyPrimitive::A::Const
A::B::C.send!(:remove_const, :Const)
A::B::C.send(:remove_const, :Const)
assert_equal 2, A::B::C.new.const
assert_raise(TypeError) {
C::CONST

Просмотреть файл

@ -19,7 +19,7 @@ class TestBasicSocket < Test::Unit::TestCase
n = s.getsockopt(Socket::SOL_SOCKET, Socket::SO_ERROR)
assert_equal([0].pack("i"), n)
val = Object.new
class << val; self end.send!(:define_method, :to_int) {
class << val; self end.send(:define_method, :to_int) {
s.close
Socket::SO_TYPE
}
@ -34,7 +34,7 @@ class TestBasicSocket < Test::Unit::TestCase
linger = [0, 0].pack("ii")
val = Object.new
class << val; self end.send!(:define_method, :to_str) {
class << val; self end.send(:define_method, :to_str) {
s.close
linger
}
@ -47,7 +47,7 @@ class TestBasicSocket < Test::Unit::TestCase
end
val = Object.new
class << val; self end.send!(:define_method, :to_int) {
class << val; self end.send(:define_method, :to_int) {
s.close
Socket::SO_LINGER
}
@ -61,7 +61,7 @@ class TestBasicSocket < Test::Unit::TestCase
def test_listen
s = nil
log = Object.new
class << log; self end.send!(:define_method, :to_int) {
class << log; self end.send(:define_method, :to_int) {
s.close
2
}

Просмотреть файл

@ -9,7 +9,7 @@ class TestUDPSocket < Test::Unit::TestCase
def test_connect # [ruby-dev:25045]
s = UDPSocket.new
host = Object.new
class << host; self end.send!(:define_method, :to_str) {
class << host; self end.send(:define_method, :to_str) {
s.close
"127.0.0.1"
}
@ -21,7 +21,7 @@ class TestUDPSocket < Test::Unit::TestCase
def test_bind # [ruby-dev:25057]
s = UDPSocket.new
host = Object.new
class << host; self end.send!(:define_method, :to_str) {
class << host; self end.send(:define_method, :to_str) {
s.close
"127.0.0.1"
}

Просмотреть файл

@ -16,12 +16,12 @@ class TestEval < YarvTestBase
def test_eval_with_send
ae %q{
__send! :eval, %{
__send__ :eval, %{
:ok
}
}
ae %q{
1.__send! :instance_eval, %{
1.__send__ :instance_eval, %{
:ok
}
}