Exemple #1
0
        private IEnumerable <Expression> GetParametersForSwitchCase(int pArgsLength)
        {
            int k = 0;

            for (int i = 0, length = parameters.Length - (tailArray ? 1 : 0); i < length; i++)
            {
                if (i == posNew)
                {
                    yield return(EcmaValueUtility.ConvertFromEcmaValueExpression(pNewTarget, parameters[i].ParameterType));
                }
                else if (i == posThis)
                {
                    yield return(EcmaValueUtility.ConvertFromEcmaValueExpression(pThisArg, parameters[i].ParameterType));
                }
                else if (k >= pArgsLength)
                {
                    yield return(GetDefaultExpression(parameters[i].ParameterType));
                }
                else
                {
                    yield return(GetArgumentFromArray(k++, i));
                }
            }
            if (tailArray)
            {
                if (k >= pArgsLength)
                {
                    yield return(Expression.MakeMemberAccess(null, typeof(EcmaValue).GetField("EmptyArray", BindingFlags.Static | BindingFlags.Public)));
                }
                else
                {
                    yield return(GetTailArray(k));
                }
            }
        }
Exemple #2
0
        private IEnumerable <Expression> GetParametersForSwitchDefault()
        {
            int k = 0;

            for (int i = 0, length = parameters.Length - (tailArray ? 1 : 0); i < length; i++)
            {
                if (i == posNew)
                {
                    yield return(EcmaValueUtility.ConvertFromEcmaValueExpression(pNewTarget, parameters[i].ParameterType));
                }
                else if (i == posThis)
                {
                    yield return(EcmaValueUtility.ConvertFromEcmaValueExpression(pThisArg, parameters[i].ParameterType));
                }
                else
                {
                    yield return(Expression.Condition(
                                     Expression.GreaterThan(Expression.Property(pArgs, "Length"), Expression.Constant(i)),
                                     EcmaValueUtility.ConvertFromEcmaValueExpression(Expression.ArrayIndex(pArgs, Expression.Constant(k++)), parameters[i].ParameterType),
                                     GetDefaultExpression(parameters[i].ParameterType)));
                }
            }
            if (tailArray)
            {
                yield return(GetTailArray(k));
            }
        }
Exemple #3
0
        public static RuntimeFunctionDelegate Compile(MethodInfo method)
        {
            NativeRuntimeFunctionCompiler        compiler = new NativeRuntimeFunctionCompiler(method);
            Expression <RuntimeFunctionDelegate> lambda   = Expression.Lambda <RuntimeFunctionDelegate>(
                EcmaValueUtility.ConvertToEcmaValueExpression(compiler.GetExpression()), pRecord, pArgs, pTarget);

            return(lambda.Compile());
        }
Exemple #4
0
 public override EcmaValue Get(EcmaPropertyKey propertyKey, RuntimeObject receiver)
 {
     if (EcmaValueUtility.TryIndexByPropertyKey(this.Target, propertyKey, out EcmaValue value))
     {
         return(value);
     }
     if (propertyKey == WellKnownProperty.Length)
     {
         return(Target.Count);
     }
     return(base.Get(propertyKey, receiver));
 }
Exemple #5
0
 public override bool TryGet(string target, EcmaPropertyKey name, out EcmaValue value)
 {
     if (name.IsArrayIndex)
     {
         return(EcmaValueUtility.TryIndexByPropertyKey(target, name, out value));
     }
     if (name.Name == "length")
     {
         value = target.Length;
         return(true);
     }
     return(base.TryGet(target, name, out value));
 }
Exemple #6
0
 public override EcmaPropertyDescriptor GetOwnProperty(EcmaPropertyKey propertyKey)
 {
     if (value.Type == EcmaValueType.String)
     {
         string str = value.ToString();
         if (EcmaValueUtility.TryIndexByPropertyKey(str, propertyKey, out EcmaValue ch))
         {
             return(new EcmaPropertyDescriptor(ch, EcmaPropertyAttributes.Enumerable));
         }
         if (propertyKey == WellKnownProperty.Length)
         {
             return(new EcmaPropertyDescriptor(str.Length, EcmaPropertyAttributes.None));
         }
     }
     return(base.GetOwnProperty(propertyKey));
 }
Exemple #7
0
 public bool SetException(Exception ex)
 {
     if (state == State.Finally)
     {
         throw ex;
     }
     if (state == State.Try && catchBlock != null)
     {
         afterYield = false;
         state      = State.Catch;
         iterator.Dispose();
         iterator = catchBlock(EcmaValueUtility.GetValueFromException(ex)).GetEnumerator();
         return(true);
     }
     exception = ex;
     return(SetFinally());
 }
Exemple #8
0
        public static EcmaValue Race([This] EcmaValue thisValue, EcmaValue iterable)
        {
            Guard.ArgumentIsObject(thisValue);
            PromiseCapability capability = PromiseCapability.CreateFromConstructor(thisValue.ToObject());

            try {
                using (EcmaIteratorEnumerator iterator = iterable.ForOf()) {
                    EcmaValue resolve = thisValue[WellKnownProperty.Resolve];
                    while (MoveNextSafe(iterator))
                    {
                        EcmaValue thenable = resolve.Call(thisValue, iterator.Current);
                        thenable.Invoke(WellKnownProperty.Then, capability.ResolveCallback, capability.RejectCallback);
                    }
                }
            } catch (Exception ex) {
                capability.Reject(EcmaValueUtility.GetValueFromException(ex));
            }
            return(capability.Promise);
        }
Exemple #9
0
        public static EcmaValue All([This] EcmaValue thisValue, EcmaValue iterable)
        {
            Guard.ArgumentIsObject(thisValue);
            PromiseCapability capability = PromiseCapability.CreateFromConstructor(thisValue.ToObject());
            PromiseAggregator aggregator = new AllFulfilledAggregator(capability);

            try {
                using (EcmaIteratorEnumerator iterator = iterable.ForOf()) {
                    EcmaValue resolve = thisValue[WellKnownProperty.Resolve];
                    while (MoveNextSafe(iterator))
                    {
                        EcmaValue thenable = resolve.Call(thisValue, iterator.Current);
                        thenable.Invoke(WellKnownProperty.Then, (PromiseResolver)aggregator.CreateHandler().ResolveHandler, capability.RejectCallback);
                    }
                }
                aggregator.ResolveIfConditionMet();
            } catch (Exception ex) {
                capability.Reject(EcmaValueUtility.GetValueFromException(ex));
            }
            return(capability.Promise);
        }
Exemple #10
0
        public static void SendUnhandledException(Exception ex)
        {
            Guard.ArgumentNotNull(ex, "ex");
            RuntimeExecution current = EnsureInstance();

            current.UnhandledException?.Invoke(null, new RuntimeUnhandledExceptionEventArgs(ex, EcmaValueUtility.GetValueFromException(ex)));
        }
Exemple #11
0
 public override double ToDouble(string value)
 {
     return(EcmaValueUtility.ParseStringNumericLiteral(value).ToDouble());
 }
Exemple #12
0
 public override int ToInt32(string value)
 {
     return(EcmaValueUtility.ParseStringNumericLiteral(value).ToInt32());
 }
Exemple #13
0
 private Expression GetArgumentFromArray(int argsIndex, int nativeParameterIndex)
 {
     return(EcmaValueUtility.ConvertFromEcmaValueExpression(Expression.ArrayIndex(pArgs, Expression.Constant(argsIndex)), parameters[nativeParameterIndex].ParameterType));
 }
Exemple #14
0
 public override long ToInt64(string value)
 {
     return(EcmaValueUtility.ParseStringNumericLiteral(value).ToInt64());
 }
Exemple #15
0
        protected override bool Matches(object actual)
        {
            EcmaValue value = EcmaValueUtility.GetValueFromException((Exception)actual);

            return(value.InstanceOf(fn));
        }
Exemple #16
0
 public static EcmaValue Apply([This] EcmaValue thisValue, EcmaValue thisArg, EcmaValue args)
 {
     Guard.ArgumentIsCallable(thisValue);
     return(thisValue.Call(thisArg, EcmaValueUtility.CreateListFromArrayLike(args)));
 }
Exemple #17
0
 public override EcmaValue ToNumber(string value)
 {
     return(EcmaValueUtility.ParseStringNumericLiteral(value));
 }