Example #1
0
        }         // 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