Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(806)

Side by Side Diff: src/harmony-array-includes.js

Issue 1283703004: Add includes method to typed arrays (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | test/mjsunit/harmony/typed-array-includes.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 the V8 project authors. All rights reserved. 1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 (function(global, utils) { 5 (function(global, utils) {
6 6
7 'use strict'; 7 'use strict';
8 8
9 %CheckIsBootstrapping(); 9 %CheckIsBootstrapping();
10 10
11 var GlobalArray = global.Array; 11 var GlobalArray = global.Array;
12 12
13 // ------------------------------------------------------------------- 13 // -------------------------------------------------------------------
14 14
15 // Proposed for ES7 15 // Proposed for ES7
16 // https://github.com/tc39/Array.prototype.includes 16 // https://github.com/tc39/Array.prototype.includes
17 // 6e3b78c927aeda20b9d40e81303f9d44596cd904 17 // 46c7532ec8499dea3e51aeb940d09e07547ed3f5
18 function ArrayIncludes(searchElement, fromIndex) { 18 function InnerArrayIncludes(searchElement, fromIndex, array, length) {
19 var array = TO_OBJECT(this); 19 if (length === 0) {
20 var len = $toLength(array.length);
21
22 if (len === 0) {
23 return false; 20 return false;
24 } 21 }
25 22
26 var n = $toInteger(fromIndex); 23 var n = $toInteger(fromIndex);
27 24
28 var k; 25 var k;
29 if (n >= 0) { 26 if (n >= 0) {
30 k = n; 27 k = n;
31 } else { 28 } else {
32 k = len + n; 29 k = length + n;
33 if (k < 0) { 30 if (k < 0) {
34 k = 0; 31 k = 0;
35 } 32 }
36 } 33 }
37 34
38 while (k < len) { 35 while (k < length) {
39 var elementK = array[k]; 36 var elementK = array[k];
40 if ($sameValueZero(searchElement, elementK)) { 37 if ($sameValueZero(searchElement, elementK)) {
41 return true; 38 return true;
42 } 39 }
43 40
44 ++k; 41 ++k;
45 } 42 }
46 43
47 return false; 44 return false;
48 } 45 }
49 46
47
48 function ArrayIncludes(searchElement, fromIndex) {
49 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.includes");
50
51 var array = TO_OBJECT(this);
52 var length = $toLength(array.length);
53
54 return InnerArrayIncludes(searchElement, fromIndex, array, length);
55 }
56
57
58 function TypedArrayIncludes(searchElement, fromIndex) {
59 if (!%_IsTypedArray(this)) throw MakeTypeError(kNotTypedArray);
60
61 var length = %_TypedArrayGetLength(this);
62
63 return InnerArrayIncludes(searchElement, fromIndex, this, length);
64 }
65
50 // ------------------------------------------------------------------- 66 // -------------------------------------------------------------------
51 67
52 %FunctionSetLength(ArrayIncludes, 1); 68 %FunctionSetLength(ArrayIncludes, 1);
69 %FunctionSetLength(TypedArrayIncludes, 1);
53 70
54 // Set up the non-enumerable functions on the Array prototype object. 71 // Set up the non-enumerable function on the Array prototype object.
55 utils.InstallFunctions(GlobalArray.prototype, DONT_ENUM, [ 72 utils.InstallFunctions(GlobalArray.prototype, DONT_ENUM, [
56 "includes", ArrayIncludes 73 "includes", ArrayIncludes
57 ]); 74 ]);
58 75
76 // Set up the non-enumerable function on the typed array prototypes.
77 // This duplicates some of the machinery in harmony-typedarray.js in order to
78 // keep includes behind the separate --harmony-array-includes flag.
79 // TODO(littledan): Fix the TypedArray proto chain (bug v8:4085).
80
81 macro TYPED_ARRAYS(FUNCTION)
82 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize.
83 FUNCTION(Uint8Array)
84 FUNCTION(Int8Array)
85 FUNCTION(Uint16Array)
86 FUNCTION(Int16Array)
87 FUNCTION(Uint32Array)
88 FUNCTION(Int32Array)
89 FUNCTION(Float32Array)
90 FUNCTION(Float64Array)
91 FUNCTION(Uint8ClampedArray)
92 endmacro
93
94 macro DECLARE_GLOBALS(NAME)
95 var GlobalNAME = global.NAME;
96 endmacro
97
98 macro EXTEND_TYPED_ARRAY(NAME)
99 // Set up non-enumerable functions on the prototype object.
100 utils.InstallFunctions(GlobalNAME.prototype, DONT_ENUM, [
101 "includes", TypedArrayIncludes
102 ]);
103 endmacro
104
105 TYPED_ARRAYS(DECLARE_GLOBALS)
106 TYPED_ARRAYS(EXTEND_TYPED_ARRAY)
107
59 }) 108 })
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/harmony/typed-array-includes.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698