Esempio n. 1
0
        internal override ScriptValue Construct([NotNull] IReadOnlyList <ScriptValue> arguments, ScriptObject newTarget)
        {
            //https://tc39.github.io/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-construct-argumentslist-newtarget
            if (ProxyHandler == null)
            {
                throw Agent.CreateTypeError();
            }
            Debug.Assert(ProxyTarget != null, nameof(ProxyTarget) + " != null");

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

            if (trap == null)
            {
                Debug.Assert(Agent.IsConstructor(ProxyTarget));
                return(Agent.Construct(ProxyTarget, arguments, newTarget));
            }

            var argumentArray = ArrayIntrinsics.CreateArrayFromList(Agent, arguments);
            var newObject     = Agent.Call(trap, ProxyHandler, ProxyTarget, argumentArray, newTarget);

            if (!newObject.IsObject)
            {
                throw Agent.CreateTypeError();
            }

            return(newObject);
        }
 /// <summary>
 /// Creates a 'new_array' intrinsic, which allocates a
 /// new array of a particular size.
 /// </summary>
 /// <param name="arrayType">
 /// The type of array to allocate.
 /// </param>
 /// <param name="sizeType">
 /// The type of integer that describes the desired length
 /// of the array to allocate.
 /// </param>
 /// <param name="sizeValue">
 /// The desired length of the array to allocate.
 /// </param>
 /// <returns>A 'new_array' intrinsic.</returns>
 public static Instruction CreateNewArrayIntrinsic(
     IType arrayType,
     IType sizeType,
     ValueTag sizeValue)
 {
     return(ArrayIntrinsics.CreateNewArrayPrototype(arrayType, sizeType)
            .Instantiate(new[] { sizeValue }));
 }
 /// <summary>
 /// Creates a 'get_length' intrinsic, which computes the
 /// number of elements in an array.
 /// </summary>
 /// <param name="sizeType">
 /// The type of integer to store the length of the array in.
 /// </param>
 /// <param name="arrayType">
 /// The type of array to inspect.
 /// </param>
 /// <param name="arrayValue">
 /// The array to inspect.
 /// </param>
 /// <returns>A 'get_length' intrinsic.</returns>
 public static Instruction CreateGetLengthIntrinsic(
     IType sizeType,
     IType arrayType,
     ValueTag arrayValue)
 {
     return(ArrayIntrinsics.CreateGetLengthPrototype(sizeType, arrayType)
            .Instantiate(new[] { arrayValue }));
 }
 /// <summary>
 /// Creates a 'load_element' intrinsic, which indexes
 /// an array and loads the indexed array element.
 /// </summary>
 /// <param name="elementType">
 /// The type of element to load.
 /// </param>
 /// <param name="arrayType">
 /// The type of array to index.
 /// </param>
 /// <param name="indexTypes">
 /// The types of indices to index the array with.
 /// </param>
 /// <param name="arrayValue">
 /// The array to index.
 /// </param>
 /// <param name="indexValues">
 /// The indices to index the array with.
 /// </param>
 /// <returns>A 'load_element' intrinsic.</returns>
 public static Instruction CreateLoadElementIntrinsic(
     IType elementType,
     IType arrayType,
     IReadOnlyList <IType> indexTypes,
     ValueTag arrayValue,
     IReadOnlyList <ValueTag> indexValues)
 {
     return(ArrayIntrinsics.CreateLoadElementPrototype(elementType, arrayType, indexTypes)
            .Instantiate(new[] { arrayValue }.Concat(indexValues).ToArray()));
 }
Esempio n. 5
0
        public void ArrayIntrinsics_Contains_Avx2()
        {
            for (int i = 2; i < 1024; i++)
            {
                var array = Enumerable.Range(0, i).ToArray();
                var item  = array[array.Length / 2];

                Assert.True(ArrayIntrinsics.Contains_Avx2(array, item));
                Assert.False(ArrayIntrinsics.Contains_Avx2(array, -42));
            }
        }
Esempio n. 6
0
 public void ArrayIntrinsics_IndexOf_Avx2()
 {
     for (int i = 1; i < 1024; i++)
     {
         var array         = Enumerable.Range(0, i).ToArray();
         var item          = array[array.Length / 2];
         var expectedIndex = Array.IndexOf(array, item);
         var actualIndex   = ArrayIntrinsics.IndexOf_Avx2(array, item);
         Assert.Equal(expectedIndex, actualIndex);
     }
 }
Esempio n. 7
0
        public void ArrayIntrinsics_Max()
        {
            for (int i = 2; i < 1024; i++)
            {
                var array = Enumerable.Range(0, i).Concat(Enumerable.Range(0, i).Reverse()).ToArray(); // 0 1 2 3 2 1 0 (for i==4)

                var expected = array.Max();
                var actual   = ArrayIntrinsics.Max_Avx2(array);

                Assert.Equal(expected, actual);
            }
        }
Esempio n. 8
0
        public void ArrayIntrinsics_Sum()
        {
            for (int i = 0; i < 1024; i++)
            {
                var array = Enumerable.Range(0, i).Select(n => n / 2.0f).ToArray();

                var expected = array.Sum();
                var actual   = ArrayIntrinsics.Sum_Avx(array);

                Assert.Equal(expected, actual);
            }
        }
Esempio n. 9
0
        public void ArrayIntrinsics_Reverse_Sse2()
        {
            for (int i = 0; i < 1024; i++)
            {
                var arrayOfInts1 = Enumerable.Range(0, i).ToArray();
                var arrayOfInts2 = arrayOfInts1.ToArray();

                Array.Reverse(arrayOfInts1);
                ArrayIntrinsics.Reverse_Sse2(arrayOfInts2);

                Assert.True(arrayOfInts1.SequenceEqual(arrayOfInts2));
            }
        }
Esempio n. 10
0
        public void ArrayIntrinsics_IsSorted_Avx2()
        {
            for (int i = 0; i < 1024; i++)
            {
                var sortedArray = Enumerable.Range(0, i).ToArray();
                Assert.True(ArrayIntrinsics.IsSorted_Avx2(sortedArray));

                if (i > 2)
                {
                    var unsortedArray = sortedArray.Concat(sortedArray).ToArray();
                    Assert.False(ArrayIntrinsics.IsSorted_Avx2(unsortedArray));
                }
            }
        }
Esempio n. 11
0
        public void ArrayIntrinsics_SequenceEqual_Avx()
        {
            for (int i = 0; i < 1024; i++)
            {
                var arrayOfFloats1 = Enumerable.Range(0, i).Select(n => n / 2.0f).ToArray();
                var arrayOfFloats2 = arrayOfFloats1.ToArray();

                var expected = Enumerable.SequenceEqual(arrayOfFloats1, arrayOfFloats2);
                var actual   = ArrayIntrinsics.SequenceEqual_Avx(arrayOfFloats1, arrayOfFloats2);
                Assert.Equal(expected, actual);

                // so now the arrays are not equal
                if (arrayOfFloats2.Length > 42)
                {
                    arrayOfFloats2[42] = -1.0f;
                    actual             = ArrayIntrinsics.SequenceEqual_Avx(arrayOfFloats1, arrayOfFloats2);
                    Assert.False(actual);
                }
            }
        }
Esempio n. 12
0
        internal override ScriptValue Call(ScriptValue thisArgument, [NotNull] IReadOnlyList <ScriptValue> arguments)
        {
            //https://tc39.github.io/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-call-thisargument-argumentslist
            if (ProxyHandler == null)
            {
                throw Agent.CreateTypeError();
            }
            Debug.Assert(ProxyTarget != null, nameof(ProxyTarget) + " != null");

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

            if (trap == null)
            {
                return(Agent.Call(ProxyTarget, thisArgument, arguments));
            }

            var argumentArray = ArrayIntrinsics.CreateArrayFromList(Agent, arguments);

            return(Agent.Call(trap, ProxyHandler, ProxyTarget, thisArgument, argumentArray));
        }
Esempio n. 13
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);
        }