Inheritance: Expression, IGenericExpression
Esempio n. 1
0
		public void Visit(NewExpression expression)
		{
			Builder.Append("new ");
			expression.Expression.Accept(this);
			Builder.Append("(");
			var first = true;
			foreach (var argument in expression.Arguments)
			{
				if (first == false)
					Builder.Append(", ");
				first = false;
				argument.Accept(this);
			}
			Builder.Append(")");
		}
Esempio n. 2
0
        public void Visit(NewExpression expression)
        {
            Result = null;

            expression.Expression.Accept(this);

            if (Result == JsUndefined.Instance && typeFullname.Length > 0 && expression.Generics.Count > 0)
            {
                string typeName = typeFullname.ToString();
                typeFullname = new StringBuilder();

                var genericParameters = new Type[expression.Generics.Count];

                try
                {
                    int i = 0;
                    foreach (Expression generic in expression.Generics)
                    {
                        generic.Accept(this);
                        genericParameters[i] = Global.Marshaller.MarshalJsValue<Type>(Result);
                        i++;
                    }
                }
                catch (Exception e)
                {
                    throw new JintException("A type parameter is required", e);
                }

                typeName += "`" + genericParameters.Length;
                Result = Global.Marshaller.MarshalClrValue<Type>(typeResolver.ResolveType(typeName).MakeGenericType(genericParameters));
            }

            if (Result != null && Result is JsFunction) {
                JsFunction function = (JsFunction)Result;

                // Process parameters
                JsInstance[] parameters = new JsInstance[expression.Arguments.Count];

                for (int i = 0; i < expression.Arguments.Count; i++) {
                    expression.Arguments[i].Accept(this);
                    parameters[i] = Result;
                }

                Result = function.Construct(parameters, null, this);

                return;
            } else
                throw new JsException(Global.ErrorClass.New("Function expected."));
        }
Esempio n. 3
0
 void Analyze(NewExpression Stmt)
 {
     SetCurrentLineAndCharNos(Stmt);
     if (Stmt.Arguments != null) Analyze(Stmt.Arguments);
     if (Stmt.Expression != null) Analyze(Stmt.Expression);
     if (Stmt.Generics != null) Analyze(Stmt.Generics);
 }
Esempio n. 4
0
        public void Visit(NewExpression expression)
        {
            int scopes = this.Scopes.Count;
            foreach (var property in expression.Identifiers)
            {
                property.Accept(this);
                if (Result == JsUndefined.Instance)
                {
                    break;
                }
                EnterScope((JsDictionaryObject)Result);
            }
            while (scopes < this.Scopes.Count)
            {
                ExitScope();
            }

            if (Result != null && Result.Class == JsFunction.TYPEOF)
            {
                JsFunction function = (JsFunction)Result;

                // Process parameters
                JsInstance[] parameters = new JsInstance[expression.Arguments.Count];

                for (int i = 0; i < expression.Arguments.Count; i++)
                {
                    expression.Arguments[i].Accept(this);
                    parameters[i] = Result;
                }

                // Calls the constructor on a brand new object
                JsObject instance = new JsObject();
                instance.Prototype = function.Prototype;

                // Once 'new' is called, the result is the new instance, given by the Execute() method on the proper constructor
                ExecuteFunction(function, instance, parameters);

                return;
            }

            if (Result.IsClr && Result.Value is Type)
            {
                Type type = (Type)Result.Value;

                object[] parameters = new object[expression.Arguments.Count];

                for (int i = 0; i < expression.Arguments.Count; i++)
                {
                    expression.Arguments[i].Accept(this);
                    parameters[i] = JsClr.ConvertParameter(Result);
                }

                ConstructorInfo constructor = null;

                constructor = constructorInvoker.Invoke(type, parameters);

                if (constructor == null)
                {
                    // Struct don't reflect their default constructor
                    if (type.IsValueType)
                    {
                        PermissionSet.PermitOnly();

                        try
                        {
                            Result = Global.WrapClr(Activator.CreateInstance(type));
                        }
                        finally
                        {
                            CodeAccessPermission.RevertPermitOnly();
                        }
                    }
                    else
                    {
                        throw new JintException("Matching constructor not found for: " + type.Name);
                    }
                }

                PermissionSet.PermitOnly();

                try
                {
                    Result = Global.WrapClr(constructor.Invoke(parameters));
                }
                finally
                {
                    CodeAccessPermission.RevertPermitOnly();
                }
            }

            // Try to get identifiers as a CLR type
            if (Result == JsUndefined.Instance)
            {
                EnsureClrAllowed();

                // Process parameters
                object[] parameters = new object[expression.Arguments.Count];

                for (int i = 0; i < expression.Arguments.Count; i++)
                {
                    expression.Arguments[i].Accept(this);
                    parameters[i] = JsClr.ConvertParameter(Result);
                }

                StringBuilder typeBuilder = new StringBuilder();
                foreach (var property in expression.Identifiers)
                {
                    typeBuilder.Append(property.Text).Append(".");
                }

                typeBuilder.Remove(typeBuilder.Length - 1, 1);

                if (expression.Generics.Count > 0)
                {
                    List<string> types = new List<string>();
                    foreach (Expression generic in expression.Generics)
                    {
                        generic.Accept(this);

                        if (!(Result.Value is Type))
                        {
                            throw new JintException("Invalid generic type");
                        }

                        types.Add(Result.Value.ToString());
                    }
                    typeBuilder.Append("`").Append(types.Count);
                    typeBuilder.Append("[");
                    typeBuilder.Append(String.Join(",", types.ToArray()));
                    typeBuilder.Append("]");
                }

                string typeName = typeBuilder.ToString();
                Type type = typeResolver.ResolveType(typeName);

                if (type == null)
                {
                    throw new JintException("Unknown type: " + typeName);
                }

                ConstructorInfo constructor = null;

                constructor = constructorInvoker.Invoke(type, parameters);

                if (constructor == null)
                {
                    // Struct don't reflect their default constructor
                    if (type.IsValueType)
                    {
                        PermissionSet.PermitOnly();

                        try
                        {
                            Result = Global.WrapClr(Activator.CreateInstance(type));
                        }
                        finally
                        {
                            CodeAccessPermission.RevertPermitOnly();
                        }
                    }
                    else
                    {
                        throw new JintException("Matching constructor not found for: " + typeName);
                    }
                }
                else
                {
                    PermissionSet.PermitOnly();

                    try
                    {
                        Result = Global.WrapClr(constructor.Invoke(parameters));
                    }
                    finally
                    {
                        CodeAccessPermission.RevertPermitOnly();
                    }
                }
            }
        }
Esempio n. 5
0
        public void Visit(NewExpression expression)
        {
            Result = null;

            expression.Expression.Accept(this);

            if (Result == JsUndefined.Instance && typeFullname.Length > 0 && expression.Generics.Count > 0)
            {
                throw new NotSupportedException();
            }

            if (Result != null && Result is JsFunction) {
                JsFunction function = (JsFunction)Result;

                // Process parameters
                JsInstance[] parameters = new JsInstance[expression.Arguments.Count];

                for (int i = 0; i < expression.Arguments.Count; i++) {
                    expression.Arguments[i].Accept(this);
                    parameters[i] = Result;
                }

                Result = function.Construct(parameters, null, this);

                return;
            }
        }