[IronPython] IronLanguages

Dino Viehland dinov at microsoft.com
Tue Mar 29 19:27:11 CEST 2011


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 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.

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.


-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Bill Chiles
Sent: Tuesday, March 29, 2011 9:57 AM
To: Discussion of IronPython
Subject: Re: [IronPython] IronLanguages

You'll want to look at the DLR overview doc and then the Sympl sample walkthrough doc:
http://dlr.codeplex.com/wikipage?title=Docs%20and%20specs&referringTitle=Home&ProjectName=dlr 

You'll want to type the parameter to testComplexObject as 'dynamic' and implement IDMOP on JSObject, which you can see how to do from the Sympl sample.  Now, the Sympl sample is VERY light on real .NET bindin, but if it all maps to GetProp/SetProp, then maybe this is fine.  If you might flow into your code a regular C# object (not just a JSObj), then you may want to make use of the DefaultBinder from the DLR project, which is what the Iron languages use to get much richer binding.  You could also make use of the C# runtime binder to get C#'s semantics for binding members of objects at runtime, but you get that for free if you declare the parameter 'dynamic' and have your JSOjbectMetaObject simply punt whenever the object is not a derived type of JSObject.  You get that for free because you'll call back on the binder at the obj.message call site, and C# will have compiled that callsite to use its binder.

Bill

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Matthias
Sent: Tuesday, March 29, 2011 3:34 AM
To: users at lists.ironpython.com
Subject: [IronPython] IronLanguages

Hello,

this question is not 100% targeted at IronPython, but I didn't know a better list to write to.

I've started writing a C# <-> javascript bridge. Unlike IronPython and IronRuby I don't want to write a javascript engine in .net, but rather use existing ones. I can already access C# classes from javascript and instance them. The opposite way turns out to be much harder for non-PODs.  
Example:

public class JSObject
{
     // has members like GetProperty/SetProperty which can act upon the javascript object }

public class TestClass
{
     public string message = "This is a C# string"; }

public class TestApp
{
     public string testComplexObject(TestClass obj)
     {
         return obj.message;
     }

     public void runTest()
     {
         JSObject jsObj = ...;
         string message = testComplexObject(jsObj);
     }
}

The problem here is the "testComplexObject(jsObj)" call. Of course the jsObj cannot be converted directly to a TestClass, because it's an arbitrary javascript object.

I am wondering how IronPython solves this problem. I've read the sources a bit and it seems it makes use of IDynamicMetaObjectProvider etc. If JSObject provided IDynamicMetaObjectProvider, would it allow "converting"  
the jsObj to a TestClass obj? How?

It's not easy to find information on using the DLR for things like this on the net, so I've asked here. Apologies if I am off-topic.

-Matthias
_______________________________________________
Users mailing list
Users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

_______________________________________________
Users mailing list
Users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com



More information about the Ironpython-users mailing list