Skip to content

Commit

Permalink
add support for ArrayBuffer and check non-Uint8Array types (#14)
Browse files Browse the repository at this point in the history
* code change

* compiled version

* put set in branch
  • Loading branch information
samthor authored Jun 16, 2020
1 parent 2489b7e commit e24e06d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 16 deletions.
31 changes: 29 additions & 2 deletions suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,41 @@ function tests(isNative, TextEncoder, TextDecoder) {
const out = dec.decode(buffer);

assert.equal(out, s);

});

suite('decoder', () => {

test('basic', () => {
const buffer = new Uint8Array([104, 101, 108, 108, 111]);
assert.equal(dec.decode(buffer), 'hello');
assert.equal(dec.decode(buffer), 'hello', 'directly Uint8Array');
assert.equal(dec.decode(buffer.buffer), 'hello', 'pass underlying ArrayBuffer');
});

test('non-8 backing', () => {
// If passed a Uint32Array, TextDecoder will still decode the real
// underlying bytes. Source data must be aligned.
const padded = new Uint8Array([104, 101, 108, 108, 111, 33, 46, 46]);
const u32 = new Uint32Array(padded.buffer);
assert.equal(padded.length >> 2, u32.length, 'u32 must be 1/4 of real data');
assert.equal(dec.decode(u32), 'hello!..', 'pass Uint32Array');
assert.equal(dec.decode(u32.buffer), 'hello!..', 'pass Uint32Array\'s buffer');

// Ensure that we don't parse larger typed arrays as uint8's. We expect
// nulls here to pad out the remaining three bytes in every word.
const u32raw = new Uint32Array([104, 105, 33, 46]);
assert.equal(dec.decode(u32raw), 'h\0\0\0i\0\0\0!\0\0\0.\0\0\0', 'u32 with nulls');
});

test('arraylike', () => {
const arr = [104, 101, 108, 108, 111];

if (isNative) {
// Native can't take Array.
assert.throws(() => dec.decode(arr));
} else {
// Polyfill can accept Array or array-like.
assert.equal(dec.decode(arr), 'hello', 'decode arraylike');
}
});

test('constructor', () => {
Expand Down
21 changes: 14 additions & 7 deletions text.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,21 @@ FastTextDecoder.prototype['decode'] = function(buffer, options={stream: false})
throw new Error(`Failed to decode: the 'stream' option is unsupported.`);
}

// Accept Uint8Array instances as-is.
let bytes = buffer;

// Look for ArrayBufferView, which isn't a real type, but basically represents
// all the valid TypedArray types plus DataView. They all have ".buffer" as
// an instance of ArrayBuffer.
if (!(bytes instanceof Uint8Array) && bytes.buffer instanceof ArrayBuffer) {
let bytes;

if (buffer instanceof Uint8Array) {
// Accept Uint8Array instances as-is.
bytes = buffer;
} else if (buffer.buffer instanceof ArrayBuffer) {
// Look for ArrayBufferView, which isn't a real type, but basically
// represents all the valid TypedArray types plus DataView. They all have
// ".buffer" as an instance of ArrayBuffer.
bytes = new Uint8Array(buffer.buffer);
} else {
// The only other valid argument here is that "buffer" is an ArrayBuffer.
// We also try to convert anything else passed to a Uint8Array, as this
// catches anything that's array-like. Native code would throw here.
bytes = new Uint8Array(buffer);
}

return decodeImpl(/** @type {!Uint8Array} */ (bytes));
Expand Down
12 changes: 6 additions & 6 deletions text.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e24e06d

Please sign in to comment.