arraybuffer.js 2.23 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
var GlobalArrayBuffer = global.ArrayBuffer;
15 16
var MaxSimple;
var MinSimple;
17
var SpeciesConstructor;
18

19
utils.Import(function(from) {
20 21
  MaxSimple = from.MaxSimple;
  MinSimple = from.MinSimple;
22
  SpeciesConstructor = from.SpeciesConstructor;
23 24
});

25 26 27 28 29
// -------------------------------------------------------------------

// ES6 Draft 15.13.5.5.3
function ArrayBufferSlice(start, end) {
  if (!IS_ARRAYBUFFER(this)) {
30
    throw %make_type_error(kIncompatibleMethodReceiver,
31
                        'ArrayBuffer.prototype.slice', this);
32 33 34
  }

  var relativeStart = TO_INTEGER(start);
35 36 37
  if (!IS_UNDEFINED(end)) {
    end = TO_INTEGER(end);
  }
38
  var first;
39
  var byte_length = %_ArrayBufferGetByteLength(this);
40
  if (relativeStart < 0) {
41
    first = MaxSimple(byte_length + relativeStart, 0);
42
  } else {
43
    first = MinSimple(relativeStart, byte_length);
44
  }
45
  var relativeEnd = IS_UNDEFINED(end) ? byte_length : end;
46 47
  var fin;
  if (relativeEnd < 0) {
48
    fin = MaxSimple(byte_length + relativeEnd, 0);
49
  } else {
50
    fin = MinSimple(relativeEnd, byte_length);
51 52
  }

53 54 55
  if (fin < first) {
    fin = first;
  }
56
  var newLen = fin - first;
57 58 59
  var constructor = SpeciesConstructor(this, GlobalArrayBuffer, true);
  var result = new constructor(newLen);
  if (!IS_ARRAYBUFFER(result)) {
60
    throw %make_type_error(kIncompatibleMethodReceiver,
61 62
                        'ArrayBuffer.prototype.slice', result);
  }
63 64 65
  // Checks for detached source/target ArrayBuffers are done inside of
  // %ArrayBufferSliceImpl; the reordering of checks does not violate
  // the spec because all exceptions thrown are TypeErrors.
66
  if (result === this) {
67
    throw %make_type_error(kArrayBufferSpeciesThis);
68 69
  }
  if (%_ArrayBufferGetByteLength(result) < newLen) {
70
    throw %make_type_error(kArrayBufferTooShort);
71
  }
72

73
  %ArrayBufferSliceImpl(this, result, first, newLen);
74 75 76
  return result;
}

77
utils.InstallFunctions(GlobalArrayBuffer.prototype, DONT_ENUM, [
78 79
  "slice", ArrayBufferSlice
]);
80

81
})