2013-07-11 11:20:54 +00:00
|
|
|
// Copyright 2013 the V8 project authors. All rights reserved.
|
2014-05-12 08:43:01 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2013-07-11 11:20:54 +00:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2014-05-09 16:37:04 +00:00
|
|
|
|
2013-07-11 11:20:54 +00:00
|
|
|
// This file relies on the fact that the following declaration has been made
|
|
|
|
// in runtime.js:
|
|
|
|
// var $Array = global.Array;
|
|
|
|
|
2014-05-09 16:37:04 +00:00
|
|
|
|
2014-03-24 16:34:06 +00:00
|
|
|
var arrayIteratorObjectSymbol = GLOBAL_PRIVATE("ArrayIterator#object");
|
|
|
|
var arrayIteratorNextIndexSymbol = GLOBAL_PRIVATE("ArrayIterator#next");
|
|
|
|
var arrayIterationKindSymbol = GLOBAL_PRIVATE("ArrayIterator#kind");
|
2013-07-11 11:20:54 +00:00
|
|
|
|
2014-05-09 16:37:04 +00:00
|
|
|
|
2013-07-11 11:20:54 +00:00
|
|
|
function ArrayIterator() {}
|
|
|
|
|
2014-05-09 16:37:04 +00:00
|
|
|
|
2014-06-12 17:31:54 +00:00
|
|
|
// TODO(wingo): Update section numbers when ES6 has stabilized. The
|
|
|
|
// section numbers below are already out of date as of the May 2014
|
|
|
|
// draft.
|
|
|
|
|
|
|
|
|
2013-07-11 11:20:54 +00:00
|
|
|
// 15.4.5.1 CreateArrayIterator Abstract Operation
|
|
|
|
function CreateArrayIterator(array, kind) {
|
|
|
|
var object = ToObject(array);
|
|
|
|
var iterator = new ArrayIterator;
|
2014-03-24 16:34:06 +00:00
|
|
|
SET_PRIVATE(iterator, arrayIteratorObjectSymbol, object);
|
Provide private symbols through internal APIs
Adds a notion of private symbols, mainly intended for internal use, especially, self-hosting of built-in types that would otherwise require new C++ classes.
On the JS side (i.e., in built-ins), private properties can be created and accessed through a set of macros:
NEW_PRIVATE(print_name)
HAS_PRIVATE(obj, sym)
GET_PRIVATE(obj, sym)
SET_PRIVATE(obj, sym, val)
DELETE_PRIVATE(obj, sym)
In the V8 API, they are accessible via a new class Private, and respective HasPrivate/Get/Private/SetPrivate/DeletePrivate methods on calss Object.
These APIs are designed and restricted such that their implementation can later be replaced by whatever ES7+ will officially provide.
R=yangguo@chromium.org
BUG=
Review URL: https://codereview.chromium.org/48923002
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@17683 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-11-13 10:34:06 +00:00
|
|
|
SET_PRIVATE(iterator, arrayIteratorNextIndexSymbol, 0);
|
|
|
|
SET_PRIVATE(iterator, arrayIterationKindSymbol, kind);
|
2013-07-11 11:20:54 +00:00
|
|
|
return iterator;
|
|
|
|
}
|
|
|
|
|
2014-05-09 16:37:04 +00:00
|
|
|
|
2013-07-11 11:20:54 +00:00
|
|
|
// 15.19.4.3.4 CreateItrResultObject
|
|
|
|
function CreateIteratorResultObject(value, done) {
|
|
|
|
return {value: value, done: done};
|
|
|
|
}
|
|
|
|
|
2014-05-09 16:37:04 +00:00
|
|
|
|
2014-06-12 17:31:54 +00:00
|
|
|
// 22.1.5.2.2 %ArrayIteratorPrototype%[@@iterator]
|
|
|
|
function ArrayIteratorIterator() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-11 11:20:54 +00:00
|
|
|
// 15.4.5.2.2 ArrayIterator.prototype.next( )
|
|
|
|
function ArrayIteratorNext() {
|
|
|
|
var iterator = ToObject(this);
|
2014-05-21 08:05:11 +00:00
|
|
|
|
|
|
|
if (!HAS_PRIVATE(iterator, arrayIteratorObjectSymbol)) {
|
2013-07-11 11:20:54 +00:00
|
|
|
throw MakeTypeError('incompatible_method_receiver',
|
|
|
|
['Array Iterator.prototype.next']);
|
|
|
|
}
|
|
|
|
|
2014-05-21 08:05:11 +00:00
|
|
|
var array = GET_PRIVATE(iterator, arrayIteratorObjectSymbol);
|
|
|
|
if (IS_UNDEFINED(array)) {
|
|
|
|
return CreateIteratorResultObject(UNDEFINED, true);
|
|
|
|
}
|
|
|
|
|
Provide private symbols through internal APIs
Adds a notion of private symbols, mainly intended for internal use, especially, self-hosting of built-in types that would otherwise require new C++ classes.
On the JS side (i.e., in built-ins), private properties can be created and accessed through a set of macros:
NEW_PRIVATE(print_name)
HAS_PRIVATE(obj, sym)
GET_PRIVATE(obj, sym)
SET_PRIVATE(obj, sym, val)
DELETE_PRIVATE(obj, sym)
In the V8 API, they are accessible via a new class Private, and respective HasPrivate/Get/Private/SetPrivate/DeletePrivate methods on calss Object.
These APIs are designed and restricted such that their implementation can later be replaced by whatever ES7+ will officially provide.
R=yangguo@chromium.org
BUG=
Review URL: https://codereview.chromium.org/48923002
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@17683 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-11-13 10:34:06 +00:00
|
|
|
var index = GET_PRIVATE(iterator, arrayIteratorNextIndexSymbol);
|
|
|
|
var itemKind = GET_PRIVATE(iterator, arrayIterationKindSymbol);
|
2013-07-11 11:20:54 +00:00
|
|
|
var length = TO_UINT32(array.length);
|
|
|
|
|
|
|
|
// "sparse" is never used.
|
|
|
|
|
|
|
|
if (index >= length) {
|
2014-05-21 08:05:11 +00:00
|
|
|
SET_PRIVATE(iterator, arrayIteratorObjectSymbol, UNDEFINED);
|
2013-10-17 10:02:45 +00:00
|
|
|
return CreateIteratorResultObject(UNDEFINED, true);
|
2013-07-11 11:20:54 +00:00
|
|
|
}
|
|
|
|
|
Provide private symbols through internal APIs
Adds a notion of private symbols, mainly intended for internal use, especially, self-hosting of built-in types that would otherwise require new C++ classes.
On the JS side (i.e., in built-ins), private properties can be created and accessed through a set of macros:
NEW_PRIVATE(print_name)
HAS_PRIVATE(obj, sym)
GET_PRIVATE(obj, sym)
SET_PRIVATE(obj, sym, val)
DELETE_PRIVATE(obj, sym)
In the V8 API, they are accessible via a new class Private, and respective HasPrivate/Get/Private/SetPrivate/DeletePrivate methods on calss Object.
These APIs are designed and restricted such that their implementation can later be replaced by whatever ES7+ will officially provide.
R=yangguo@chromium.org
BUG=
Review URL: https://codereview.chromium.org/48923002
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@17683 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-11-13 10:34:06 +00:00
|
|
|
SET_PRIVATE(iterator, arrayIteratorNextIndexSymbol, index + 1);
|
2013-07-11 11:20:54 +00:00
|
|
|
|
2014-05-21 08:05:11 +00:00
|
|
|
if (itemKind == ITERATOR_KIND_VALUES) {
|
2013-08-06 13:10:07 +00:00
|
|
|
return CreateIteratorResultObject(array[index], false);
|
2014-05-21 08:05:11 +00:00
|
|
|
}
|
2013-07-11 11:20:54 +00:00
|
|
|
|
2014-05-21 08:05:11 +00:00
|
|
|
if (itemKind == ITERATOR_KIND_ENTRIES) {
|
2013-08-06 13:10:07 +00:00
|
|
|
return CreateIteratorResultObject([index, array[index]], false);
|
2014-05-21 08:05:11 +00:00
|
|
|
}
|
2013-07-11 11:20:54 +00:00
|
|
|
|
2013-08-06 13:10:07 +00:00
|
|
|
return CreateIteratorResultObject(index, false);
|
2013-07-11 11:20:54 +00:00
|
|
|
}
|
|
|
|
|
2014-05-09 16:37:04 +00:00
|
|
|
|
2013-07-11 11:20:54 +00:00
|
|
|
function ArrayEntries() {
|
2014-04-17 17:45:32 +00:00
|
|
|
return CreateArrayIterator(this, ITERATOR_KIND_ENTRIES);
|
2013-07-11 11:20:54 +00:00
|
|
|
}
|
|
|
|
|
2014-05-09 16:37:04 +00:00
|
|
|
|
2013-07-11 11:20:54 +00:00
|
|
|
function ArrayValues() {
|
2014-04-17 17:45:32 +00:00
|
|
|
return CreateArrayIterator(this, ITERATOR_KIND_VALUES);
|
2013-07-11 11:20:54 +00:00
|
|
|
}
|
|
|
|
|
2014-05-09 16:37:04 +00:00
|
|
|
|
2013-07-11 11:20:54 +00:00
|
|
|
function ArrayKeys() {
|
2014-04-17 17:45:32 +00:00
|
|
|
return CreateArrayIterator(this, ITERATOR_KIND_KEYS);
|
2013-07-11 11:20:54 +00:00
|
|
|
}
|
|
|
|
|
2014-05-09 16:37:04 +00:00
|
|
|
|
2013-07-11 11:20:54 +00:00
|
|
|
function SetUpArrayIterator() {
|
|
|
|
%CheckIsBootstrapping();
|
|
|
|
|
2014-05-09 16:37:04 +00:00
|
|
|
%FunctionSetPrototype(ArrayIterator, new $Object());
|
2013-07-11 11:20:54 +00:00
|
|
|
%FunctionSetInstanceClassName(ArrayIterator, 'Array Iterator');
|
|
|
|
|
|
|
|
InstallFunctions(ArrayIterator.prototype, DONT_ENUM, $Array(
|
|
|
|
'next', ArrayIteratorNext
|
|
|
|
));
|
2014-06-12 17:31:54 +00:00
|
|
|
%FunctionSetName(ArrayIteratorIterator, '[Symbol.iterator]');
|
2014-07-14 14:05:30 +00:00
|
|
|
%AddNamedProperty(ArrayIterator.prototype, symbolIterator,
|
|
|
|
ArrayIteratorIterator, DONT_ENUM);
|
2013-07-11 11:20:54 +00:00
|
|
|
}
|
|
|
|
SetUpArrayIterator();
|
|
|
|
|
2014-05-09 16:37:04 +00:00
|
|
|
|
2013-07-11 11:20:54 +00:00
|
|
|
function ExtendArrayPrototype() {
|
|
|
|
%CheckIsBootstrapping();
|
|
|
|
|
|
|
|
InstallFunctions($Array.prototype, DONT_ENUM, $Array(
|
|
|
|
'entries', ArrayEntries,
|
|
|
|
'values', ArrayValues,
|
|
|
|
'keys', ArrayKeys
|
|
|
|
));
|
2014-06-25 07:32:57 +00:00
|
|
|
|
2014-07-14 14:05:30 +00:00
|
|
|
%AddNamedProperty($Array.prototype, symbolIterator, ArrayValues, DONT_ENUM);
|
2013-07-11 11:20:54 +00:00
|
|
|
}
|
|
|
|
ExtendArrayPrototype();
|
2014-06-25 08:46:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
function ExtendTypedArrayPrototypes() {
|
|
|
|
%CheckIsBootstrapping();
|
|
|
|
|
|
|
|
macro TYPED_ARRAYS(FUNCTION)
|
|
|
|
FUNCTION(Uint8Array)
|
|
|
|
FUNCTION(Int8Array)
|
|
|
|
FUNCTION(Uint16Array)
|
|
|
|
FUNCTION(Int16Array)
|
|
|
|
FUNCTION(Uint32Array)
|
|
|
|
FUNCTION(Int32Array)
|
|
|
|
FUNCTION(Float32Array)
|
|
|
|
FUNCTION(Float64Array)
|
|
|
|
FUNCTION(Uint8ClampedArray)
|
|
|
|
endmacro
|
|
|
|
|
|
|
|
macro EXTEND_TYPED_ARRAY(NAME)
|
2014-07-14 14:05:30 +00:00
|
|
|
%AddNamedProperty($NAME.prototype, 'entries', ArrayEntries, DONT_ENUM);
|
|
|
|
%AddNamedProperty($NAME.prototype, 'values', ArrayValues, DONT_ENUM);
|
|
|
|
%AddNamedProperty($NAME.prototype, 'keys', ArrayKeys, DONT_ENUM);
|
|
|
|
%AddNamedProperty($NAME.prototype, symbolIterator, ArrayValues, DONT_ENUM);
|
2014-06-25 08:46:53 +00:00
|
|
|
endmacro
|
|
|
|
|
|
|
|
TYPED_ARRAYS(EXTEND_TYPED_ARRAY)
|
|
|
|
}
|
|
|
|
ExtendTypedArrayPrototypes();
|