joyent/node — pull request #2967 by mjor (2012-03-20 14:49:32+00:00)
Title: Remove unnecessary offset check when creating a typed array view of a Buffer

A check in the TypedArray constructor in `src/v8_typed_array.cc` makes it impossible to create a view of a Buffer with an offset that is not a multiple of the array's `BYTES_PER_ELEMENT` property. Because of this, to create a `Uint32Array` view of the Buffer `buf`, the second constructor argument (`byteOffset`) must be a multiple of 4, or the constructor call will throw an error.

// this will work fine
new Uint32Array(buf.slice(5, 9), 0, 1);

// this should reference the same part of the memory as the previous line
// but will throw "Byte offset not aligned."
new Uint32Array(buf, 5, 1);.

I may have overlooked some fundamental reason as to why this check is necessary (the [spec draft](http://www.khronos.org/registry/typedarray/specs/latest/) doesn't mention one). The way I see it, it only causes unnecessary pain when, for example, serializing a typed array from a stream.

I also took the liberty of rewriting the tests for typed arrays (`test/simple/test-typed-arrays.js`). The old version incorrectly used `assert(a, b)` instead of `assert.equals(a, b)`, meaning it practically couldn't fail.