Chromium Code Reviews| Index: src/js/typedarray.js |
| diff --git a/src/js/typedarray.js b/src/js/typedarray.js |
| index 0bfa8abf20de928a1877687d304dde323e252cb8..161040524f2e97e5ea6afacd7bdb6298c4bef641 100644 |
| --- a/src/js/typedarray.js |
| +++ b/src/js/typedarray.js |
| @@ -11,9 +11,11 @@ |
| // ------------------------------------------------------------------- |
| // Imports |
| -var ArrayFrom; |
| +var AddIndexedProperty; |
| var ArrayToString; |
| var ArrayValues; |
| +var GetIterator; |
| +var GetMethod; |
| var GlobalArray = global.Array; |
| var GlobalArrayBuffer = global.ArrayBuffer; |
| var GlobalDataView = global.DataView; |
| @@ -67,9 +69,11 @@ endmacro |
| TYPED_ARRAYS(DECLARE_GLOBALS) |
| utils.Import(function(from) { |
| - ArrayFrom = from.ArrayFrom; |
| + AddIndexedProperty = from.AddIndexedProperty; |
| ArrayToString = from.ArrayToString; |
| ArrayValues = from.ArrayValues; |
| + GetIterator = from.GetIterator; |
| + GetMethod = from.GetMethod; |
| InnerArrayCopyWithin = from.InnerArrayCopyWithin; |
| InnerArrayEvery = from.InnerArrayEvery; |
| InnerArrayFill = from.InnerArrayFill; |
| @@ -760,14 +764,48 @@ function TypedArrayOf() { |
| } |
| +// ES#sec-iterabletoarraylike Runtime Semantics: IterableToArrayLike( items ) |
| +function IterableToArrayLike(items) { |
| + var iterable = GetMethod(items, iteratorSymbol); |
| + if (!IS_UNDEFINED(iterable)) { |
| + var array = new GlobalArray(); |
|
adamk
2016/02/26 22:18:48
Can you use an InternalArray here and %MoveArrayCo
Dan Ehrenberg
2016/02/29 23:24:44
Done
|
| + var i = 0; |
| + for (var value of |
| + { [iteratorSymbol]() { return GetIterator(items, iterable) } }) { |
| + AddIndexedProperty(array, i, value); |
| + i++; |
| + } |
| + return array; |
| + } |
| + return TO_OBJECT(items); |
| +} |
| + |
| + |
| +// ES#sec-%typedarray%.from |
| +// %TypedArray%.from ( source [ , mapfn [ , thisArg ] ] ) |
| function TypedArrayFrom(source, mapfn, thisArg) { |
| - // TODO(littledan): Investigate if there is a receiver which could be |
| - // faster to accumulate on than Array, e.g., a TypedVector. |
| - // TODO(littledan) BUG(v8:4782): Rewrite this code to ensure that things |
| - // happen in the right order, e.g., the constructor needs to be called |
| - // before the mapping function on array-likes. |
| - var array = %_Call(ArrayFrom, GlobalArray, source, mapfn, thisArg); |
| - return TypedArrayCreate(this, array); |
| + if (!%IsConstructor(this)) throw MakeTypeError(kNotConstructor, this); |
| + var mapping; |
| + if (!IS_UNDEFINED(mapfn)) { |
| + if (!IS_CALLABLE(mapfn)) throw MakeTypeError(kCalledNonCallable, this); |
| + mapping = true; |
| + } else { |
| + mapping = false; |
| + } |
| + var arrayLike = IterableToArrayLike(source); |
| + var length = TO_LENGTH(arrayLike.length); |
| + var targetObject = TypedArrayCreate(this, length); |
| + var value, mappedValue; |
| + for (var i = 0; i < length; i++) { |
| + value = arrayLike[i]; |
| + if (mapping) { |
| + mappedValue = %_Call(mapfn, thisArg, value, i); |
| + } else { |
| + mappedValue = value; |
| + } |
| + targetObject[i] = mappedValue; |
| + } |
| + return targetObject; |
| } |
| %FunctionSetLength(TypedArrayFrom, 1); |