IronJS CommonObject, are dynamic properties possible?
In IronJS, we have a custom object derived from CommonObject. We're wanting to intercept calls to undefined properties on the object, and provide dynamic responses. (This is required, as it is not possible in our situation to preregister all the properties.)
We can capture function calls on this object by overriding the BoxedValue Get(string name)
function, and provide functions "on the fly" without preregistering them on the object.
We're hoping we can do the same with properties, but none of the overrides seem to be able to handle this. I'm hoping that someone has enough experience with IronJS to suggest how we can better approach this.
Hopefully this clarifies what we're trying to achieve:
IronJS.Hosting.CSharp.Context ctx = new IronJS.Hosting.CSharp.Context();
ctx.SetGlobal("data", new MyCustomObject());
string script = @"var x = data.mydynamicproperty;";
ctx.Execute(script);
When the script executes, we are wanting to be able to override and return a custom value. For example (on the MyCustomObject class declaration):
public override BoxedValue Get(string name) {
if (name == "mydynamicproperty") {
return BoxedValue.Box("test");
}
}
The above override is called for functions (e.g. var x = data.mydynamicfunction();
) but not for prop开发者_JAVA技巧erties.
Any help or suggestions would be greatly appreciated.
It does not seem like this is currently possible (as of 1/14/12) due to the way function invoking and property access are compiled.
Function invoking seems to ask the CommonObject
implementation for the object with the property name, but normal property access seems to only hit the runtime's internal property cache and not the object's overrides.
I'm trying to get the code modified to handle this, you can follow progress on the mailing list.
Once the answer is figured out can post it here. If I knew F# better could probably make better progress, so if someone else does, please contribute :)
精彩评论