harmony-array.js 7.85 KB
Newer Older
1
// Copyright 2013 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
(function(global, utils) {
6

7 8
'use strict';

9 10
%CheckIsBootstrapping();

11 12 13
// -------------------------------------------------------------------
// Imports

14 15
var GlobalArray = global.Array;
var GlobalSymbol = global.Symbol;
16

17 18
var GetIterator;
var GetMethod;
19 20
var MathMax;
var MathMin;
21
var ObjectIsFrozen;
22
var ObjectDefineProperty;
23 24

utils.Import(function(from) {
25 26
  GetIterator = from.GetIterator;
  GetMethod = from.GetMethod;
27 28
  MathMax = from.MathMax;
  MathMin = from.MathMin;
29
  ObjectIsFrozen = from.ObjectIsFrozen;
30
  ObjectDefineProperty = from.ObjectDefineProperty;
31 32
});

33 34
// -------------------------------------------------------------------

35
function InnerArrayCopyWithin(target, start, end, array, length) {
36 37 38
  target = TO_INTEGER(target);
  var to;
  if (target < 0) {
39
    to = MathMax(length + target, 0);
40
  } else {
41
    to = MathMin(target, length);
42 43 44 45 46
  }

  start = TO_INTEGER(start);
  var from;
  if (start < 0) {
47
    from = MathMax(length + start, 0);
48
  } else {
49
    from = MathMin(start, length);
50 51 52 53 54
  }

  end = IS_UNDEFINED(end) ? length : TO_INTEGER(end);
  var final;
  if (end < 0) {
55
    final = MathMax(length + end, 0);
56
  } else {
57
    final = MathMin(end, length);
58 59
  }

60
  var count = MathMin(final - from, length - to);
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  var direction = 1;
  if (from < to && to < (from + count)) {
    direction = -1;
    from = from + count - 1;
    to = to + count - 1;
  }

  while (count > 0) {
    if (from in array) {
      array[to] = array[from];
    } else {
      delete array[to];
    }
    from = from + direction;
    to = to + direction;
    count--;
  }

  return array;
}
81 82 83 84 85 86 87 88 89 90

// ES6 draft 03-17-15, section 22.1.3.3
function ArrayCopyWithin(target, start, end) {
  CHECK_OBJECT_COERCIBLE(this, "Array.prototype.copyWithin");

  var array = TO_OBJECT_INLINE(this);
  var length = $toLength(array.length);

  return InnerArrayCopyWithin(target, start, end, array, length);
}
91

92
function InnerArrayFind(predicate, thisArg, array, length) {
93
  if (!IS_SPEC_FUNCTION(predicate)) {
94
    throw MakeTypeError(kCalledNonCallable, predicate);
95 96
  }

97
  var needs_wrapper = false;
98 99 100
  if (IS_NULL(thisArg)) {
    if (%IsSloppyModeFunction(predicate)) thisArg = UNDEFINED;
  } else if (!IS_UNDEFINED(thisArg)) {
101
    needs_wrapper = SHOULD_CREATE_WRAPPER(predicate, thisArg);
102 103 104
  }

  for (var i = 0; i < length; i++) {
105 106 107 108
    var element = array[i];
    var newThisArg = needs_wrapper ? $toObject(thisArg) : thisArg;
    if (%_CallFunction(newThisArg, element, i, array, predicate)) {
      return element;
109 110 111 112 113 114
    }
  }

  return;
}

115 116 117
// ES6 draft 07-15-13, section 15.4.3.23
function ArrayFind(predicate, thisArg) {
  CHECK_OBJECT_COERCIBLE(this, "Array.prototype.find");
118

119 120
  var array = $toObject(this);
  var length = $toInteger(array.length);
121

122 123 124 125
  return InnerArrayFind(predicate, thisArg, array, length);
}

function InnerArrayFindIndex(predicate, thisArg, array, length) {
126
  if (!IS_SPEC_FUNCTION(predicate)) {
127
    throw MakeTypeError(kCalledNonCallable, predicate);
128 129
  }

130
  var needs_wrapper = false;
131 132 133
  if (IS_NULL(thisArg)) {
    if (%IsSloppyModeFunction(predicate)) thisArg = UNDEFINED;
  } else if (!IS_UNDEFINED(thisArg)) {
134
    needs_wrapper = SHOULD_CREATE_WRAPPER(predicate, thisArg);
135 136 137
  }

  for (var i = 0; i < length; i++) {
138 139 140 141
    var element = array[i];
    var newThisArg = needs_wrapper ? $toObject(thisArg) : thisArg;
    if (%_CallFunction(newThisArg, element, i, array, predicate)) {
      return i;
142 143 144 145 146 147
    }
  }

  return -1;
}

148 149 150
// ES6 draft 07-15-13, section 15.4.3.24
function ArrayFindIndex(predicate, thisArg) {
  CHECK_OBJECT_COERCIBLE(this, "Array.prototype.findIndex");
151

152
  var array = $toObject(this);
153
  var length = $toInteger(array.length);
154

155 156
  return InnerArrayFindIndex(predicate, thisArg, array, length);
}
157

158 159 160 161
// ES6, draft 04-05-14, section 22.1.3.6
function InnerArrayFill(value, start, end, array, length) {
  var i = IS_UNDEFINED(start) ? 0 : TO_INTEGER(start);
  var end = IS_UNDEFINED(end) ? length : TO_INTEGER(end);
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

  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;
  }

177
  if ((end - i) > 0 && ObjectIsFrozen(array)) {
178
    throw MakeTypeError(kArrayFunctionsOnFrozen);
179 180 181 182 183 184
  }

  for (; i < end; i++)
    array[i] = value;
  return array;
}
185 186 187 188 189 190 191 192 193 194

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

  var array = $toObject(this);
  var length = TO_UINT32(array.length);

  return InnerArrayFill(value, start, end, array, length);
}
195

196 197 198 199 200 201 202 203 204 205
function AddArrayElement(constructor, array, i, value) {
  if (constructor === GlobalArray) {
    %AddElement(array, i, value);
  } else {
    ObjectDefineProperty(array, i, {
      value: value, writable: true, configurable: true, enumerable: true
    });
  }
}

caitpotter88's avatar
caitpotter88 committed
206 207
// ES6, draft 10-14-14, section 22.1.2.1
function ArrayFrom(arrayLike, mapfn, receiver) {
208
  var items = $toObject(arrayLike);
caitpotter88's avatar
caitpotter88 committed
209 210 211 212
  var mapping = !IS_UNDEFINED(mapfn);

  if (mapping) {
    if (!IS_SPEC_FUNCTION(mapfn)) {
213
      throw MakeTypeError(kCalledNonCallable, mapfn);
214 215 216 217 218 219
    } else if (%IsSloppyModeFunction(mapfn)) {
      if (IS_NULL(receiver)) {
        receiver = UNDEFINED;
      } else if (!IS_UNDEFINED(receiver)) {
        receiver = TO_OBJECT_INLINE(receiver);
      }
caitpotter88's avatar
caitpotter88 committed
220 221 222
    }
  }

223
  var iterable = GetMethod(items, symbolIterator);
caitpotter88's avatar
caitpotter88 committed
224 225 226 227 228 229
  var k;
  var result;
  var mappedValue;
  var nextValue;

  if (!IS_UNDEFINED(iterable)) {
230
    result = %IsConstructor(this) ? new this() : [];
caitpotter88's avatar
caitpotter88 committed
231

232
    var iterator = GetIterator(items, iterable);
233

caitpotter88's avatar
caitpotter88 committed
234
    k = 0;
235 236 237 238
    while (true) {
      var next = iterator.next();

      if (!IS_OBJECT(next)) {
239
        throw MakeTypeError(kIteratorResultNotAnObject, next);
240 241 242 243 244 245 246 247 248 249 250 251 252
      }

      if (next.done) {
        result.length = k;
        return result;
      }

      nextValue = next.value;
      if (mapping) {
        mappedValue = %_CallFunction(receiver, nextValue, k, mapfn);
      } else {
        mappedValue = nextValue;
      }
253 254
      AddArrayElement(this, result, k, mappedValue);
      k++;
caitpotter88's avatar
caitpotter88 committed
255 256
    }
  } else {
257 258
    var len = $toLength(items.length);
    result = %IsConstructor(this) ? new this(len) : new GlobalArray(len);
caitpotter88's avatar
caitpotter88 committed
259 260 261

    for (k = 0; k < len; ++k) {
      nextValue = items[k];
262 263 264 265 266
      if (mapping) {
        mappedValue = %_CallFunction(receiver, nextValue, k, mapfn);
      } else {
        mappedValue = nextValue;
      }
267
      AddArrayElement(this, result, k, mappedValue);
caitpotter88's avatar
caitpotter88 committed
268 269 270 271 272 273 274
    }

    result.length = k;
    return result;
  }
}

275 276 277 278 279
// 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)
280
  var array = %IsConstructor(constructor) ? new constructor(length) : [];
281
  for (var i = 0; i < length; i++) {
282
    AddArrayElement(constructor, array, i, %_Arguments(i));
283 284 285 286 287
  }
  array.length = length;
  return array;
}

288 289
// -------------------------------------------------------------------

290
%FunctionSetLength(ArrayCopyWithin, 2);
291
%FunctionSetLength(ArrayFrom, 1);
292 293 294
%FunctionSetLength(ArrayFill, 1);
%FunctionSetLength(ArrayFind, 1);
%FunctionSetLength(ArrayFindIndex, 1);
295

296
// Set up non-enumerable functions on the Array object.
297
utils.InstallFunctions(GlobalArray, DONT_ENUM, [
298 299 300
  "from", ArrayFrom,
  "of", ArrayOf
]);
301

302
// Set up the non-enumerable functions on the Array prototype object.
303
utils.InstallFunctions(GlobalArray.prototype, DONT_ENUM, [
304
  "copyWithin", ArrayCopyWithin,
305 306 307 308
  "find", ArrayFind,
  "findIndex", ArrayFindIndex,
  "fill", ArrayFill
]);
309

310 311 312 313 314 315 316 317 318 319 320
// -------------------------------------------------------------------
// Exports

utils.Export(function(to) {
  to.ArrayFrom = ArrayFrom;
  to.InnerArrayCopyWithin = InnerArrayCopyWithin;
  to.InnerArrayFill = InnerArrayFill;
  to.InnerArrayFind = InnerArrayFind;
  to.InnerArrayFindIndex = InnerArrayFindIndex;
});

321
})