Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utils.createHostFunction and casted delegates #10

Closed
jhlllnd opened this issue Mar 5, 2011 · 5 comments
Closed

Utils.createHostFunction and casted delegates #10

jhlllnd opened this issue Mar 5, 2011 · 5 comments
Labels

Comments

@jhlllnd
Copy link

jhlllnd commented Mar 5, 2011

Hi,

first, thank you for this awesome code :)

I have found an annoying limitation. If the delegate at createHostFunction is casted to a Delegate it throws a NullReferenceException.

Utils.createHostFunction(env, (Delegate)new Action(this.test));

A quick fix from this:
ArgTypes = FSKit.Reflection.getDelegateArgTypesT<'a>
ReturnType = FSKit.Reflection.getDelegateReturnTypeT<'a>
to this:
ArgTypes = FSKit.Reflection.getDelegateArgTypes(delegate'.GetType())
ReturnType = FSKit.Reflection.getDelegateReturnType(delegate'.GetType())
doesn't help.

I am trying to bind dynamically methods of an object to js functions with Delegate.CreateDelegate like so:
List args = new List(method.GetParameters().Select(p => p.ParameterType));
Type delegateType;
if(method.ReturnType == typeof(void))
{
delegateType = Expression.GetActionType(args.ToArray());
}
else
{
args.Add(method.ReturnType);
delegateType = Expression.GetFuncType(args.ToArray());
}

var del = Delegate.CreateDelegate(delegateType, target, method);

Utils.createHostFunction(env, del);
@fholm
Copy link
Owner

fholm commented Mar 6, 2011

Thanks for reporting this issue, it should be fixxed in commit a77557f by adding a function in IronJS.Native.Utils called createHostFunctionDynamic which takes a System.Delegate as last argument and uses reflection to resolve all types.

@fholm
Copy link
Owner

fholm commented Mar 6, 2011

Also, here's an example of it would look when used:

//Example of using createHostFunctionDynamic
let print = new Action<string>(System.Console.WriteLine) :> Delegate
let printFunc = IronJS.Native.Utils.createHostFunctionDynamic ctx.Environment print
ctx.PutGlobal("print", printFunc)
ctx.Execute("print('lol')");

@jhlllnd
Copy link
Author

jhlllnd commented Mar 6, 2011

Many thanks for the very quick fix!

But I think you forgot to upload the latest FSKit changes :)

@fholm
Copy link
Owner

fholm commented Mar 6, 2011

Pushed latest FSKit now :)

@fholm
Copy link
Owner

fholm commented Mar 6, 2011

Also note that I changed the fix around a bit, so just call createHostFunction like you use to and it will check if the argument is "System.Delegate" and invoke createHostFunctionDynamic by itself.

asbjornu pushed a commit to asbjornu/IronJS that referenced this issue Jun 29, 2011
…Delegate), added Utils.createHostFunctionDynamic which is statically typed to Delegate and resolves all HostFunction<_> types by using reflection.
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants