Example #1
0
        public JsNode VisitDynamicInvocationResolveResult(DynamicInvocationResolveResult res)
        {
            var target2 = VisitExpression(res.Target);
            var args2   = VisitExpressions(res.Arguments);

            if (res.InvocationType == DynamicInvocationType.Invocation)
            {
                var node2 = target2.Invoke(args2.ToArray());
                return(node2);
            }
            else if (res.InvocationType == DynamicInvocationType.ObjectCreation)
            {
                var node2 = Js.New(target2, args2.ToArray());
                return(node2);
            }
            else if (res.InvocationType == DynamicInvocationType.Indexing)
            {
                var node2 = target2.IndexerAccess(args2.Single());
                return(node2);
            }
            else
            {
                throw new NotImplementedException("Dynamics: " + res.InvocationType);
            }
        }
 public virtual TResult VisitDynamicInvocationResolveResult(DynamicInvocationResolveResult rr, TData data)
 {
     VisitChildResolveResults(rr, data);
     return(default(TResult));
 }
Example #3
0
 public virtual object VisitDynamicInvocationResolveResult(DynamicInvocationResolveResult rr, object data)
 {
     VisitChildResolveResults(rr, data);
     return(null);
 }
Example #4
0
        private void BuildDynamicArgumentsList(DynamicInvocationResolveResult drr, IList <Expression> arguments)
        {
            Expression paramsArg    = null;
            string     paramArgName = null;
            IMethod    method       = null;
            var        group        = drr.Target as MethodGroupResolveResult;

            if (group != null && group.Methods.Count() > 1)
            {
                method = group.Methods.FirstOrDefault(m =>
                {
                    if (drr.Arguments.Count != m.Parameters.Count)
                    {
                        return(false);
                    }

                    for (int i = 0; i < m.Parameters.Count; i++)
                    {
                        var argType = drr.Arguments[i].Type;

                        if (argType.Kind == TypeKind.Dynamic)
                        {
                            argType = this.Emitter.Resolver.Compilation.FindType(TypeCode.Object);
                        }

                        if (!m.Parameters[i].Type.Equals(argType))
                        {
                            return(false);
                        }
                    }

                    return(true);
                });

                if (method == null)
                {
                    throw new EmitterException(this.Expression, Bridge.Translator.Constants.Messages.Exceptions.DYNAMIC_INVOCATION_TOO_MANY_OVERLOADS);
                }
            }

            if (method != null)
            {
                var member     = method;
                var parameters = method.Parameters;

                Expression[] result            = new Expression[parameters.Count];
                string[]     names             = new string[result.Length];
                bool         named             = false;
                int          i                 = 0;
                bool         isInterfaceMember = false;

                if (member != null)
                {
                    var inlineStr = this.Emitter.GetInline(member);
                    named = !string.IsNullOrEmpty(inlineStr);

                    isInterfaceMember = member.DeclaringTypeDefinition != null &&
                                        member.DeclaringTypeDefinition.Kind == TypeKind.Interface;
                }

                foreach (var arg in arguments)
                {
                    if (arg is NamedArgumentExpression)
                    {
                        NamedArgumentExpression namedArg = (NamedArgumentExpression)arg;
                        var namedParam = parameters.First(p => p.Name == namedArg.Name);
                        var index      = parameters.IndexOf(namedParam);

                        result[index] = namedArg.Expression;
                        names[index]  = namedArg.Name;
                        named         = true;

                        if (paramsArg == null && (parameters.Count > i) && parameters[i].IsParams)
                        {
                            if (member.DeclaringTypeDefinition == null || !this.Emitter.Validator.IsExternalType(member.DeclaringTypeDefinition))
                            {
                                paramsArg = namedArg.Expression;
                            }

                            paramArgName = namedArg.Name;
                        }
                    }
                    else
                    {
                        if (paramsArg == null && (parameters.Count > i) && parameters[i].IsParams)
                        {
                            if (member.DeclaringTypeDefinition == null || !this.Emitter.Validator.IsExternalType(member.DeclaringTypeDefinition))
                            {
                                paramsArg = arg;
                            }

                            paramArgName = parameters[i].Name;
                        }

                        if (i >= result.Length)
                        {
                            var list = result.ToList();
                            list.AddRange(new Expression[arguments.Count - i]);

                            var strList = names.ToList();
                            strList.AddRange(new string[arguments.Count - i]);

                            result = list.ToArray();
                            names  = strList.ToArray();
                        }

                        result[i] = arg;
                        names[i]  = i < parameters.Count ? parameters[i].Name : paramArgName;
                    }

                    i++;
                }

                for (i = 0; i < result.Length; i++)
                {
                    if (result[i] == null)
                    {
                        var    p = parameters[i];
                        object t = null;
                        if (p.Type.Kind == TypeKind.Enum)
                        {
                            t = Helpers.GetEnumValue(this.Emitter, p.Type, p.ConstantValue);
                        }
                        else
                        {
                            t = p.ConstantValue;
                        }
                        if ((named || isInterfaceMember) && !p.IsParams)
                        {
                            if (t == null)
                            {
                                result[i] = new PrimitiveExpression(new RawValue("void 0"));
                            }
                            else
                            {
                                result[i] = new PrimitiveExpression(t);
                            }
                        }

                        names[i] = parameters[i].Name;
                    }
                }

                this.ArgumentsExpressions = result;
                this.ArgumentsNames       = names;
                this.ParamsExpression     = paramsArg;
                this.NamedExpressions     = this.CreateNamedExpressions(names, result);
            }
            else
            {
                this.ArgumentsExpressions = arguments.ToArray();
            }
        }
 public object VisitDynamicInvocationResolveResult(DynamicInvocationResolveResult res)
 {
     throw new NotImplementedException();
 }