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

Side by Side Diff: src/typedarray.js

Issue 286073004: Fix builtin/runtime name clashes generated by macros (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 7 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 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 "use strict"; 5 "use strict";
6 6
7 // This file relies on the fact that the following declaration has been made 7 // This file relies on the fact that the following declaration has been made
8 // in runtime.js: 8 // in runtime.js:
9 // var $Array = global.Array; 9 // var $Array = global.Array;
10 var $ArrayBuffer = global.ArrayBuffer; 10 var $ArrayBuffer = global.ArrayBuffer;
11 11
12 12
13 // --------------- Typed Arrays --------------------- 13 // --------------- Typed Arrays ---------------------
14 macro TYPED_ARRAYS(FUNCTION) 14 macro TYPED_ARRAYS(FUNCTION)
15 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. 15 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize.
16 FUNCTION(1, Uint8Array, 1) 16 FUNCTION(1, Uint8Array, 1)
17 FUNCTION(2, Int8Array, 1) 17 FUNCTION(2, Int8Array, 1)
18 FUNCTION(3, Uint16Array, 2) 18 FUNCTION(3, Uint16Array, 2)
19 FUNCTION(4, Int16Array, 2) 19 FUNCTION(4, Int16Array, 2)
20 FUNCTION(5, Uint32Array, 4) 20 FUNCTION(5, Uint32Array, 4)
21 FUNCTION(6, Int32Array, 4) 21 FUNCTION(6, Int32Array, 4)
22 FUNCTION(7, Float32Array, 4) 22 FUNCTION(7, Float32Array, 4)
23 FUNCTION(8, Float64Array, 8) 23 FUNCTION(8, Float64Array, 8)
24 FUNCTION(9, Uint8ClampedArray, 1) 24 FUNCTION(9, Uint8ClampedArray, 1)
25 endmacro 25 endmacro
26 26
27 macro TYPED_ARRAY_CONSTRUCTOR(ARRAY_ID, NAME, ELEMENT_SIZE) 27 macro TYPED_ARRAY_CONSTRUCTOR(ARRAY_ID, NAME, ELEMENT_SIZE)
28 function NAMEConstructByArrayBuffer(obj, buffer, byteOffset, length) { 28 function NAMEConstructByArrayBuffer(obj, buffer, byteOffset, length) {
29 if (!IS_UNDEFINED(byteOffset)) { 29 if (!IS_UNDEFINED(byteOffset)) {
30 byteOffset = 30 byteOffset =
31 ToPositiveInteger(byteOffset, "invalid_typed_array_length"); 31 ToPositiveInteger(byteOffset, "invalid_typed_array_length");
32 } 32 }
33 if (!IS_UNDEFINED(length)) { 33 if (!IS_UNDEFINED(length)) {
34 length = ToPositiveInteger(length, "invalid_typed_array_length"); 34 length = ToPositiveInteger(length, "invalid_typed_array_length");
35 }
36
37 var bufferByteLength = %_ArrayBufferGetByteLength(buffer);
38 var offset;
39 if (IS_UNDEFINED(byteOffset)) {
40 offset = 0;
41 } else {
42 offset = byteOffset;
43
44 if (offset % ELEMENT_SIZE !== 0) {
45 throw MakeRangeError("invalid_typed_array_alignment",
46 ["start offset", "NAME", ELEMENT_SIZE]);
47 }
48 if (offset > bufferByteLength) {
49 throw MakeRangeError("invalid_typed_array_offset");
50 }
51 }
52
53 var newByteLength;
54 var newLength;
55 if (IS_UNDEFINED(length)) {
56 if (bufferByteLength % ELEMENT_SIZE !== 0) {
57 throw MakeRangeError("invalid_typed_array_alignment",
58 ["byte length", "NAME", ELEMENT_SIZE]);
59 }
60 newByteLength = bufferByteLength - offset;
61 newLength = newByteLength / ELEMENT_SIZE;
62 } else {
63 var newLength = length;
64 newByteLength = newLength * ELEMENT_SIZE;
65 }
66 if ((offset + newByteLength > bufferByteLength)
67 || (newLength > %_MaxSmi())) {
68 throw MakeRangeError("invalid_typed_array_length");
69 }
70 %_TypedArrayInitialize(obj, ARRAY_ID, buffer, offset, newByteLength);
71 } 35 }
72 36
73 function NAMEConstructByLength(obj, length) { 37 var bufferByteLength = %_ArrayBufferGetByteLength(buffer);
74 var l = IS_UNDEFINED(length) ? 38 var offset;
75 0 : ToPositiveInteger(length, "invalid_typed_array_length"); 39 if (IS_UNDEFINED(byteOffset)) {
76 if (l > %_MaxSmi()) { 40 offset = 0;
77 throw MakeRangeError("invalid_typed_array_length"); 41 } else {
42 offset = byteOffset;
43
44 if (offset % ELEMENT_SIZE !== 0) {
45 throw MakeRangeError("invalid_typed_array_alignment",
46 ["start offset", "NAME", ELEMENT_SIZE]);
78 } 47 }
79 var byteLength = l * ELEMENT_SIZE; 48 if (offset > bufferByteLength) {
80 if (byteLength > %_TypedArrayMaxSizeInHeap()) { 49 throw MakeRangeError("invalid_typed_array_offset");
81 var buffer = new $ArrayBuffer(byteLength);
82 %_TypedArrayInitialize(obj, ARRAY_ID, buffer, 0, byteLength);
83 } else {
84 %_TypedArrayInitialize(obj, ARRAY_ID, null, 0, byteLength);
85 } 50 }
86 } 51 }
87 52
88 function NAMEConstructByArrayLike(obj, arrayLike) { 53 var newByteLength;
89 var length = arrayLike.length; 54 var newLength;
90 var l = ToPositiveInteger(length, "invalid_typed_array_length"); 55 if (IS_UNDEFINED(length)) {
56 if (bufferByteLength % ELEMENT_SIZE !== 0) {
57 throw MakeRangeError("invalid_typed_array_alignment",
58 ["byte length", "NAME", ELEMENT_SIZE]);
59 }
60 newByteLength = bufferByteLength - offset;
61 newLength = newByteLength / ELEMENT_SIZE;
62 } else {
63 var newLength = length;
64 newByteLength = newLength * ELEMENT_SIZE;
65 }
66 if ((offset + newByteLength > bufferByteLength)
67 || (newLength > %_MaxSmi())) {
68 throw MakeRangeError("invalid_typed_array_length");
69 }
70 %_TypedArrayInitialize(obj, ARRAY_ID, buffer, offset, newByteLength);
71 }
91 72
92 if (l > %_MaxSmi()) { 73 function NAMEConstructByLength(obj, length) {
93 throw MakeRangeError("invalid_typed_array_length"); 74 var l = IS_UNDEFINED(length) ?
94 } 75 0 : ToPositiveInteger(length, "invalid_typed_array_length");
95 if(!%TypedArrayInitializeFromArrayLike(obj, ARRAY_ID, arrayLike, l)) { 76 if (l > %_MaxSmi()) {
96 for (var i = 0; i < l; i++) { 77 throw MakeRangeError("invalid_typed_array_length");
97 // It is crucial that we let any execptions from arrayLike[i] 78 }
98 // propagate outside the function. 79 var byteLength = l * ELEMENT_SIZE;
99 obj[i] = arrayLike[i]; 80 if (byteLength > %_TypedArrayMaxSizeInHeap()) {
100 } 81 var buffer = new $ArrayBuffer(byteLength);
82 %_TypedArrayInitialize(obj, ARRAY_ID, buffer, 0, byteLength);
83 } else {
84 %_TypedArrayInitialize(obj, ARRAY_ID, null, 0, byteLength);
85 }
86 }
87
88 function NAMEConstructByArrayLike(obj, arrayLike) {
89 var length = arrayLike.length;
90 var l = ToPositiveInteger(length, "invalid_typed_array_length");
91
92 if (l > %_MaxSmi()) {
93 throw MakeRangeError("invalid_typed_array_length");
94 }
95 if(!%TypedArrayInitializeFromArrayLike(obj, ARRAY_ID, arrayLike, l)) {
96 for (var i = 0; i < l; i++) {
97 // It is crucial that we let any execptions from arrayLike[i]
98 // propagate outside the function.
99 obj[i] = arrayLike[i];
101 } 100 }
102 } 101 }
102 }
103 103
104 function NAMEConstructor(arg1, arg2, arg3) { 104 function NAMEConstructor(arg1, arg2, arg3) {
105 if (%_IsConstructCall()) { 105 if (%_IsConstructCall()) {
106 if (IS_ARRAYBUFFER(arg1)) { 106 if (IS_ARRAYBUFFER(arg1)) {
107 NAMEConstructByArrayBuffer(this, arg1, arg2, arg3); 107 NAMEConstructByArrayBuffer(this, arg1, arg2, arg3);
108 } else if (IS_NUMBER(arg1) || IS_STRING(arg1) || 108 } else if (IS_NUMBER(arg1) || IS_STRING(arg1) ||
109 IS_BOOLEAN(arg1) || IS_UNDEFINED(arg1)) { 109 IS_BOOLEAN(arg1) || IS_UNDEFINED(arg1)) {
110 NAMEConstructByLength(this, arg1); 110 NAMEConstructByLength(this, arg1);
111 } else {
112 NAMEConstructByArrayLike(this, arg1);
113 }
114 } else { 111 } else {
115 throw MakeTypeError("constructor_not_function", ["NAME"]) 112 NAMEConstructByArrayLike(this, arg1);
116 } 113 }
114 } else {
115 throw MakeTypeError("constructor_not_function", ["NAME"])
116 }
117 }
118
119 function NAME_GetBuffer() {
120 if (!(%_ClassOf(this) === 'NAME')) {
121 throw MakeTypeError('incompatible_method_receiver',
122 ["NAME.buffer", this]);
123 }
124 return %TypedArrayGetBuffer(this);
125 }
126
127 function NAME_GetByteLength() {
128 if (!(%_ClassOf(this) === 'NAME')) {
129 throw MakeTypeError('incompatible_method_receiver',
130 ["NAME.byteLength", this]);
131 }
132 return %_ArrayBufferViewGetByteLength(this);
133 }
134
135 function NAME_GetByteOffset() {
136 if (!(%_ClassOf(this) === 'NAME')) {
137 throw MakeTypeError('incompatible_method_receiver',
138 ["NAME.byteOffset", this]);
139 }
140 return %_ArrayBufferViewGetByteOffset(this);
141 }
142
143 function NAME_GetLength() {
144 if (!(%_ClassOf(this) === 'NAME')) {
145 throw MakeTypeError('incompatible_method_receiver',
146 ["NAME.length", this]);
147 }
148 return %_TypedArrayGetLength(this);
149 }
150
151 var $NAME = global.NAME;
152
153 function NAMESubArray(begin, end) {
154 if (!(%_ClassOf(this) === 'NAME')) {
155 throw MakeTypeError('incompatible_method_receiver',
156 ["NAME.subarray", this]);
157 }
158 var beginInt = TO_INTEGER(begin);
159 if (!IS_UNDEFINED(end)) {
160 end = TO_INTEGER(end);
117 } 161 }
118 162
119 function NAME_GetBuffer() { 163 var srcLength = %_TypedArrayGetLength(this);
120 if (!(%_ClassOf(this) === 'NAME')) { 164 if (beginInt < 0) {
121 throw MakeTypeError('incompatible_method_receiver', 165 beginInt = MathMax(0, srcLength + beginInt);
122 ["NAME.buffer", this]); 166 } else {
123 } 167 beginInt = MathMin(srcLength, beginInt);
124 return %TypedArrayGetBuffer(this);
125 } 168 }
126 169
127 function NAME_GetByteLength() { 170 var endInt = IS_UNDEFINED(end) ? srcLength : end;
128 if (!(%_ClassOf(this) === 'NAME')) { 171 if (endInt < 0) {
129 throw MakeTypeError('incompatible_method_receiver', 172 endInt = MathMax(0, srcLength + endInt);
130 ["NAME.byteLength", this]); 173 } else {
131 } 174 endInt = MathMin(endInt, srcLength);
132 return %_ArrayBufferViewGetByteLength(this);
133 } 175 }
134 176 if (endInt < beginInt) {
135 function NAME_GetByteOffset() { 177 endInt = beginInt;
136 if (!(%_ClassOf(this) === 'NAME')) {
137 throw MakeTypeError('incompatible_method_receiver',
138 ["NAME.byteOffset", this]);
139 }
140 return %_ArrayBufferViewGetByteOffset(this);
141 } 178 }
142 179 var newLength = endInt - beginInt;
143 function NAME_GetLength() { 180 var beginByteOffset =
144 if (!(%_ClassOf(this) === 'NAME')) { 181 %_ArrayBufferViewGetByteOffset(this) + beginInt * ELEMENT_SIZE;
145 throw MakeTypeError('incompatible_method_receiver', 182 return new $NAME(%TypedArrayGetBuffer(this),
146 ["NAME.length", this]); 183 beginByteOffset, newLength);
147 } 184 }
148 return %_TypedArrayGetLength(this);
149 }
150
151 var $NAME = global.NAME;
152
153 function NAMESubArray(begin, end) {
154 if (!(%_ClassOf(this) === 'NAME')) {
155 throw MakeTypeError('incompatible_method_receiver',
156 ["NAME.subarray", this]);
157 }
158 var beginInt = TO_INTEGER(begin);
159 if (!IS_UNDEFINED(end)) {
160 end = TO_INTEGER(end);
161 }
162
163 var srcLength = %_TypedArrayGetLength(this);
164 if (beginInt < 0) {
165 beginInt = MathMax(0, srcLength + beginInt);
166 } else {
167 beginInt = MathMin(srcLength, beginInt);
168 }
169
170 var endInt = IS_UNDEFINED(end) ? srcLength : end;
171 if (endInt < 0) {
172 endInt = MathMax(0, srcLength + endInt);
173 } else {
174 endInt = MathMin(endInt, srcLength);
175 }
176 if (endInt < beginInt) {
177 endInt = beginInt;
178 }
179 var newLength = endInt - beginInt;
180 var beginByteOffset =
181 %_ArrayBufferViewGetByteOffset(this) + beginInt * ELEMENT_SIZE;
182 return new $NAME(%TypedArrayGetBuffer(this),
183 beginByteOffset, newLength);
184 }
185 endmacro 185 endmacro
186 186
187 TYPED_ARRAYS(TYPED_ARRAY_CONSTRUCTOR) 187 TYPED_ARRAYS(TYPED_ARRAY_CONSTRUCTOR)
188 188
189 189
190 function TypedArraySetFromArrayLike(target, source, sourceLength, offset) { 190 function TypedArraySetFromArrayLike(target, source, sourceLength, offset) {
191 if (offset > 0) { 191 if (offset > 0) {
192 for (var i = 0; i < sourceLength; i++) { 192 for (var i = 0; i < sourceLength; i++) {
193 target[offset + i] = source[i]; 193 target[offset + i] = source[i];
194 } 194 }
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 : byteLength; 350 : byteLength;
351 if (length < 0 || offset + length > bufferByteLength) { 351 if (length < 0 || offset + length > bufferByteLength) {
352 throw new MakeRangeError('invalid_data_view_length'); 352 throw new MakeRangeError('invalid_data_view_length');
353 } 353 }
354 %_DataViewInitialize(this, buffer, offset, length); 354 %_DataViewInitialize(this, buffer, offset, length);
355 } else { 355 } else {
356 throw MakeTypeError('constructor_not_function', ["DataView"]); 356 throw MakeTypeError('constructor_not_function', ["DataView"]);
357 } 357 }
358 } 358 }
359 359
360 function DataViewGetBuffer() { 360 function DataViewGetBufferJS() {
361 if (!IS_DATAVIEW(this)) { 361 if (!IS_DATAVIEW(this)) {
362 throw MakeTypeError('incompatible_method_receiver', 362 throw MakeTypeError('incompatible_method_receiver',
363 ['DataView.buffer', this]); 363 ['DataView.buffer', this]);
364 } 364 }
365 return %DataViewGetBuffer(this); 365 return %DataViewGetBuffer(this);
366 } 366 }
367 367
368 function DataViewGetByteOffset() { 368 function DataViewGetByteOffset() {
369 if (!IS_DATAVIEW(this)) { 369 if (!IS_DATAVIEW(this)) {
370 throw MakeTypeError('incompatible_method_receiver', 370 throw MakeTypeError('incompatible_method_receiver',
(...skipping 20 matching lines...) Expand all
391 FUNCTION(Float32) 391 FUNCTION(Float32)
392 FUNCTION(Float64) 392 FUNCTION(Float64)
393 endmacro 393 endmacro
394 394
395 function ToPositiveDataViewOffset(offset) { 395 function ToPositiveDataViewOffset(offset) {
396 return ToPositiveInteger(offset, 'invalid_data_view_accessor_offset'); 396 return ToPositiveInteger(offset, 'invalid_data_view_accessor_offset');
397 } 397 }
398 398
399 399
400 macro DATA_VIEW_GETTER_SETTER(TYPENAME) 400 macro DATA_VIEW_GETTER_SETTER(TYPENAME)
401 function DataViewGetTYPENAME(offset, little_endian) { 401 function DataViewGetTYPENAMEJS(offset, little_endian) {
402 if (!IS_DATAVIEW(this)) { 402 if (!IS_DATAVIEW(this)) {
403 throw MakeTypeError('incompatible_method_receiver', 403 throw MakeTypeError('incompatible_method_receiver',
404 ['DataView.getTYPENAME', this]); 404 ['DataView.getTYPENAME', this]);
405 } 405 }
406 if (%_ArgumentsLength() < 1) { 406 if (%_ArgumentsLength() < 1) {
407 throw MakeTypeError('invalid_argument'); 407 throw MakeTypeError('invalid_argument');
408 } 408 }
409 return %DataViewGetTYPENAME(this, 409 return %DataViewGetTYPENAME(this,
410 ToPositiveDataViewOffset(offset), 410 ToPositiveDataViewOffset(offset),
411 !!little_endian); 411 !!little_endian);
412 } 412 }
413 413
414 function DataViewSetTYPENAME(offset, value, little_endian) { 414 function DataViewSetTYPENAMEJS(offset, value, little_endian) {
415 if (!IS_DATAVIEW(this)) { 415 if (!IS_DATAVIEW(this)) {
416 throw MakeTypeError('incompatible_method_receiver', 416 throw MakeTypeError('incompatible_method_receiver',
417 ['DataView.setTYPENAME', this]); 417 ['DataView.setTYPENAME', this]);
418 } 418 }
419 if (%_ArgumentsLength() < 2) { 419 if (%_ArgumentsLength() < 2) {
420 throw MakeTypeError('invalid_argument'); 420 throw MakeTypeError('invalid_argument');
421 } 421 }
422 %DataViewSetTYPENAME(this, 422 %DataViewSetTYPENAME(this,
423 ToPositiveDataViewOffset(offset), 423 ToPositiveDataViewOffset(offset),
424 TO_NUMBER_INLINE(value), 424 TO_NUMBER_INLINE(value),
425 !!little_endian); 425 !!little_endian);
426 } 426 }
427 endmacro 427 endmacro
428 428
429 DATA_VIEW_TYPES(DATA_VIEW_GETTER_SETTER) 429 DATA_VIEW_TYPES(DATA_VIEW_GETTER_SETTER)
430 430
431 function SetupDataView() { 431 function SetupDataView() {
432 %CheckIsBootstrapping(); 432 %CheckIsBootstrapping();
433 433
434 // Setup the DataView constructor. 434 // Setup the DataView constructor.
435 %SetCode($DataView, DataViewConstructor); 435 %SetCode($DataView, DataViewConstructor);
436 %FunctionSetPrototype($DataView, new $Object); 436 %FunctionSetPrototype($DataView, new $Object);
437 437
438 // Set up constructor property on the DataView prototype. 438 // Set up constructor property on the DataView prototype.
439 %SetProperty($DataView.prototype, "constructor", $DataView, DONT_ENUM); 439 %SetProperty($DataView.prototype, "constructor", $DataView, DONT_ENUM);
440 440
441 InstallGetter($DataView.prototype, "buffer", DataViewGetBuffer); 441 InstallGetter($DataView.prototype, "buffer", DataViewGetBufferJS);
442 InstallGetter($DataView.prototype, "byteOffset", DataViewGetByteOffset); 442 InstallGetter($DataView.prototype, "byteOffset", DataViewGetByteOffset);
443 InstallGetter($DataView.prototype, "byteLength", DataViewGetByteLength); 443 InstallGetter($DataView.prototype, "byteLength", DataViewGetByteLength);
444 444
445 InstallFunctions($DataView.prototype, DONT_ENUM, $Array( 445 InstallFunctions($DataView.prototype, DONT_ENUM, $Array(
446 "getInt8", DataViewGetInt8, 446 "getInt8", DataViewGetInt8JS,
447 "setInt8", DataViewSetInt8, 447 "setInt8", DataViewSetInt8JS,
448 448
449 "getUint8", DataViewGetUint8, 449 "getUint8", DataViewGetUint8JS,
450 "setUint8", DataViewSetUint8, 450 "setUint8", DataViewSetUint8JS,
451 451
452 "getInt16", DataViewGetInt16, 452 "getInt16", DataViewGetInt16JS,
453 "setInt16", DataViewSetInt16, 453 "setInt16", DataViewSetInt16JS,
454 454
455 "getUint16", DataViewGetUint16, 455 "getUint16", DataViewGetUint16JS,
456 "setUint16", DataViewSetUint16, 456 "setUint16", DataViewSetUint16JS,
457 457
458 "getInt32", DataViewGetInt32, 458 "getInt32", DataViewGetInt32JS,
459 "setInt32", DataViewSetInt32, 459 "setInt32", DataViewSetInt32JS,
460 460
461 "getUint32", DataViewGetUint32, 461 "getUint32", DataViewGetUint32JS,
462 "setUint32", DataViewSetUint32, 462 "setUint32", DataViewSetUint32JS,
463 463
464 "getFloat32", DataViewGetFloat32, 464 "getFloat32", DataViewGetFloat32JS,
465 "setFloat32", DataViewSetFloat32, 465 "setFloat32", DataViewSetFloat32JS,
466 466
467 "getFloat64", DataViewGetFloat64, 467 "getFloat64", DataViewGetFloat64JS,
468 "setFloat64", DataViewSetFloat64 468 "setFloat64", DataViewSetFloat64JS
469 )); 469 ));
470 } 470 }
471 471
472 SetupDataView(); 472 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