Skip to content

Commit

Permalink
temporarily remove length validation from Uint8Array constructor …
Browse files Browse the repository at this point in the history
…wrapper - bug in `ws` module

karma-runner/karma#1768
websockets/ws#645
  • Loading branch information
zloirock committed Jan 3, 2016
1 parent 167f993 commit 978d5d2
Show file tree
Hide file tree
Showing 8 changed files with 6,647 additions and 33 deletions.
11 changes: 7 additions & 4 deletions library/modules/_typed-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ if(require('./_descriptors')){
new Uint8Array(1).set({});
});

var strictToLength = function(it){
var strictToLength = function(it, SAME){
if(it === undefined)throw TypeError(WRONG_LENGTH);
var number = +it
, length = toLength(it);
if(!same(number, length))throw RangeError(WRONG_LENGTH);
if(SAME && !same(number, length))throw RangeError(WRONG_LENGTH);
return length;
};

Expand Down Expand Up @@ -326,6 +326,7 @@ if(require('./_descriptors')){
module.exports = function(KEY, BYTES, wrapper, CLAMPED){
CLAMPED = !!CLAMPED;
var NAME = KEY + (CLAMPED ? 'Clamped' : '') + 'Array'
, ISNT_UINT8 = NAME != 'Uint8Array'
, GETTER = 'get' + KEY
, SETTER = 'set' + KEY
, TypedArray = global[NAME]
Expand Down Expand Up @@ -361,7 +362,7 @@ if(require('./_descriptors')){
, offset = 0
, buffer, byteLength, length;
if(!isObject(data)){
length = strictToLength(data)
length = strictToLength(data, true)
byteLength = length * BYTES;
buffer = new $ArrayBuffer(byteLength);
} else if(data instanceof $ArrayBuffer){
Expand Down Expand Up @@ -401,7 +402,9 @@ if(require('./_descriptors')){
}, true)){
TypedArray = wrapper(function(that, data, $offset, $length){
anInstance(that, TypedArray, NAME);
if(!isObject(data))return new Base(strictToLength(data));
// `ws` module bug, temporarily remove validation length for Uint8Array
// https://github.com/websockets/ws/pull/645
if(!isObject(data))return new Base(strictToLength(data, ISNT_UINT8));
if(data instanceof $ArrayBuffer)return $length !== undefined
? new Base(data, toOffset($offset, BYTES), $length)
: $offset !== undefined
Expand Down
11 changes: 7 additions & 4 deletions modules/_typed-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ if(require('./_descriptors')){
new Uint8Array(1).set({});
});

var strictToLength = function(it){
var strictToLength = function(it, SAME){
if(it === undefined)throw TypeError(WRONG_LENGTH);
var number = +it
, length = toLength(it);
if(!same(number, length))throw RangeError(WRONG_LENGTH);
if(SAME && !same(number, length))throw RangeError(WRONG_LENGTH);
return length;
};

Expand Down Expand Up @@ -326,6 +326,7 @@ if(require('./_descriptors')){
module.exports = function(KEY, BYTES, wrapper, CLAMPED){
CLAMPED = !!CLAMPED;
var NAME = KEY + (CLAMPED ? 'Clamped' : '') + 'Array'
, ISNT_UINT8 = NAME != 'Uint8Array'
, GETTER = 'get' + KEY
, SETTER = 'set' + KEY
, TypedArray = global[NAME]
Expand Down Expand Up @@ -361,7 +362,7 @@ if(require('./_descriptors')){
, offset = 0
, buffer, byteLength, length;
if(!isObject(data)){
length = strictToLength(data)
length = strictToLength(data, true)
byteLength = length * BYTES;
buffer = new $ArrayBuffer(byteLength);
} else if(data instanceof $ArrayBuffer){
Expand Down Expand Up @@ -401,7 +402,9 @@ if(require('./_descriptors')){
}, true)){
TypedArray = wrapper(function(that, data, $offset, $length){
anInstance(that, TypedArray, NAME);
if(!isObject(data))return new Base(strictToLength(data));
// `ws` module bug, temporarily remove validation length for Uint8Array
// https://github.com/websockets/ws/pull/645
if(!isObject(data))return new Base(strictToLength(data, ISNT_UINT8));
if(data instanceof $ArrayBuffer)return $length !== undefined
? new Base(data, toOffset($offset, BYTES), $length)
: $offset !== undefined
Expand Down
11 changes: 6 additions & 5 deletions tests/es.js

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

11 changes: 6 additions & 5 deletions tests/library.js

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

11 changes: 6 additions & 5 deletions tests/library/es6.typed.constructors.ls
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ if DESCRIPTORS
for $name, $bytes of {Float32Array: 4, Float64Array: 8, Int8Array: 1, Int16Array: 2, Int32Array: 4, Uint8Array: 1, Uint16Array: 2, Uint32Array: 4, Uint8ClampedArray: 1}
let name = $name, bytes = $bytes
Typed = core[name]
test "#{name} constructor", !(assert)~>
NATIVE_OR_ISNT_UINT8 = NATIVE or name isnt \Uint8Array
test "#{name} constructor" !(assert)~>
assert.isFunction Typed

assert.same Typed.BYTES_PER_ELEMENT, bytes, "#{name}.BYTES_PER_ELEMENT"
Expand All @@ -32,10 +33,10 @@ if DESCRIPTORS

assert.throws (!-> new Typed), TypeError, 'throws without argument'
assert.throws (!-> new Typed void), TypeError, 'throws on undefined'
assert.throws (!-> new Typed 1.5), RangeError, 'throws on 1.5'
assert.throws (!-> new Typed -1), RangeError, 'throws on -1'
assert.throws (!-> new Typed -0), RangeError, 'throws on -0'
assert.throws (!-> new Typed NaN), RangeError, 'throws on NaN'
NATIVE_OR_ISNT_UINT8 and assert.throws (!-> new Typed 1.5), RangeError, 'throws on 1.5'
NATIVE_OR_ISNT_UINT8 and assert.throws (!-> new Typed -1), RangeError, 'throws on -1'
NATIVE_OR_ISNT_UINT8 and assert.throws (!-> new Typed -0), RangeError, 'throws on -0'
NATIVE_OR_ISNT_UINT8 and assert.throws (!-> new Typed NaN), RangeError, 'throws on NaN'

try
a = new Typed null # throws in most engines
Expand Down
Loading

0 comments on commit 978d5d2

Please sign in to comment.