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

* numeric.c (fix_equal, fix_cmp, fix_gt, fix_ge, fix_lt, fix_le):

reduce coercing when a method knows about a operand type.
  [ruby-dev:26789]

* lib/delegate.rb: simplifies Delegator classes; SimpleDelegator
  now uses method_missing for all methods.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8970 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2005-08-12 07:17:36 +00:00
parent d3de1ac85b
commit ec14c2c9b9
3 changed files with 53 additions and 51 deletions

View file

@ -2,6 +2,12 @@ Thu Aug 11 23:29:03 2005 Nobuyoshi Nakada <nobu@ruby-lang.org>
* ext/stringio/stringio.c: keep holding string after closed. * ext/stringio/stringio.c: keep holding string after closed.
Thu Aug 11 20:48:40 2005 Tadashi Saito <shiba@mail2.accsnet.ne.jp>
* numeric.c (fix_equal, fix_cmp, fix_gt, fix_ge, fix_lt, fix_le):
reduce coercing when a method knows about a operand type.
[ruby-dev:26789]
Thu Aug 11 13:01:48 2005 Kouhei Sutou <kou@cozmixng.org> Thu Aug 11 13:01:48 2005 Kouhei Sutou <kou@cozmixng.org>
* lib/rss: fixed sort bug. [ruby-list:41018] * lib/rss: fixed sort bug. [ruby-list:41018]
@ -26,6 +32,11 @@ Thu Aug 11 13:01:48 2005 Kouhei Sutou <kou@cozmixng.org>
(RSS::TestSetupMaker10::test_setup_maker_items_sort): added some (RSS::TestSetupMaker10::test_setup_maker_items_sort): added some
tests for RSS::Maker::ItemsBase#do_sort. tests for RSS::Maker::ItemsBase#do_sort.
Wed Aug 10 12:01:20 2005 Yukihiro Matsumoto <matz@ruby-lang.org>
* lib/delegate.rb: simplifies Delegator classes; SimpleDelegator
now uses method_missing for all methods.
Wed Aug 10 10:38:50 2005 Yukihiro Matsumoto <matz@ruby-lang.org> Wed Aug 10 10:38:50 2005 Yukihiro Matsumoto <matz@ruby-lang.org>
* bignum.c (rb_big_mul0): multiply two numbers (x, y) without * bignum.c (rb_big_mul0): multiply two numbers (x, y) without
@ -251,12 +262,12 @@ Wed Aug 3 21:59:16 2005 Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp>
Wed Aug 3 12:40:28 2005 Tadashi Saito <shiba@mail2.accsnet.ne.jp> Wed Aug 3 12:40:28 2005 Tadashi Saito <shiba@mail2.accsnet.ne.jp>
* numeric.c (fix_minus, fix_mul, fix_quo, fix_div, fix_mod,
fix_divmod, fix_pow): ditto.
* numeric.c (fix_plus): reduce coercing when a method knows about * numeric.c (fix_plus): reduce coercing when a method knows about
a operand type. [ruby-dev:26723] a operand type. [ruby-dev:26723]
* numeric.c (fix_minus, fix_mul, fix_quo, fix_div, fix_mod,
fix_divmod, fix_pow): ditto.
* bignum.c (rb_big_div, rb_big_modulo): export to reduce * bignum.c (rb_big_div, rb_big_modulo): export to reduce
coercing. coercing.

View file

@ -17,44 +17,27 @@
# end # end
class Delegator class Delegator
preserved = ["__id__", "object_id", "__send__", "respond_to?"]
instance_methods.each do |m|
next if preserved.include?(m)
undef_method m
end
def initialize(obj) def initialize(obj)
preserved = ::Kernel.public_instance_methods(false) __setobj__(obj)
preserved -= ["to_s","to_a","inspect","==","=~","==="]
for t in self.class.ancestors
preserved |= t.public_instance_methods(false)
preserved |= t.private_instance_methods(false)
preserved |= t.protected_instance_methods(false)
break if t == Delegator
end
preserved << "singleton_method_added"
for method in obj.methods
next if preserved.include? method
begin
eval <<-EOS
def self.#{method}(*args, &block)
begin
__getobj__.__send__(:#{method}, *args, &block)
rescue Exception
$@.delete_if{|s| /:in `__getobj__'$/ =~ s} #`
$@.delete_if{|s| /^\\(eval\\):/ =~ s}
::Kernel::raise
end
end
EOS
rescue SyntaxError
raise NameError, "invalid identifier %s" % method, caller(4)
end
end
end end
alias initialize_methods initialize
def method_missing(m, *args) def method_missing(m, *args)
target = self.__getobj__ begin
unless target.respond_to?(m) target = self.__getobj__
super(m, *args) unless target.respond_to?(m)
super(m, *args)
end
target.__send__(m, *args)
rescue Exception
$@.delete_if{|s| /^#{__FILE__}:\d+:in `method_missing'$/ =~ s} #`
::Kernel::raise
end end
target.__send__(m, *args)
end end
def respond_to?(m) def respond_to?(m)
@ -66,21 +49,19 @@ class Delegator
raise NotImplementedError, "need to define `__getobj__'" raise NotImplementedError, "need to define `__getobj__'"
end end
def __setobj__(obj)
raise NotImplementedError, "need to define `__setobj__'"
end
def marshal_dump def marshal_dump
__getobj__ __getobj__
end end
def marshal_load(obj) def marshal_load(obj)
initialize_methods(obj) __setobj__(obj)
end end
end end
class SimpleDelegator<Delegator class SimpleDelegator<Delegator
def initialize(obj)
super
@_sd_obj = obj
end
def __getobj__ def __getobj__
@_sd_obj @_sd_obj
end end
@ -91,12 +72,14 @@ class SimpleDelegator<Delegator
end end
def clone def clone
super copy = super
__setobj__(__getobj__.clone) copy.__setobj__(__getobj__.clone)
copy
end end
def dup def dup
super copy = super
__setobj__(__getobj__.dup) copy.__setobj__(__getobj__.dup)
copy
end end
end end
@ -108,8 +91,11 @@ SimpleDelegater = SimpleDelegator
def DelegateClass(superclass) def DelegateClass(superclass)
klass = Class.new klass = Class.new
methods = superclass.public_instance_methods(true) methods = superclass.public_instance_methods(true)
methods -= ::Kernel.public_instance_methods(false) methods -= [
methods |= ["to_s","to_a","inspect","==","=~","==="] "__id__", "object_id", "__send__", "respond_to?",
"initialize", "method_missing", "__getobj__", "__setobj__",
"clone", "dup", "marshal_dump", "marshal_load",
]
klass.module_eval { klass.module_eval {
def initialize(obj) def initialize(obj)
@_dc_obj = obj @_dc_obj = obj

View file

@ -18,7 +18,8 @@ class WeakRef<Delegator
@@id_map = {} # obj -> [ref,...] @@id_map = {} # obj -> [ref,...]
@@id_rev_map = {} # ref -> obj @@id_rev_map = {} # ref -> obj
@@final = lambda{|id| @@final = lambda {|id|
printf "final: %p\n", id
__old_status = Thread.critical __old_status = Thread.critical
Thread.critical = true Thread.critical = true
begin begin
@ -42,6 +43,7 @@ class WeakRef<Delegator
def initialize(orig) def initialize(orig)
@__id = orig.object_id @__id = orig.object_id
printf "orig: %p\n", @__id
ObjectSpace.define_finalizer orig, @@final ObjectSpace.define_finalizer orig, @@final
ObjectSpace.define_finalizer self, @@final ObjectSpace.define_finalizer self, @@final
__old_status = Thread.critical __old_status = Thread.critical
@ -53,7 +55,7 @@ class WeakRef<Delegator
end end
@@id_map[@__id].push self.object_id @@id_map[@__id].push self.object_id
@@id_rev_map[self.object_id] = @__id @@id_rev_map[self.object_id] = @__id
super super
end end
def __getobj__ def __getobj__
@ -66,6 +68,8 @@ class WeakRef<Delegator
Kernel::raise RefError, "Illegal Reference - probably recycled", Kernel::caller(2) Kernel::raise RefError, "Illegal Reference - probably recycled", Kernel::caller(2)
end end
end end
def __setobj__(obj)
end
def weakref_alive? def weakref_alive?
@@id_rev_map[self.object_id] == @__id @@id_rev_map[self.object_id] == @__id
@ -73,11 +77,12 @@ class WeakRef<Delegator
end end
if __FILE__ == $0 if __FILE__ == $0
require 'thread' # require 'thread'
foo = Object.new foo = Object.new
p foo.to_s # original's class p foo.to_s # original's class
foo = WeakRef.new(foo) foo = WeakRef.new(foo)
p foo.to_s # should be same class p foo.to_s # should be same class
ObjectSpace.garbage_collect ObjectSpace.garbage_collect
ObjectSpace.garbage_collect
p foo.to_s # should raise exception (recycled) p foo.to_s # should raise exception (recycled)
end end