summaryrefslogtreecommitdiff
path: root/deps/v8/src/harmony-array.js
blob: 88b878f0a76ef594f64f7905d61459c05749724b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

'use strict';

// This file relies on the fact that the following declaration has been made
// in runtime.js:
// var $Array = global.Array;

// -------------------------------------------------------------------

// ES6 draft 07-15-13, section 15.4.3.23
function ArrayFind(predicate /* thisArg */) {  // length == 1
  CHECK_OBJECT_COERCIBLE(this, "Array.prototype.find");

  var array = ToObject(this);
  var length = ToInteger(array.length);

  if (!IS_SPEC_FUNCTION(predicate)) {
    throw MakeTypeError('called_non_callable', [predicate]);
  }

  var thisArg;
  if (%_ArgumentsLength() > 1) {
    thisArg = %_Arguments(1);
  }

  if (IS_NULL_OR_UNDEFINED(thisArg)) {
    thisArg = %GetDefaultReceiver(predicate) || thisArg;
  } else if (!IS_SPEC_OBJECT(thisArg) && %IsSloppyModeFunction(predicate)) {
    thisArg = ToObject(thisArg);
  }

  for (var i = 0; i < length; i++) {
    if (i in array) {
      var element = array[i];
      if (%_CallFunction(thisArg, element, i, array, predicate)) {
        return element;
      }
    }
  }

  return;
}


// ES6 draft 07-15-13, section 15.4.3.24
function ArrayFindIndex(predicate /* thisArg */) {  // length == 1
  CHECK_OBJECT_COERCIBLE(this, "Array.prototype.findIndex");

  var array = ToObject(this);
  var length = ToInteger(array.length);

  if (!IS_SPEC_FUNCTION(predicate)) {
    throw MakeTypeError('called_non_callable', [predicate]);
  }

  var thisArg;
  if (%_ArgumentsLength() > 1) {
    thisArg = %_Arguments(1);
  }

  if (IS_NULL_OR_UNDEFINED(thisArg)) {
    thisArg = %GetDefaultReceiver(predicate) || thisArg;
  } else if (!IS_SPEC_OBJECT(thisArg) && %IsSloppyModeFunction(predicate)) {
    thisArg = ToObject(thisArg);
  }

  for (var i = 0; i < length; i++) {
    if (i in array) {
      var element = array[i];
      if (%_CallFunction(thisArg, element, i, array, predicate)) {
        return i;
      }
    }
  }

  return -1;
}


// ES6, draft 04-05-14, section 22.1.3.6
function ArrayFill(value /* [, start [, end ] ] */) {  // length == 1
  CHECK_OBJECT_COERCIBLE(this, "Array.prototype.fill");

  var array = ToObject(this);
  var length = TO_UINT32(array.length);

  var i = 0;
  var end = length;

  if (%_ArgumentsLength() > 1) {
    i = %_Arguments(1);
    i = IS_UNDEFINED(i) ? 0 : TO_INTEGER(i);
    if (%_ArgumentsLength() > 2) {
      end = %_Arguments(2);
      end = IS_UNDEFINED(end) ? length : TO_INTEGER(end);
    }
  }

  if (i < 0) {
    i += length;
    if (i < 0) i = 0;
  } else {
    if (i > length) i = length;
  }

  if (end < 0) {
    end += length;
    if (end < 0) end = 0;
  } else {
    if (end > length) end = length;
  }

  if ((end - i) > 0 && ObjectIsFrozen(array)) {
    throw MakeTypeError("array_functions_on_frozen",
                        ["Array.prototype.fill"]);
  }

  for (; i < end; i++)
    array[i] = value;
  return array;
}

// ES6, draft 05-22-14, section 22.1.2.3
function ArrayOf() {
  var length = %_ArgumentsLength();
  var constructor = this;
  // TODO: Implement IsConstructor (ES6 section 7.2.5)
  var array = IS_SPEC_FUNCTION(constructor) ? new constructor(length) : [];
  for (var i = 0; i < length; i++) {
    %AddElement(array, i, %_Arguments(i), NONE);
  }
  array.length = length;
  return array;
}

// -------------------------------------------------------------------

function HarmonyArrayExtendArrayPrototype() {
  %CheckIsBootstrapping();

  // Set up non-enumerable functions on the Array object.
  InstallFunctions($Array, DONT_ENUM, $Array(
    "of", ArrayOf
  ));

  // Set up the non-enumerable functions on the Array prototype object.
  InstallFunctions($Array.prototype, DONT_ENUM, $Array(
    "find", ArrayFind,
    "findIndex", ArrayFindIndex,
    "fill", ArrayFill
  ));
}

HarmonyArrayExtendArrayPrototype();