Chromium Code Reviews| Index: src/harmony-typedarray.js |
| diff --git a/src/harmony-typedarray.js b/src/harmony-typedarray.js |
| index 5b666b5645f09a676b2cf8606f0f5a7b16d266dc..a163355e854f3322db61d821d5fe682bec55b12b 100644 |
| --- a/src/harmony-typedarray.js |
| +++ b/src/harmony-typedarray.js |
| @@ -10,29 +10,77 @@ |
| macro TYPED_ARRAYS(FUNCTION) |
| // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. |
| -FUNCTION(1, Uint8Array, 1) |
| -FUNCTION(2, Int8Array, 1) |
| -FUNCTION(3, Uint16Array, 2) |
| -FUNCTION(4, Int16Array, 2) |
| -FUNCTION(5, Uint32Array, 4) |
| -FUNCTION(6, Int32Array, 4) |
| -FUNCTION(7, Float32Array, 4) |
| -FUNCTION(8, Float64Array, 8) |
| -FUNCTION(9, Uint8ClampedArray, 1) |
| +FUNCTION(Uint8Array) |
| +FUNCTION(Int8Array) |
| +FUNCTION(Uint16Array) |
| +FUNCTION(Int16Array) |
| +FUNCTION(Uint32Array) |
| +FUNCTION(Int32Array) |
| +FUNCTION(Float32Array) |
| +FUNCTION(Float64Array) |
| +FUNCTION(Uint8ClampedArray) |
| endmacro |
| -macro DECLARE_GLOBALS(INDEX, NAME, SIZE) |
| +macro DECLARE_GLOBALS(NAME) |
| var GlobalNAME = global.NAME; |
| endmacro |
| TYPED_ARRAYS(DECLARE_GLOBALS) |
| -// ------------------------------------------------------------------- |
| +// ES6 draft 03-17-15, section 22.2.3.5 |
| +function TypedArrayCopyWithin(target, start, end) { |
| + if (!%IsTypedArray(this)) { |
| + throw MakeTypeError('not_typed_array', []); |
| + } |
| + |
| + var length = %_TypedArrayGetLength(this); |
| + |
| + target = TO_INTEGER(target); |
| + var to; |
| + if (target < 0) { |
| + to = $max(length + target, 0); |
| + } else { |
| + to = $min(target, length); |
| + } |
| + |
| + start = TO_INTEGER(start); |
| + var from; |
| + if (start < 0) { |
| + from = $max(length + start, 0); |
| + } else { |
| + from = $min(start, length); |
| + } |
| + |
| + end = IS_UNDEFINED(end) ? length : TO_INTEGER(end); |
| + var final; |
| + if (end < 0) { |
| + final = $max(length + end, 0); |
| + } else { |
| + final = $min(end, length); |
| + } |
| + |
| + var count = $min(final - from, length - to); |
| + var direction = 1; |
| + if (from < to && to < (from + count)) { |
| + direction = -1; |
| + from = from + count - 1; |
| + to = to + count - 1; |
| + } |
| -macro TYPED_ARRAY_HARMONY_ADDITIONS(ARRAY_ID, NAME, ELEMENT_SIZE) |
| + while (count > 0) { |
| + this[to] = this[from]; |
|
caitp (gmail)
2015/05/07 20:13:20
This is a pretty good first implementation.
Might
adamk
2015/05/07 20:28:07
To expand on this a bit, is there any reason not t
|
| + from = from + direction; |
| + to = to + direction; |
| + count--; |
| + } |
| + |
| + return this; |
| +} |
| + |
| +// ------------------------------------------------------------------- |
| // ES6 draft 05-05-15, section 22.2.3.7 |
| -function NAMEEvery(f /* thisArg */) { // length == 1 |
| +function TypedArrayEvery(f /* thisArg */) { // length == 1 |
|
caitp (gmail)
2015/05/07 20:13:20
Moving the rest of these outside the macro is good
|
| if (!%IsTypedArray(this)) { |
| throw MakeTypeError('not_typed_array', []); |
| } |
| @@ -66,7 +114,7 @@ function NAMEEvery(f /* thisArg */) { // length == 1 |
| } |
| // ES6 draft 08-24-14, section 22.2.3.12 |
| -function NAMEForEach(f /* thisArg */) { // length == 1 |
| +function TypedArrayForEach(f /* thisArg */) { // length == 1 |
| if (!%IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); |
| if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f); |
| @@ -95,7 +143,7 @@ function NAMEForEach(f /* thisArg */) { // length == 1 |
| } |
| // ES6 draft 08-24-14, section 22.2.2.2 |
| -function NAMEOf() { // length == 0 |
| +function TypedArrayOf() { // length == 0 |
| var length = %_ArgumentsLength(); |
| var array = new this(length); |
| for (var i = 0; i < length; i++) { |
| @@ -104,21 +152,19 @@ function NAMEOf() { // length == 0 |
| return array; |
| } |
| -endmacro |
| - |
| -TYPED_ARRAYS(TYPED_ARRAY_HARMONY_ADDITIONS) |
| - |
| +%FunctionSetLength(TypedArrayCopyWithin, 2); |
| -macro EXTEND_TYPED_ARRAY(ARRAY_ID, NAME, ELEMENT_SIZE) |
| +macro EXTEND_TYPED_ARRAY(NAME) |
| // Set up non-enumerable functions on the object. |
| $installFunctions(GlobalNAME, DONT_ENUM | DONT_DELETE | READ_ONLY, [ |
| - "of", NAMEOf |
| + "of", TypedArrayOf |
| ]); |
| // Set up non-enumerable functions on the prototype object. |
| $installFunctions(GlobalNAME.prototype, DONT_ENUM, [ |
| - "every", NAMEEvery, |
| - "forEach", NAMEForEach |
| + "copyWithin", TypedArrayCopyWithin, |
| + "every", TypedArrayEvery, |
| + "forEach", TypedArrayForEach |
| ]); |
| endmacro |