Daniele Alessandri
2009-Feb-12 14:30 UTC
[Ironruby-core] Throwing Exceptions defined in Ruby from C#
Hi, I''m refactoring and polishing the source code of my port of json/ext to IronRuby (it''s feature complete by now) but I''m wondering if there is a better way to throw an Exception defined in ruby from C# compared to the only solution I''ve came up with so far. Here is the code I''m using right now, stripped down of checks and condensed in one method just for the sake of brevity: ----- ruby ----- module JSON class JSONError < StandardError; end class ParserError < JSONError; end end ----- C# ----- public static void RaiseParserError(RubyScope scope, String msg) { RubyModule eParserError; scope.RubyContext.TryGetModule(scope.GlobalScope, "JSON::ParserError", out eParserError ); RubyClass exceptionClass = eParserError as RubyClass; Type underlyingType = exceptionClass.GetUnderlyingSystemType(); BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance; ConstructorInfo constructor = underlyingType.GetConstructor( bindingFlags, null, new[] { typeof(RubyClass), typeof(String) }, null ); Exception exceptionInstance = constructor.Invoke( new object[] { exceptionClass, msg } ) as Exception; throw exceptionInstance; } -- Daniele Alessandri http://www.clorophilla.net/blog/
Tomas Matousek
2009-Feb-13 03:33 UTC
[Ironruby-core] Throwing Exceptions defined in Ruby from C#
Why don''t you declare the exceptions in C#? I assume the library is primarily to be used from Ruby code, not from C#, right? Tomas -----Original Message----- From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Daniele Alessandri Sent: Thursday, February 12, 2009 6:31 AM To: ironruby-core at rubyforge.org Subject: [Ironruby-core] Throwing Exceptions defined in Ruby from C# Hi, I''m refactoring and polishing the source code of my port of json/ext to IronRuby (it''s feature complete by now) but I''m wondering if there is a better way to throw an Exception defined in ruby from C# compared to the only solution I''ve came up with so far. Here is the code I''m using right now, stripped down of checks and condensed in one method just for the sake of brevity: ----- ruby ----- module JSON class JSONError < StandardError; end class ParserError < JSONError; end end ----- C# ----- public static void RaiseParserError(RubyScope scope, String msg) { RubyModule eParserError; scope.RubyContext.TryGetModule(scope.GlobalScope, "JSON::ParserError", out eParserError ); RubyClass exceptionClass = eParserError as RubyClass; Type underlyingType = exceptionClass.GetUnderlyingSystemType(); BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance; ConstructorInfo constructor = underlyingType.GetConstructor( bindingFlags, null, new[] { typeof(RubyClass), typeof(String) }, null ); Exception exceptionInstance = constructor.Invoke( new object[] { exceptionClass, msg } ) as Exception; throw exceptionInstance; } -- Daniele Alessandri http://www.clorophilla.net/blog/ _______________________________________________ Ironruby-core mailing list Ironruby-core at rubyforge.org http://rubyforge.org/mailman/listinfo/ironruby-core
Daniele Alessandri
2009-Feb-13 07:59 UTC
[Ironruby-core] Throwing Exceptions defined in Ruby from C#
Yes it is meant to be used primarily from ruby as it is a port of a rather widespread library, and a dependency for a bunch of other libraries (see http://json.rubyforge.org/). The reason is simply to maintain compatibility with the ruby bits of the original json library in which exceptions are defined inside a common.rb file: this is due to the fact that this lib comes in two flavours, json-pure (everything is ruby) and json-ext (the generator and parser are implemented in C, the rest is ruby), so there are parts of the code that are shared between the two and eventually invoked from within the native code. Anyway I''m curious, I haven''t found other means to do that in a much cleaner way :) On Fri, Feb 13, 2009 at 04:33, Tomas Matousek <Tomas.Matousek at microsoft.com> wrote:> Why don''t you declare the exceptions in C#? I assume the library is primarily to be used from Ruby code, not from C#, right? > > Tomas > > -----Original Message----- > From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Daniele Alessandri > Sent: Thursday, February 12, 2009 6:31 AM > To: ironruby-core at rubyforge.org > Subject: [Ironruby-core] Throwing Exceptions defined in Ruby from C# > > Hi, > > I''m refactoring and polishing the source code of my port of json/ext > to IronRuby (it''s feature complete by now) but I''m wondering if there > is a better way to throw an Exception defined in ruby from C# compared > to the only solution I''ve came up with so far. Here is the code I''m > using right now, stripped down of checks and condensed in one method > just for the sake of brevity: > > ----- ruby ----- > > module JSON > class JSONError < StandardError; end > class ParserError < JSONError; end > end > > ----- C# ----- > > public static void RaiseParserError(RubyScope scope, String msg) { > RubyModule eParserError; > > scope.RubyContext.TryGetModule(scope.GlobalScope, > "JSON::ParserError", > out eParserError > ); > > RubyClass exceptionClass = eParserError as RubyClass; > Type underlyingType = exceptionClass.GetUnderlyingSystemType(); > > BindingFlags bindingFlags = BindingFlags.NonPublic | > BindingFlags.Public | > BindingFlags.Instance; > > ConstructorInfo constructor = underlyingType.GetConstructor( > bindingFlags, > null, > new[] { typeof(RubyClass), typeof(String) }, > null > ); > > Exception exceptionInstance = constructor.Invoke( > new object[] { exceptionClass, msg } > ) as Exception; > > throw exceptionInstance; > } > > > -- > Daniele Alessandri > http://www.clorophilla.net/blog/ > _______________________________________________ > Ironruby-core mailing list > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core > > _______________________________________________ > Ironruby-core mailing list > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core >-- Daniele Alessandri http://www.clorophilla.net/blog/
Shri Borde
2009-Feb-13 17:55 UTC
[Ironruby-core] Throwing Exceptions defined in Ruby from C#
I don''t think your code below will work as multiple Ruby types share the same underlying CLR type. If you want to do things in a strongly-bound way, you would need to use the RubyUtils and other runtime helpers to find the method "new", invoke it, etc. instead of using System.Reflection. An alternative is to just do an eval. Something like: RubyUtils.Evaluate("raise JSONError", scope) -----Original Message----- From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Daniele Alessandri Sent: Thursday, February 12, 2009 11:59 PM To: ironruby-core at rubyforge.org Subject: Re: [Ironruby-core] Throwing Exceptions defined in Ruby from C# Yes it is meant to be used primarily from ruby as it is a port of a rather widespread library, and a dependency for a bunch of other libraries (see http://json.rubyforge.org/). The reason is simply to maintain compatibility with the ruby bits of the original json library in which exceptions are defined inside a common.rb file: this is due to the fact that this lib comes in two flavours, json-pure (everything is ruby) and json-ext (the generator and parser are implemented in C, the rest is ruby), so there are parts of the code that are shared between the two and eventually invoked from within the native code. Anyway I''m curious, I haven''t found other means to do that in a much cleaner way :) On Fri, Feb 13, 2009 at 04:33, Tomas Matousek <Tomas.Matousek at microsoft.com> wrote:> Why don''t you declare the exceptions in C#? I assume the library is primarily to be used from Ruby code, not from C#, right? > > Tomas > > -----Original Message----- > From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Daniele Alessandri > Sent: Thursday, February 12, 2009 6:31 AM > To: ironruby-core at rubyforge.org > Subject: [Ironruby-core] Throwing Exceptions defined in Ruby from C# > > Hi, > > I''m refactoring and polishing the source code of my port of json/ext > to IronRuby (it''s feature complete by now) but I''m wondering if there > is a better way to throw an Exception defined in ruby from C# compared > to the only solution I''ve came up with so far. Here is the code I''m > using right now, stripped down of checks and condensed in one method > just for the sake of brevity: > > ----- ruby ----- > > module JSON > class JSONError < StandardError; end > class ParserError < JSONError; end > end > > ----- C# ----- > > public static void RaiseParserError(RubyScope scope, String msg) { > RubyModule eParserError; > > scope.RubyContext.TryGetModule(scope.GlobalScope, > "JSON::ParserError", > out eParserError > ); > > RubyClass exceptionClass = eParserError as RubyClass; > Type underlyingType = exceptionClass.GetUnderlyingSystemType(); > > BindingFlags bindingFlags = BindingFlags.NonPublic | > BindingFlags.Public | > BindingFlags.Instance; > > ConstructorInfo constructor = underlyingType.GetConstructor( > bindingFlags, > null, > new[] { typeof(RubyClass), typeof(String) }, > null > ); > > Exception exceptionInstance = constructor.Invoke( > new object[] { exceptionClass, msg } > ) as Exception; > > throw exceptionInstance; > } > > > -- > Daniele Alessandri > http://www.clorophilla.net/blog/ > _______________________________________________ > Ironruby-core mailing list > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core > > _______________________________________________ > Ironruby-core mailing list > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core >-- Daniele Alessandri http://www.clorophilla.net/blog/ _______________________________________________ Ironruby-core mailing list Ironruby-core at rubyforge.org http://rubyforge.org/mailman/listinfo/ironruby-core
Tomas Matousek
2009-Feb-13 18:34 UTC
[Ironruby-core] Throwing Exceptions defined in Ruby from C#
You can use KernelOps.RaiseException: public static void RaiseException(RespondToStorage/*!*/ respondToStorage, UnaryOpStorage/*!*/ storage0, BinaryOpStorage/*!*/ storage1, RubyContext/*!*/ context, object self, object/*!*/ obj, [Optional]object arg, [Optional]RubyArray backtrace) { You''ll need to allocate local site storages to do so. The storages are allocated automatically for methods called from Ruby. I assume you have some entry point to the C# part of the parser that you call from Ruby. That method should declare the storages and pass them thru to the place where you want to raise the exception. It might be easier to change your C# implementation not to throw Ruby exceptions. You can throw a C# exception, catch it in Ruby and rethrow the corresponding Ruby exception. This way your C# code would be usable from C# apps as well, not only from Ruby code. Tomas -----Original Message----- From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Shri Borde Sent: Friday, February 13, 2009 9:56 AM To: ironruby-core at rubyforge.org Subject: Re: [Ironruby-core] Throwing Exceptions defined in Ruby from C# I don''t think your code below will work as multiple Ruby types share the same underlying CLR type. If you want to do things in a strongly-bound way, you would need to use the RubyUtils and other runtime helpers to find the method "new", invoke it, etc. instead of using System.Reflection. An alternative is to just do an eval. Something like: RubyUtils.Evaluate("raise JSONError", scope) -----Original Message----- From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Daniele Alessandri Sent: Thursday, February 12, 2009 11:59 PM To: ironruby-core at rubyforge.org Subject: Re: [Ironruby-core] Throwing Exceptions defined in Ruby from C# Yes it is meant to be used primarily from ruby as it is a port of a rather widespread library, and a dependency for a bunch of other libraries (see http://json.rubyforge.org/). The reason is simply to maintain compatibility with the ruby bits of the original json library in which exceptions are defined inside a common.rb file: this is due to the fact that this lib comes in two flavours, json-pure (everything is ruby) and json-ext (the generator and parser are implemented in C, the rest is ruby), so there are parts of the code that are shared between the two and eventually invoked from within the native code. Anyway I''m curious, I haven''t found other means to do that in a much cleaner way :) On Fri, Feb 13, 2009 at 04:33, Tomas Matousek <Tomas.Matousek at microsoft.com> wrote:> Why don''t you declare the exceptions in C#? I assume the library is primarily to be used from Ruby code, not from C#, right? > > Tomas > > -----Original Message----- > From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Daniele Alessandri > Sent: Thursday, February 12, 2009 6:31 AM > To: ironruby-core at rubyforge.org > Subject: [Ironruby-core] Throwing Exceptions defined in Ruby from C# > > Hi, > > I''m refactoring and polishing the source code of my port of json/ext > to IronRuby (it''s feature complete by now) but I''m wondering if there > is a better way to throw an Exception defined in ruby from C# compared > to the only solution I''ve came up with so far. Here is the code I''m > using right now, stripped down of checks and condensed in one method > just for the sake of brevity: > > ----- ruby ----- > > module JSON > class JSONError < StandardError; end > class ParserError < JSONError; end > end > > ----- C# ----- > > public static void RaiseParserError(RubyScope scope, String msg) { > RubyModule eParserError; > > scope.RubyContext.TryGetModule(scope.GlobalScope, > "JSON::ParserError", > out eParserError > ); > > RubyClass exceptionClass = eParserError as RubyClass; > Type underlyingType = exceptionClass.GetUnderlyingSystemType(); > > BindingFlags bindingFlags = BindingFlags.NonPublic | > BindingFlags.Public | > BindingFlags.Instance; > > ConstructorInfo constructor = underlyingType.GetConstructor( > bindingFlags, > null, > new[] { typeof(RubyClass), typeof(String) }, > null > ); > > Exception exceptionInstance = constructor.Invoke( > new object[] { exceptionClass, msg } > ) as Exception; > > throw exceptionInstance; > } > > > -- > Daniele Alessandri > http://www.clorophilla.net/blog/ > _______________________________________________ > Ironruby-core mailing list > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core > > _______________________________________________ > Ironruby-core mailing list > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core >-- Daniele Alessandri http://www.clorophilla.net/blog/ _______________________________________________ Ironruby-core mailing list Ironruby-core at rubyforge.org http://rubyforge.org/mailman/listinfo/ironruby-core _______________________________________________ Ironruby-core mailing list Ironruby-core at rubyforge.org http://rubyforge.org/mailman/listinfo/ironruby-core
Pete Bacon Darwin
2009-Feb-13 18:57 UTC
[Ironruby-core] Throwing Exceptions defined in Ruby from C#
Could you not achieve the wanted effect through a kind of monkey patching? I.E. Create the exception class in C# then redeclare it in the common.rb. The new class in Ruby would extend the one created in C# but wouldn''t change anything if the C# one was there in the first place. If it was there, i.e. you were using the pure Ruby version, then it would just be creating it for the first time. No harm done. Off the top of my head, I haven''t thought hard about this or tried it... There are probably issues of order declaration or something. Pete
Shri Borde
2009-Feb-13 19:12 UTC
[Ironruby-core] Throwing Exceptions defined in Ruby from C#
Yes, the approach of throwing a C# exception and converting it to a Ruby exception is ideal as it preserves good layering and allows more code reuse (since the C# code could be used by IronPython, C#, VB.Net, etc). -----Original Message----- From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Tomas Matousek Sent: Friday, February 13, 2009 10:35 AM To: ironruby-core at rubyforge.org Subject: Re: [Ironruby-core] Throwing Exceptions defined in Ruby from C# You can use KernelOps.RaiseException: public static void RaiseException(RespondToStorage/*!*/ respondToStorage, UnaryOpStorage/*!*/ storage0, BinaryOpStorage/*!*/ storage1, RubyContext/*!*/ context, object self, object/*!*/ obj, [Optional]object arg, [Optional]RubyArray backtrace) { You''ll need to allocate local site storages to do so. The storages are allocated automatically for methods called from Ruby. I assume you have some entry point to the C# part of the parser that you call from Ruby. That method should declare the storages and pass them thru to the place where you want to raise the exception. It might be easier to change your C# implementation not to throw Ruby exceptions. You can throw a C# exception, catch it in Ruby and rethrow the corresponding Ruby exception. This way your C# code would be usable from C# apps as well, not only from Ruby code. Tomas -----Original Message----- From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Shri Borde Sent: Friday, February 13, 2009 9:56 AM To: ironruby-core at rubyforge.org Subject: Re: [Ironruby-core] Throwing Exceptions defined in Ruby from C# I don''t think your code below will work as multiple Ruby types share the same underlying CLR type. If you want to do things in a strongly-bound way, you would need to use the RubyUtils and other runtime helpers to find the method "new", invoke it, etc. instead of using System.Reflection. An alternative is to just do an eval. Something like: RubyUtils.Evaluate("raise JSONError", scope) -----Original Message----- From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Daniele Alessandri Sent: Thursday, February 12, 2009 11:59 PM To: ironruby-core at rubyforge.org Subject: Re: [Ironruby-core] Throwing Exceptions defined in Ruby from C# Yes it is meant to be used primarily from ruby as it is a port of a rather widespread library, and a dependency for a bunch of other libraries (see http://json.rubyforge.org/). The reason is simply to maintain compatibility with the ruby bits of the original json library in which exceptions are defined inside a common.rb file: this is due to the fact that this lib comes in two flavours, json-pure (everything is ruby) and json-ext (the generator and parser are implemented in C, the rest is ruby), so there are parts of the code that are shared between the two and eventually invoked from within the native code. Anyway I''m curious, I haven''t found other means to do that in a much cleaner way :) On Fri, Feb 13, 2009 at 04:33, Tomas Matousek <Tomas.Matousek at microsoft.com> wrote:> Why don''t you declare the exceptions in C#? I assume the library is primarily to be used from Ruby code, not from C#, right? > > Tomas > > -----Original Message----- > From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Daniele Alessandri > Sent: Thursday, February 12, 2009 6:31 AM > To: ironruby-core at rubyforge.org > Subject: [Ironruby-core] Throwing Exceptions defined in Ruby from C# > > Hi, > > I''m refactoring and polishing the source code of my port of json/ext > to IronRuby (it''s feature complete by now) but I''m wondering if there > is a better way to throw an Exception defined in ruby from C# compared > to the only solution I''ve came up with so far. Here is the code I''m > using right now, stripped down of checks and condensed in one method > just for the sake of brevity: > > ----- ruby ----- > > module JSON > class JSONError < StandardError; end > class ParserError < JSONError; end > end > > ----- C# ----- > > public static void RaiseParserError(RubyScope scope, String msg) { > RubyModule eParserError; > > scope.RubyContext.TryGetModule(scope.GlobalScope, > "JSON::ParserError", > out eParserError > ); > > RubyClass exceptionClass = eParserError as RubyClass; > Type underlyingType = exceptionClass.GetUnderlyingSystemType(); > > BindingFlags bindingFlags = BindingFlags.NonPublic | > BindingFlags.Public | > BindingFlags.Instance; > > ConstructorInfo constructor = underlyingType.GetConstructor( > bindingFlags, > null, > new[] { typeof(RubyClass), typeof(String) }, > null > ); > > Exception exceptionInstance = constructor.Invoke( > new object[] { exceptionClass, msg } > ) as Exception; > > throw exceptionInstance; > } > > > -- > Daniele Alessandri > http://www.clorophilla.net/blog/ > _______________________________________________ > Ironruby-core mailing list > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core > > _______________________________________________ > Ironruby-core mailing list > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core >-- Daniele Alessandri http://www.clorophilla.net/blog/ _______________________________________________ Ironruby-core mailing list Ironruby-core at rubyforge.org http://rubyforge.org/mailman/listinfo/ironruby-core _______________________________________________ Ironruby-core mailing list Ironruby-core at rubyforge.org http://rubyforge.org/mailman/listinfo/ironruby-core _______________________________________________ Ironruby-core mailing list Ironruby-core at rubyforge.org http://rubyforge.org/mailman/listinfo/ironruby-core
Daniele Alessandri
2009-Feb-13 20:36 UTC
[Ironruby-core] Throwing Exceptions defined in Ruby from C#
Yeah, I don''t really like my solution exactly because it is dependent on the underlying type. By the way I already tried to call JSON::ParserError#new from C# to get an instance of the class and it actually worked, but then the exception was getting thrown with an empty stack trace (well I can guess the reason for that and it is not something unexpected). On Fri, Feb 13, 2009 at 18:55, Shri Borde <Shri.Borde at microsoft.com> wrote:> I don''t think your code below will work as multiple Ruby types share the same underlying CLR type. If you want to do things in a strongly-bound way, you would need to use the RubyUtils and other runtime helpers to find the method "new", invoke it, etc. instead of using System.Reflection. > > An alternative is to just do an eval. Something like: > RubyUtils.Evaluate("raise JSONError", scope) > > -----Original Message----- > From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Daniele Alessandri > Sent: Thursday, February 12, 2009 11:59 PM > To: ironruby-core at rubyforge.org > Subject: Re: [Ironruby-core] Throwing Exceptions defined in Ruby from C# > > Yes it is meant to be used primarily from ruby as it is a port of a > rather widespread library, and a dependency for a bunch of other > libraries (see http://json.rubyforge.org/). > > The reason is simply to maintain compatibility with the ruby bits of > the original json library in which exceptions are defined inside a > common.rb file: this is due to the fact that this lib comes in two > flavours, json-pure (everything is ruby) and json-ext (the generator > and parser are implemented in C, the rest is ruby), so there are parts > of the code that are shared between the two and eventually invoked > from within the native code. > > Anyway I''m curious, I haven''t found other means to do that in a much > cleaner way :) > > > On Fri, Feb 13, 2009 at 04:33, Tomas Matousek > <Tomas.Matousek at microsoft.com> wrote: >> Why don''t you declare the exceptions in C#? I assume the library is primarily to be used from Ruby code, not from C#, right? >> >> Tomas >> >> -----Original Message----- >> From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Daniele Alessandri >> Sent: Thursday, February 12, 2009 6:31 AM >> To: ironruby-core at rubyforge.org >> Subject: [Ironruby-core] Throwing Exceptions defined in Ruby from C# >> >> Hi, >> >> I''m refactoring and polishing the source code of my port of json/ext >> to IronRuby (it''s feature complete by now) but I''m wondering if there >> is a better way to throw an Exception defined in ruby from C# compared >> to the only solution I''ve came up with so far. Here is the code I''m >> using right now, stripped down of checks and condensed in one method >> just for the sake of brevity: >> >> ----- ruby ----- >> >> module JSON >> class JSONError < StandardError; end >> class ParserError < JSONError; end >> end >> >> ----- C# ----- >> >> public static void RaiseParserError(RubyScope scope, String msg) { >> RubyModule eParserError; >> >> scope.RubyContext.TryGetModule(scope.GlobalScope, >> "JSON::ParserError", >> out eParserError >> ); >> >> RubyClass exceptionClass = eParserError as RubyClass; >> Type underlyingType = exceptionClass.GetUnderlyingSystemType(); >> >> BindingFlags bindingFlags = BindingFlags.NonPublic | >> BindingFlags.Public | >> BindingFlags.Instance; >> >> ConstructorInfo constructor = underlyingType.GetConstructor( >> bindingFlags, >> null, >> new[] { typeof(RubyClass), typeof(String) }, >> null >> ); >> >> Exception exceptionInstance = constructor.Invoke( >> new object[] { exceptionClass, msg } >> ) as Exception; >> >> throw exceptionInstance; >> } >> >> >> -- >> Daniele Alessandri >> http://www.clorophilla.net/blog/ >> _______________________________________________ >> Ironruby-core mailing list >> Ironruby-core at rubyforge.org >> http://rubyforge.org/mailman/listinfo/ironruby-core >> >> _______________________________________________ >> Ironruby-core mailing list >> Ironruby-core at rubyforge.org >> http://rubyforge.org/mailman/listinfo/ironruby-core >> > > > > -- > Daniele Alessandri > http://www.clorophilla.net/blog/ > _______________________________________________ > Ironruby-core mailing list > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core > > _______________________________________________ > Ironruby-core mailing list > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core >-- Daniele Alessandri http://www.clorophilla.net/blog/
Daniele Alessandri
2009-Feb-13 20:51 UTC
[Ironruby-core] Throwing Exceptions defined in Ruby from C#
I will try going with this solution, thanks. As for the reusability of the code from other applications/languages, I think it would be something impossibile to begin with as the library is tightly coupled with ruby (especially the generator class, it interacts a lot with ruby code) and this is due to an intrinsic factor of its conceptual design (which goes back to the original implementation) and IMHO it is not really a matter of bad design choices. On Fri, Feb 13, 2009 at 19:34, Tomas Matousek <Tomas.Matousek at microsoft.com> wrote:> You can use KernelOps.RaiseException: > > public static void RaiseException(RespondToStorage/*!*/ respondToStorage, UnaryOpStorage/*!*/ storage0, BinaryOpStorage/*!*/ storage1, > RubyContext/*!*/ context, object self, object/*!*/ obj, [Optional]object arg, [Optional]RubyArray backtrace) { > > > You''ll need to allocate local site storages to do so. The storages are allocated automatically for methods called from Ruby. > I assume you have some entry point to the C# part of the parser that you call from Ruby. That method should declare the storages and pass them thru to the place where you want to raise the exception. > > It might be easier to change your C# implementation not to throw Ruby exceptions. You can throw a C# exception, catch it in Ruby and rethrow the corresponding Ruby exception. This way your C# code would be usable from C# apps as well, not only from Ruby code. > > Tomas > > -----Original Message----- > From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Shri Borde > Sent: Friday, February 13, 2009 9:56 AM > To: ironruby-core at rubyforge.org > Subject: Re: [Ironruby-core] Throwing Exceptions defined in Ruby from C# > > I don''t think your code below will work as multiple Ruby types share the same underlying CLR type. If you want to do things in a strongly-bound way, you would need to use the RubyUtils and other runtime helpers to find the method "new", invoke it, etc. instead of using System.Reflection. > > An alternative is to just do an eval. Something like: > RubyUtils.Evaluate("raise JSONError", scope) > > -----Original Message----- > From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Daniele Alessandri > Sent: Thursday, February 12, 2009 11:59 PM > To: ironruby-core at rubyforge.org > Subject: Re: [Ironruby-core] Throwing Exceptions defined in Ruby from C# > > Yes it is meant to be used primarily from ruby as it is a port of a > rather widespread library, and a dependency for a bunch of other > libraries (see http://json.rubyforge.org/). > > The reason is simply to maintain compatibility with the ruby bits of > the original json library in which exceptions are defined inside a > common.rb file: this is due to the fact that this lib comes in two > flavours, json-pure (everything is ruby) and json-ext (the generator > and parser are implemented in C, the rest is ruby), so there are parts > of the code that are shared between the two and eventually invoked > from within the native code. > > Anyway I''m curious, I haven''t found other means to do that in a much > cleaner way :) > > > On Fri, Feb 13, 2009 at 04:33, Tomas Matousek > <Tomas.Matousek at microsoft.com> wrote: >> Why don''t you declare the exceptions in C#? I assume the library is primarily to be used from Ruby code, not from C#, right? >> >> Tomas >> >> -----Original Message----- >> From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Daniele Alessandri >> Sent: Thursday, February 12, 2009 6:31 AM >> To: ironruby-core at rubyforge.org >> Subject: [Ironruby-core] Throwing Exceptions defined in Ruby from C# >> >> Hi, >> >> I''m refactoring and polishing the source code of my port of json/ext >> to IronRuby (it''s feature complete by now) but I''m wondering if there >> is a better way to throw an Exception defined in ruby from C# compared >> to the only solution I''ve came up with so far. Here is the code I''m >> using right now, stripped down of checks and condensed in one method >> just for the sake of brevity: >> >> ----- ruby ----- >> >> module JSON >> class JSONError < StandardError; end >> class ParserError < JSONError; end >> end >> >> ----- C# ----- >> >> public static void RaiseParserError(RubyScope scope, String msg) { >> RubyModule eParserError; >> >> scope.RubyContext.TryGetModule(scope.GlobalScope, >> "JSON::ParserError", >> out eParserError >> ); >> >> RubyClass exceptionClass = eParserError as RubyClass; >> Type underlyingType = exceptionClass.GetUnderlyingSystemType(); >> >> BindingFlags bindingFlags = BindingFlags.NonPublic | >> BindingFlags.Public | >> BindingFlags.Instance; >> >> ConstructorInfo constructor = underlyingType.GetConstructor( >> bindingFlags, >> null, >> new[] { typeof(RubyClass), typeof(String) }, >> null >> ); >> >> Exception exceptionInstance = constructor.Invoke( >> new object[] { exceptionClass, msg } >> ) as Exception; >> >> throw exceptionInstance; >> } >> >> >> -- >> Daniele Alessandri >> http://www.clorophilla.net/blog/ >> _______________________________________________ >> Ironruby-core mailing list >> Ironruby-core at rubyforge.org >> http://rubyforge.org/mailman/listinfo/ironruby-core >> >> _______________________________________________ >> Ironruby-core mailing list >> Ironruby-core at rubyforge.org >> http://rubyforge.org/mailman/listinfo/ironruby-core >> > > > > -- > Daniele Alessandri > http://www.clorophilla.net/blog/ > _______________________________________________ > Ironruby-core mailing list > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core > > _______________________________________________ > Ironruby-core mailing list > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core > > _______________________________________________ > Ironruby-core mailing list > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core >-- Daniele Alessandri http://www.clorophilla.net/blog/
Daniele Alessandri
2009-Feb-13 20:54 UTC
[Ironruby-core] Throwing Exceptions defined in Ruby from C#
I might try that just out of curiosity to see how it behaves, even though (honestly) I don''t find it too much of a clean solution. Thanks! On Fri, Feb 13, 2009 at 19:57, Pete Bacon Darwin <bacondarwin at googlemail.com> wrote:> Could you not achieve the wanted effect through a kind of monkey patching? > I.E. Create the exception class in C# then redeclare it in the common.rb. > The new class in Ruby would extend the one created in C# but wouldn''t change > anything if the C# one was there in the first place. If it was there, i.e. > you were using the pure Ruby version, then it would just be creating it for > the first time. No harm done. > Off the top of my head, I haven''t thought hard about this or tried it... > There are probably issues of order declaration or something. > Pete > > _______________________________________________ > Ironruby-core mailing list > Ironruby-core at rubyforge.org > http://rubyforge.org/mailman/listinfo/ironruby-core >-- Daniele Alessandri http://www.clorophilla.net/blog/