Ejemplo n.º 1
0
        private ObjectInfo CallableValueInfo(EvaluationContext context, CallableValue cv)
        {
            if (cv.IsProperty)
            {
                return(GetObjectInfo(context, cv.Apply(context, EvaluationStackFrame.Empty())));
            }

            var           captures = EvaluationStackFrame.Empty();
            var           @null    = UndefinedValue.Instance;
            Func <object> func     = CreateFunc();

            return(func != null
                ? FuncObjInfo(func, RenderCallableSignature(cv.CallableMember))
                : new ObjectInfo("function(" + RenderCallableSignature(cv.CallableMember) + ")"));

            // Local functions
            Func <object> CreateFunc()
            {
                var undefined = EvaluationResult.Undefined;

                switch (cv.CallableMember.Kind)
                {
                case SyntaxKind.Function0:
                    return(() => cv.Apply(context, captures).Value);

                case SyntaxKind.Function1 when cv.CallableMember.MinArity == 0:
                    return(() => cv.Apply(context, undefined, captures).Value);

                case SyntaxKind.Function2 when cv.CallableMember.MinArity == 0:
                    return(() => cv.Apply(context, undefined, undefined, captures).Value);

                case SyntaxKind.FunctionN when cv.CallableMember.MinArity == 0:
                    return(() => cv.Apply(context, BuildXL.Utilities.Collections.CollectionUtilities.EmptyArray <EvaluationResult>(), captures).Value);
                }

                return(null);
            }
        }
Ejemplo n.º 2
0
        private EvaluationResult EvaluateAmbientProperty(Context context, ModuleLiteral env, CallableValue property)
        {
            context.SetPropertyProvenance(env.Path, Location);

            try
            {
                using (var frame = EvaluationStackFrame.Empty())
                {
                    return(property.Apply(context, frame));
                }
            }
            catch (EvaluationException e)
            {
                e.ReportError(context.Errors, env, Location, expression: this, context: context);
            }
            catch (OperationCanceledException)
            {
                return(EvaluationResult.Canceled);
            }
            catch (Exception exception)
            {
                context.Errors.ReportUnexpectedAmbientException(env, exception, Location);
                throw;
            }
            finally
            {
                context.UnsetPropertyProvenance();
            }

            return(EvaluationResult.Error);
        }
        public void TestCallableValueInt()
        {
            CallableValue <int> node = new CallableValue <int>(1, GetCallableMember0());

            Assert.Throws <NotImplementedException>(() => CheckSerializationRoundTrip(node));
        }
Ejemplo n.º 4
0
        private EvaluationResult EvaluateAmbientValueN(Context context, ModuleLiteral env, EvaluationStackFrame frame, CallableValue invocable, ref InvocationStatisticHandler statisticsHandler)
        {
            int numOfParams = invocable.CallableMember.MaxArity < short.MaxValue ? invocable.CallableMember.MaxArity : m_arguments.Length;

            // TODO: switch values to use EvaluationResult to avoid boxing.
            var values = new EvaluationResult[numOfParams];

            int i = 0;

            for (; i < invocable.CallableMember.MinArity; ++i)
            {
                values[i] = m_arguments[i].Eval(context, env, frame);

                if (values[i].IsErrorValue)
                {
                    return(EvaluationResult.Error);
                }
            }

            for (; i < numOfParams; ++i)
            {
                if (i >= m_arguments.Length)
                {
                    values[i] = EvaluationResult.Undefined;
                }
                else if (i < m_arguments.Length && i == numOfParams - 1 && invocable.CallableMember.Rest)
                {
                    values[i] = EvaluateRestArg(context, env, frame, i);
                }
                else
                {
                    values[i] = m_arguments[i].Eval(context, env, frame);
                }

                if (values[i].IsErrorValue)
                {
                    return(EvaluationResult.Error);
                }
            }

            statisticsHandler.CaptureStatistics(invocable.CallableMember.Statistic);

            return(invocable.Apply(context, values, frame));
        }
Ejemplo n.º 5
0
        private EvaluationResult EvaluateAmbientValue2(Context context, ModuleLiteral env, EvaluationStackFrame frame, CallableValue invocable, ref InvocationStatisticHandler statisticsHandler)
        {
            if (m_arguments.Length == 0)
            {
                return(invocable.Apply(context, EvaluationResult.Undefined, EvaluationResult.Undefined, frame));
            }

            if (m_arguments.Length == 1)
            {
                var argValue = m_arguments[0].Eval(context, env, frame);
                return(argValue.IsErrorValue ? argValue : invocable.Apply(context, argValue, EvaluationResult.Undefined, frame));
            }

            var argValue0 = m_arguments[0].Eval(context, env, frame);

            if (argValue0.IsErrorValue)
            {
                return(argValue0);
            }

            var argValue1 = !invocable.CallableMember.Rest ? m_arguments[1].Eval(context, env, frame) : EvaluateRestArg(context, env, frame, 1);

            if (argValue1.IsErrorValue)
            {
                return(EvaluationResult.Error);
            }

            statisticsHandler.CaptureStatistics(invocable.CallableMember.Statistic);
            // TODO: change Apply to take EvaluationResult but not objects!
            return(invocable.Apply(context, argValue0, argValue1, frame));
        }
Ejemplo n.º 6
0
        private EvaluationResult EvaluateAmbientValue1(Context context, ModuleLiteral env, EvaluationStackFrame frame, CallableValue invocable, ref InvocationStatisticHandler statisticsHandler)
        {
            if (m_arguments.Length == 0)
            {
                return(invocable.Apply(context, EvaluationResult.Undefined, frame));
            }

            var argValue = !invocable.CallableMember.Rest ? m_arguments[0].Eval(context, env, frame) : EvaluateRestArg(context, env, frame, 0);

            if (argValue.IsErrorValue)
            {
                return(argValue);
            }

            statisticsHandler.CaptureStatistics(invocable.CallableMember.Statistic);
            return(invocable.Apply(context, argValue, frame));
        }
Ejemplo n.º 7
0
 private static EvaluationResult EvaluateAmbientValue0(Context context, EvaluationStackFrame frame, CallableValue invocable, ref InvocationStatisticHandler statisticsHandler)
 {
     statisticsHandler.CaptureStatistics(invocable.CallableMember.Statistic);
     return(invocable.Apply(context, frame));
 }
Ejemplo n.º 8
0
        private EvaluationResult HandleCallableValue(Context context, ModuleLiteral currentEnv, EvaluationStackFrame frame, CallableValue invocable, ref InvocationStatisticHandler statisticsHandler)
        {
            // functor is a member function or property
            if (invocable.CallableMember.MinArity > m_arguments.Length)
            {
                context.Errors.ReportApplyAmbientNumberOfArgumentsLessThanMinArity(
                    currentEnv,
                    Functor,
                    invocable.CallableMember.MinArity,
                    m_arguments.Length,
                    Location);

                return(EvaluationResult.Error);
            }

            using (context.PrepareStackEntryForAmbient(this, currentEnv, frame))
            {
                if (context.IsStackOverflow)
                {
                    context.Errors.ReportStackOverflow(currentEnv, Location);
                    return(EvaluationResult.Error);
                }

                switch (invocable.CallableMember.MaxArity)
                {
                case 0:
                    return(EvaluateAmbientValue0(context, frame, invocable, ref statisticsHandler));

                case 1:
                    return(EvaluateAmbientValue1(context, currentEnv, frame, invocable, ref statisticsHandler));

                case 2:
                    return(EvaluateAmbientValue2(context, currentEnv, frame, invocable, ref statisticsHandler));

                default:
                    return(EvaluateAmbientValueN(context, currentEnv, frame, invocable, ref statisticsHandler));
                }
            }
        }