Exemplo n.º 1
0
        private static ScriptValue GetLength([NotNull] ScriptArguments arg)
        {
            //https://tc39.github.io/ecma262/#sec-get-%typedarray%.prototype.length
            var obj = arg.ThisValue;

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

            var realObject = (ScriptObject)obj;

            if (!(realObject is ScriptIntegerIndexedObject))
            {
                throw arg.Agent.CreateTypeError();
            }

            var buffer = ((ScriptIntegerIndexedObject)realObject).ViewedArrayBuffer;

            if (ArrayBufferIntrinsics.IsDetachedBuffer(buffer))
            {
                return(0);
            }

            return(((ScriptIntegerIndexedObject)realObject).ArrayLength);
        }
Exemplo n.º 2
0
        private static ScriptValue DataView([NotNull] ScriptArguments arg)
        {
            //https://tc39.github.io/ecma262/#sec-dataview-buffer-byteoffset-bytelength
            if (arg.NewTarget == null)
            {
                throw arg.Agent.CreateTypeError();
            }

            var buffer = arg[0].AsObject(arg.Agent);

            if (buffer.SpecialObjectType != SpecialObjectType.ArrayBuffer)
            {
                throw arg.Agent.CreateTypeError();
            }

            var offset = arg.Agent.ToIndex(arg[1]);

            if (ArrayBufferIntrinsics.IsDetachedBuffer(buffer))
            {
                throw arg.Agent.CreateTypeError();
            }

            var bufferByteLength = buffer.ArrayBuffer.Data.LongLength;

            if (offset > bufferByteLength)
            {
                throw arg.Agent.CreateRangeError();
            }

            long viewByteLength;

            if (arg[2] == ScriptValue.Undefined)
            {
                viewByteLength = bufferByteLength - offset;
            }
            else
            {
                viewByteLength = arg.Agent.ToIndex(arg[2]);
                if (offset + viewByteLength > bufferByteLength)
                {
                    throw arg.Agent.CreateRangeError();
                }
            }

            var obj = arg.Agent.OrdinaryCreateFromConstructor(arg.NewTarget, arg.Function.Realm.DataViewPrototype, SpecialObjectType.DataView);

            obj.DataView.ViewedArrayBuffer = buffer;
            obj.DataView.ByteLength        = viewByteLength;
            obj.DataView.ByteOffset        = offset;
            return(obj);
        }