Here’s a drop of my RbDynamicMethod library. You’ll need to have some version of Visual C++ 2005 installed to compile it. I’ve supplied a Rakefile, so the build is pretty painless provided that you have cl.exe somewhere on your path.
All of the C++ code can be found in RbDynamicMethod.h. All of the Ruby code can be found in Tests\ruby_dynamic_method.rb. The documentation is in Tests\tests.rb :)
My most recent addition is a create_safe_ruby_method wrapper. It creates a method that is callable from Ruby, but wraps the entire user-defined block in an exception handler that traps nearly all1 CLR exceptions and maps them to a Ruby RuntimeError exception.
Here’s a normal create_ruby_method from the unit tests. The weird ldc_i4_4 instruction is required since this is a Ruby varargs method that has a VALUE (int, VALUE*, VALUE) signature, and I need to return a Qnil (integer value 4) from this method.
create_ruby_method('convert_clr_exception') do
try
ldstr 'error'
newobj 'Exception.ctor(String)'
throw_ex
catch_ex 'Exception'
call 'static ExceptionHelper.RaiseRubyException(Exception)'
end_try
ldc_i4_4
ret
endHere’s the same method via create_safe_ruby_method, also from the unit tests:
create_safe_ruby_method('catch_clr_exception') do
ldstr 'error'
newobj 'Exception.ctor(String)'
throw_ex
endComments and suggestions would be greatly appreciated.
[1] My code catches exceptions that derive only from Exception whereas it’s possible to throw exceptions that derive from Object.