You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var script = @"
def inc(x):
return x+1
";
var pythonEngine = Python.CreateEngine();
var scope = pythonEngine.CreateScope();
pythonEngine.Execute(script, scope);
var inc = (Func<int, int>)scope.GetVariable("inc");
var three = inc(2);
I'm trying to do something similar with IronJS:
var script = @"
function inc(x) {
return x+1;
}
";
var context = new IronJS.Hosting.CSharp.Context();
context.Execute(script);
var inc = context.GetGlobalAs<Func<int, int>>("inc");
var three = inc(2);
However, I get the following exception:
System.InvalidCastException: Unable to cast object of type 'IronJS.FunctionObject' to type 'System.Func`2[System.Int32,System.Int32]'.
at Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicFunctions.UnboxGeneric[T](Object source)
at IronJS.Hosting.CSharp.Context.GetGlobalAs[a](String name)
Is there a better way to export script as an executable delegate? Or if not, is this something you think IronJS could/should support? I'd be glad to help, but am unsure exactly where to start...maybe TypeConverter.ConvertTo()?
The text was updated successfully, but these errors were encountered:
Yes there is a way to get a JS function as a CLR delegate, it's currently a bit clunky as it doesn't masqurade the internal parameters or return types and they get exposed to the hosting code, but here's the snippet:
varctx=new IronJS.Hosting.CSharp.Context();
ctx.Execute(@" function multiply(a, b) { return a * b; }");varmultiply= ctx.GetGlobalAs<FunctionObject>("multiply");vardelegate_= multiply.MetaData.GetDelegate<Func<FunctionObject,CommonObject,double,double,BoxedValue>>(multiply);varresult= delegate_.Invoke(multiply, ctx.Globals,4.0,4.0).ClrBoxed;
I'll still mark this ticket as "missing feature" because we want to return a delegate that doesn't expose the internal stuff.
This if fixed in commit a6aecac0cb4 by adding a new function to the Hosting.CSharp.Context object (C#) and the Hosting.FSharp module called GetFunctionAs(name) / getFunctionAs<'a> name.
varctx=new IronJS.Hosting.CSharp.Context();
ctx.Execute(@" function multiply(a, b) { return a * b; }");varmultiply= ctx.GetFunctionAs<Func<double,double,double>>("multiply");varresult= multiply.Invoke(4.0,4.0);
With IronPython, I can do this:
I'm trying to do something similar with IronJS:
However, I get the following exception:
Is there a better way to export script as an executable delegate? Or if not, is this something you think IronJS could/should support? I'd be glad to help, but am unsure exactly where to start...maybe
TypeConverter.ConvertTo()
?The text was updated successfully, but these errors were encountered: