Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: src/typedarray.js

Issue 21353002: Reimplement TypedArray.set in Javascript. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/runtime.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 endInt = beginInt; 137 endInt = beginInt;
138 } 138 }
139 var newLength = endInt - beginInt; 139 var newLength = endInt - beginInt;
140 var beginByteOffset = 140 var beginByteOffset =
141 %TypedArrayGetByteOffset(this) + beginInt * elementSize; 141 %TypedArrayGetByteOffset(this) + beginInt * elementSize;
142 return new constructor(%TypedArrayGetBuffer(this), 142 return new constructor(%TypedArrayGetBuffer(this),
143 beginByteOffset, newLength); 143 beginByteOffset, newLength);
144 } 144 }
145 } 145 }
146 146
147 function TypedArraySetFromArrayLike(target, source, sourceLength, offset) {
148 if (offset > 0) {
149 for (var i = 0; i < sourceLength; i++) {
150 target[offset + i] = source[i];
151 }
152 }
153 else {
154 for (var i = 0; i < sourceLength; i++) {
155 target[i] = source[i];
156 }
157 }
158 }
159
160 function TypedArraySetFromOverlappingTypedArray(target, source, offset) {
161 var sourceElementSize = source.BYTES_PER_ELEMENT;
162 var targetElementSize = target.BYTES_PER_ELEMENT;
163 var sourceLength = source.length;
164
165 // Copy left part.
166 function CopyLeftPart() {
167 // First un-mutated byte after the next write
168 var targetPtr = target.byteOffset + (offset + 1) * targetElementSize;
169 // Next read at sourcePtr. We do not care for memory changing before
170 // sourcePtr - we have already copied it.
171 var sourcePtr = source.byteOffset;
172 for (var leftIndex = 0;
173 leftIndex < sourceLength && targetPtr <= sourcePtr;
174 leftIndex++) {
175 target[offset + leftIndex] = source[leftIndex];
176 targetPtr += targetElementSize;
177 sourcePtr += sourceElementSize;
178 }
179 return leftIndex;
180 }
181 var leftIndex = CopyLeftPart();
182
183 // Copy rigth part;
184 function CopyRightPart() {
185 // First unmutated byte before the next write
186 var targetPtr =
187 target.byteOffset + (offset + sourceLength - 1) * targetElementSize;
188 // Next read before sourcePtr. We do not care for memory changing after
189 // sourcePtr - we have already copied it.
190 var sourcePtr =
191 source.byteOffset + sourceLength * sourceElementSize;
192 for(var rightIndex = sourceLength - 1;
193 rightIndex >= leftIndex && targetPtr >= sourcePtr;
194 rightIndex--) {
195 target[offset + rightIndex] = source[rightIndex];
196 targetPtr -= targetElementSize;
197 sourcePtr -= sourceElementSize;
198 }
199 return rightIndex;
200 }
201 var rightIndex = CopyRightPart();
202
203 var temp = new $Array(rightIndex + 1 - leftIndex);
204 for (var i = leftIndex; i <= rightIndex; i++) {
205 temp[i - leftIndex] = source[i];
206 }
207 for (i = leftIndex; i <= rightIndex; i++) {
208 target[offset + i] = temp[i - leftIndex];
209 }
210 }
211
147 function TypedArraySet(obj, offset) { 212 function TypedArraySet(obj, offset) {
148 var intOffset = IS_UNDEFINED(offset) ? 0 : TO_INTEGER(offset); 213 var intOffset = IS_UNDEFINED(offset) ? 0 : TO_INTEGER(offset);
149 if (intOffset < 0) { 214 if (intOffset < 0) {
150 throw MakeTypeError("typed_array_set_negative_offset"); 215 throw MakeTypeError("typed_array_set_negative_offset");
151 } 216 }
152 if (%TypedArraySetFastCases(this, obj, intOffset)) 217 switch (%TypedArraySetFastCases(this, obj, intOffset)) {
153 return; 218 // These numbers should be synchronized with runtime.cc.
154 219 case 0: // TYPED_ARRAY_SET_TYPED_ARRAY_SAME_TYPE
155 var l = obj.length; 220 return;
156 if (IS_UNDEFINED(l)) { 221 case 1: // TYPED_ARRAY_SET_TYPED_ARRAY_OVERLAPPING
157 if (IS_NUMBER(obj)) { 222 TypedArraySetFromOverlappingTypedArray(this, obj, intOffset);
158 // For number as a first argument, throw TypeError 223 return;
159 // instead of silently ignoring the call, so that 224 case 2: // TYPED_ARRAY_SET_TYPED_ARRAY_NONOVERLAPPING
160 // the user knows (s)he did something wrong. 225 TypedArraySetFromArrayLike(this, obj, obj.length, intOffset);
161 // (Consistent with Firefox and Blink/WebKit) 226 return;
162 throw MakeTypeError("invalid_argument"); 227 case 3: // TYPED_ARRAY_SET_NON_TYPED_ARRAY
163 } 228 var l = obj.length;
164 return; 229 if (IS_UNDEFINED(l)) {
165 } 230 if (IS_NUMBER(obj)) {
166 if (intOffset + l > this.length) { 231 // For number as a first argument, throw TypeError
167 throw MakeRangeError("typed_array_set_source_too_large"); 232 // instead of silently ignoring the call, so that
168 } 233 // the user knows (s)he did something wrong.
169 for (var i = 0; i < l; i++) { 234 // (Consistent with Firefox and Blink/WebKit)
170 this[intOffset + i] = obj[i]; 235 throw MakeTypeError("invalid_argument");
236 }
237 return;
238 }
239 if (intOffset + l > this.length) {
240 throw MakeRangeError("typed_array_set_source_too_large");
241 }
242 TypedArraySetFromArrayLike(this, obj, l, intOffset);
243 return;
171 } 244 }
172 } 245 }
173 246
174 // ------------------------------------------------------------------- 247 // -------------------------------------------------------------------
175 248
176 function SetupTypedArray(arrayId, name, constructor, elementSize) { 249 function SetupTypedArray(arrayId, name, constructor, elementSize) {
177 %CheckIsBootstrapping(); 250 %CheckIsBootstrapping();
178 var fun = CreateTypedArrayConstructor(name, elementSize, 251 var fun = CreateTypedArrayConstructor(name, elementSize,
179 arrayId, constructor); 252 arrayId, constructor);
180 %SetCode(constructor, fun); 253 %SetCode(constructor, fun);
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after
514 587
515 "getFloat32", DataViewGetFloat32, 588 "getFloat32", DataViewGetFloat32,
516 "setFloat32", DataViewSetFloat32, 589 "setFloat32", DataViewSetFloat32,
517 590
518 "getFloat64", DataViewGetFloat64, 591 "getFloat64", DataViewGetFloat64,
519 "setFloat64", DataViewSetFloat64 592 "setFloat64", DataViewSetFloat64
520 )); 593 ));
521 } 594 }
522 595
523 SetupDataView(); 596 SetupDataView();
OLDNEW
« no previous file with comments | « src/runtime.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698