Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monkey Patching extension classes #3471

Closed
monkstone opened this issue Nov 19, 2015 · 7 comments
Closed

Monkey Patching extension classes #3471

monkstone opened this issue Nov 19, 2015 · 7 comments

Comments

@monkstone
Copy link
Contributor

I am not sure whether this an issue, or wrinkle that needs to be made more known? In ruby-processing/JRubyArt I have monkey patched Numeric to provide radians and degrees methods, and this works without any problems. When I tried to monkey-patch my Vec3D class (which is a jruby extension) the same way there was an error, however if instead I used this form

Vec3D.class_eval do
   def rotate_x(theta)
      ...
   end
end

It worked as expected see gist https://gist.github.com/monkstone/1620da6a63cba504931b

@headius
Copy link
Member

headius commented Nov 19, 2015

Can you provide the error as well? I don't know why there would be an issue patching an extension class, though frequently extensions make direct calls to the Java methods (as do C extensions in some cases), bypassing your patches.

@monkstone
Copy link
Contributor Author

Well I've done a bit more research trying to produce a simple test case (but that doesn't produce the error), so it may have something to do with where the patch is done

This was the error I got:-

Java::OrgJrubyExceptions::RaiseException
  (ArgumentError) wrong number of arguments calling `initialize` (3 for 0)
    RUBY.mouse_moved(mesh_doodle.rb:62)

So it looks like the Vec3D of the patch is seen in this case (lacks constructor of the original)?

PS: I was using java 8 lambda in constructor but I think that is irrelevant as I used the same in my simple test case, which failed to create error. This is probably an edge case, and there is a decent workaround, do you think it needs pursuing?

@monkstone
Copy link
Contributor Author

Had a bit of brain-wave, created and this test case https://github.com/ruby-processing/monkey reproduces behaviour, whether it is an error or just something to advise about...
rake to compile and run tests.

@monkstone
Copy link
Contributor Author

@headius Updated build to use polyglot build https://github.com/ruby-processing/monkey, problem seem to be that the regular monkey patching does something to class constructor.
https://travis-ci.org/ruby-processing/monkey/builds/101874806

@monkstone
Copy link
Contributor Author

Just thought I had better test using without lambda8 constructor:-

  /**
  *
  * @param runtime
  */
  public static void createVec(final Ruby runtime) {
      RubyClass vecCls = runtime.defineClass("Vec2D", runtime.getObject(), new ObjectAllocator() {
        @Override
        public IRubyObject allocate(Ruby runtime, RubyClass rubyClass) {
          return new Vec(runtime, rubyClass);
        }
    });
    vecCls.defineAnnotatedMethods(Vec.class);
  }

Still get same pattern ie class_eval works, regular class monkey patch fails.

@monkstone
Copy link
Contributor Author

Interesting monkey patch instance, as below just works

gem 'minitest' # don't use bundled minitest
require 'java'
require 'minitest/autorun'
require 'minitest/pride'
require_relative '../../monkey/lib/monkey.rb'

Dir.chdir(File.dirname(__FILE__))

module Processing

  class VecTest < Minitest::Test
    def test_rotate
      a = Vec2D.new(3, 5)
      class << a
        def rotate(theta)
          self.x *= Math.cos(theta)
          self.y *= Math.sin(theta)
          self 
        end
      end
      a.rotate(Math::PI / 4)
      b = Vec2D.new(3 * Math.cos(Math::PI / 4), 5 * Math.sin(Math::PI / 4))
      assert_equal(a, b, 'Failed Rotate')
    end 
  end
end

@monkstone
Copy link
Contributor Author

Still failing with:-
jruby 9.1.0.0-SNAPSHOT (2.3.0) 2016-02-27 e657f19 OpenJDK 64-Bit Server VM 25.74-b02 on 1.8.0_74-b02 +jit [linux-amd64]

@kares kares added this to the Invalid or Duplicate milestone Feb 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants