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

Side by Side Diff: src/typedarray.js

Issue 71163006: Merge bleeding_edge r17376:17693. (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Fix all.gyp Created 7 years, 1 month 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/stub-cache.cc ('k') | src/unicode.h » ('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 16 matching lines...) Expand all
27 27
28 "use strict"; 28 "use strict";
29 29
30 // This file relies on the fact that the following declaration has been made 30 // This file relies on the fact that the following declaration has been made
31 // in runtime.js: 31 // in runtime.js:
32 // var $Array = global.Array; 32 // var $Array = global.Array;
33 var $ArrayBuffer = global.ArrayBuffer; 33 var $ArrayBuffer = global.ArrayBuffer;
34 34
35 35
36 // --------------- Typed Arrays --------------------- 36 // --------------- Typed Arrays ---------------------
37 macro TYPED_ARRAYS(FUNCTION)
38 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize.
39 FUNCTION(1, Uint8Array, 1)
40 FUNCTION(2, Int8Array, 1)
41 FUNCTION(3, Uint16Array, 2)
42 FUNCTION(4, Int16Array, 2)
43 FUNCTION(5, Uint32Array, 4)
44 FUNCTION(6, Int32Array, 4)
45 FUNCTION(7, Float32Array, 4)
46 FUNCTION(8, Float64Array, 8)
47 FUNCTION(9, Uint8ClampedArray, 1)
48 endmacro
37 49
38 function CreateTypedArrayConstructor(name, elementSize, arrayId, constructor) { 50 macro TYPED_ARRAY_CONSTRUCTOR(ARRAY_ID, NAME, ELEMENT_SIZE)
39 function ConstructByArrayBuffer(obj, buffer, byteOffset, length) { 51 function NAMEConstructByArrayBuffer(obj, buffer, byteOffset, length) {
40 var offset = ToPositiveInteger(byteOffset, "invalid_typed_array_length") 52 var bufferByteLength = buffer.byteLength;
53 var offset;
54 if (IS_UNDEFINED(byteOffset)) {
55 offset = 0;
56 } else {
57 offset = ToPositiveInteger(byteOffset, "invalid_typed_array_length");
41 58
42 if (offset % elementSize !== 0) { 59 if (offset % ELEMENT_SIZE !== 0) {
43 throw MakeRangeError("invalid_typed_array_alignment", 60 throw MakeRangeError("invalid_typed_array_alignment",
44 "start offset", name, elementSize); 61 "start offset", "NAME", ELEMENT_SIZE);
45 } 62 }
46 var bufferByteLength = %ArrayBufferGetByteLength(buffer); 63 if (offset > bufferByteLength) {
47 if (offset > bufferByteLength) { 64 throw MakeRangeError("invalid_typed_array_offset");
48 throw MakeRangeError("invalid_typed_array_offset"); 65 }
49 } 66 }
50 67
51 var newByteLength; 68 var newByteLength;
52 var newLength; 69 var newLength;
53 if (IS_UNDEFINED(length)) { 70 if (IS_UNDEFINED(length)) {
54 if (bufferByteLength % elementSize !== 0) { 71 if (bufferByteLength % ELEMENT_SIZE !== 0) {
55 throw MakeRangeError("invalid_typed_array_alignment", 72 throw MakeRangeError("invalid_typed_array_alignment",
56 "byte length", name, elementSize); 73 "byte length", "NAME", ELEMENT_SIZE);
57 } 74 }
58 newByteLength = bufferByteLength - offset; 75 newByteLength = bufferByteLength - offset;
59 newLength = newByteLength / elementSize; 76 newLength = newByteLength / ELEMENT_SIZE;
60 } else { 77 } else {
61 var newLength = ToPositiveInteger(length, "invalid_typed_array_length"); 78 var newLength = ToPositiveInteger(length, "invalid_typed_array_length");
62 newByteLength = newLength * elementSize; 79 newByteLength = newLength * ELEMENT_SIZE;
63 } 80 }
64 if (offset + newByteLength > bufferByteLength) { 81 if (offset + newByteLength > bufferByteLength) {
65 throw MakeRangeError("invalid_typed_array_length"); 82 throw MakeRangeError("invalid_typed_array_length");
66 } 83 }
67 %TypedArrayInitialize(obj, arrayId, buffer, offset, newByteLength); 84 %TypedArrayInitialize(obj, ARRAY_ID, buffer, offset, newByteLength);
68 } 85 }
69 86
70 function ConstructByLength(obj, length) { 87 function NAMEConstructByLength(obj, length) {
71 var l = ToPositiveInteger(length, "invalid_typed_array_length"); 88 var l = IS_UNDEFINED(length) ?
72 var byteLength = l * elementSize; 89 0 : ToPositiveInteger(length, "invalid_typed_array_length");
90 var byteLength = l * ELEMENT_SIZE;
73 var buffer = new $ArrayBuffer(byteLength); 91 var buffer = new $ArrayBuffer(byteLength);
74 %TypedArrayInitialize(obj, arrayId, buffer, 0, byteLength); 92 %TypedArrayInitialize(obj, ARRAY_ID, buffer, 0, byteLength);
75 } 93 }
76 94
77 function ConstructByArrayLike(obj, arrayLike) { 95 function NAMEConstructByArrayLike(obj, arrayLike) {
78 var length = arrayLike.length; 96 var length = arrayLike.length;
79 var l = ToPositiveInteger(length, "invalid_typed_array_length"); 97 var l = ToPositiveInteger(length, "invalid_typed_array_length");
80 if(!%TypedArrayInitializeFromArrayLike(obj, arrayId, arrayLike, l)) { 98 if(!%TypedArrayInitializeFromArrayLike(obj, ARRAY_ID, arrayLike, l)) {
81 for (var i = 0; i < l; i++) { 99 for (var i = 0; i < l; i++) {
82 // It is crucial that we let any execptions from arrayLike[i] 100 // It is crucial that we let any execptions from arrayLike[i]
83 // propagate outside the function. 101 // propagate outside the function.
84 obj[i] = arrayLike[i]; 102 obj[i] = arrayLike[i];
85 } 103 }
86 } 104 }
87 } 105 }
88 106
89 return function (arg1, arg2, arg3) { 107 function NAMEConstructor(arg1, arg2, arg3) {
108
90 if (%_IsConstructCall()) { 109 if (%_IsConstructCall()) {
91 if (IS_ARRAYBUFFER(arg1)) { 110 if (IS_ARRAYBUFFER(arg1)) {
92 ConstructByArrayBuffer(this, arg1, arg2, arg3); 111 NAMEConstructByArrayBuffer(this, arg1, arg2, arg3);
93 } else if (IS_NUMBER(arg1) || IS_STRING(arg1) || 112 } else if (IS_NUMBER(arg1) || IS_STRING(arg1) ||
94 IS_BOOLEAN(arg1) || IS_UNDEFINED(arg1)) { 113 IS_BOOLEAN(arg1) || IS_UNDEFINED(arg1)) {
95 ConstructByLength(this, arg1); 114 NAMEConstructByLength(this, arg1);
96 } else { 115 } else {
97 ConstructByArrayLike(this, arg1); 116 NAMEConstructByArrayLike(this, arg1);
98 } 117 }
99 } else { 118 } else {
100 throw MakeTypeError("constructor_not_function", [name]) 119 throw MakeTypeError("constructor_not_function", ["NAME"])
101 } 120 }
102 } 121 }
103 } 122 endmacro
123
124 TYPED_ARRAYS(TYPED_ARRAY_CONSTRUCTOR)
104 125
105 function TypedArrayGetBuffer() { 126 function TypedArrayGetBuffer() {
106 return %TypedArrayGetBuffer(this); 127 return %TypedArrayGetBuffer(this);
107 } 128 }
108 129
109 function TypedArrayGetByteLength() { 130 function TypedArrayGetByteLength() {
110 return %TypedArrayGetByteLength(this); 131 return %TypedArrayGetByteLength(this);
111 } 132 }
112 133
113 function TypedArrayGetByteOffset() { 134 function TypedArrayGetByteOffset() {
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 if (intOffset + l > this.length) { 261 if (intOffset + l > this.length) {
241 throw MakeRangeError("typed_array_set_source_too_large"); 262 throw MakeRangeError("typed_array_set_source_too_large");
242 } 263 }
243 TypedArraySetFromArrayLike(this, obj, l, intOffset); 264 TypedArraySetFromArrayLike(this, obj, l, intOffset);
244 return; 265 return;
245 } 266 }
246 } 267 }
247 268
248 // ------------------------------------------------------------------- 269 // -------------------------------------------------------------------
249 270
250 function SetupTypedArray(arrayId, name, constructor, elementSize) { 271 function SetupTypedArray(constructor, fun, elementSize) {
251 %CheckIsBootstrapping(); 272 %CheckIsBootstrapping();
252 var fun = CreateTypedArrayConstructor(name, elementSize,
253 arrayId, constructor);
254 %SetCode(constructor, fun); 273 %SetCode(constructor, fun);
255 %FunctionSetPrototype(constructor, new $Object()); 274 %FunctionSetPrototype(constructor, new $Object());
256 275
257 %SetProperty(constructor, "BYTES_PER_ELEMENT", elementSize, 276 %SetProperty(constructor, "BYTES_PER_ELEMENT", elementSize,
258 READ_ONLY | DONT_ENUM | DONT_DELETE); 277 READ_ONLY | DONT_ENUM | DONT_DELETE);
259 %SetProperty(constructor.prototype, 278 %SetProperty(constructor.prototype,
260 "constructor", constructor, DONT_ENUM); 279 "constructor", constructor, DONT_ENUM);
261 %SetProperty(constructor.prototype, 280 %SetProperty(constructor.prototype,
262 "BYTES_PER_ELEMENT", elementSize, 281 "BYTES_PER_ELEMENT", elementSize,
263 READ_ONLY | DONT_ENUM | DONT_DELETE); 282 READ_ONLY | DONT_ENUM | DONT_DELETE);
264 InstallGetter(constructor.prototype, "buffer", TypedArrayGetBuffer); 283 InstallGetter(constructor.prototype, "buffer", TypedArrayGetBuffer);
265 InstallGetter(constructor.prototype, "byteOffset", TypedArrayGetByteOffset); 284 InstallGetter(constructor.prototype, "byteOffset", TypedArrayGetByteOffset);
266 InstallGetter(constructor.prototype, "byteLength", TypedArrayGetByteLength); 285 InstallGetter(constructor.prototype, "byteLength", TypedArrayGetByteLength);
267 InstallGetter(constructor.prototype, "length", TypedArrayGetLength); 286 InstallGetter(constructor.prototype, "length", TypedArrayGetLength);
268 287
269 InstallFunctions(constructor.prototype, DONT_ENUM, $Array( 288 InstallFunctions(constructor.prototype, DONT_ENUM, $Array(
270 "subarray", CreateSubArray(elementSize, constructor), 289 "subarray", CreateSubArray(elementSize, constructor),
271 "set", TypedArraySet 290 "set", TypedArraySet
272 )); 291 ));
273 } 292 }
274 293
275 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. 294 macro SETUP_TYPED_ARRAY(ARRAY_ID, NAME, ELEMENT_SIZE)
276 SetupTypedArray(1, "Uint8Array", global.Uint8Array, 1); 295 SetupTypedArray (global.NAME, NAMEConstructor, ELEMENT_SIZE);
277 SetupTypedArray(2, "Int8Array", global.Int8Array, 1); 296 endmacro
278 SetupTypedArray(3, "Uint16Array", global.Uint16Array, 2);
279 SetupTypedArray(4, "Int16Array", global.Int16Array, 2);
280 SetupTypedArray(5, "Uint32Array", global.Uint32Array, 4);
281 SetupTypedArray(6, "Int32Array", global.Int32Array, 4);
282 SetupTypedArray(7, "Float32Array", global.Float32Array, 4);
283 SetupTypedArray(8, "Float64Array", global.Float64Array, 8);
284 SetupTypedArray(9, "Uint8ClampedArray", global.Uint8ClampedArray, 1);
285 297
298 TYPED_ARRAYS(SETUP_TYPED_ARRAY)
286 299
287 // --------------------------- DataView ----------------------------- 300 // --------------------------- DataView -----------------------------
288 301
289 var $DataView = global.DataView; 302 var $DataView = global.DataView;
290 303
291 function DataViewConstructor(buffer, byteOffset, byteLength) { // length = 3 304 function DataViewConstructor(buffer, byteOffset, byteLength) { // length = 3
292 if (%_IsConstructCall()) { 305 if (%_IsConstructCall()) {
293 if (!IS_ARRAYBUFFER(buffer)) { 306 if (!IS_ARRAYBUFFER(buffer)) {
294 throw MakeTypeError('data_view_not_array_buffer', []); 307 throw MakeTypeError('data_view_not_array_buffer', []);
295 } 308 }
296 var bufferByteLength = %ArrayBufferGetByteLength(buffer); 309 var bufferByteLength = %ArrayBufferGetByteLength(buffer);
297 var offset = ToPositiveInteger(byteOffset, 'invalid_data_view_offset'); 310 var offset = IS_UNDEFINED(byteOffset) ?
311 0 : ToPositiveInteger(byteOffset, 'invalid_data_view_offset');
298 if (offset > bufferByteLength) { 312 if (offset > bufferByteLength) {
299 throw MakeRangeError('invalid_data_view_offset'); 313 throw MakeRangeError('invalid_data_view_offset');
300 } 314 }
301 var length = IS_UNDEFINED(byteLength) ? 315 var length = IS_UNDEFINED(byteLength) ?
302 bufferByteLength - offset : TO_INTEGER(byteLength); 316 bufferByteLength - offset : TO_INTEGER(byteLength);
303 if (length < 0 || offset + length > bufferByteLength) { 317 if (length < 0 || offset + length > bufferByteLength) {
304 throw new MakeRangeError('invalid_data_view_length'); 318 throw new MakeRangeError('invalid_data_view_length');
305 } 319 }
306 %DataViewInitialize(this, buffer, offset, length); 320 %DataViewInitialize(this, buffer, offset, length);
307 } else { 321 } else {
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
588 602
589 "getFloat32", DataViewGetFloat32, 603 "getFloat32", DataViewGetFloat32,
590 "setFloat32", DataViewSetFloat32, 604 "setFloat32", DataViewSetFloat32,
591 605
592 "getFloat64", DataViewGetFloat64, 606 "getFloat64", DataViewGetFloat64,
593 "setFloat64", DataViewSetFloat64 607 "setFloat64", DataViewSetFloat64
594 )); 608 ));
595 } 609 }
596 610
597 SetupDataView(); 611 SetupDataView();
OLDNEW
« no previous file with comments | « src/stub-cache.cc ('k') | src/unicode.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698