} // func IndexSetExpression private static Expression InvokeExpression(Scope scope, Token tStart, Expression instance, InvokeResult result, ArgumentsList arguments, bool lParse) { MethodInfo mi; ConstantExpression constInstance = instance as ConstantExpression; LuaType t; if (constInstance != null && (t = constInstance.Value as LuaType) != null && t.Type != null) // we have a type, bind the ctor { var type = t.Type; var typeInfo = type.GetTypeInfo(); var ci = typeInfo.IsValueType && arguments.Count == 0 ? null : LuaEmit.FindMember(typeInfo.DeclaredConstructors.Where(c => c.IsPublic), arguments.CallInfo, arguments.Expressions, getExpressionTypeFunction, false); if (ci == null && !typeInfo.IsValueType) { throw ParseError(tStart, String.Format(Properties.Resources.rsMemberNotResolved, type.Name, "ctor")); } return(SafeExpression(() => LuaEmit.BindParameter(scope.Runtime, args => ci == null ? Expression.New(type) : Expression.New(ci, args), ci == null ? new ParameterInfo[0] : ci.GetParameters(), arguments.CallInfo, arguments.Expressions, getExpressionFunction, getExpressionTypeFunction, true), tStart)); } else if (LuaEmit.IsDynamicType(instance.Type)) { // fallback is a dynamic call return(EnsureInvokeResult(scope, tStart, DynamicExpression.Dynamic(scope.Runtime.GetInvokeBinder(arguments.CallInfo), typeof(object), new Expression[] { ConvertExpression(scope.Runtime, tStart, instance, typeof(object)) }.Concat( from c in arguments.Expressions select Lua.EnsureType(c, typeof(object)) ) ), result, instance, null )); } else if (typeof(Delegate).GetTypeInfo().IsAssignableFrom(instance.Type.GetTypeInfo()) && // test if the type is assignable from delegate (mi = instance.Type.GetRuntimeMethods().Where(c => !c.IsStatic && c.IsPublic && c.Name == "Invoke").FirstOrDefault()) != null) // Search the Invoke method for the arguments { return(EnsureInvokeResult(scope, tStart, SafeExpression(() => LuaEmit.BindParameter <Expression>( scope.Runtime, args => Expression.Invoke(instance, args), mi.GetParameters(), arguments.CallInfo, arguments.Expressions, getExpressionFunction, getExpressionTypeFunction, true), tStart), result, instance, null )); } else { throw ParseError(tStart, LuaEmitException.GetMessageText(LuaEmitException.InvokeNoDelegate, instance.Type.Name)); } } // func InvokeExpression
} // func InvokeMemberExpression private static Expression InvokeMemberExpressionBind(MethodInfo method, Lua runtime, Expression instance, Expression[] arguments) { if (method.IsStatic) { return(LuaEmit.BindParameter(runtime, args => Expression.Call(null, method, args), method.GetParameters(), new Expression[] { instance }.Concat( from a in arguments select Lua.EnsureType(a, typeof(object)) ).ToArray(), getExpressionFunction, getExpressionTypeFunction, true)); } else { return(LuaEmit.BindParameter(runtime, args => Expression.Call(instance, method, args), method.GetParameters(), arguments, getExpressionFunction, getExpressionTypeFunction, true)); } } // func InvokeMemberExpressionBind
} // func FallbackInvoke public override DynamicMetaObject FallbackInvokeMember(DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion) { // defer target and all arguments if (!target.HasValue || args.Any(c => !c.HasValue)) { return(Defer(target, args)); } if (target.Value == null) { return(errorSuggestion ?? new DynamicMetaObject( ThrowExpression(Properties.Resources.rsNilNotCallable, typeof(object)), target.Restrictions.Merge(BindingRestrictions.GetInstanceRestriction(target.Expression, null)) )); } else { MethodInfo method = LuaEmit.FindMethod( LuaType.GetType(target.LimitType).GetInstanceMethods(BindingFlags.Public, Name), args, mo => mo.LimitType, true); if (method == null) { if (errorSuggestion != null) { return(errorSuggestion); } return(new DynamicMetaObject(ThrowExpression(String.Format(Properties.Resources.rsMemberNotResolved, target.LimitType.Name, Name), typeof(object)), GetMethodSignatureRestriction(target, args))); } try { Expression expr; if (method.IsStatic) { expr = LuaEmit.BindParameter(lua, a => Expression.Call(null, method, a), method.GetParameters(), (new DynamicMetaObject[] { target }).Concat(args).ToArray(), mo => mo.Expression, mo => mo.LimitType, false); } else { expr = LuaEmit.BindParameter(lua, a => Expression.Call(EnsureType(target.Expression, target.LimitType), method, a), method.GetParameters(), args, mo => mo.Expression, mo => mo.LimitType, false); } return (new DynamicMetaObject( EnsureType(expr, typeof(object), true), GetMethodSignatureRestriction(target, args) )); } catch (LuaEmitException e) { if (errorSuggestion != null) { return(errorSuggestion); } return(new DynamicMetaObject(ThrowExpression(e.Message, typeof(object)), GetMethodSignatureRestriction(target, args))); } } } // func FallbackInvokeMember
} // ctor public override DynamicMetaObject FallbackInvoke(DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion) { //defer the target and all arguments if (!target.HasValue || args.Any(c => !c.HasValue)) { return(Defer(target, args)); } if (target.Value == null) // Invoke on null value { return(errorSuggestion ?? new DynamicMetaObject( ThrowExpression(Properties.Resources.rsNilNotCallable), BindingRestrictions.GetInstanceRestriction(target.Expression, null) )); } else { BindingRestrictions restrictions = GetMethodSignatureRestriction(target, args); Expression expr; Delegate invokeTarget = target.Value as Delegate; if (invokeTarget == null) { if (errorSuggestion != null) { return(errorSuggestion); } expr = ThrowExpression(LuaEmitException.GetMessageText(LuaEmitException.InvokeNoDelegate, target.LimitType.Name), typeof(object)); } else { ParameterInfo[] methodParameters = invokeTarget.Method.GetParameters(); ParameterInfo[] parameters = null; MethodInfo mi = target.LimitType.GetMethod("Invoke"); if (mi != null) { ParameterInfo[] typeParameters = mi.GetParameters(); if (typeParameters.Length != methodParameters.Length) { parameters = new ParameterInfo[typeParameters.Length]; // the hidden parameters are normally at the beginning if (parameters.Length > 0) { Array.Copy(methodParameters, methodParameters.Length - typeParameters.Length, parameters, 0, parameters.Length); } } else { parameters = methodParameters; } } else { parameters = methodParameters; } try { expr = EnsureType( LuaEmit.BindParameter(lua, _args => Expression.Invoke(EnsureType(target.Expression, target.LimitType), _args), parameters, args, mo => mo.Expression, mo => mo.LimitType, false), typeof(object), true); } catch (LuaEmitException e) { if (errorSuggestion != null) { return(errorSuggestion); } expr = ThrowExpression(e.Message, ReturnType); } } return(new DynamicMetaObject(expr, restrictions)); } } // func FallbackInvoke