arraybuffer.js 1.98 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
var MakeTypeError;
16 17
var MaxSimple;
var MinSimple;
18

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

25 26
// -------------------------------------------------------------------

27
function ArrayBufferGetByteLen() {
28
  if (!IS_ARRAYBUFFER(this)) {
29 30
    throw MakeTypeError(kIncompatibleMethodReceiver,
                        'ArrayBuffer.prototype.byteLength', this);
31
  }
32
  return %_ArrayBufferGetByteLength(this);
33 34 35 36 37
}

// ES6 Draft 15.13.5.5.3
function ArrayBufferSlice(start, end) {
  if (!IS_ARRAYBUFFER(this)) {
38 39
    throw MakeTypeError(kIncompatibleMethodReceiver,
                        'ArrayBuffer.prototype.slice', this);
40 41 42
  }

  var relativeStart = TO_INTEGER(start);
43 44 45
  if (!IS_UNDEFINED(end)) {
    end = TO_INTEGER(end);
  }
46
  var first;
47
  var byte_length = %_ArrayBufferGetByteLength(this);
48
  if (relativeStart < 0) {
49
    first = MaxSimple(byte_length + relativeStart, 0);
50
  } else {
51
    first = MinSimple(relativeStart, byte_length);
52
  }
53
  var relativeEnd = IS_UNDEFINED(end) ? byte_length : end;
54 55
  var fin;
  if (relativeEnd < 0) {
56
    fin = MaxSimple(byte_length + relativeEnd, 0);
57
  } else {
58
    fin = MinSimple(relativeEnd, byte_length);
59 60
  }

61 62 63
  if (fin < first) {
    fin = first;
  }
64 65
  var newLen = fin - first;
  // TODO(dslomov): implement inheritance
66
  var result = new GlobalArrayBuffer(newLen);
67 68 69 70 71

  %ArrayBufferSliceImpl(this, result, first);
  return result;
}

72 73
utils.InstallGetter(GlobalArrayBuffer.prototype, "byteLength",
                    ArrayBufferGetByteLen);
74

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

79
})