| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 "use strict"; | 28 "use strict"; |
| 29 | 29 |
| 30 // This file relies on the fact that the following declaration has been made | 30 // This file relies on the fact that the following declaration has been made |
| 31 // in runtime.js: | 31 // in runtime.js: |
| 32 // var $Array = global.Array; | 32 // var $Array = global.Array; |
| 33 | 33 |
| 34 var $ArrayBuffer = global.ArrayBuffer; | |
| 35 | 34 |
| 36 // ------------------------------------------------------------------- | |
| 37 | |
| 38 function ArrayBufferConstructor(byteLength) { // length = 1 | |
| 39 if (%_IsConstructCall()) { | |
| 40 var l = TO_POSITIVE_INTEGER(byteLength); | |
| 41 %ArrayBufferInitialize(this, l); | |
| 42 } else { | |
| 43 return new $ArrayBuffer(byteLength); | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 function ArrayBufferGetByteLength() { | |
| 48 if (!IS_ARRAYBUFFER(this)) { | |
| 49 throw MakeTypeError('incompatible_method_receiver', | |
| 50 ['ArrayBuffer.prototype.byteLength', this]); | |
| 51 } | |
| 52 return %ArrayBufferGetByteLength(this); | |
| 53 } | |
| 54 | |
| 55 // ES6 Draft 15.13.5.5.3 | |
| 56 function ArrayBufferSlice(start, end) { | |
| 57 if (!IS_ARRAYBUFFER(this)) { | |
| 58 throw MakeTypeError('incompatible_method_receiver', | |
| 59 ['ArrayBuffer.prototype.slice', this]); | |
| 60 } | |
| 61 | |
| 62 var relativeStart = TO_INTEGER(start); | |
| 63 var first; | |
| 64 if (relativeStart < 0) { | |
| 65 first = MathMax(this.byteLength + relativeStart, 0); | |
| 66 } else { | |
| 67 first = MathMin(relativeStart, this.byteLength); | |
| 68 } | |
| 69 var relativeEnd = IS_UNDEFINED(end) ? this.byteLength : TO_INTEGER(end); | |
| 70 var fin; | |
| 71 if (relativeEnd < 0) { | |
| 72 fin = MathMax(this.byteLength + relativeEnd, 0); | |
| 73 } else { | |
| 74 fin = MathMin(relativeEnd, this.byteLength); | |
| 75 } | |
| 76 | |
| 77 var newLen = fin - first; | |
| 78 // TODO(dslomov): implement inheritance | |
| 79 var result = new $ArrayBuffer(newLen); | |
| 80 | |
| 81 %ArrayBufferSliceImpl(this, result, first); | |
| 82 return result; | |
| 83 } | |
| 84 | 35 |
| 85 // --------------- Typed Arrays --------------------- | 36 // --------------- Typed Arrays --------------------- |
| 86 | 37 |
| 87 function CreateTypedArrayConstructor(name, elementSize, arrayId, constructor) { | 38 function CreateTypedArrayConstructor(name, elementSize, arrayId, constructor) { |
| 88 function ConstructByArrayBuffer(obj, buffer, byteOffset, length) { | 39 function ConstructByArrayBuffer(obj, buffer, byteOffset, length) { |
| 89 var offset = IS_UNDEFINED(byteOffset) ? 0 : TO_POSITIVE_INTEGER(byteOffset); | 40 var offset = IS_UNDEFINED(byteOffset) ? 0 : TO_POSITIVE_INTEGER(byteOffset); |
| 90 | 41 |
| 91 if (offset % elementSize !== 0) { | 42 if (offset % elementSize !== 0) { |
| 92 throw MakeRangeError("invalid_typed_array_alignment", | 43 throw MakeRangeError("invalid_typed_array_alignment", |
| 93 "start offset", name, elementSize); | 44 "start offset", name, elementSize); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 112 } | 63 } |
| 113 if (offset + newByteLength > bufferByteLength) { | 64 if (offset + newByteLength > bufferByteLength) { |
| 114 throw MakeRangeError("invalid_typed_array_length"); | 65 throw MakeRangeError("invalid_typed_array_length"); |
| 115 } | 66 } |
| 116 %TypedArrayInitialize(obj, arrayId, buffer, offset, newByteLength); | 67 %TypedArrayInitialize(obj, arrayId, buffer, offset, newByteLength); |
| 117 } | 68 } |
| 118 | 69 |
| 119 function ConstructByLength(obj, length) { | 70 function ConstructByLength(obj, length) { |
| 120 var l = IS_UNDEFINED(length) ? 0 : TO_POSITIVE_INTEGER(length); | 71 var l = IS_UNDEFINED(length) ? 0 : TO_POSITIVE_INTEGER(length); |
| 121 var byteLength = l * elementSize; | 72 var byteLength = l * elementSize; |
| 122 var buffer = new $ArrayBuffer(byteLength); | 73 var buffer = new global.ArrayBuffer(byteLength); |
| 123 %TypedArrayInitialize(obj, arrayId, buffer, 0, byteLength); | 74 %TypedArrayInitialize(obj, arrayId, buffer, 0, byteLength); |
| 124 } | 75 } |
| 125 | 76 |
| 126 function ConstructByArrayLike(obj, arrayLike) { | 77 function ConstructByArrayLike(obj, arrayLike) { |
| 127 var length = arrayLike.length; | 78 var length = arrayLike.length; |
| 128 var l = IS_UNDEFINED(length) ? 0 : TO_POSITIVE_INTEGER(length); | 79 var l = IS_UNDEFINED(length) ? 0 : TO_POSITIVE_INTEGER(length); |
| 129 var byteLength = l * elementSize; | 80 var byteLength = l * elementSize; |
| 130 var buffer = new $ArrayBuffer(byteLength); | 81 var buffer = new $ArrayBuffer(byteLength); |
| 131 %TypedArrayInitialize(obj, arrayId, buffer, 0, byteLength); | 82 %TypedArrayInitialize(obj, arrayId, buffer, 0, byteLength); |
| 132 for (var i = 0; i < l; i++) { | 83 for (var i = 0; i < l; i++) { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 var beginByteOffset = | 141 var beginByteOffset = |
| 191 %TypedArrayGetByteOffset(this) + beginInt * elementSize; | 142 %TypedArrayGetByteOffset(this) + beginInt * elementSize; |
| 192 return new constructor(%TypedArrayGetBuffer(this), | 143 return new constructor(%TypedArrayGetBuffer(this), |
| 193 beginByteOffset, newLength); | 144 beginByteOffset, newLength); |
| 194 } | 145 } |
| 195 } | 146 } |
| 196 | 147 |
| 197 | 148 |
| 198 // ------------------------------------------------------------------- | 149 // ------------------------------------------------------------------- |
| 199 | 150 |
| 200 function SetUpArrayBuffer() { | |
| 201 %CheckIsBootstrapping(); | |
| 202 | |
| 203 // Set up the ArrayBuffer constructor function. | |
| 204 %SetCode($ArrayBuffer, ArrayBufferConstructor); | |
| 205 %FunctionSetPrototype($ArrayBuffer, new $Object()); | |
| 206 | |
| 207 // Set up the constructor property on the ArrayBuffer prototype object. | |
| 208 %SetProperty($ArrayBuffer.prototype, "constructor", $ArrayBuffer, DONT_ENUM); | |
| 209 | |
| 210 InstallGetter($ArrayBuffer.prototype, "byteLength", ArrayBufferGetByteLength); | |
| 211 | |
| 212 InstallFunctions($ArrayBuffer.prototype, DONT_ENUM, $Array( | |
| 213 "slice", ArrayBufferSlice | |
| 214 )); | |
| 215 } | |
| 216 | |
| 217 SetUpArrayBuffer(); | |
| 218 | |
| 219 function SetupTypedArray(arrayId, name, constructor, elementSize) { | 151 function SetupTypedArray(arrayId, name, constructor, elementSize) { |
| 220 %CheckIsBootstrapping(); | 152 %CheckIsBootstrapping(); |
| 221 var fun = CreateTypedArrayConstructor(name, elementSize, | 153 var fun = CreateTypedArrayConstructor(name, elementSize, |
| 222 arrayId, constructor); | 154 arrayId, constructor); |
| 223 %SetCode(constructor, fun); | 155 %SetCode(constructor, fun); |
| 224 %FunctionSetPrototype(constructor, new $Object()); | 156 %FunctionSetPrototype(constructor, new $Object()); |
| 225 | 157 |
| 226 %SetProperty(constructor.prototype, | 158 %SetProperty(constructor.prototype, |
| 227 "constructor", constructor, DONT_ENUM); | 159 "constructor", constructor, DONT_ENUM); |
| 228 %SetProperty(constructor.prototype, | 160 %SetProperty(constructor.prototype, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 241 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. | 173 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. |
| 242 SetupTypedArray(1, "Uint8Array", global.Uint8Array, 1); | 174 SetupTypedArray(1, "Uint8Array", global.Uint8Array, 1); |
| 243 SetupTypedArray(2, "Int8Array", global.Int8Array, 1); | 175 SetupTypedArray(2, "Int8Array", global.Int8Array, 1); |
| 244 SetupTypedArray(3, "Uint16Array", global.Uint16Array, 2); | 176 SetupTypedArray(3, "Uint16Array", global.Uint16Array, 2); |
| 245 SetupTypedArray(4, "Int16Array", global.Int16Array, 2); | 177 SetupTypedArray(4, "Int16Array", global.Int16Array, 2); |
| 246 SetupTypedArray(5, "Uint32Array", global.Uint32Array, 4); | 178 SetupTypedArray(5, "Uint32Array", global.Uint32Array, 4); |
| 247 SetupTypedArray(6, "Int32Array", global.Int32Array, 4); | 179 SetupTypedArray(6, "Int32Array", global.Int32Array, 4); |
| 248 SetupTypedArray(7, "Float32Array", global.Float32Array, 4); | 180 SetupTypedArray(7, "Float32Array", global.Float32Array, 4); |
| 249 SetupTypedArray(8, "Float64Array", global.Float64Array, 8); | 181 SetupTypedArray(8, "Float64Array", global.Float64Array, 8); |
| 250 SetupTypedArray(9, "Uint8ClampedArray", global.Uint8ClampedArray, 1); | 182 SetupTypedArray(9, "Uint8ClampedArray", global.Uint8ClampedArray, 1); |
| OLD | NEW |