Ejemplo n.º 1
0
        public EcmaValue(RuntimeObject value)
        {
            IEcmaValueBinder binder = (IEcmaValueBinder)value ?? UndefinedBinder.Default;

            this.handle  = binder.ToHandle(value);
            this.binder_ = binder;
        }
Ejemplo n.º 2
0
        public EcmaValue(string value)
        {
            if (value == null)
            {
                this.handle  = default;
                this.binder_ = default;
            }
            else
            {
                IEcmaValueBinder binder = WellKnownPropertyNameBinder.IsWellKnownPropertyName(value) ? WellKnownPropertyNameBinder.Default : PrimitiveBinderWrapper <string> .GetBinder(value, StringBinder.Default);

                this.handle  = binder.ToHandle(value);
                this.binder_ = binder;
            }
        }
Ejemplo n.º 3
0
        public EcmaValue(Symbol value)
        {
            if (value.SymbolType != 0)
            {
                this.handle  = new EcmaValueHandle((long)value.SymbolType);
                this.binder_ = WellKnownSymbolBinder.Default;
            }
            else
            {
                IEcmaValueBinder binder = PrimitiveBinderWrapper <Symbol> .GetBinder(value, SymbolBinder.Default);

                this.handle  = binder.ToHandle(value);
                this.binder_ = binder;
            }
        }
Ejemplo n.º 4
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));
        }