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 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 } | 160 } |
161 | 161 |
162 function TypedArrayGetByteOffset() { | 162 function TypedArrayGetByteOffset() { |
163 return %TypedArrayGetByteOffset(this); | 163 return %TypedArrayGetByteOffset(this); |
164 } | 164 } |
165 | 165 |
166 function TypedArrayGetLength() { | 166 function TypedArrayGetLength() { |
167 return %TypedArrayGetLength(this); | 167 return %TypedArrayGetLength(this); |
168 } | 168 } |
169 | 169 |
| 170 function CreateSubArray(elementSize, constructor) { |
| 171 return function(begin, end) { |
| 172 var srcLength = %TypedArrayGetLength(this); |
| 173 var beginInt = TO_INTEGER(begin); |
| 174 if (beginInt < 0) { |
| 175 beginInt = MathMax(0, srcLength + beginInt); |
| 176 } else { |
| 177 beginInt = MathMin(srcLength, beginInt); |
| 178 } |
| 179 |
| 180 var endInt = IS_UNDEFINED(end) ? srcLength : TO_INTEGER(end); |
| 181 if (endInt < 0) { |
| 182 endInt = MathMax(0, srcLength + endInt); |
| 183 } else { |
| 184 endInt = MathMin(endInt, srcLength); |
| 185 } |
| 186 if (endInt < beginInt) { |
| 187 endInt = beginInt; |
| 188 } |
| 189 var newLength = endInt - beginInt; |
| 190 var beginByteOffset = |
| 191 %TypedArrayGetByteOffset(this) + beginInt * elementSize; |
| 192 return new constructor(%TypedArrayGetBuffer(this), |
| 193 beginByteOffset, newLength); |
| 194 } |
| 195 } |
| 196 |
170 | 197 |
171 // ------------------------------------------------------------------- | 198 // ------------------------------------------------------------------- |
172 | 199 |
173 function SetUpArrayBuffer() { | 200 function SetUpArrayBuffer() { |
174 %CheckIsBootstrapping(); | 201 %CheckIsBootstrapping(); |
175 | 202 |
176 // Set up the ArrayBuffer constructor function. | 203 // Set up the ArrayBuffer constructor function. |
177 %SetCode($ArrayBuffer, ArrayBufferConstructor); | 204 %SetCode($ArrayBuffer, ArrayBufferConstructor); |
178 %FunctionSetPrototype($ArrayBuffer, new $Object()); | 205 %FunctionSetPrototype($ArrayBuffer, new $Object()); |
179 | 206 |
(...skipping 18 matching lines...) Expand all Loading... |
198 | 225 |
199 %SetProperty(constructor.prototype, | 226 %SetProperty(constructor.prototype, |
200 "constructor", constructor, DONT_ENUM); | 227 "constructor", constructor, DONT_ENUM); |
201 %SetProperty(constructor.prototype, | 228 %SetProperty(constructor.prototype, |
202 "BYTES_PER_ELEMENT", elementSize, | 229 "BYTES_PER_ELEMENT", elementSize, |
203 READ_ONLY | DONT_ENUM | DONT_DELETE); | 230 READ_ONLY | DONT_ENUM | DONT_DELETE); |
204 InstallGetter(constructor.prototype, "buffer", TypedArrayGetBuffer); | 231 InstallGetter(constructor.prototype, "buffer", TypedArrayGetBuffer); |
205 InstallGetter(constructor.prototype, "byteOffset", TypedArrayGetByteOffset); | 232 InstallGetter(constructor.prototype, "byteOffset", TypedArrayGetByteOffset); |
206 InstallGetter(constructor.prototype, "byteLength", TypedArrayGetByteLength); | 233 InstallGetter(constructor.prototype, "byteLength", TypedArrayGetByteLength); |
207 InstallGetter(constructor.prototype, "length", TypedArrayGetLength); | 234 InstallGetter(constructor.prototype, "length", TypedArrayGetLength); |
| 235 |
| 236 InstallFunctions(constructor.prototype, DONT_ENUM, $Array( |
| 237 "subarray", CreateSubArray(elementSize, constructor) |
| 238 )); |
208 } | 239 } |
209 | 240 |
210 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. | 241 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. |
211 SetupTypedArray(1, "Uint8Array", global.Uint8Array, 1); | 242 SetupTypedArray(1, "Uint8Array", global.Uint8Array, 1); |
212 SetupTypedArray(2, "Int8Array", global.Int8Array, 1); | 243 SetupTypedArray(2, "Int8Array", global.Int8Array, 1); |
213 SetupTypedArray(3, "Uint16Array", global.Uint16Array, 2); | 244 SetupTypedArray(3, "Uint16Array", global.Uint16Array, 2); |
214 SetupTypedArray(4, "Int16Array", global.Int16Array, 2); | 245 SetupTypedArray(4, "Int16Array", global.Int16Array, 2); |
215 SetupTypedArray(5, "Uint32Array", global.Uint32Array, 4); | 246 SetupTypedArray(5, "Uint32Array", global.Uint32Array, 4); |
216 SetupTypedArray(6, "Int32Array", global.Int32Array, 4); | 247 SetupTypedArray(6, "Int32Array", global.Int32Array, 4); |
217 SetupTypedArray(7, "Float32Array", global.Float32Array, 4); | 248 SetupTypedArray(7, "Float32Array", global.Float32Array, 4); |
218 SetupTypedArray(8, "Float64Array", global.Float64Array, 8); | 249 SetupTypedArray(8, "Float64Array", global.Float64Array, 8); |
219 SetupTypedArray(9, "Uint8ClampedArray", global.Uint8ClampedArray, 1); | 250 SetupTypedArray(9, "Uint8ClampedArray", global.Uint8ClampedArray, 1); |
OLD | NEW |