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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 FUNCTION(4, Int16Array, 2) | 42 FUNCTION(4, Int16Array, 2) |
43 FUNCTION(5, Uint32Array, 4) | 43 FUNCTION(5, Uint32Array, 4) |
44 FUNCTION(6, Int32Array, 4) | 44 FUNCTION(6, Int32Array, 4) |
45 FUNCTION(7, Float32Array, 4) | 45 FUNCTION(7, Float32Array, 4) |
46 FUNCTION(8, Float64Array, 8) | 46 FUNCTION(8, Float64Array, 8) |
47 FUNCTION(9, Uint8ClampedArray, 1) | 47 FUNCTION(9, Uint8ClampedArray, 1) |
48 endmacro | 48 endmacro |
49 | 49 |
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)) { | |
53 byteOffset = | |
54 ToPositiveInteger(byteOffset, "invalid_typed_array_length"); | |
55 } | |
56 if (!IS_UNDEFINED(length)) { | |
57 length = ToPositiveInteger(length, "invalid_typed_array_length"); | |
58 } | |
59 | |
60 var bufferByteLength = %ArrayBufferGetByteLength(buffer); | 52 var bufferByteLength = %ArrayBufferGetByteLength(buffer); |
61 var offset; | 53 var offset; |
62 if (IS_UNDEFINED(byteOffset)) { | 54 if (IS_UNDEFINED(byteOffset)) { |
63 offset = 0; | 55 offset = 0; |
64 } else { | 56 } else { |
65 offset = byteOffset; | 57 offset = ToPositiveInteger(byteOffset, "invalid_typed_array_length"); |
66 | 58 |
67 if (offset % ELEMENT_SIZE !== 0) { | 59 if (offset % ELEMENT_SIZE !== 0) { |
68 throw MakeRangeError("invalid_typed_array_alignment", | 60 throw MakeRangeError("invalid_typed_array_alignment", |
69 ["start offset", "NAME", ELEMENT_SIZE]); | 61 ["start offset", "NAME", ELEMENT_SIZE]); |
70 } | 62 } |
71 if (offset > bufferByteLength) { | 63 if (offset > bufferByteLength) { |
72 throw MakeRangeError("invalid_typed_array_offset"); | 64 throw MakeRangeError("invalid_typed_array_offset"); |
73 } | 65 } |
74 } | 66 } |
75 | 67 |
76 var newByteLength; | 68 var newByteLength; |
77 var newLength; | 69 var newLength; |
78 if (IS_UNDEFINED(length)) { | 70 if (IS_UNDEFINED(length)) { |
79 if (bufferByteLength % ELEMENT_SIZE !== 0) { | 71 if (bufferByteLength % ELEMENT_SIZE !== 0) { |
80 throw MakeRangeError("invalid_typed_array_alignment", | 72 throw MakeRangeError("invalid_typed_array_alignment", |
81 ["byte length", "NAME", ELEMENT_SIZE]); | 73 ["byte length", "NAME", ELEMENT_SIZE]); |
82 } | 74 } |
83 newByteLength = bufferByteLength - offset; | 75 newByteLength = bufferByteLength - offset; |
84 newLength = newByteLength / ELEMENT_SIZE; | 76 newLength = newByteLength / ELEMENT_SIZE; |
85 } else { | 77 } else { |
86 var newLength = length; | 78 var newLength = ToPositiveInteger(length, "invalid_typed_array_length"); |
87 newByteLength = newLength * ELEMENT_SIZE; | 79 newByteLength = newLength * ELEMENT_SIZE; |
88 } | 80 } |
89 if ((offset + newByteLength > bufferByteLength) | 81 if ((offset + newByteLength > bufferByteLength) |
90 || (newLength > %MaxSmi())) { | 82 || (newLength > %MaxSmi())) { |
91 throw MakeRangeError("invalid_typed_array_length"); | 83 throw MakeRangeError("invalid_typed_array_length"); |
92 } | 84 } |
93 %TypedArrayInitialize(obj, ARRAY_ID, buffer, offset, newByteLength); | 85 %TypedArrayInitialize(obj, ARRAY_ID, buffer, offset, newByteLength); |
94 } | 86 } |
95 | 87 |
96 function NAMEConstructByLength(obj, length) { | 88 function NAMEConstructByLength(obj, length) { |
97 var l = IS_UNDEFINED(length) ? | 89 var l = IS_UNDEFINED(length) ? |
98 0 : ToPositiveInteger(length, "invalid_typed_array_length"); | 90 0 : ToPositiveInteger(length, "invalid_typed_array_length"); |
99 if (l > %MaxSmi()) { | 91 if (l > %MaxSmi()) { |
100 throw MakeRangeError("invalid_typed_array_length"); | 92 throw MakeRangeError("invalid_typed_array_length"); |
101 } | 93 } |
102 var byteLength = l * ELEMENT_SIZE; | 94 var byteLength = l * ELEMENT_SIZE; |
103 var buffer = new $ArrayBuffer(byteLength); | 95 var buffer = new $ArrayBuffer(byteLength); |
104 %TypedArrayInitialize(obj, ARRAY_ID, buffer, 0, byteLength); | 96 %TypedArrayInitialize(obj, ARRAY_ID, buffer, 0, byteLength); |
105 } | 97 } |
106 | 98 |
107 function NAMEConstructByArrayLike(obj, arrayLike) { | 99 function NAMEConstructByArrayLike(obj, arrayLike) { |
108 var length = arrayLike.length; | 100 var length = arrayLike.length; |
109 var l = ToPositiveInteger(length, "invalid_typed_array_length"); | 101 var l = ToPositiveInteger(length, "invalid_typed_array_length"); |
110 | |
111 if (l > %MaxSmi()) { | 102 if (l > %MaxSmi()) { |
112 throw MakeRangeError("invalid_typed_array_length"); | 103 throw MakeRangeError("invalid_typed_array_length"); |
113 } | 104 } |
114 if(!%TypedArrayInitializeFromArrayLike(obj, ARRAY_ID, arrayLike, l)) { | 105 if(!%TypedArrayInitializeFromArrayLike(obj, ARRAY_ID, arrayLike, l)) { |
115 for (var i = 0; i < l; i++) { | 106 for (var i = 0; i < l; i++) { |
116 // It is crucial that we let any execptions from arrayLike[i] | 107 // It is crucial that we let any execptions from arrayLike[i] |
117 // propagate outside the function. | 108 // propagate outside the function. |
118 obj[i] = arrayLike[i]; | 109 obj[i] = arrayLike[i]; |
119 } | 110 } |
120 } | 111 } |
(...skipping 29 matching lines...) Expand all Loading... |
150 function TypedArrayGetByteOffset() { | 141 function TypedArrayGetByteOffset() { |
151 return %TypedArrayGetByteOffset(this); | 142 return %TypedArrayGetByteOffset(this); |
152 } | 143 } |
153 | 144 |
154 function TypedArrayGetLength() { | 145 function TypedArrayGetLength() { |
155 return %TypedArrayGetLength(this); | 146 return %TypedArrayGetLength(this); |
156 } | 147 } |
157 | 148 |
158 function CreateSubArray(elementSize, constructor) { | 149 function CreateSubArray(elementSize, constructor) { |
159 return function(begin, end) { | 150 return function(begin, end) { |
| 151 var srcLength = %TypedArrayGetLength(this); |
160 var beginInt = TO_INTEGER(begin); | 152 var beginInt = TO_INTEGER(begin); |
161 if (!IS_UNDEFINED(end)) { | |
162 end = TO_INTEGER(end); | |
163 } | |
164 | |
165 var srcLength = %TypedArrayGetLength(this); | |
166 if (beginInt < 0) { | 153 if (beginInt < 0) { |
167 beginInt = MathMax(0, srcLength + beginInt); | 154 beginInt = MathMax(0, srcLength + beginInt); |
168 } else { | 155 } else { |
169 beginInt = MathMin(srcLength, beginInt); | 156 beginInt = MathMin(srcLength, beginInt); |
170 } | 157 } |
171 | 158 |
172 var endInt = IS_UNDEFINED(end) ? srcLength : end; | 159 var endInt = IS_UNDEFINED(end) ? srcLength : TO_INTEGER(end); |
173 if (endInt < 0) { | 160 if (endInt < 0) { |
174 endInt = MathMax(0, srcLength + endInt); | 161 endInt = MathMax(0, srcLength + endInt); |
175 } else { | 162 } else { |
176 endInt = MathMin(endInt, srcLength); | 163 endInt = MathMin(endInt, srcLength); |
177 } | 164 } |
178 if (endInt < beginInt) { | 165 if (endInt < beginInt) { |
179 endInt = beginInt; | 166 endInt = beginInt; |
180 } | 167 } |
181 var newLength = endInt - beginInt; | 168 var newLength = endInt - beginInt; |
182 var beginByteOffset = | 169 var beginByteOffset = |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 | 310 |
324 // --------------------------- DataView ----------------------------- | 311 // --------------------------- DataView ----------------------------- |
325 | 312 |
326 var $DataView = global.DataView; | 313 var $DataView = global.DataView; |
327 | 314 |
328 function DataViewConstructor(buffer, byteOffset, byteLength) { // length = 3 | 315 function DataViewConstructor(buffer, byteOffset, byteLength) { // length = 3 |
329 if (%_IsConstructCall()) { | 316 if (%_IsConstructCall()) { |
330 if (!IS_ARRAYBUFFER(buffer)) { | 317 if (!IS_ARRAYBUFFER(buffer)) { |
331 throw MakeTypeError('data_view_not_array_buffer', []); | 318 throw MakeTypeError('data_view_not_array_buffer', []); |
332 } | 319 } |
333 if (!IS_UNDEFINED(byteOffset)) { | |
334 byteOffset = ToPositiveInteger(byteOffset, 'invalid_data_view_offset'); | |
335 } | |
336 if (!IS_UNDEFINED(byteLength)) { | |
337 byteLength = TO_INTEGER(byteLength); | |
338 } | |
339 | |
340 var bufferByteLength = %ArrayBufferGetByteLength(buffer); | 320 var bufferByteLength = %ArrayBufferGetByteLength(buffer); |
341 | 321 var offset = IS_UNDEFINED(byteOffset) ? |
342 var offset = IS_UNDEFINED(byteOffset) ? 0 : byteOffset; | 322 0 : ToPositiveInteger(byteOffset, 'invalid_data_view_offset'); |
343 if (offset > bufferByteLength) { | 323 if (offset > bufferByteLength) { |
344 throw MakeRangeError('invalid_data_view_offset'); | 324 throw MakeRangeError('invalid_data_view_offset'); |
345 } | 325 } |
346 | 326 var length = IS_UNDEFINED(byteLength) ? |
347 var length = IS_UNDEFINED(byteLength) | 327 bufferByteLength - offset : TO_INTEGER(byteLength); |
348 ? bufferByteLength - offset | |
349 : byteLength; | |
350 if (length < 0 || offset + length > bufferByteLength) { | 328 if (length < 0 || offset + length > bufferByteLength) { |
351 throw new MakeRangeError('invalid_data_view_length'); | 329 throw new MakeRangeError('invalid_data_view_length'); |
352 } | 330 } |
353 %DataViewInitialize(this, buffer, offset, length); | 331 %DataViewInitialize(this, buffer, offset, length); |
354 } else { | 332 } else { |
355 throw MakeTypeError('constructor_not_function', ["DataView"]); | 333 throw MakeTypeError('constructor_not_function', ["DataView"]); |
356 } | 334 } |
357 } | 335 } |
358 | 336 |
359 function DataViewGetBuffer() { | 337 function DataViewGetBuffer() { |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 | 440 |
463 "getFloat32", DataViewGetFloat32, | 441 "getFloat32", DataViewGetFloat32, |
464 "setFloat32", DataViewSetFloat32, | 442 "setFloat32", DataViewSetFloat32, |
465 | 443 |
466 "getFloat64", DataViewGetFloat64, | 444 "getFloat64", DataViewGetFloat64, |
467 "setFloat64", DataViewSetFloat64 | 445 "setFloat64", DataViewSetFloat64 |
468 )); | 446 )); |
469 } | 447 } |
470 | 448 |
471 SetupDataView(); | 449 SetupDataView(); |
OLD | NEW |