jira.codehaus.org

  • Log In Access more options
    • Online Help
    • Keyboard Shortcuts
    • About JIRA
    • JIRA Credits
    • What?s New
  • Dashboards Access more options (Alt+d)
  • Projects Access more options (Alt+p)
  • Issues Access more options (Alt+i)
  • JRuby
  • JRUBY-399

MRI desn't allow defining singleton methods for Floats and Bignums

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: Improvement Improvement
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: None
  • Fix Version/s: JRuby 0.9.8
  • Component/s: Core Classes/Modules
  • Labels:
    None
  • Environment:
    jruby-trunk/windows/linux/jdk6

Description

MRI desn't allow defining singleton methods for Floats and Bignums

class << 10.0
def singleton
end
end

either

class << 1000000000000000000000
def singleton
end
end

MRI: can't define singleton method "singleton" for Float (TypeError)

  • Options
    • Sort By Name
    • Sort By Date
    • Ascending
    • Descending
    • Download All

Attachments

  1. Text File
    JIRA-399.patch
    23/Jan/07 3:03 PM
    5 kB
    Marcin Mielzynski
  2. Text File
    JRUBY-399.patch
    02/Jan/07 7:53 PM
    3 kB
    Michael Studman
  3. Text File
    numeric_singleton_added.patch
    04/Jan/07 1:57 PM
    6 kB
    Charles Oliver Nutter

Activity

Ascending order - Click to sort in descending order
  • All
  • Comments
  • Work Log
  • History
  • Activity
Hide
Permalink
Michael Studman added a comment - 02/Jan/07 7:53 PM

Patch with test cases. This patch highlights a separate issue:

class << 10
end

should fail with "TypeError: no virtual class for Fixnum" (according to MRI) but instead succeeds. I will raise a separate patch.

Show
Michael Studman added a comment - 02/Jan/07 7:53 PM Patch with test cases. This patch highlights a separate issue: class << 10 end should fail with "TypeError: no virtual class for Fixnum" (according to MRI) but instead succeeds. I will raise a separate patch.
Hide
Permalink
Ola Bini added a comment - 03/Jan/07 4:55 AM

Michael, the patch seems mangled and when I apply it testing loops eternally.

Show
Ola Bini added a comment - 03/Jan/07 4:55 AM Michael, the patch seems mangled and when I apply it testing loops eternally.
Hide
Permalink
Thomas E Enebo added a comment - 03/Jan/07 8:08 PM

Patch applied (commit 2662). Thanks Marcin, Michael

Show
Thomas E Enebo added a comment - 03/Jan/07 8:08 PM Patch applied (commit 2662). Thanks Marcin, Michael
Hide
Permalink
Charles Oliver Nutter added a comment - 04/Jan/07 1:55 PM

The patch applied does not allow for singleton_method_added being redefined in Numeric to let singleton methods through. Note Marcin's recent example, which should run successfully:

class Numeric
      def singleton_method_added name
          puts "really ?"
      end
end

class << 1.0
     def some_method
     end
end

The behavior appears to be:

  • singleton_method_added is called before the method is actually added
  • Numeric provides a default impl that throws the TypeError
  • redefining singleton_method_added in Numeric can eliminate the TypeError and allow singleton methods through

This behavior could also apply for method_added; I will attach a patch that addresses both.

HOWEVER...this behavior (specifically the order-of-operations regarding the _added methods) must first be confirmed with more test cases or by showing examples of C Ruby source that prove it without a doubt.

Show
Charles Oliver Nutter added a comment - 04/Jan/07 1:55 PM The patch applied does not allow for singleton_method_added being redefined in Numeric to let singleton methods through. Note Marcin's recent example, which should run successfully:
class Numeric
      def singleton_method_added name
          puts "really ?"
      end
end

class << 1.0
     def some_method
     end
end
The behavior appears to be:
  • singleton_method_added is called before the method is actually added
  • Numeric provides a default impl that throws the TypeError
  • redefining singleton_method_added in Numeric can eliminate the TypeError and allow singleton methods through
This behavior could also apply for method_added; I will attach a patch that addresses both. HOWEVER...this behavior (specifically the order-of-operations regarding the _added methods) must first be confirmed with more test cases or by showing examples of C Ruby source that prove it without a doubt.
Hide
Permalink
Charles Oliver Nutter added a comment - 04/Jan/07 1:57 PM

numeric_singleton_added.patch modifies the addition of singleton methods to invoke singleton_method_added first, and provides an implementation for Numeric that throws the TypeError referenced by this bug. This resolves Marcin's additional test case.

Show
Charles Oliver Nutter added a comment - 04/Jan/07 1:57 PM numeric_singleton_added.patch modifies the addition of singleton methods to invoke singleton_method_added first, and provides an implementation for Numeric that throws the TypeError referenced by this bug. This resolves Marcin's additional test case.
Hide
Permalink
Marcin Mielzynski added a comment - 04/Jan/07 2:48 PM

singleton_method_added is called after the method is actually added and it doesn't change the fact that the above example will run successfully. I'd like to emphasise that recent patches put the "can't define singleton method" TypeError at NodeTypes.DEFNNODE in EvaluationState.evalInternal() which is incorrect.

Show
Marcin Mielzynski added a comment - 04/Jan/07 2:48 PM singleton_method_added is called after the method is actually added and it doesn't change the fact that the above example will run successfully. I'd like to emphasise that recent patches put the "can't define singleton method" TypeError at NodeTypes.DEFNNODE in EvaluationState.evalInternal() which is incorrect.
Hide
Permalink
Marcin Mielzynski added a comment - 04/Jan/07 3:15 PM

It makes my mind crazy but it seams that the code:

ruby_frame = ruby_frame->prev; /* pop frame for "singleton_method_added" */
/* Numerics should be values; singleton_methods should not be added to them */

in MRI Numeric#singleton_method_added standard implementation removes the method just after the method has been added

Show
Marcin Mielzynski added a comment - 04/Jan/07 3:15 PM It makes my mind crazy but it seams that the code: ruby_frame = ruby_frame->prev; /* pop frame for "singleton_method_added" */ /* Numerics should be values; singleton_methods should not be added to them */ in MRI Numeric#singleton_method_added standard implementation removes the method just after the method has been added
Hide
Permalink
Michael Studman added a comment - 05/Jan/07 3:12 AM

Recording findings for posterity...

The actual behavior for singleton methods on Numerics appears to be:

singleton_method_added is called before the method is actually added
Numeric provides a default impl that throws the TypeError
Redefining singleton_method_added in Numeric can eliminate the TypeError but regardless, the method will still be added to the singleton class

It initially appeared that exception-throwing singleton_method_added methods would cause removal of the associated method. This was not the case - what happened was because any two numeric literals are not the same object then adding singleton methods in the manner below only applies to each instance's singleton class.

E.g

class << 1.0 #Float instance number 1
def hello #will cause a type error to be raised but hello will still be defined
end
end

1.0.hello #Float instance number 2 will fail with NoMethodError

If you rewrite this like follows the true behaviour becomes apparent:

i = 1.0 #One and only Float instance
class << i
def hello #will cause a type error to be raised but hello will still be defined
end
end

i.hello #will succeed

Fixnums and Symbols are still special cases. Singleton methods cannot be used with them and MRI's handles this in the part of the interpreter handling NODE_DEFS .

Show
Michael Studman added a comment - 05/Jan/07 3:12 AM Recording findings for posterity... The actual behavior for singleton methods on Numerics appears to be: singleton_method_added is called before the method is actually added Numeric provides a default impl that throws the TypeError Redefining singleton_method_added in Numeric can eliminate the TypeError but regardless, the method will still be added to the singleton class It initially appeared that exception-throwing singleton_method_added methods would cause removal of the associated method. This was not the case - what happened was because any two numeric literals are not the same object then adding singleton methods in the manner below only applies to each instance's singleton class. E.g class << 1.0 #Float instance number 1 def hello #will cause a type error to be raised but hello will still be defined end end 1.0.hello #Float instance number 2 will fail with NoMethodError If you rewrite this like follows the true behaviour becomes apparent: i = 1.0 #One and only Float instance class << i def hello #will cause a type error to be raised but hello will still be defined end end i.hello #will succeed Fixnums and Symbols are still special cases. Singleton methods cannot be used with them and MRI's handles this in the part of the interpreter handling NODE_DEFS .
Hide
Permalink
Michael Studman added a comment - 05/Jan/07 3:13 AM

I'll add this to Rubyspec BTW

Show
Michael Studman added a comment - 05/Jan/07 3:13 AM I'll add this to Rubyspec BTW
Hide
Permalink
Marcin Mielzynski added a comment - 23/Jan/07 3:03 PM

The patch removes singletonMethodsAllowed() from IRubyObject and further descendants (RubyObject, RubySymbol, RubyFixnum) - it's not needed (and it doesn't allow to change this behaviour) since proper default Numeric#singleton_method_added has been added with the Numerics compatibility fix
(error message in singleton_method_added and initialize_copy has been slightly changed as well):

EvaluationState is also changed to reflect MRI at NODE_DEFS:

if (FIXNUM_P(recv) || SYMBOL_P(recv)) { rb_raise(rb_eTypeError, "can't define singleton method \"%s\" for %s", rb_id2name(node->nd_mid), rb_obj_classname(recv)); }

in JRuby (at DEFSNODE):

if (receiver.getMetaClass() == runtime.getFixnum() || receiver.getMetaClass() == runtime.getClass("Symbol")) { throw runtime.newTypeError("can't define singleton method \"" + iVisited.getName() + "\" for " + receiver.getType()); }

YARVMachine has been also updated in this manner

now this will succeed:

class Numeric
def singleton_method_added a # turn off the default behaviour
end
end

a = 1.0

class << a

def hello
p "hello"
end

end

a.hello

Show
Marcin Mielzynski added a comment - 23/Jan/07 3:03 PM The patch removes singletonMethodsAllowed() from IRubyObject and further descendants (RubyObject, RubySymbol, RubyFixnum) - it's not needed (and it doesn't allow to change this behaviour) since proper default Numeric#singleton_method_added has been added with the Numerics compatibility fix (error message in singleton_method_added and initialize_copy has been slightly changed as well): EvaluationState is also changed to reflect MRI at NODE_DEFS: if (FIXNUM_P(recv) || SYMBOL_P(recv)) { rb_raise(rb_eTypeError, "can't define singleton method \"%s\" for %s", rb_id2name(node->nd_mid), rb_obj_classname(recv)); } in JRuby (at DEFSNODE): if (receiver.getMetaClass() == runtime.getFixnum() || receiver.getMetaClass() == runtime.getClass("Symbol")) { throw runtime.newTypeError("can't define singleton method \"" + iVisited.getName() + "\" for " + receiver.getType()); } YARVMachine has been also updated in this manner now this will succeed: class Numeric def singleton_method_added a # turn off the default behaviour end end a = 1.0 class << a def hello p "hello" end end a.hello
Hide
Permalink
Charles Oliver Nutter added a comment - 02/Feb/07 12:39 AM

So is this patch valid? complete? ready? Where do we stand, gents?

Show
Charles Oliver Nutter added a comment - 02/Feb/07 12:39 AM So is this patch valid? complete? ready? Where do we stand, gents?
Hide
Permalink
Marcin Mielzynski added a comment - 02/Feb/07 4:17 AM

JIRA-399.patch is still valid (although it can have some conflicts to resolve now)

the guards cannot be hardcoded in EvaluationState and singletonMethodsAllowed should go away

Show
Marcin Mielzynski added a comment - 02/Feb/07 4:17 AM JIRA-399.patch is still valid (although it can have some conflicts to resolve now) the guards cannot be hardcoded in EvaluationState and singletonMethodsAllowed should go away
Hide
Permalink
Thomas E Enebo added a comment - 02/Mar/07 10:46 AM

Fixed in commit 3105. Thanks Marcin

Show
Thomas E Enebo added a comment - 02/Mar/07 10:46 AM Fixed in commit 3105. Thanks Marcin

People

  • Assignee:
    Charles Oliver Nutter
    Reporter:
    Marcin Mielzynski
Vote (0)
Watch (0)

Dates

  • Created:
    02/Jan/07 2:24 PM
    Updated:
    30/Apr/07 3:12 AM
    Resolved:
    02/Mar/07 10:46 AM
  • Atlassian JIRA (v5.0.4#731-sha1:3aa7374)
  • Report a problem
  • Powered by a free Atlassian JIRA open source license for Codehaus. Try JIRA - bug tracking software for your team.