mirror of
https://github.com/shaka-project/shaka-player.git
synced 2026-06-16 16:16:40 +03:00
18b59c5294
Using "new Uint8Array" with a TypedArray creates a copy of the buffer; this is unnecessarily expensive for large buffers. This adds a rule to disallow using it in favor of a new utility that correctly creates a new "view" on the same buffer. Note it is fine to pass an ArrayBuffer to "new Uint8Array" and it won't copy; but there there are many cases where the type is BufferSource, so it could be a TypedArray. Unfortunately, there are many other cases where we explicitly pass an ArrayBuffer; but the compiler rules don't allow us to whitelist this case (since ArrayBuffer is part of BufferSource). Change-Id: I58696a85a9cbcc188c0b16919c9eeb63e56edca1
171 lines
6.0 KiB
JavaScript
171 lines
6.0 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2016 Google Inc.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
describe('BufferUtils', () => {
|
|
const BufferUtils = shaka.util.BufferUtils;
|
|
|
|
describe('equal', () => {
|
|
it('allows null', () => {
|
|
const buffer = new Uint8Array([0]);
|
|
expect(BufferUtils.equal(buffer, null)).toBe(false);
|
|
expect(BufferUtils.equal(null, buffer)).toBe(false);
|
|
expect(BufferUtils.equal(null, null)).toBe(true);
|
|
});
|
|
|
|
it('checks length', () => {
|
|
const a = new Uint8Array([0]);
|
|
const b = new Uint8Array([0, 1]);
|
|
expect(BufferUtils.equal(a, b)).toBe(false);
|
|
});
|
|
|
|
it('compares values', () => {
|
|
const a = new Uint8Array([0, 1]);
|
|
const b = new Uint8Array([0, 1]);
|
|
const c = new Uint8Array([0, 2]);
|
|
expect(a).not.toBe(b);
|
|
expect(a.buffer).not.toBe(b.buffer);
|
|
expect(BufferUtils.equal(a, a)).toBe(true);
|
|
expect(BufferUtils.equal(a, b)).toBe(true);
|
|
expect(BufferUtils.equal(a, c)).toBe(false);
|
|
});
|
|
|
|
it('compares different types', () => {
|
|
const a = new Uint8Array([0, 1, 2, 3]);
|
|
const b = new DataView(new ArrayBuffer(4));
|
|
b.setUint16(0, 0x0001, false);
|
|
b.setUint16(2, 0x0203, false);
|
|
expect(BufferUtils.equal(a, b)).toBe(true);
|
|
});
|
|
|
|
it('compares with same buffer', () => {
|
|
const a = new Uint8Array([0, 1, 2, 3]);
|
|
const b = new Uint16Array(a.buffer);
|
|
expect(BufferUtils.equal(a, b)).toBe(true);
|
|
});
|
|
|
|
it('compares different views', () => {
|
|
const top = new Uint8Array([0, 1, 2, 3, 0, 1, 2, 3]);
|
|
const a = new Uint8Array(top.buffer, 0, 4);
|
|
const b = new Uint8Array(top.buffer, 2, 4);
|
|
const c = new Uint8Array(top.buffer, 4, 4);
|
|
const d = new Uint16Array(top.buffer, 0, 2);
|
|
const e = new DataView(top.buffer, 0, 4);
|
|
expect(BufferUtils.equal(top, a)).toBe(false);
|
|
expect(BufferUtils.equal(top, b)).toBe(false);
|
|
expect(BufferUtils.equal(top, c)).toBe(false);
|
|
expect(BufferUtils.equal(a, b)).toBe(false);
|
|
expect(BufferUtils.equal(a, c)).toBe(true);
|
|
expect(BufferUtils.equal(a, d)).toBe(true);
|
|
expect(BufferUtils.equal(a, e)).toBe(true);
|
|
expect(BufferUtils.equal(a, e)).toBe(true);
|
|
});
|
|
|
|
it('compares ArrayBuffers', () => {
|
|
const a = new Uint8Array([0, 1, 2, 3]);
|
|
const b = new Uint8Array([0, 1, 2, 3]);
|
|
const c = new Uint8Array([0, 1, 2, 4]);
|
|
const d = new Uint8Array([0, 1, 2]);
|
|
expect(BufferUtils.equal(a.buffer, a.buffer)).toBe(true);
|
|
expect(BufferUtils.equal(a.buffer, b.buffer)).toBe(true);
|
|
expect(BufferUtils.equal(a.buffer, c.buffer)).toBe(false);
|
|
expect(BufferUtils.equal(a.buffer, d.buffer)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('toUint8', () => {
|
|
it('allows passing ArrayBuffer', () => {
|
|
const buffer = new ArrayBuffer(10);
|
|
const value = BufferUtils.toUint8(buffer);
|
|
expect(value.buffer).toBe(buffer);
|
|
expect(value.byteOffset).toBe(0);
|
|
expect(value.byteLength).toBe(buffer.byteLength);
|
|
});
|
|
|
|
it('allows passing Uint8Array', () => {
|
|
const buffer = new Uint8Array(10);
|
|
const value = BufferUtils.toUint8(buffer);
|
|
expect(value).not.toBe(buffer);
|
|
expect(value.buffer).toBe(buffer.buffer);
|
|
expect(value.byteOffset).toBe(0);
|
|
expect(value.byteLength).toBe(buffer.byteLength);
|
|
});
|
|
|
|
it('allows passing partial Uint8Array', () => {
|
|
const buffer = new ArrayBuffer(10);
|
|
const view = new Uint8Array(buffer, 4, 4);
|
|
const value = BufferUtils.toUint8(view);
|
|
expect(value).not.toBe(view);
|
|
expect(value.buffer).toBe(buffer);
|
|
expect(value.byteOffset).toBe(4);
|
|
expect(value.byteLength).toBe(4);
|
|
});
|
|
|
|
it('allows setting offset/length with ArrayBuffer', () => {
|
|
const buffer = new ArrayBuffer(10);
|
|
const value = BufferUtils.toUint8(buffer, 3, 5);
|
|
expect(value.buffer).toBe(buffer);
|
|
expect(value.byteOffset).toBe(3);
|
|
expect(value.byteLength).toBe(5);
|
|
});
|
|
|
|
it('allows setting offset/length with Uint8Array', () => {
|
|
const buffer = new Uint8Array(10);
|
|
const value = BufferUtils.toUint8(buffer, 3, 5);
|
|
expect(value).not.toBe(buffer);
|
|
expect(value.buffer).toBe(buffer.buffer);
|
|
expect(value.byteOffset).toBe(3);
|
|
expect(value.byteLength).toBe(5);
|
|
});
|
|
|
|
it('allows setting offset/length with partial Uint8Array', () => {
|
|
const buffer = new ArrayBuffer(20);
|
|
const view = new Uint8Array(buffer, 5, 10);
|
|
const value = BufferUtils.toUint8(view, 3, 5);
|
|
expect(value).not.toBe(view);
|
|
expect(value.buffer).toBe(buffer);
|
|
expect(value.byteOffset).toBe(8);
|
|
expect(value.byteLength).toBe(5);
|
|
});
|
|
|
|
it('clamps offset to buffer', () => {
|
|
const buffer = new ArrayBuffer(10);
|
|
const value = BufferUtils.toUint8(buffer, 12);
|
|
expect(value.buffer).toBe(buffer);
|
|
expect(value.byteOffset).toBe(10);
|
|
expect(value.byteLength).toBe(0);
|
|
});
|
|
|
|
it('clamps offset to view', () => {
|
|
const buffer = new ArrayBuffer(20);
|
|
const view = new Uint8Array(buffer, 5, 10);
|
|
const value = BufferUtils.toUint8(view, 12);
|
|
expect(value.buffer).toBe(buffer);
|
|
expect(value.byteOffset).toBe(15);
|
|
expect(value.byteLength).toBe(0);
|
|
});
|
|
|
|
it('clamps length to view', () => {
|
|
const buffer = new ArrayBuffer(20);
|
|
const view = new Uint8Array(buffer, 5, 10);
|
|
const value = BufferUtils.toUint8(view, 4, 20);
|
|
expect(value.buffer).toBe(buffer);
|
|
expect(value.byteOffset).toBe(9);
|
|
expect(value.byteLength).toBe(6);
|
|
});
|
|
});
|
|
});
|