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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 macro TYPED_ARRAY_CONSTRUCTOR(ARRAY_ID, NAME, ELEMENT_SIZE) | 50 macro TYPED_ARRAY_CONSTRUCTOR(ARRAY_ID, NAME, ELEMENT_SIZE) |
51 function NAMEConstructByArrayBuffer(obj, buffer, byteOffset, length) { | 51 function NAMEConstructByArrayBuffer(obj, buffer, byteOffset, length) { |
52 if (!IS_UNDEFINED(byteOffset)) { | 52 if (!IS_UNDEFINED(byteOffset)) { |
53 byteOffset = | 53 byteOffset = |
54 ToPositiveInteger(byteOffset, "invalid_typed_array_length"); | 54 ToPositiveInteger(byteOffset, "invalid_typed_array_length"); |
55 } | 55 } |
56 if (!IS_UNDEFINED(length)) { | 56 if (!IS_UNDEFINED(length)) { |
57 length = ToPositiveInteger(length, "invalid_typed_array_length"); | 57 length = ToPositiveInteger(length, "invalid_typed_array_length"); |
58 } | 58 } |
59 | 59 |
60 var bufferByteLength = %_ArrayBufferGetByteLength(buffer); | 60 var bufferByteLength = %ArrayBufferGetByteLength(buffer); |
61 var offset; | 61 var offset; |
62 if (IS_UNDEFINED(byteOffset)) { | 62 if (IS_UNDEFINED(byteOffset)) { |
63 offset = 0; | 63 offset = 0; |
64 } else { | 64 } else { |
65 offset = byteOffset; | 65 offset = byteOffset; |
66 | 66 |
67 if (offset % ELEMENT_SIZE !== 0) { | 67 if (offset % ELEMENT_SIZE !== 0) { |
68 throw MakeRangeError("invalid_typed_array_alignment", | 68 throw MakeRangeError("invalid_typed_array_alignment", |
69 ["start offset", "NAME", ELEMENT_SIZE]); | 69 ["start offset", "NAME", ELEMENT_SIZE]); |
70 } | 70 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 if(!%TypedArrayInitializeFromArrayLike(obj, ARRAY_ID, arrayLike, l)) { | 118 if(!%TypedArrayInitializeFromArrayLike(obj, ARRAY_ID, arrayLike, l)) { |
119 for (var i = 0; i < l; i++) { | 119 for (var i = 0; i < l; i++) { |
120 // It is crucial that we let any execptions from arrayLike[i] | 120 // It is crucial that we let any execptions from arrayLike[i] |
121 // propagate outside the function. | 121 // propagate outside the function. |
122 obj[i] = arrayLike[i]; | 122 obj[i] = arrayLike[i]; |
123 } | 123 } |
124 } | 124 } |
125 } | 125 } |
126 | 126 |
127 function NAMEConstructor(arg1, arg2, arg3) { | 127 function NAMEConstructor(arg1, arg2, arg3) { |
| 128 |
128 if (%_IsConstructCall()) { | 129 if (%_IsConstructCall()) { |
129 if (IS_ARRAYBUFFER(arg1)) { | 130 if (IS_ARRAYBUFFER(arg1)) { |
130 NAMEConstructByArrayBuffer(this, arg1, arg2, arg3); | 131 NAMEConstructByArrayBuffer(this, arg1, arg2, arg3); |
131 } else if (IS_NUMBER(arg1) || IS_STRING(arg1) || | 132 } else if (IS_NUMBER(arg1) || IS_STRING(arg1) || |
132 IS_BOOLEAN(arg1) || IS_UNDEFINED(arg1)) { | 133 IS_BOOLEAN(arg1) || IS_UNDEFINED(arg1)) { |
133 NAMEConstructByLength(this, arg1); | 134 NAMEConstructByLength(this, arg1); |
134 } else { | 135 } else { |
135 NAMEConstructByArrayLike(this, arg1); | 136 NAMEConstructByArrayLike(this, arg1); |
136 } | 137 } |
137 } else { | 138 } else { |
138 throw MakeTypeError("constructor_not_function", ["NAME"]) | 139 throw MakeTypeError("constructor_not_function", ["NAME"]) |
139 } | 140 } |
140 } | 141 } |
| 142 endmacro |
141 | 143 |
142 function NAME_GetBuffer() { | 144 TYPED_ARRAYS(TYPED_ARRAY_CONSTRUCTOR) |
143 if (!(%_ClassOf(this) === 'NAME')) { | |
144 throw MakeTypeError('incompatible_method_receiver', | |
145 ["NAME.buffer", this]); | |
146 } | |
147 return %TypedArrayGetBuffer(this); | |
148 } | |
149 | 145 |
150 function NAME_GetByteLength() { | 146 function TypedArrayGetBuffer() { |
151 if (!(%_ClassOf(this) === 'NAME')) { | 147 return %TypedArrayGetBuffer(this); |
152 throw MakeTypeError('incompatible_method_receiver', | 148 } |
153 ["NAME.byteLength", this]); | |
154 } | |
155 return %_ArrayBufferViewGetByteLength(this); | |
156 } | |
157 | 149 |
158 function NAME_GetByteOffset() { | 150 function TypedArrayGetByteLength() { |
159 if (!(%_ClassOf(this) === 'NAME')) { | 151 return %TypedArrayGetByteLength(this); |
160 throw MakeTypeError('incompatible_method_receiver', | 152 } |
161 ["NAME.byteOffset", this]); | |
162 } | |
163 return %_ArrayBufferViewGetByteOffset(this); | |
164 } | |
165 | 153 |
166 function NAME_GetLength() { | 154 function TypedArrayGetByteOffset() { |
167 if (!(%_ClassOf(this) === 'NAME')) { | 155 return %TypedArrayGetByteOffset(this); |
168 throw MakeTypeError('incompatible_method_receiver', | 156 } |
169 ["NAME.length", this]); | |
170 } | |
171 return %_TypedArrayGetLength(this); | |
172 } | |
173 | 157 |
174 var $NAME = global.NAME; | 158 function TypedArrayGetLength() { |
| 159 return %TypedArrayGetLength(this); |
| 160 } |
175 | 161 |
176 function NAMESubArray(begin, end) { | 162 function CreateSubArray(elementSize, constructor) { |
177 if (!(%_ClassOf(this) === 'NAME')) { | 163 return function(begin, end) { |
178 throw MakeTypeError('incompatible_method_receiver', | |
179 ["NAME.subarray", this]); | |
180 } | |
181 var beginInt = TO_INTEGER(begin); | 164 var beginInt = TO_INTEGER(begin); |
182 if (!IS_UNDEFINED(end)) { | 165 if (!IS_UNDEFINED(end)) { |
183 end = TO_INTEGER(end); | 166 end = TO_INTEGER(end); |
184 } | 167 } |
185 | 168 |
186 var srcLength = %_TypedArrayGetLength(this); | 169 var srcLength = %TypedArrayGetLength(this); |
187 if (beginInt < 0) { | 170 if (beginInt < 0) { |
188 beginInt = MathMax(0, srcLength + beginInt); | 171 beginInt = MathMax(0, srcLength + beginInt); |
189 } else { | 172 } else { |
190 beginInt = MathMin(srcLength, beginInt); | 173 beginInt = MathMin(srcLength, beginInt); |
191 } | 174 } |
192 | 175 |
193 var endInt = IS_UNDEFINED(end) ? srcLength : end; | 176 var endInt = IS_UNDEFINED(end) ? srcLength : end; |
194 if (endInt < 0) { | 177 if (endInt < 0) { |
195 endInt = MathMax(0, srcLength + endInt); | 178 endInt = MathMax(0, srcLength + endInt); |
196 } else { | 179 } else { |
197 endInt = MathMin(endInt, srcLength); | 180 endInt = MathMin(endInt, srcLength); |
198 } | 181 } |
199 if (endInt < beginInt) { | 182 if (endInt < beginInt) { |
200 endInt = beginInt; | 183 endInt = beginInt; |
201 } | 184 } |
202 var newLength = endInt - beginInt; | 185 var newLength = endInt - beginInt; |
203 var beginByteOffset = | 186 var beginByteOffset = |
204 %_ArrayBufferViewGetByteOffset(this) + beginInt * ELEMENT_SIZE; | 187 %TypedArrayGetByteOffset(this) + beginInt * elementSize; |
205 return new $NAME(%TypedArrayGetBuffer(this), | 188 return new constructor(%TypedArrayGetBuffer(this), |
206 beginByteOffset, newLength); | 189 beginByteOffset, newLength); |
207 } | 190 } |
208 endmacro | 191 } |
209 | |
210 TYPED_ARRAYS(TYPED_ARRAY_CONSTRUCTOR) | |
211 | |
212 | 192 |
213 function TypedArraySetFromArrayLike(target, source, sourceLength, offset) { | 193 function TypedArraySetFromArrayLike(target, source, sourceLength, offset) { |
214 if (offset > 0) { | 194 if (offset > 0) { |
215 for (var i = 0; i < sourceLength; i++) { | 195 for (var i = 0; i < sourceLength; i++) { |
216 target[offset + i] = source[i]; | 196 target[offset + i] = source[i]; |
217 } | 197 } |
218 } | 198 } |
219 else { | 199 else { |
220 for (var i = 0; i < sourceLength; i++) { | 200 for (var i = 0; i < sourceLength; i++) { |
221 target[i] = source[i]; | 201 target[i] = source[i]; |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 if (intOffset + l > this.length) { | 289 if (intOffset + l > this.length) { |
310 throw MakeRangeError("typed_array_set_source_too_large"); | 290 throw MakeRangeError("typed_array_set_source_too_large"); |
311 } | 291 } |
312 TypedArraySetFromArrayLike(this, obj, l, intOffset); | 292 TypedArraySetFromArrayLike(this, obj, l, intOffset); |
313 return; | 293 return; |
314 } | 294 } |
315 } | 295 } |
316 | 296 |
317 // ------------------------------------------------------------------- | 297 // ------------------------------------------------------------------- |
318 | 298 |
319 function SetupTypedArrays() { | 299 function SetupTypedArray(constructor, fun, elementSize) { |
320 macro SETUP_TYPED_ARRAY(ARRAY_ID, NAME, ELEMENT_SIZE) | |
321 %CheckIsBootstrapping(); | 300 %CheckIsBootstrapping(); |
322 %SetCode(global.NAME, NAMEConstructor); | 301 %SetCode(constructor, fun); |
323 %FunctionSetPrototype(global.NAME, new $Object()); | 302 %FunctionSetPrototype(constructor, new $Object()); |
324 | 303 |
325 %SetProperty(global.NAME, "BYTES_PER_ELEMENT", ELEMENT_SIZE, | 304 %SetProperty(constructor, "BYTES_PER_ELEMENT", elementSize, |
326 READ_ONLY | DONT_ENUM | DONT_DELETE); | 305 READ_ONLY | DONT_ENUM | DONT_DELETE); |
327 %SetProperty(global.NAME.prototype, | 306 %SetProperty(constructor.prototype, |
328 "constructor", global.NAME, DONT_ENUM); | 307 "constructor", constructor, DONT_ENUM); |
329 %SetProperty(global.NAME.prototype, | 308 %SetProperty(constructor.prototype, |
330 "BYTES_PER_ELEMENT", ELEMENT_SIZE, | 309 "BYTES_PER_ELEMENT", elementSize, |
331 READ_ONLY | DONT_ENUM | DONT_DELETE); | 310 READ_ONLY | DONT_ENUM | DONT_DELETE); |
332 InstallGetter(global.NAME.prototype, "buffer", NAME_GetBuffer); | 311 InstallGetter(constructor.prototype, "buffer", TypedArrayGetBuffer); |
333 InstallGetter(global.NAME.prototype, "byteOffset", NAME_GetByteOffset); | 312 InstallGetter(constructor.prototype, "byteOffset", TypedArrayGetByteOffset); |
334 InstallGetter(global.NAME.prototype, "byteLength", NAME_GetByteLength); | 313 InstallGetter(constructor.prototype, "byteLength", TypedArrayGetByteLength); |
335 InstallGetter(global.NAME.prototype, "length", NAME_GetLength); | 314 InstallGetter(constructor.prototype, "length", TypedArrayGetLength); |
336 | 315 |
337 InstallFunctions(global.NAME.prototype, DONT_ENUM, $Array( | 316 InstallFunctions(constructor.prototype, DONT_ENUM, $Array( |
338 "subarray", NAMESubArray, | 317 "subarray", CreateSubArray(elementSize, constructor), |
339 "set", TypedArraySet | 318 "set", TypedArraySet |
340 )); | 319 )); |
| 320 } |
| 321 |
| 322 macro SETUP_TYPED_ARRAY(ARRAY_ID, NAME, ELEMENT_SIZE) |
| 323 SetupTypedArray (global.NAME, NAMEConstructor, ELEMENT_SIZE); |
341 endmacro | 324 endmacro |
342 | 325 |
343 TYPED_ARRAYS(SETUP_TYPED_ARRAY) | 326 TYPED_ARRAYS(SETUP_TYPED_ARRAY) |
344 } | |
345 | |
346 SetupTypedArrays(); | |
347 | 327 |
348 // --------------------------- DataView ----------------------------- | 328 // --------------------------- DataView ----------------------------- |
349 | 329 |
350 var $DataView = global.DataView; | 330 var $DataView = global.DataView; |
351 | 331 |
352 function DataViewConstructor(buffer, byteOffset, byteLength) { // length = 3 | 332 function DataViewConstructor(buffer, byteOffset, byteLength) { // length = 3 |
353 if (%_IsConstructCall()) { | 333 if (%_IsConstructCall()) { |
354 if (!IS_ARRAYBUFFER(buffer)) { | 334 if (!IS_ARRAYBUFFER(buffer)) { |
355 throw MakeTypeError('data_view_not_array_buffer', []); | 335 throw MakeTypeError('data_view_not_array_buffer', []); |
356 } | 336 } |
357 if (!IS_UNDEFINED(byteOffset)) { | 337 if (!IS_UNDEFINED(byteOffset)) { |
358 byteOffset = ToPositiveInteger(byteOffset, 'invalid_data_view_offset'); | 338 byteOffset = ToPositiveInteger(byteOffset, 'invalid_data_view_offset'); |
359 } | 339 } |
360 if (!IS_UNDEFINED(byteLength)) { | 340 if (!IS_UNDEFINED(byteLength)) { |
361 byteLength = TO_INTEGER(byteLength); | 341 byteLength = TO_INTEGER(byteLength); |
362 } | 342 } |
363 | 343 |
364 var bufferByteLength = %_ArrayBufferGetByteLength(buffer); | 344 var bufferByteLength = %ArrayBufferGetByteLength(buffer); |
365 | 345 |
366 var offset = IS_UNDEFINED(byteOffset) ? 0 : byteOffset; | 346 var offset = IS_UNDEFINED(byteOffset) ? 0 : byteOffset; |
367 if (offset > bufferByteLength) { | 347 if (offset > bufferByteLength) { |
368 throw MakeRangeError('invalid_data_view_offset'); | 348 throw MakeRangeError('invalid_data_view_offset'); |
369 } | 349 } |
370 | 350 |
371 var length = IS_UNDEFINED(byteLength) | 351 var length = IS_UNDEFINED(byteLength) |
372 ? bufferByteLength - offset | 352 ? bufferByteLength - offset |
373 : byteLength; | 353 : byteLength; |
374 if (length < 0 || offset + length > bufferByteLength) { | 354 if (length < 0 || offset + length > bufferByteLength) { |
(...skipping 11 matching lines...) Expand all Loading... |
386 ['DataView.buffer', this]); | 366 ['DataView.buffer', this]); |
387 } | 367 } |
388 return %DataViewGetBuffer(this); | 368 return %DataViewGetBuffer(this); |
389 } | 369 } |
390 | 370 |
391 function DataViewGetByteOffset() { | 371 function DataViewGetByteOffset() { |
392 if (!IS_DATAVIEW(this)) { | 372 if (!IS_DATAVIEW(this)) { |
393 throw MakeTypeError('incompatible_method_receiver', | 373 throw MakeTypeError('incompatible_method_receiver', |
394 ['DataView.byteOffset', this]); | 374 ['DataView.byteOffset', this]); |
395 } | 375 } |
396 return %_ArrayBufferViewGetByteOffset(this); | 376 return %DataViewGetByteOffset(this); |
397 } | 377 } |
398 | 378 |
399 function DataViewGetByteLength() { | 379 function DataViewGetByteLength() { |
400 if (!IS_DATAVIEW(this)) { | 380 if (!IS_DATAVIEW(this)) { |
401 throw MakeTypeError('incompatible_method_receiver', | 381 throw MakeTypeError('incompatible_method_receiver', |
402 ['DataView.byteLength', this]); | 382 ['DataView.byteLength', this]); |
403 } | 383 } |
404 return %_ArrayBufferViewGetByteLength(this); | 384 return %DataViewGetByteLength(this); |
405 } | 385 } |
406 | 386 |
407 macro DATA_VIEW_TYPES(FUNCTION) | 387 macro DATA_VIEW_TYPES(FUNCTION) |
408 FUNCTION(Int8) | 388 FUNCTION(Int8) |
409 FUNCTION(Uint8) | 389 FUNCTION(Uint8) |
410 FUNCTION(Int16) | 390 FUNCTION(Int16) |
411 FUNCTION(Uint16) | 391 FUNCTION(Uint16) |
412 FUNCTION(Int32) | 392 FUNCTION(Int32) |
413 FUNCTION(Uint32) | 393 FUNCTION(Uint32) |
414 FUNCTION(Float32) | 394 FUNCTION(Float32) |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 | 466 |
487 "getFloat32", DataViewGetFloat32, | 467 "getFloat32", DataViewGetFloat32, |
488 "setFloat32", DataViewSetFloat32, | 468 "setFloat32", DataViewSetFloat32, |
489 | 469 |
490 "getFloat64", DataViewGetFloat64, | 470 "getFloat64", DataViewGetFloat64, |
491 "setFloat64", DataViewSetFloat64 | 471 "setFloat64", DataViewSetFloat64 |
492 )); | 472 )); |
493 } | 473 } |
494 | 474 |
495 SetupDataView(); | 475 SetupDataView(); |
OLD | NEW |