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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 function ConstructByLength(obj, length) { | 70 function ConstructByLength(obj, length) { |
71 var l = ToPositiveInteger(length, "invalid_typed_array_length"); | 71 var l = ToPositiveInteger(length, "invalid_typed_array_length"); |
72 var byteLength = l * elementSize; | 72 var byteLength = l * elementSize; |
73 var buffer = new global.ArrayBuffer(byteLength); | 73 var buffer = new global.ArrayBuffer(byteLength); |
74 %TypedArrayInitialize(obj, arrayId, buffer, 0, byteLength); | 74 %TypedArrayInitialize(obj, arrayId, buffer, 0, byteLength); |
75 } | 75 } |
76 | 76 |
77 function ConstructByArrayLike(obj, arrayLike) { | 77 function ConstructByArrayLike(obj, arrayLike) { |
78 var length = arrayLike.length; | 78 var length = arrayLike.length; |
79 var l = ToPositiveInteger(length, "invalid_typed_array_length"); | 79 var l = ToPositiveInteger(length, "invalid_typed_array_length"); |
80 var byteLength = l * elementSize; | 80 if(!%TypedArrayInitializeFromArrayLike(obj, arrayId, arrayLike, l)) { |
81 var buffer = new $ArrayBuffer(byteLength); | 81 for (var i = 0; i < l; i++) { |
82 %TypedArrayInitialize(obj, arrayId, buffer, 0, byteLength); | 82 obj[i] = arrayLike[i]; |
83 for (var i = 0; i < l; i++) { | 83 } |
84 obj[i] = arrayLike[i]; | |
85 } | 84 } |
86 } | 85 } |
87 | 86 |
88 return function (arg1, arg2, arg3) { | 87 return function (arg1, arg2, arg3) { |
89 if (%_IsConstructCall()) { | 88 if (%_IsConstructCall()) { |
90 if (IS_ARRAYBUFFER(arg1)) { | 89 if (IS_ARRAYBUFFER(arg1)) { |
91 ConstructByArrayBuffer(this, arg1, arg2, arg3); | 90 ConstructByArrayBuffer(this, arg1, arg2, arg3); |
92 } else if (IS_NUMBER(arg1) || IS_STRING(arg1) || | 91 } else if (IS_NUMBER(arg1) || IS_STRING(arg1) || |
93 IS_BOOLEAN(arg1) || IS_UNDEFINED(arg1)) { | 92 IS_BOOLEAN(arg1) || IS_UNDEFINED(arg1)) { |
94 ConstructByLength(this, arg1); | 93 ConstructByLength(this, arg1); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 endInt = beginInt; | 136 endInt = beginInt; |
138 } | 137 } |
139 var newLength = endInt - beginInt; | 138 var newLength = endInt - beginInt; |
140 var beginByteOffset = | 139 var beginByteOffset = |
141 %TypedArrayGetByteOffset(this) + beginInt * elementSize; | 140 %TypedArrayGetByteOffset(this) + beginInt * elementSize; |
142 return new constructor(%TypedArrayGetBuffer(this), | 141 return new constructor(%TypedArrayGetBuffer(this), |
143 beginByteOffset, newLength); | 142 beginByteOffset, newLength); |
144 } | 143 } |
145 } | 144 } |
146 | 145 |
| 146 function TypedArraySetFromArrayLike(target, source, sourceLength, offset) { |
| 147 if (offset > 0) { |
| 148 for (var i = 0; i < sourceLength; i++) { |
| 149 target[offset + i] = source[i]; |
| 150 } |
| 151 } |
| 152 else { |
| 153 for (var i = 0; i < sourceLength; i++) { |
| 154 target[i] = source[i]; |
| 155 } |
| 156 } |
| 157 } |
| 158 |
| 159 function TypedArraySetFromOverlappingTypedArray(target, source, offset) { |
| 160 var sourceElementSize = source.BYTES_PER_ELEMENT; |
| 161 var targetElementSize = target.BYTES_PER_ELEMENT; |
| 162 var sourceLength = source.length; |
| 163 |
| 164 // Copy left part. |
| 165 function CopyLeftPart() { |
| 166 // First un-mutated byte after the next write |
| 167 var targetPtr = target.byteOffset + (offset + 1) * targetElementSize; |
| 168 // Next read at sourcePtr. We do not care for memory changing before |
| 169 // sourcePtr - we have already copied it. |
| 170 var sourcePtr = source.byteOffset; |
| 171 for (var leftIndex = 0; |
| 172 leftIndex < sourceLength && targetPtr <= sourcePtr; |
| 173 leftIndex++) { |
| 174 target[offset + leftIndex] = source[leftIndex]; |
| 175 targetPtr += targetElementSize; |
| 176 sourcePtr += sourceElementSize; |
| 177 } |
| 178 return leftIndex; |
| 179 } |
| 180 var leftIndex = CopyLeftPart(); |
| 181 |
| 182 // Copy rigth part; |
| 183 function CopyRightPart() { |
| 184 // First unmutated byte before the next write |
| 185 var targetPtr = |
| 186 target.byteOffset + (offset + sourceLength - 1) * targetElementSize; |
| 187 // Next read before sourcePtr. We do not care for memory changing after |
| 188 // sourcePtr - we have already copied it. |
| 189 var sourcePtr = |
| 190 source.byteOffset + sourceLength * sourceElementSize; |
| 191 for(var rightIndex = sourceLength - 1; |
| 192 rightIndex >= leftIndex && targetPtr >= sourcePtr; |
| 193 rightIndex--) { |
| 194 target[offset + rightIndex] = source[rightIndex]; |
| 195 targetPtr -= targetElementSize; |
| 196 sourcePtr -= sourceElementSize; |
| 197 } |
| 198 return rightIndex; |
| 199 } |
| 200 var rightIndex = CopyRightPart(); |
| 201 |
| 202 var temp = new $Array(rightIndex + 1 - leftIndex); |
| 203 for (var i = leftIndex; i <= rightIndex; i++) { |
| 204 temp[i - leftIndex] = source[i]; |
| 205 } |
| 206 for (i = leftIndex; i <= rightIndex; i++) { |
| 207 target[offset + i] = temp[i - leftIndex]; |
| 208 } |
| 209 } |
| 210 |
147 function TypedArraySet(obj, offset) { | 211 function TypedArraySet(obj, offset) { |
148 var intOffset = IS_UNDEFINED(offset) ? 0 : TO_INTEGER(offset); | 212 var intOffset = IS_UNDEFINED(offset) ? 0 : TO_INTEGER(offset); |
149 if (intOffset < 0) { | 213 if (intOffset < 0) { |
150 throw MakeTypeError("typed_array_set_negative_offset"); | 214 throw MakeTypeError("typed_array_set_negative_offset"); |
151 } | 215 } |
152 if (%TypedArraySetFastCases(this, obj, intOffset)) | 216 switch (%TypedArraySetFastCases(this, obj, intOffset)) { |
153 return; | 217 // These numbers should be synchronized with runtime.cc. |
154 | 218 case 0: // TYPED_ARRAY_SET_TYPED_ARRAY_SAME_TYPE |
155 var l = obj.length; | 219 return; |
156 if (IS_UNDEFINED(l)) { | 220 case 1: // TYPED_ARRAY_SET_TYPED_ARRAY_OVERLAPPING |
157 if (IS_NUMBER(obj)) { | 221 TypedArraySetFromOverlappingTypedArray(this, obj, intOffset); |
158 // For number as a first argument, throw TypeError | 222 return; |
159 // instead of silently ignoring the call, so that | 223 case 2: // TYPED_ARRAY_SET_TYPED_ARRAY_NONOVERLAPPING |
160 // the user knows (s)he did something wrong. | 224 TypedArraySetFromArrayLike(this, obj, obj.length, intOffset); |
161 // (Consistent with Firefox and Blink/WebKit) | 225 return; |
162 throw MakeTypeError("invalid_argument"); | 226 case 3: // TYPED_ARRAY_SET_NON_TYPED_ARRAY |
163 } | 227 var l = obj.length; |
164 return; | 228 if (IS_UNDEFINED(l)) { |
165 } | 229 if (IS_NUMBER(obj)) { |
166 if (intOffset + l > this.length) { | 230 // For number as a first argument, throw TypeError |
167 throw MakeRangeError("typed_array_set_source_too_large"); | 231 // instead of silently ignoring the call, so that |
168 } | 232 // the user knows (s)he did something wrong. |
169 for (var i = 0; i < l; i++) { | 233 // (Consistent with Firefox and Blink/WebKit) |
170 this[intOffset + i] = obj[i]; | 234 throw MakeTypeError("invalid_argument"); |
| 235 } |
| 236 return; |
| 237 } |
| 238 if (intOffset + l > this.length) { |
| 239 throw MakeRangeError("typed_array_set_source_too_large"); |
| 240 } |
| 241 TypedArraySetFromArrayLike(this, obj, l, intOffset); |
| 242 return; |
171 } | 243 } |
172 } | 244 } |
173 | 245 |
174 // ------------------------------------------------------------------- | 246 // ------------------------------------------------------------------- |
175 | 247 |
176 function SetupTypedArray(arrayId, name, constructor, elementSize) { | 248 function SetupTypedArray(arrayId, name, constructor, elementSize) { |
177 %CheckIsBootstrapping(); | 249 %CheckIsBootstrapping(); |
178 var fun = CreateTypedArrayConstructor(name, elementSize, | 250 var fun = CreateTypedArrayConstructor(name, elementSize, |
179 arrayId, constructor); | 251 arrayId, constructor); |
180 %SetCode(constructor, fun); | 252 %SetCode(constructor, fun); |
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
514 | 586 |
515 "getFloat32", DataViewGetFloat32, | 587 "getFloat32", DataViewGetFloat32, |
516 "setFloat32", DataViewSetFloat32, | 588 "setFloat32", DataViewSetFloat32, |
517 | 589 |
518 "getFloat64", DataViewGetFloat64, | 590 "getFloat64", DataViewGetFloat64, |
519 "setFloat64", DataViewSetFloat64 | 591 "setFloat64", DataViewSetFloat64 |
520 )); | 592 )); |
521 } | 593 } |
522 | 594 |
523 SetupDataView(); | 595 SetupDataView(); |
OLD | NEW |