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

Side by Side Diff: src/typedarray.js

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

Powered by Google App Engine
This is Rietveld 408576698