This project has moved and is read-only. For the latest updates, please go here.

Add property reference as host object

Feb 18, 2013 at 1:06 PM

I am continue playing with clear script, and found interesting thing.

For example i want to expose property reference.
My property has getter and setter. I have set breakpoints on both.

I am exposing property via Script object, like this:
engine.Script.height = myContainer.Height; //int
At this point i see that getter was fired, and engine took Height property value.

But when i try to modify .net value from script, and i try to do following:
height = 11;
inner value, that in the script, has been changed, but .net value remains the same.
And setter didn't fire.

So my question - is there any way to bind property reference to script variable?
Can we make script change .net value instead of local copy?

Feb 19, 2013 at 1:56 AM
Hi pro100sanya!

Your first line of code above reads the myContainer.Height property and assigns its value to a new property on the global script object. Those two properties are not bound together in any way.

If you exposed myContainer instead, then script code could modify its Height property and hit your getter and setter methods. If that's not an option, you could use delegates or script functions to set up getHeight and setHeight methods on the global script object, but that's probably the closest you could come to your desired behavior.

It sounds like what you're looking for is the ability to use a host object in place of the global script object. Is that correct? If so, that feature might be doable with V8, but not with the Windows Script engines, and it would require new APIs.

Normally we'd prefer to keep the APIs identical across supported script engines, but please tell us more about what you're trying to do!
Feb 19, 2013 at 7:43 PM
Edited Aug 3, 2013 at 3:36 AM
Hi again!

After doing some investigation, we have good news and bad news :)

The good news: It is possible, in the current version of ClearScript, to expose myContainer so that its properties appear within the global namespace:
// C#
engine.AddHostObject("unusedName", HostItemFlags.GlobalMembers, myContainer);
This way the host object is "merged" with the global script object. Script code can do this:
// JavaScript
Height = 11;
This assignment will invoke the myContainer.Height setter.

The bad news: This capability is currently broken in V8ScriptEngine. It works for property reads but not writes. We will get that fixed ASAP!
Feb 19, 2013 at 8:28 PM
We're tracking this issue here. We are testing a fix, so please stay tuned!
Feb 20, 2013 at 2:11 AM
We've posted an update that fixes the issue in V8ScriptEngine.