/// <summary>
 /// Creates an 'unbox_any' intrinsic.
 /// Its return type can either be a value type or a
 /// reference type (aka box pointer).
 /// If its return type is set to a value type, 'unbox_any'
 /// unboxes its argument and loads it.
 /// If 'unbox_any's return value is set to a reference type,
 /// 'unbox_any' checks that its argument is a subtype of the
 /// return type.
 /// </summary>
 /// <param name="targetType">
 /// The target type: the type to unbox or cast a box pointer to.
 /// </param>
 /// <param name="sourceType">
 /// The source type: the type of the value to convert.
 /// </param>
 /// <param name="value">The value to convert.</param>
 /// <returns>An 'unbox_any' intrinsic.</returns>
 public static Instruction CreateUnboxAnyIntrinsic(
     IType targetType,
     IType sourceType,
     ValueTag value)
 {
     return(ObjectIntrinsics.CreateUnboxAnyPrototype(targetType, sourceType)
            .Instantiate(new[] { value }));
 }
Example #2
0
        internal override bool DefineOwnProperty(ScriptValue property, PropertyDescriptor descriptor)
        {
            //https://tc39.github.io/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-defineownproperty-p-desc
            Debug.Assert(Agent.IsPropertyKey(property));
            if (ProxyHandler == null)
            {
                throw Agent.CreateTypeError();
            }
            Debug.Assert(ProxyTarget != null, nameof(ProxyTarget) + " != null");

            var trap = Agent.GetMethod(ProxyHandler, "defineProperty");

            if (trap == null)
            {
                return(ProxyTarget.DefineOwnProperty(property, descriptor));
            }

            var descriptorObject  = ObjectIntrinsics.FromPropertyDescriptor(Agent, descriptor);
            var booleanTrapResult = Agent.ToBoolean(Agent.Call(trap, ProxyHandler, ProxyTarget, property, descriptorObject));

            if (!booleanTrapResult)
            {
                return(false);
            }

            var targetDescriptor = ProxyTarget.GetOwnProperty(property);
            var extensibleTarget = ProxyTarget.IsExtensible;

            var settingConfigFalse = descriptor.Configurable.HasValue && !descriptor.Configurable;

            if (targetDescriptor == null)
            {
                if (!extensibleTarget)
                {
                    throw Agent.CreateTypeError();
                }

                if (settingConfigFalse)
                {
                    throw Agent.CreateTypeError();
                }
            }
            else
            {
                if (!ValidateAndApplyPropertyDescriptor(null, ScriptValue.Undefined, extensibleTarget, descriptor, targetDescriptor))
                {
                    throw Agent.CreateTypeError();
                }
                if (settingConfigFalse && targetDescriptor.Configurable)
                {
                    throw Agent.CreateTypeError();
                }
            }

            return(true);
        }
Example #3
0
        internal Realm([NotNull] Agent agent, string name)
        {
            this.name = name;
            Agent     = agent;
            //https://tc39.github.io/ecma262/#sec-createrealm
            //https://tc39.github.io/ecma262/#sec-createintrinsics
            ObjectPrototype   = Agent.ObjectCreate(this, null);
            ThrowTypeError    = Agent.CreateBuiltinFunction(this, arguments => throw arguments.Agent.CreateTypeError(), null);
            FunctionPrototype = Agent.CreateBuiltinFunction(this, arguments => ScriptValue.Undefined, ObjectPrototype);
            ThrowTypeError.SetPrototypeOf(FunctionPrototype);
            AddRestrictedFunctionProperties(FunctionPrototype);

            IteratorPrototype = IteratorIntrinsics.Initialise(agent, this, ObjectPrototype, FunctionPrototype);

            (Array, ArrayPrototype, ArrayIteratorPrototype, ArrayProtoEntries, ArrayProtoForEach, ArrayProtoKeys, ArrayProtoValues) = ArrayIntrinsics.Initialise(agent, this, ObjectPrototype, FunctionPrototype);
            (ArrayBuffer, ArrayBufferPrototype)     = ArrayBufferIntrinsics.Initialise(agent, this);
            (AsyncFunction, AsyncFunctionPrototype) = AsyncFunctionIntrinsics.Initialise(agent, this);
            Atomics = AtomicsIntrinsics.Initialise(agent, this);
            (Boolean, BooleanPrototype)   = BooleanIntrinsics.Initialise(agent, this, ObjectPrototype, FunctionPrototype);
            (DataView, DataViewPrototype) = DataViewIntrinsics.Initialise(agent, this);
            (Date, DatePrototype)         = DateIntrinsics.Initialise(agent, this, ObjectPrototype, FunctionPrototype);
            DecodeURI               = Intrinsics.CreateBuiltinFunction(this, DecodeURIImplementation, FunctionPrototype, 1, "decodeURI");
            DecodeURIComponent      = Intrinsics.CreateBuiltinFunction(this, DecodeURIComponentImplementation, FunctionPrototype, 1, "decodeURIComponent");
            EncodeURI               = Intrinsics.CreateBuiltinFunction(this, EncodeURIImplementation, FunctionPrototype, 1, "encodeURI");
            EncodeURIComponent      = Intrinsics.CreateBuiltinFunction(this, EncodeURIComponentImplementation, FunctionPrototype, 1, "encodeURIComponent");
            (Error, ErrorPrototype) = ErrorIntrinsics.Initialise(agent, this, ObjectPrototype, FunctionPrototype);
            Eval = Intrinsics.CreateBuiltinFunction(this, EvalImplementation, FunctionPrototype, 1, "eval");
            (EvalError, EvalErrorPrototype) = ErrorIntrinsics.InitialiseNativeError(agent, "EvalError", this, Error, ErrorPrototype);
            Function = FunctionIntrinsics.Initialise(this, FunctionPrototype);
            (Generator, GeneratorPrototype, GeneratorFunction) = GeneratorInstrinsics.Initialise(agent, this, FunctionPrototype);
            IsFinite          = Intrinsics.CreateBuiltinFunction(this, IsFiniteImplementation, FunctionPrototype, 1, "isFinite");
            IsNaN             = Intrinsics.CreateBuiltinFunction(this, IsNaNImplementation, FunctionPrototype, 1, "isNaN");
            (Json, JsonParse) = JsonIntrinsics.Initialise(agent, this);
            (Map, MapPrototype, MapIteratorPrototype) = MapIntrinsics.Initialise(agent, this);
            Math = MathsIntrinsics.Initialise(agent, this);
            (Number, NumberPrototype) = NumberIntrinsics.Initialise(agent, this, ObjectPrototype, FunctionPrototype);
            (ObjectConstructor, ObjProtoToString, ObjProtoValueOf) = ObjectIntrinsics.Initialise(this, ObjectPrototype, FunctionPrototype);
            ParseFloat = Intrinsics.CreateBuiltinFunction(this, ParseFloatImplementation, FunctionPrototype, 1, "parseFloat");
            ParseInt   = Intrinsics.CreateBuiltinFunction(this, ParseIntImplementation, FunctionPrototype, 2, "parseInt");
            (Promise, PromisePrototype, PromiseProtoThen, PromiseAll, PromiseReject, PromiseResolve) = PromiseIntrinsics.Initialise(agent, this);
            Proxy = ProxyIntrinsics.Initialise(this);
            (RangeError, RangeErrorPrototype)         = ErrorIntrinsics.InitialiseNativeError(agent, "RangeError", this, Error, ErrorPrototype);
            (ReferenceError, ReferenceErrorPrototype) = ErrorIntrinsics.InitialiseNativeError(agent, "ReferenceError", this, Error, ErrorPrototype);
            Reflect = ReflectIntrinsics.Initialise(agent, this);
            (RegExp, RegExpPrototype) = RegExpIntrinsics.Initialise(agent, this);
            (Set, SetPrototype, SetIteratorPrototype)       = SetIntrinsics.Initialise(agent, this);
            (SharedArrayBuffer, SharedArrayBufferPrototype) = SharedArrayBufferIntrinsics.Initialise(agent, this);
            (StringConstructor, StringIteratorPrototype, StringPrototype) = StringIntrinsics.Initialise(agent, this, ObjectPrototype, FunctionPrototype);
            (Symbol, SymbolPrototype)           = SymbolIntrinsics.Initialise(agent, this, ObjectPrototype, FunctionPrototype);
            (SyntaxError, SyntaxErrorPrototype) = ErrorIntrinsics.InitialiseNativeError(agent, "SyntaxError", this, Error, ErrorPrototype);
            (TypeError, TypeErrorPrototype)     = ErrorIntrinsics.InitialiseNativeError(agent, "TypeError", this, Error, ErrorPrototype);
            (TypedArray, TypedArrayPrototype)   = TypedArrayIntrinsics.Initialise(agent, this);
            (UriError, UriErrorPrototype)       = ErrorIntrinsics.InitialiseNativeError(agent, "UriError", this, Error, ErrorPrototype);
            (WeakMap, WeakMapPrototype)         = WeakMapIntrinsics.Initialise(agent, this);
            (WeakSet, WeakSetPrototype)         = WeakSetIntrinsics.Initialise(agent, this);

            (Float32Array, Float32ArrayPrototype)           = TypedArrayIntrinsics.InitialiseType <float>(agent, this);
            (Float64Array, Float64ArrayPrototype)           = TypedArrayIntrinsics.InitialiseType <double>(agent, this);
            (Int8Array, Int8ArrayPrototype)                 = TypedArrayIntrinsics.InitialiseType <sbyte>(agent, this);
            (Int16Array, Int16ArrayPrototype)               = TypedArrayIntrinsics.InitialiseType <short>(agent, this);
            (Int32Array, Int32ArrayPrototype)               = TypedArrayIntrinsics.InitialiseType <int>(agent, this);
            (Uint8Array, Uint8ArrayPrototype)               = TypedArrayIntrinsics.InitialiseType <byte>(agent, this);
            (Uint8ClampedArray, Uint8ClampedArrayPrototype) = TypedArrayIntrinsics.InitialiseType <byte>(agent, this, true);
            (Uint16Array, Uint16ArrayPrototype)             = TypedArrayIntrinsics.InitialiseType <ushort>(agent, this);
            (Uint32Array, Uint32ArrayPrototype)             = TypedArrayIntrinsics.InitialiseType <uint>(agent, this);
        }
Example #4
0
        internal override PropertyDescriptor GetOwnProperty(ScriptValue property)
        {
            //https://tc39.github.io/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-getownproperty-p
            Debug.Assert(Agent.IsPropertyKey(property));

            if (ProxyHandler == null)
            {
                throw Agent.CreateTypeError();
            }
            Debug.Assert(ProxyTarget != null, nameof(ProxyTarget) + " != null");

            var trap = Agent.GetMethod(ProxyHandler, "getOwnPropertyDescriptor");

            if (trap == null)
            {
                return(ProxyTarget.GetOwnProperty(property));
            }

            var trapResultObj = Agent.Call(trap, ProxyHandler, ProxyTarget, property);

            if (!trapResultObj.IsObject && trapResultObj != ScriptValue.Undefined)
            {
                throw Agent.CreateTypeError();
            }

            var  targetDescriptor = ProxyTarget.GetOwnProperty(property);
            bool extensibleTarget;

            if (trapResultObj == ScriptValue.Undefined)
            {
                if (targetDescriptor == null)
                {
                    return(null);
                }

                if (!targetDescriptor.Configurable)
                {
                    throw Agent.CreateTypeError();
                }

                extensibleTarget = ProxyTarget.IsExtensible;
                if (!extensibleTarget)
                {
                    throw Agent.CreateTypeError();
                }

                return(null);
            }

            extensibleTarget = ProxyTarget.IsExtensible;
            var resultDescriptor = ObjectIntrinsics.ToPropertyDescriptor(Agent, trapResultObj);

            resultDescriptor.CompletePropertyDescriptor();
            var valid = ValidateAndApplyPropertyDescriptor(null, ScriptValue.Undefined, extensibleTarget, resultDescriptor, targetDescriptor);

            if (!valid)
            {
                throw Agent.CreateTypeError();
            }

            if (!resultDescriptor.Configurable)
            {
                if (targetDescriptor == null || targetDescriptor.Configurable)
                {
                    throw Agent.CreateTypeError();
                }
            }

            return(resultDescriptor);
        }