Home | History | Annotate | Download | only in js
      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 
     14 var GlobalArrayBuffer = global.ArrayBuffer;
     15 var MaxSimple;
     16 var MinSimple;
     17 var SpeciesConstructor;
     18 
     19 utils.Import(function(from) {
     20   MaxSimple = from.MaxSimple;
     21   MinSimple = from.MinSimple;
     22   SpeciesConstructor = from.SpeciesConstructor;
     23 });
     24 
     25 // -------------------------------------------------------------------
     26 
     27 // ES6 Draft 15.13.5.5.3
     28 function ArrayBufferSlice(start, end) {
     29   if (!IS_ARRAYBUFFER(this)) {
     30     throw %make_type_error(kIncompatibleMethodReceiver,
     31                         'ArrayBuffer.prototype.slice', this);
     32   }
     33 
     34   var relativeStart = TO_INTEGER(start);
     35   if (!IS_UNDEFINED(end)) {
     36     end = TO_INTEGER(end);
     37   }
     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   var fin;
     47   if (relativeEnd < 0) {
     48     fin = MaxSimple(byte_length + relativeEnd, 0);
     49   } else {
     50     fin = MinSimple(relativeEnd, byte_length);
     51   }
     52 
     53   if (fin < first) {
     54     fin = first;
     55   }
     56   var newLen = fin - first;
     57   var constructor = SpeciesConstructor(this, GlobalArrayBuffer, true);
     58   var result = new constructor(newLen);
     59   if (!IS_ARRAYBUFFER(result)) {
     60     throw %make_type_error(kIncompatibleMethodReceiver,
     61                         'ArrayBuffer.prototype.slice', result);
     62   }
     63   // Checks for detached source/target ArrayBuffers are done inside of
     64   // %ArrayBufferSliceImpl; the reordering of checks does not violate
     65   // 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   return result;
     75 }
     76 
     77 utils.InstallFunctions(GlobalArrayBuffer.prototype, DONT_ENUM, [
     78   "slice", ArrayBufferSlice
     79 ]);
     80 
     81 })
     82