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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 } | 75 } |
76 | 76 |
77 var newLen = fin - first; | 77 var newLen = fin - first; |
78 // TODO(dslomov): implement inheritance | 78 // TODO(dslomov): implement inheritance |
79 var result = new $ArrayBuffer(newLen); | 79 var result = new $ArrayBuffer(newLen); |
80 | 80 |
81 %ArrayBufferSliceImpl(this, result, first); | 81 %ArrayBufferSliceImpl(this, result, first); |
82 return result; | 82 return result; |
83 } | 83 } |
84 | 84 |
| 85 // --------------- Typed Arrays --------------------- |
| 86 |
| 87 function CreateTypedArrayConstructor(name, elementSize, arrayId, constructor) { |
| 88 return function (buffer, byteOffset, length) { |
| 89 if (%_IsConstructCall()) { |
| 90 if (!IS_ARRAYBUFFER(buffer)) { |
| 91 throw MakeTypeError("Type error!"); |
| 92 } |
| 93 var offset = IS_UNDEFINED(byteOffset) |
| 94 ? 0 : offset = TO_POSITIVE_INTEGER(byteOffset); |
| 95 |
| 96 if (offset % elementSize !== 0) { |
| 97 throw MakeRangeError("invalid_typed_array_alignment", |
| 98 "start offset", name, elementSize); |
| 99 } |
| 100 var bufferByteLength = %ArrayBufferGetByteLength(buffer); |
| 101 if (offset >= bufferByteLength) { |
| 102 throw MakeRangeError("invalid_typed_array_offset"); |
| 103 } |
| 104 |
| 105 var newByteLength; |
| 106 var newLength; |
| 107 if (IS_UNDEFINED(length)) { |
| 108 if (bufferByteLength % elementSize !== 0) { |
| 109 throw MakeRangeError("invalid_typed_array_alignment", |
| 110 "byte length", name, elementSize); |
| 111 } |
| 112 newByteLength = bufferByteLength - offset; |
| 113 newLength = newByteLength / elementSize; |
| 114 } else { |
| 115 var newLength = TO_POSITIVE_INTEGER(length); |
| 116 newByteLength = newLength * elementSize; |
| 117 } |
| 118 if (newByteLength > bufferByteLength) { |
| 119 throw MakeRangeError("invalid_typed_array_length"); |
| 120 } |
| 121 %TypedArrayInitialize(this, arrayId, buffer, offset, newByteLength); |
| 122 } else { |
| 123 return new constructor(buffer, byteOffset, length); |
| 124 } |
| 125 } |
| 126 } |
| 127 |
| 128 function TypedArrayGetBuffer() { |
| 129 return %TypedArrayGetBuffer(this); |
| 130 } |
| 131 |
| 132 function TypedArrayGetByteLength() { |
| 133 return %TypedArrayGetByteLength(this); |
| 134 } |
| 135 |
| 136 function TypedArrayGetByteOffset() { |
| 137 return %TypedArrayGetByteOffset(this); |
| 138 } |
| 139 |
| 140 function TypedArrayGetLength() { |
| 141 return %TypedArrayGetLength(this); |
| 142 } |
| 143 |
85 | 144 |
86 // ------------------------------------------------------------------- | 145 // ------------------------------------------------------------------- |
87 | 146 |
88 function SetUpArrayBuffer() { | 147 function SetUpArrayBuffer() { |
89 %CheckIsBootstrapping(); | 148 %CheckIsBootstrapping(); |
90 | 149 |
91 // Set up the Uint16Array constructor function. | 150 // Set up the ArrayBuffer constructor function. |
92 %SetCode($ArrayBuffer, ArrayBufferConstructor); | 151 %SetCode($ArrayBuffer, ArrayBufferConstructor); |
| 152 %FunctionSetPrototype($ArrayBuffer, new $Object()); |
93 | 153 |
94 // Set up the constructor property on the ArrayBuffer prototype object. | 154 // Set up the constructor property on the ArrayBuffer prototype object. |
95 %SetProperty($ArrayBuffer.prototype, "constructor", $ArrayBuffer, DONT_ENUM); | 155 %SetProperty($ArrayBuffer.prototype, "constructor", $ArrayBuffer, DONT_ENUM); |
96 | 156 |
97 InstallGetter($ArrayBuffer.prototype, "byteLength", ArrayBufferGetByteLength); | 157 InstallGetter($ArrayBuffer.prototype, "byteLength", ArrayBufferGetByteLength); |
98 | 158 |
99 InstallFunctions($ArrayBuffer.prototype, DONT_ENUM, $Array( | 159 InstallFunctions($ArrayBuffer.prototype, DONT_ENUM, $Array( |
100 "slice", ArrayBufferSlice | 160 "slice", ArrayBufferSlice |
101 )); | 161 )); |
102 } | 162 } |
103 | 163 |
104 SetUpArrayBuffer(); | 164 SetUpArrayBuffer(); |
| 165 |
| 166 function SetupTypedArray(arrayId, name, constructor, elementSize) { |
| 167 var f = CreateTypedArrayConstructor(name, elementSize, |
| 168 arrayId, constructor); |
| 169 %SetCode(constructor, f); |
| 170 %FunctionSetPrototype(constructor, new $Object()); |
| 171 |
| 172 %SetProperty(constructor.prototype, |
| 173 "constructor", constructor, DONT_ENUM); |
| 174 %SetProperty(constructor.prototype, |
| 175 "BYTES_PER_ELEMENT", elementSize, |
| 176 READ_ONLY | DONT_ENUM | DONT_DELETE); |
| 177 InstallGetter(constructor.prototype, "buffer", TypedArrayGetBuffer); |
| 178 InstallGetter(constructor.prototype, "byteOffset", TypedArrayGetByteOffset); |
| 179 InstallGetter(constructor.prototype, "byteLength", TypedArrayGetByteLength); |
| 180 InstallGetter(constructor.prototype, "length", TypedArrayGetLength); |
| 181 } |
| 182 |
| 183 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. |
| 184 SetupTypedArray(1, "Uint8Array", global.__Uint8Array, 1); |
| 185 SetupTypedArray(2, "Int8Array", global.__Int8Array, 1); |
| 186 SetupTypedArray(3, "Uint16Array", global.__Uint16Array, 2); |
| 187 SetupTypedArray(4, "Int16Array", global.__Int16Array, 2); |
| 188 SetupTypedArray(5, "Uint32Array", global.__Uint32Array, 4); |
| 189 SetupTypedArray(6, "Int32Array", global.__Int32Array, 4); |
| 190 SetupTypedArray(7, "Float32Array", global.__Float32Array, 4); |
| 191 SetupTypedArray(8, "Float64Array", global.__Float64Array, 8); |
| 192 |
OLD | NEW |