-
-
Notifications
You must be signed in to change notification settings - Fork 925
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
Comments
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. |
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? |
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... |
@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. |
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. |
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 |
Still failing with:- |
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
It worked as expected see gist https://gist.github.com/monkstone/1620da6a63cba504931b
The text was updated successfully, but these errors were encountered: