• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2013 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5(function(global, utils) {
6
7"use strict";
8
9%CheckIsBootstrapping();
10
11// -------------------------------------------------------------------
12// Imports
13
14var GlobalArrayBuffer = global.ArrayBuffer;
15var MaxSimple;
16var MinSimple;
17var SpeciesConstructor;
18var speciesSymbol = utils.ImportNow("species_symbol");
19
20utils.Import(function(from) {
21  MaxSimple = from.MaxSimple;
22  MinSimple = from.MinSimple;
23  SpeciesConstructor = from.SpeciesConstructor;
24});
25
26// -------------------------------------------------------------------
27
28// ES6 Draft 15.13.5.5.3
29function ArrayBufferSlice(start, end) {
30  if (!IS_ARRAYBUFFER(this)) {
31    throw %make_type_error(kIncompatibleMethodReceiver,
32                        'ArrayBuffer.prototype.slice', this);
33  }
34
35  var relativeStart = TO_INTEGER(start);
36  if (!IS_UNDEFINED(end)) {
37    end = TO_INTEGER(end);
38  }
39  var first;
40  var byte_length = %_ArrayBufferGetByteLength(this);
41  if (relativeStart < 0) {
42    first = MaxSimple(byte_length + relativeStart, 0);
43  } else {
44    first = MinSimple(relativeStart, byte_length);
45  }
46  var relativeEnd = IS_UNDEFINED(end) ? byte_length : end;
47  var fin;
48  if (relativeEnd < 0) {
49    fin = MaxSimple(byte_length + relativeEnd, 0);
50  } else {
51    fin = MinSimple(relativeEnd, byte_length);
52  }
53
54  if (fin < first) {
55    fin = first;
56  }
57  var newLen = fin - first;
58  var constructor = SpeciesConstructor(this, GlobalArrayBuffer, true);
59  var result = new constructor(newLen);
60  if (!IS_ARRAYBUFFER(result)) {
61    throw %make_type_error(kIncompatibleMethodReceiver,
62                        'ArrayBuffer.prototype.slice', result);
63  }
64  // Checks for detached source/target ArrayBuffers are done inside of
65  // %ArrayBufferSliceImpl; the reordering of checks does not violate
66  // the spec because all exceptions thrown are TypeErrors.
67  if (result === this) {
68    throw %make_type_error(kArrayBufferSpeciesThis);
69  }
70  if (%_ArrayBufferGetByteLength(result) < newLen) {
71    throw %make_type_error(kArrayBufferTooShort);
72  }
73
74  %ArrayBufferSliceImpl(this, result, first, newLen);
75  return result;
76}
77
78
79function ArrayBufferSpecies() {
80  return this;
81}
82
83utils.InstallGetter(GlobalArrayBuffer, speciesSymbol, ArrayBufferSpecies);
84
85utils.InstallFunctions(GlobalArrayBuffer.prototype, DONT_ENUM, [
86  "slice", ArrayBufferSlice
87]);
88
89})
90