[IronPython] IronLanguages

Matthias nitro at dr-code.org
Tue Mar 29 11:57:38 PDT 2011

Am 29.03.2011, 19:27 Uhr, schrieb Dino Viehland <dinov at microsoft.com>:

> Just to chime in on how to do the conversion: the answer is that you  
> probably can't, at least not for something like TestClass.  You could  
> look at what sort of type you're converting from in JS (number, string,  
> function, etc...) and see if TestClass has any implicit conversions to  
> it from primitive .NET types (double, string, delegate, etc...) and if  
> so you could invoke one of those conversions.  But most likely you won't

This is what I am doing now already and it works quite nicely for simple  

> be able to convert directly to TestClass.  If TestClass was something  
> more like IList then you could do a conversion there as well.  The only  
> way you're likely able to convert to TestClass is if the user got a  
> TestClass object from .NET, brought it into JS (where you wrapped it in  
> some object of your own), and then you brought it back to .NET - but  
> then when you bring it back to .NET you should bring it back as the real  
> TestClass instead of your wrapped TestClass.

Yes, I have thought to force users to "inherit" from (use a prototype  
object) which comes from .net.

> The only other way would be figuring out somehow to allow a JavaScript  
> developer to subclass a .NET TestClass type.  We allow this in  
> IronPython via our NewTypeMaker class but we are lucky in that  
> everything is in the .NET world.  One way you could go about this is  
> having a function like CreateClass which takes a subtype (TestClass) and  
> a dictionary of string -> JS functions which you then call into for the  
> implementation of the various subclass methods.  You would then have a  
> .NET type which is being extended by JavaScript - there's probably going  
> to be some fun and tricky problems in doing this though.  Then the user  
> can call the resulting class you give them, they'll get an instance  
> which is wrapped in JavaScript, and when you pass it back to .NET you  
> can unwrap it.

Yes, this was my main idea. It's very similar how SWIG directors handle  
cross-language polymorphism. At runtime I'd create a class which derives  
 from TestClass and which overrides all virtual methods and properties. The  
C# overrides would call the JSObject to see if there's a javascript  
implementation for them. If there is, it will call the javascript  
implementation, otherwise it will call the base class's (TestClass in the  
example) implementation.


More information about the Users mailing list