Пример #1
0
        public TypedArrayInstance Of(params object[] elements)
        {
            int length = elements.Length;
            var result = new TypedArrayInstance(this.InstancePrototype, this.type, Engine.ArrayBuffer.Construct(length * BytesPerElement), 0, length);

            for (int i = 0; i < length; i++)
            {
                result[i] = elements[i];
            }
            return(result);
        }
Пример #2
0
        //     INITIALIZATION
        //_________________________________________________________________________________________

        /// <summary>
        /// Creates a new typed array constructor.
        /// </summary>
        /// <param name="prototype"> The next object in the prototype chain. </param>
        /// <param name="type"> Defines the element type and behaviour of the typed array. </param>
        internal TypedArrayConstructor(ObjectInstance prototype, TypedArrayType type)
            : base(prototype, __STUB__Construct, __STUB__Call)
        {
            this.type = type;

            // Initialize the constructor properties.
            var properties = GetDeclarativeProperties(Engine);

            InitializeConstructorProperties(properties, type.ToString(), 3, TypedArrayInstance.CreatePrototype(Engine, this));
            properties.Add(new PropertyNameAndValue("BYTES_PER_ELEMENT", BytesPerElement, PropertyAttributes.Sealed));
            FastSetProperties(properties);
        }
Пример #3
0
        public TypedArrayInstance From(object source, FunctionInstance mapFn = null, object thisArg = null)
        {
            var items = TypeConverter.ToObject(Engine, source);

            var iterator = TypeUtilities.GetIterator(Engine, items);

            if (iterator != null)
            {
                // Loop.
                var values = new List <object>();
                foreach (var value in TypeUtilities.Iterate(Engine, iterator))
                {
                    // Collect the values.
                    values.Add(value);
                }

                // Convert the values into a typed array instance.
                var result = new TypedArrayInstance(this.InstancePrototype, this.type,
                                                    Engine.ArrayBuffer.Construct(values.Count * BytesPerElement), 0, values.Count);
                for (int i = 0; i < values.Count; i++)
                {
                    if (mapFn != null)
                    {
                        result[i] = mapFn.Call(thisArg, values[i], i);
                    }
                    else
                    {
                        result[i] = values[i];
                    }
                }
                return(result);
            }
            else
            {
                // There was no iterator symbol value, so fall back on the alternate method.
                int length = TypeConverter.ToInt32(items["length"]);
                var result = new TypedArrayInstance(this.InstancePrototype, this.type, Engine.ArrayBuffer.Construct(length * BytesPerElement), 0, length);
                for (int i = 0; i < length; i++)
                {
                    if (mapFn != null)
                    {
                        result[i] = mapFn.Call(thisArg, items[i], i);
                    }
                    else
                    {
                        result[i] = items[i];
                    }
                }
                return(result);
            }
        }
Пример #4
0
        //     .NET HELPER METHODS
        //_________________________________________________________________________________________

        /// <summary>
        /// Creates a new typed array from a .NET array.
        /// </summary>
        /// <param name="source"> A .NET array </param>
        /// <returns> A new typed array instance. </returns>
        public TypedArrayInstance From(object[] source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            var result = new TypedArrayInstance(this.InstancePrototype, this.type,
                                                Engine.ArrayBuffer.Construct(source.Length * BytesPerElement), 0, source.Length);

            for (int i = 0; i < source.Length; i++)
            {
                result[i] = source[i];
            }
            return(result);
        }
Пример #5
0
 /// <summary>
 /// Creates a new TypedArrayWrapper instance.
 /// </summary>
 /// <param name="wrappedInstance"> The typed array that is being wrapped. </param>
 public TypedArrayAdapter(TypedArrayInstance wrappedInstance)
     : base(wrappedInstance, wrappedInstance.Length)
 {
 }
Пример #6
0
 public TypedArrayInstance Of(params object[] elements)
 {
     int length = elements.Length;
     var result = new TypedArrayInstance(this.InstancePrototype, this.type, Engine.ArrayBuffer.Construct(length * BytesPerElement), 0, length);
     for (int i = 0; i < length; i++)
     {
         result[i] = elements[i];
     }
     return result;
 }
Пример #7
0
        public TypedArrayInstance From(object source, FunctionInstance mapFn = null, object thisArg = null)
        {
            var items = TypeConverter.ToObject(Engine, source);

            var iterator = TypeUtilities.GetIterator(Engine, items);
            if (iterator != null)
            {
                // Loop.
                var values = new List<object>();
                foreach (var value in TypeUtilities.Iterate(Engine, iterator))
                {
                    // Collect the values.
                    values.Add(value);
                }

                // Convert the values into a typed array instance.
                var result = new TypedArrayInstance(this.InstancePrototype, this.type,
                    Engine.ArrayBuffer.Construct(values.Count * BytesPerElement), 0, values.Count);
                for (int i = 0; i < values.Count; i++)
                {
                    if (mapFn != null)
                        result[i] = mapFn.Call(thisArg, values[i], i);
                    else
                        result[i] = values[i];
                }
                return result;
            }
            else
            {
                // There was no iterator symbol value, so fall back on the alternate method.
                int length = TypeConverter.ToInt32(items["length"]);
                var result = new TypedArrayInstance(this.InstancePrototype, this.type, Engine.ArrayBuffer.Construct(length * BytesPerElement), 0, length);
                for (int i = 0; i < length; i++)
                {
                    if (mapFn != null)
                        result[i] = mapFn.Call(thisArg, items[i], i);
                    else
                        result[i] = items[i];
                }
                return result;
            }
        }
Пример #8
0
 //     .NET HELPER METHODS
 //_________________________________________________________________________________________
 /// <summary>
 /// Creates a new typed array from a .NET array.
 /// </summary>
 /// <param name="source"> A .NET array </param>
 /// <returns> A new typed array instance. </returns>
 public TypedArrayInstance From(object[] source)
 {
     if (source == null)
         throw new ArgumentNullException("source");
     var result = new TypedArrayInstance(this.InstancePrototype, this.type,
         Engine.ArrayBuffer.Construct(source.Length * BytesPerElement), 0, source.Length);
     for (int i = 0; i < source.Length; i ++)
     {
         result[i] = source[i];
     }
     return result;
 }
Пример #9
0
        public TypedArrayInstance Construct(object arg, int byteOffset = 0, int? length = null)
        {
            // new Int8Array(typedArray);
            // new Int8Array(object);
            // new Int8Array(buffer[, byteOffset[, length]]);
            if (arg is TypedArrayInstance)
            {
                // new %TypedArray%(typedArray);
                var typedArray = (TypedArrayInstance)arg;

                // Copy the items one by one.
                var result = new TypedArrayInstance(this.InstancePrototype, this.type,
                    Engine.ArrayBuffer.Construct(typedArray.Length * BytesPerElement), 0, typedArray.Length);
                for (int i = 0; i < typedArray.Length; i++)
                {
                    result[i] = typedArray[i];
                }
                return result;
            }
            else if (arg is ArrayBufferInstance)
            {
                // new %TypedArray%(buffer[, byteOffset[, length]]);
                var buffer = (ArrayBufferInstance)arg;
                int bytesPerElement = BytesPerElement;
                int actualLength;
                if (length == null)
                {
                    if (byteOffset < 0)
                        throw new JavaScriptException(Engine, ErrorType.RangeError, "Invalid typed array offset");
                    if ((byteOffset % BytesPerElement) != 0)
                        throw new JavaScriptException(Engine, ErrorType.RangeError, $"Start offset of {this.type} should be a multiple of {BytesPerElement}");
                    if ((buffer.ByteLength % BytesPerElement) != 0)
                        throw new JavaScriptException(Engine, ErrorType.RangeError, $"Byte length of {this.type} should be a multiple of {BytesPerElement}");
                    actualLength = (buffer.ByteLength - byteOffset) / bytesPerElement;
                    if (actualLength < 0)
                        throw new JavaScriptException(Engine, ErrorType.RangeError, "Start offset is too large");
                }
                else
                {
                    actualLength = length.Value;
                    if (byteOffset + actualLength * bytesPerElement > buffer.ByteLength)
                        throw new JavaScriptException(Engine, ErrorType.RangeError, "Invalid typed array length");
                }
                return new TypedArrayInstance(this.InstancePrototype, this.type, buffer, byteOffset, actualLength);
            }
            else if (arg is ObjectInstance)
            {
                // new %TypedArray%(object);
                return From(arg);
            }
            else
            {
                // new %TypedArray%(length);
                if (TypeUtilities.IsUndefined(arg))
                    throw new JavaScriptException(Engine, ErrorType.TypeError, "Argument cannot be undefined");
                int argLength = TypeConverter.ToInteger(arg);
                return new TypedArrayInstance(this.InstancePrototype, this.type, Engine.ArrayBuffer.Construct(argLength * BytesPerElement), 0, argLength);
            }
        }
Пример #10
0
        public TypedArrayInstance Construct(object arg, int byteOffset = 0, int?length = null)
        {
            // new Int8Array(typedArray);
            // new Int8Array(object);
            // new Int8Array(buffer[, byteOffset[, length]]);
            if (arg is TypedArrayInstance)
            {
                // new %TypedArray%(typedArray);
                var typedArray = (TypedArrayInstance)arg;

                // Copy the items one by one.
                var result = new TypedArrayInstance(this.InstancePrototype, this.type,
                                                    Engine.ArrayBuffer.Construct(typedArray.Length * BytesPerElement), 0, typedArray.Length);
                for (int i = 0; i < typedArray.Length; i++)
                {
                    result[i] = typedArray[i];
                }
                return(result);
            }
            else if (arg is ArrayBufferInstance)
            {
                // new %TypedArray%(buffer[, byteOffset[, length]]);
                var buffer          = (ArrayBufferInstance)arg;
                int bytesPerElement = BytesPerElement;
                int actualLength;
                if (length == null)
                {
                    if (byteOffset < 0)
                    {
                        throw new JavaScriptException(Engine, ErrorType.RangeError, "Invalid typed array offset");
                    }
                    if ((byteOffset % BytesPerElement) != 0)
                    {
                        throw new JavaScriptException(Engine, ErrorType.RangeError, $"Start offset of {this.type} should be a multiple of {BytesPerElement}");
                    }
                    if ((buffer.ByteLength % BytesPerElement) != 0)
                    {
                        throw new JavaScriptException(Engine, ErrorType.RangeError, $"Byte length of {this.type} should be a multiple of {BytesPerElement}");
                    }
                    actualLength = (buffer.ByteLength - byteOffset) / bytesPerElement;
                    if (actualLength < 0)
                    {
                        throw new JavaScriptException(Engine, ErrorType.RangeError, "Start offset is too large");
                    }
                }
                else
                {
                    actualLength = length.Value;
                    if (byteOffset + actualLength * bytesPerElement > buffer.ByteLength)
                    {
                        throw new JavaScriptException(Engine, ErrorType.RangeError, "Invalid typed array length");
                    }
                }
                return(new TypedArrayInstance(this.InstancePrototype, this.type, buffer, byteOffset, actualLength));
            }
            else if (arg is ObjectInstance)
            {
                // new %TypedArray%(object);
                return(From(arg));
            }
            else
            {
                // new %TypedArray%(length);
                if (TypeUtilities.IsUndefined(arg))
                {
                    throw new JavaScriptException(Engine, ErrorType.TypeError, "Argument cannot be undefined");
                }
                int argLength = TypeConverter.ToInteger(arg);
                return(new TypedArrayInstance(this.InstancePrototype, this.type, Engine.ArrayBuffer.Construct(argLength * BytesPerElement), 0, argLength));
            }
        }
Пример #11
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="typedArrayInstance">The displayed TypedArrayInstance</param>
 public TypedArrayInstanceDebugView(TypedArrayInstance typedArrayInstance)
 {
     this.typedArrayInstance = typedArrayInstance;
 }