ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 1 | // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | "use strict"; |
| 29 | |
| 30 | var $ArrayBuffer = global.ArrayBuffer; |
| 31 | |
| 32 | // ------------------------------------------------------------------- |
| 33 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 34 | function ArrayBufferConstructor(length) { // length = 1 |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 35 | if (%_IsConstructCall()) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 36 | var byteLength = ToPositiveInteger(length, 'invalid_array_buffer_length'); |
| 37 | %ArrayBufferInitialize(this, byteLength); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 38 | } else { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 39 | throw MakeTypeError('constructor_not_function', ["ArrayBuffer"]); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
| 43 | function ArrayBufferGetByteLength() { |
| 44 | if (!IS_ARRAYBUFFER(this)) { |
| 45 | throw MakeTypeError('incompatible_method_receiver', |
| 46 | ['ArrayBuffer.prototype.byteLength', this]); |
| 47 | } |
| 48 | return %ArrayBufferGetByteLength(this); |
| 49 | } |
| 50 | |
| 51 | // ES6 Draft 15.13.5.5.3 |
| 52 | function ArrayBufferSlice(start, end) { |
| 53 | if (!IS_ARRAYBUFFER(this)) { |
| 54 | throw MakeTypeError('incompatible_method_receiver', |
| 55 | ['ArrayBuffer.prototype.slice', this]); |
| 56 | } |
| 57 | |
| 58 | var relativeStart = TO_INTEGER(start); |
| 59 | var first; |
| 60 | if (relativeStart < 0) { |
| 61 | first = MathMax(this.byteLength + relativeStart, 0); |
| 62 | } else { |
| 63 | first = MathMin(relativeStart, this.byteLength); |
| 64 | } |
| 65 | var relativeEnd = IS_UNDEFINED(end) ? this.byteLength : TO_INTEGER(end); |
| 66 | var fin; |
| 67 | if (relativeEnd < 0) { |
| 68 | fin = MathMax(this.byteLength + relativeEnd, 0); |
| 69 | } else { |
| 70 | fin = MathMin(relativeEnd, this.byteLength); |
| 71 | } |
| 72 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 73 | if (fin < first) { |
| 74 | fin = first; |
| 75 | } |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 76 | var newLen = fin - first; |
| 77 | // TODO(dslomov): implement inheritance |
| 78 | var result = new $ArrayBuffer(newLen); |
| 79 | |
| 80 | %ArrayBufferSliceImpl(this, result, first); |
| 81 | return result; |
| 82 | } |
| 83 | |
mvstanton@chromium.org | 63ea3d2 | 2013-10-10 09:24:12 +0000 | [diff] [blame] | 84 | function ArrayBufferIsView(obj) { |
| 85 | return %ArrayBufferIsView(obj); |
| 86 | } |
| 87 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 88 | function SetUpArrayBuffer() { |
| 89 | %CheckIsBootstrapping(); |
| 90 | |
| 91 | // Set up the ArrayBuffer constructor function. |
| 92 | %SetCode($ArrayBuffer, ArrayBufferConstructor); |
| 93 | %FunctionSetPrototype($ArrayBuffer, new $Object()); |
| 94 | |
| 95 | // Set up the constructor property on the ArrayBuffer prototype object. |
| 96 | %SetProperty($ArrayBuffer.prototype, "constructor", $ArrayBuffer, DONT_ENUM); |
| 97 | |
| 98 | InstallGetter($ArrayBuffer.prototype, "byteLength", ArrayBufferGetByteLength); |
| 99 | |
mvstanton@chromium.org | 63ea3d2 | 2013-10-10 09:24:12 +0000 | [diff] [blame] | 100 | InstallFunctions($ArrayBuffer, DONT_ENUM, $Array( |
| 101 | "isView", ArrayBufferIsView |
| 102 | )); |
| 103 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 104 | InstallFunctions($ArrayBuffer.prototype, DONT_ENUM, $Array( |
| 105 | "slice", ArrayBufferSlice |
| 106 | )); |
| 107 | } |
| 108 | |
| 109 | SetUpArrayBuffer(); |
| 110 | |
| 111 | |