Beispiel #1
0
        protected override void OnAfterInitializing(IList <RuntimeObject> runtimeObject)
        {
            base.OnAfterInitializing(runtimeObject);

            RuntimeModule module = RuntimeRealm.IntrinsicModule;

            module.OverrideProperty(WellKnownObject.ArrayPrototype, "toLocaleString", DelegateRuntimeFunction.FromDelegate((Func <EcmaValue, EcmaValue, EcmaValue, EcmaValue>)BuiltInFunctionReplacement.ArrayPrototypeToLocaleString));
            module.OverrideProperty(WellKnownObject.DatePrototype, "toLocaleString", DelegateRuntimeFunction.FromDelegate((Func <EcmaValue, EcmaValue, EcmaValue, EcmaValue>)BuiltInFunctionReplacement.DatePrototypeToLocaleString));
            module.OverrideProperty(WellKnownObject.DatePrototype, "toLocaleDateString", DelegateRuntimeFunction.FromDelegate((Func <EcmaValue, EcmaValue, EcmaValue, EcmaValue>)BuiltInFunctionReplacement.DatePrototypeToLocaleDateString));
            module.OverrideProperty(WellKnownObject.DatePrototype, "toLocaleTimeString", DelegateRuntimeFunction.FromDelegate((Func <EcmaValue, EcmaValue, EcmaValue, EcmaValue>)BuiltInFunctionReplacement.DatePrototypeToLocaleTimeString));
            module.OverrideProperty(WellKnownObject.NumberPrototype, "toLocaleString", DelegateRuntimeFunction.FromDelegate((Func <EcmaValue, EcmaValue, EcmaValue, EcmaValue>)BuiltInFunctionReplacement.NumberPrototypeToLocaleString));
            module.OverrideProperty(WellKnownObject.StringPrototype, "localeCompare", DelegateRuntimeFunction.FromDelegate((Func <EcmaValue, EcmaValue, EcmaValue, EcmaValue, EcmaValue>)BuiltInFunctionReplacement.StringPrototypeLocaleCompare));
            module.OverrideProperty(WellKnownObject.StringPrototype, "toLocaleLowerCase", DelegateRuntimeFunction.FromDelegate((Func <EcmaValue, EcmaValue, EcmaValue>)BuiltInFunctionReplacement.StringPrototypeToLocaleLowerCase));
            module.OverrideProperty(WellKnownObject.StringPrototype, "toLocaleUpperCase", DelegateRuntimeFunction.FromDelegate((Func <EcmaValue, EcmaValue, EcmaValue>)BuiltInFunctionReplacement.StringPrototypeToLocaleUpperCase));
        }
Beispiel #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));
        }