コード例 #1
0
        public virtual EcmaValue Next()
        {
            if (this.IteratedObject == null)
            {
                return(EcmaValueUtility.CreateIterResultObject(EcmaValue.Undefined, true));
            }
            if (iterator.MoveNext())
            {
                KeyValuePair <EcmaValue, EcmaValue> entry = iterator.Current;
                switch (this.ResultKind)
                {
                case EcmaIteratorResultKind.Key:
                    return(EcmaValueUtility.CreateIterResultObject(entry.Key, false));

                case EcmaIteratorResultKind.Value:
                    return(EcmaValueUtility.CreateIterResultObject(entry.Value, false));

                case EcmaIteratorResultKind.Entry:
                    return(EcmaValueUtility.CreateIterResultObject(new EcmaArray(new[] { entry.Key, entry.Value }), false));
                }
                throw new InvalidOperationException("Unknown kind value");
            }
            Close();
            return(EcmaValueUtility.CreateIterResultObject(EcmaValue.Undefined, true));
        }
コード例 #2
0
        public override DynamicMetaObject BindSetMember(SetMemberBinder binder, DynamicMetaObject value)
        {
            Expression expr = Expression.Assign(Expression.Property(Expression.Convert(Expression, typeof(EcmaValue)), "Item", Expression.Constant(new EcmaPropertyKey(binder.Name))),
                                                EcmaValueUtility.ConvertToEcmaValueExpression(value.Expression));

            return(ReturnDynamicMetaObject(binder, expr));
        }
コード例 #3
0
        public EcmaValue(object value)
        {
            EcmaValue resolved = EcmaValueUtility.ConvertFromObject(value);

            this.handle  = resolved.handle;
            this.binder_ = resolved.binder;
        }
コード例 #4
0
 private EcmaValue TransformValue(RuntimeObject holder, EcmaPropertyKey property, EcmaValue value)
 {
     if (value.Type == EcmaValueType.Object)
     {
         EcmaValue toJson = value[WellKnownProperty.ToJSON];
         if (toJson.IsCallable)
         {
             value = toJson.Call(value, property.ToValue());
         }
     }
     if (replacer != EcmaValue.Undefined)
     {
         value = replacer.Call(holder, property.ToValue(), value);
     }
     if (value.Type == EcmaValueType.Object)
     {
         EcmaValue primitive = EcmaValueUtility.UnboxPrimitiveObject(value);
         switch (primitive.Type)
         {
         case EcmaValueType.Number:
         case EcmaValueType.String:
         case EcmaValueType.Boolean:
             return(primitive);
         }
     }
     return(value);
 }
コード例 #5
0
 private void ExecuteCallback(PromiseExecutor executor)
 {
     try {
         executor(ResolveSelf, RejectSelf);
     } catch (Exception ex) {
         RejectSelf(EcmaValueUtility.GetValueFromException(ex));
     }
 }
コード例 #6
0
 private void HandleCompletedPromise(Promise previous)
 {
     try {
         HandleResult(previous.State, previous.Value);
     } catch (Exception ex) {
         RejectCallback.Call(EcmaValue.Undefined, EcmaValueUtility.GetValueFromException(ex));
     }
 }
コード例 #7
0
        public EcmaValue(SerializationInfo info, StreamingContext context)
        {
            Type      type     = (Type)info.GetValue("ut", typeof(Type));
            object    value    = info.GetValue("uo", type);
            EcmaValue resolved = EcmaValueUtility.ConvertFromObject(value);

            this.handle  = resolved.handle;
            this.binder_ = resolved.binder;
        }
コード例 #8
0
        private void RejectFromFaultedTask(Task task)
        {
            AggregateException ex     = task.Exception.Flatten();
            Exception          reason = ex;

            if (ex.InnerExceptions.Count == 1)
            {
                reason = ex.InnerExceptions[0];
            }
            SetState(PromiseState.Rejected, EcmaValueUtility.GetValueFromException(reason), false);
        }
コード例 #9
0
        EcmaValue IGeneratorEnumerable.Resume(GeneratorResumeState state, EcmaValue value)
        {
            switch (state)
            {
            case GeneratorResumeState.Return:
                throw new GeneratorClosedException(value);

            case GeneratorResumeState.Throw:
                throw EcmaException.FromValue(value);
            }
            if (MoveNext())
            {
                return(EcmaValueUtility.CreateIterResultObject(this.Current, false));
            }
            return(EcmaValueUtility.CreateIterResultObject(EcmaValue.Undefined, true));
        }
コード例 #10
0
        public override EcmaValue Resume(GeneratorResumeState state, EcmaValue value)
        {
            if (this.State == GeneratorState.Running)
            {
                throw new EcmaTypeErrorException(InternalString.Error.GeneratorRunning);
            }
            nextState = state;
            if (this.State != GeneratorState.SuspendedYield)
            {
                switch (state)
                {
                case GeneratorResumeState.Throw:
                    Close();
                    throw EcmaException.FromValue(value);

                case GeneratorResumeState.Return:
                    Close();
                    return(EcmaValueUtility.CreateIterResultObject(value, true));
                }
            }
            else
            {
                resumeValue = value;
            }
            if (this.State != GeneratorState.Closed)
            {
                try {
                    this.State = GeneratorState.Running;
                    if (TryYieldNext(out EcmaValue result))
                    {
                        return(EcmaValueUtility.CreateIterResultObject(result, false));
                    }
                } catch {
                    if (nextState != GeneratorResumeState.Return)
                    {
                        Close();
                        throw;
                    }
                } finally {
                    if (this.State != GeneratorState.Closed)
                    {
                        this.State = GeneratorState.SuspendedYield;
                    }
                }
                Close();
            }
            return(EcmaValueUtility.CreateIterResultObject(nextState == GeneratorResumeState.Return ? resumeValue : default, true));
コード例 #11
0
        public override EcmaPropertyDescriptor GetOwnProperty(EcmaPropertyKey propertyKey)
        {
            EcmaPropertyDescriptor current = base.GetOwnProperty(propertyKey);

            if (EcmaValueUtility.TryIndexByPropertyKey(arguments, propertyKey, out EcmaValue value))
            {
                if (taintedLevel == null)
                {
                    return(new EcmaPropertyDescriptor(value, EcmaPropertyAttributes.DefaultDataProperty));
                }
                if (taintedLevel[propertyKey.ToArrayIndex()] == TaintLevel.AttributeTainted)
                {
                    current.Value = value;
                }
            }
            return(current);
        }
コード例 #12
0
        public static EcmaValue Stringify(EcmaValue value, EcmaValue replacer, EcmaValue space)
        {
            string indentString;

            space = EcmaValueUtility.UnboxPrimitiveObject(space);
            if (space.Type == EcmaValueType.Number)
            {
                indentString = new String(' ', Math.Max(0, Math.Min(10, space.ToInt32())));
            }
            else if (space.Type == EcmaValueType.String)
            {
                indentString = space.ToStringOrThrow();
            }
            else
            {
                indentString = String.Empty;
            }
            string result;

            if (replacer.IsCallable)
            {
                result = new EcmaJsonWriter(indentString, replacer).Serialize(value);
            }
            else if (EcmaArray.IsArray(replacer))
            {
                HashSet <string> propertyList = new HashSet <string>();
                for (long i = 0, length = replacer[WellKnownProperty.Length].ToLength(); i < length; i++)
                {
                    EcmaValue item = EcmaValueUtility.UnboxPrimitiveObject(replacer[i]);
                    if (item.Type == EcmaValueType.String || item.Type == EcmaValueType.Number)
                    {
                        propertyList.Add(item.ToStringOrThrow());
                    }
                }
                result = new EcmaJsonWriter(indentString, propertyList).Serialize(value);
            }
            else
            {
                result = new EcmaJsonWriter(indentString).Serialize(value);
            }
            return(result ?? EcmaValue.Undefined);
        }
コード例 #13
0
        public override EcmaValue Resume(GeneratorResumeState state, EcmaValue value)
        {
            if (this.State != GeneratorState.Closed)
            {
                this.State = GeneratorState.Running;
            }
            try {
                EcmaValue result = default;
                bool      done   = true;
                switch (state)
                {
                case GeneratorResumeState.Resume:
                    done   = !iterator.MoveNext(value);
                    result = iterator.Current;
                    break;

                case GeneratorResumeState.Return:
                    if (!TryGetResult(WellKnownProperty.Return, value, out done, out result))
                    {
                        this.State = GeneratorState.Closed;
                        return(Promise.Resolve(EcmaValueUtility.CreateIterResultObject(value, true)));
                    }
                    break;

                case GeneratorResumeState.Throw:
                    if (!TryGetResult(WellKnownProperty.Throw, value, out done, out result))
                    {
                        this.State = GeneratorState.Closed;
                        return(Promise.Reject(value));
                    }
                    break;
                }
                if (done)
                {
                    this.State = GeneratorState.Closed;
                }
                return(Promise.Resolve(result, new ResultHandler(done).CreateIterResultObject, null));
            } catch (Exception ex) {
                return(Promise.Reject(ex));
            }
        }
コード例 #14
0
        private void SetState(PromiseState state, EcmaValue value, bool finalize)
        {
            PromiseCallback callback = state == PromiseState.Fulfilled ? fulfilledCallback : rejectedCallback;

            fulfilledCallback = null;
            rejectedCallback  = null;
            if (finalize || callback == null)
            {
                SetStateFinalize(state, value);
            }
            else
            {
                RuntimeExecution.Enqueue(() => {
                    try {
                        value = callback(value);
                        ResolveSelf(value, true);
                    } catch (Exception ex) {
                        SetStateFinalize(PromiseState.Rejected, EcmaValueUtility.GetValueFromException(ex));
                    }
                });
            }
        }
コード例 #15
0
 private void YieldNext()
 {
     if (this.State != GeneratorState.Closed)
     {
         try {
             this.State = GeneratorState.Running;
             if (TryYieldNext(out EcmaValue result))
             {
                 Promise.Resolve(result, ResolveFromYield, RejectFromYield);
                 return;
             }
         } catch (GeneratorClosedException ex) {
             Resolve(ex.ReturnValue, true);
         } catch (Exception ex) {
             Reject(EcmaValueUtility.GetValueFromException(ex));
         }
         if (this.State != GeneratorState.AwaitingReturn)
         {
             Close();
         }
     }
 }
コード例 #16
0
 private void ResolveSelf(EcmaValue value, bool finalize)
 {
     if (value.Type == EcmaValueType.Object)
     {
         RuntimeObject obj = value.ToObject();
         if (obj == this)
         {
             SetStateFinalize(PromiseState.Rejected, new EcmaTypeErrorException(InternalString.Error.PromiseSelfResolution).ToValue());
             return;
         }
         try {
             RuntimeObject then = obj.GetMethod(WellKnownProperty.Then);
             if (then != null)
             {
                 then.Call(value, (PromiseResolver)ResolveSelf, (PromiseResolver)RejectSelf);
                 return;
             }
         } catch (Exception ex) {
             SetStateFinalize(PromiseState.Rejected, EcmaValueUtility.GetValueFromException(ex));
             return;
         }
     }
     SetState(PromiseState.Fulfilled, value, finalize);
 }
コード例 #17
0
        public static EcmaValue ParseInt(EcmaValue str, EcmaValue radix)
        {
            string inputString = str.ToStringOrThrow();

            return(EcmaValueUtility.ParseIntInternal(inputString, radix.ToInt32(), true));
        }
コード例 #18
0
 private void Resolve(EcmaValue value, bool done)
 {
     queue.Dequeue().Capability.Resolve(EcmaValueUtility.CreateIterResultObject(value, done));
 }
コード例 #19
0
 private Expression GetConvertToEcmaValueExpression(DynamicMetaObject arg)
 {
     return(arg.HasValue ? Expression.Constant(new EcmaValue(arg.Value)) : EcmaValueUtility.ConvertToEcmaValueExpression(arg.Expression));
 }
コード例 #20
0
 public override DynamicMetaObject BindConvert(ConvertBinder binder)
 {
     return(ReturnDynamicMetaObject(binder, EcmaValueUtility.ConvertFromEcmaValueExpression(Expression, binder.ReturnType)));
 }
コード例 #21
0
        public static EcmaValue ParseFloat(EcmaValue str)
        {
            string inputString = str.ToStringOrThrow();

            return(EcmaValueUtility.ParseFloatInternal(inputString, true));
        }
コード例 #22
0
 public EcmaValue CreateIterResultObject(EcmaValue value)
 {
     return(EcmaValueUtility.CreateIterResultObject(value, done));
 }
コード例 #23
0
 public static Promise Reject(Exception ex)
 {
     return(Reject(EcmaValueUtility.GetValueFromException(ex)));
 }