Пример #1
0
 private void AwaitReturn(EcmaValue value, bool resume)
 {
     this.State = GeneratorState.AwaitingReturn;
     Promise.Resolve(value, resume ? (PromiseCallback)ResumeFromReturn : ResolveFromReturn, RejectFromYield);
 }
Пример #2
0
        public static EcmaValue ConvertFromObject(object target)
        {
            switch (target)
            {
            case null:
                return(EcmaValue.Undefined);

            case EcmaValue value:
                return(value);

            case string str:
                return(new EcmaValue(str));

            case Symbol sym:
                return(new EcmaValue(sym));

            case RuntimeObject obj:
                return(new EcmaValue(obj));

            case DateTime dt:
                return(new EcmaDate(dt));

            case Delegate del:
                return(DelegateRuntimeFunction.FromDelegate(del));

            case Task task:
                return(Promise.FromTask(task));

            case Exception ex:
                return(GetValueFromException(ex));
            }
            Type             type   = target.GetType();
            IEcmaValueBinder binder = null;

            if (type.IsEnum)
            {
                binder = binderTypes.GetOrAdd(type, t => (IEcmaValueBinder)Activator.CreateInstance(typeof(EnumBinder <>).MakeGenericType(t)));
            }
            else
            {
                switch (Type.GetTypeCode(type))
                {
                case TypeCode.Boolean:
                    return(new EcmaValue(BooleanBinder.Default.ToHandle((bool)target), BooleanBinder.Default));

                case TypeCode.Byte:
                case TypeCode.SByte:
                case TypeCode.Char:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                    return(new EcmaValue(Int32Binder.Default.ToHandle(Convert.ToInt32(target)), Int32Binder.Default));

                case TypeCode.Int32:
                    return(new EcmaValue(Int32Binder.Default.ToHandle((int)target), Int32Binder.Default));

                case TypeCode.UInt32:
                    return(new EcmaValue(Int64Binder.Default.ToHandle(Convert.ToInt64(target)), Int64Binder.Default));

                case TypeCode.Int64:
                    return(new EcmaValue(Int64Binder.Default.ToHandle((long)target), Int64Binder.Default));

                case TypeCode.UInt64:
                case TypeCode.Single:
                    return(new EcmaValue(DoubleBinder.Default.ToHandle(Convert.ToDouble(target)), DoubleBinder.Default));

                case TypeCode.Double:
                    return(new EcmaValue(DoubleBinder.Default.ToHandle((double)target), DoubleBinder.Default));
                }
                binder = RuntimeRealm.Current.ResolveRuntimeObject(target);
            }
            return(new EcmaValue(binder.ToHandle(target), binder));
        }