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

Side by Side Diff: src/typedarray.js

Issue 44173003: Add simple inline macros to js2c and use that for typed array constructors. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Unified typed array types iterations 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 | « no previous file | tools/js2c.py » ('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 NAMEConstructor(arg1, arg2, arg3) {
40 var offset = ToPositiveInteger(byteOffset, "invalid_typed_array_length") 52 function ConstructByArrayBuffer(obj, buffer, byteOffset, length) {
53 var offset = ToPositiveInteger(byteOffset, "invalid_typed_array_length")
41 54
42 if (offset % elementSize !== 0) { 55 if (offset % ELEMENT_SIZE !== 0) {
43 throw MakeRangeError("invalid_typed_array_alignment", 56 throw MakeRangeError("invalid_typed_array_alignment",
44 "start offset", name, elementSize); 57 "start offset", "NAME", ELEMENT_SIZE);
45 } 58 }
46 var bufferByteLength = %ArrayBufferGetByteLength(buffer); 59 var bufferByteLength = %ArrayBufferGetByteLength(buffer);
47 if (offset > bufferByteLength) { 60 if (offset > bufferByteLength) {
48 throw MakeRangeError("invalid_typed_array_offset"); 61 throw MakeRangeError("invalid_typed_array_offset");
62 }
63
64 var newByteLength;
65 var newLength;
66 if (IS_UNDEFINED(length)) {
67 if (bufferByteLength % ELEMENT_SIZE !== 0) {
68 throw MakeRangeError("invalid_typed_array_alignment",
69 "byte length", "NAME", ELEMENT_SIZE);
70 }
71 newByteLength = bufferByteLength - offset;
72 newLength = newByteLength / ELEMENT_SIZE;
73 } else {
74 var newLength = ToPositiveInteger(length, "invalid_typed_array_length");
75 newByteLength = newLength * ELEMENT_SIZE;
76 }
77 if (offset + newByteLength > bufferByteLength) {
78 throw MakeRangeError("invalid_typed_array_length");
79 }
80 %TypedArrayInitialize(obj, ARRAY_ID, buffer, offset, newByteLength);
49 } 81 }
50 82
51 var newByteLength; 83 function ConstructByLength(obj, length) {
52 var newLength; 84 var l = ToPositiveInteger(length, "invalid_typed_array_length");
53 if (IS_UNDEFINED(length)) { 85 var byteLength = l * ELEMENT_SIZE;
54 if (bufferByteLength % elementSize !== 0) { 86 var buffer = new $ArrayBuffer(byteLength);
55 throw MakeRangeError("invalid_typed_array_alignment", 87 %TypedArrayInitialize(obj, ARRAY_ID, buffer, 0, byteLength);
56 "byte length", name, elementSize);
57 }
58 newByteLength = bufferByteLength - offset;
59 newLength = newByteLength / elementSize;
60 } else {
61 var newLength = ToPositiveInteger(length, "invalid_typed_array_length");
62 newByteLength = newLength * elementSize;
63 } 88 }
64 if (offset + newByteLength > bufferByteLength) {
65 throw MakeRangeError("invalid_typed_array_length");
66 }
67 %TypedArrayInitialize(obj, arrayId, buffer, offset, newByteLength);
68 }
69 89
70 function ConstructByLength(obj, length) { 90 function ConstructByArrayLike(obj, arrayLike) {
71 var l = ToPositiveInteger(length, "invalid_typed_array_length"); 91 var length = arrayLike.length;
72 var byteLength = l * elementSize; 92 var l = ToPositiveInteger(length, "invalid_typed_array_length");
73 var buffer = new $ArrayBuffer(byteLength); 93 if(!%TypedArrayInitializeFromArrayLike(obj, ARRAY_ID, arrayLike, l)) {
74 %TypedArrayInitialize(obj, arrayId, buffer, 0, byteLength); 94 for (var i = 0; i < l; i++) {
75 } 95 // It is crucial that we let any execptions from arrayLike[i]
76 96 // propagate outside the function.
77 function ConstructByArrayLike(obj, arrayLike) { 97 obj[i] = arrayLike[i];
78 var length = arrayLike.length; 98 }
79 var l = ToPositiveInteger(length, "invalid_typed_array_length");
80 if(!%TypedArrayInitializeFromArrayLike(obj, arrayId, arrayLike, l)) {
81 for (var i = 0; i < l; i++) {
82 // It is crucial that we let any execptions from arrayLike[i]
83 // propagate outside the function.
84 obj[i] = arrayLike[i];
85 } 99 }
86 } 100 }
87 }
88 101
89 return function (arg1, arg2, arg3) {
90 if (%_IsConstructCall()) { 102 if (%_IsConstructCall()) {
91 if (IS_ARRAYBUFFER(arg1)) { 103 if (IS_ARRAYBUFFER(arg1)) {
92 ConstructByArrayBuffer(this, arg1, arg2, arg3); 104 ConstructByArrayBuffer(this, arg1, arg2, arg3);
93 } else if (IS_NUMBER(arg1) || IS_STRING(arg1) || 105 } else if (IS_NUMBER(arg1) || IS_STRING(arg1) ||
94 IS_BOOLEAN(arg1) || IS_UNDEFINED(arg1)) { 106 IS_BOOLEAN(arg1) || IS_UNDEFINED(arg1)) {
95 ConstructByLength(this, arg1); 107 ConstructByLength(this, arg1);
96 } else { 108 } else {
97 ConstructByArrayLike(this, arg1); 109 ConstructByArrayLike(this, arg1);
98 } 110 }
99 } else { 111 } else {
100 throw MakeTypeError("constructor_not_function", [name]) 112 throw MakeTypeError("constructor_not_function", ["NAME"])
101 } 113 }
102 } 114 }
103 } 115 endmacro
116
117 TYPED_ARRAYS(TYPED_ARRAY_CONSTRUCTOR)
104 118
105 function TypedArrayGetBuffer() { 119 function TypedArrayGetBuffer() {
106 return %TypedArrayGetBuffer(this); 120 return %TypedArrayGetBuffer(this);
107 } 121 }
108 122
109 function TypedArrayGetByteLength() { 123 function TypedArrayGetByteLength() {
110 return %TypedArrayGetByteLength(this); 124 return %TypedArrayGetByteLength(this);
111 } 125 }
112 126
113 function TypedArrayGetByteOffset() { 127 function TypedArrayGetByteOffset() {
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 if (intOffset + l > this.length) { 254 if (intOffset + l > this.length) {
241 throw MakeRangeError("typed_array_set_source_too_large"); 255 throw MakeRangeError("typed_array_set_source_too_large");
242 } 256 }
243 TypedArraySetFromArrayLike(this, obj, l, intOffset); 257 TypedArraySetFromArrayLike(this, obj, l, intOffset);
244 return; 258 return;
245 } 259 }
246 } 260 }
247 261
248 // ------------------------------------------------------------------- 262 // -------------------------------------------------------------------
249 263
250 function SetupTypedArray(arrayId, name, constructor, elementSize) { 264 function SetupTypedArray(constructor, fun, elementSize) {
251 %CheckIsBootstrapping(); 265 %CheckIsBootstrapping();
252 var fun = CreateTypedArrayConstructor(name, elementSize,
253 arrayId, constructor);
254 %SetCode(constructor, fun); 266 %SetCode(constructor, fun);
255 %FunctionSetPrototype(constructor, new $Object()); 267 %FunctionSetPrototype(constructor, new $Object());
256 268
257 %SetProperty(constructor, "BYTES_PER_ELEMENT", elementSize, 269 %SetProperty(constructor, "BYTES_PER_ELEMENT", elementSize,
258 READ_ONLY | DONT_ENUM | DONT_DELETE); 270 READ_ONLY | DONT_ENUM | DONT_DELETE);
259 %SetProperty(constructor.prototype, 271 %SetProperty(constructor.prototype,
260 "constructor", constructor, DONT_ENUM); 272 "constructor", constructor, DONT_ENUM);
261 %SetProperty(constructor.prototype, 273 %SetProperty(constructor.prototype,
262 "BYTES_PER_ELEMENT", elementSize, 274 "BYTES_PER_ELEMENT", elementSize,
263 READ_ONLY | DONT_ENUM | DONT_DELETE); 275 READ_ONLY | DONT_ENUM | DONT_DELETE);
264 InstallGetter(constructor.prototype, "buffer", TypedArrayGetBuffer); 276 InstallGetter(constructor.prototype, "buffer", TypedArrayGetBuffer);
265 InstallGetter(constructor.prototype, "byteOffset", TypedArrayGetByteOffset); 277 InstallGetter(constructor.prototype, "byteOffset", TypedArrayGetByteOffset);
266 InstallGetter(constructor.prototype, "byteLength", TypedArrayGetByteLength); 278 InstallGetter(constructor.prototype, "byteLength", TypedArrayGetByteLength);
267 InstallGetter(constructor.prototype, "length", TypedArrayGetLength); 279 InstallGetter(constructor.prototype, "length", TypedArrayGetLength);
268 280
269 InstallFunctions(constructor.prototype, DONT_ENUM, $Array( 281 InstallFunctions(constructor.prototype, DONT_ENUM, $Array(
270 "subarray", CreateSubArray(elementSize, constructor), 282 "subarray", CreateSubArray(elementSize, constructor),
271 "set", TypedArraySet 283 "set", TypedArraySet
272 )); 284 ));
273 } 285 }
274 286
275 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize.
276 SetupTypedArray(1, "Uint8Array", global.Uint8Array, 1);
277 SetupTypedArray(2, "Int8Array", global.Int8Array, 1);
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 287
288 macro SETUP_TYPED_ARRAY(ARRAY_ID, NAME, ELEMENT_SIZE)
289 SetupTypedArray (global.NAME, NAMEConstructor, ELEMENT_SIZE);
290 endmacro
291
292 TYPED_ARRAYS(SETUP_TYPED_ARRAY)
286 293
287 // --------------------------- DataView ----------------------------- 294 // --------------------------- DataView -----------------------------
288 295
289 var $DataView = global.DataView; 296 var $DataView = global.DataView;
290 297
291 function DataViewConstructor(buffer, byteOffset, byteLength) { // length = 3 298 function DataViewConstructor(buffer, byteOffset, byteLength) { // length = 3
292 if (%_IsConstructCall()) { 299 if (%_IsConstructCall()) {
293 if (!IS_ARRAYBUFFER(buffer)) { 300 if (!IS_ARRAYBUFFER(buffer)) {
294 throw MakeTypeError('data_view_not_array_buffer', []); 301 throw MakeTypeError('data_view_not_array_buffer', []);
295 } 302 }
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after
588 595
589 "getFloat32", DataViewGetFloat32, 596 "getFloat32", DataViewGetFloat32,
590 "setFloat32", DataViewSetFloat32, 597 "setFloat32", DataViewSetFloat32,
591 598
592 "getFloat64", DataViewGetFloat64, 599 "getFloat64", DataViewGetFloat64,
593 "setFloat64", DataViewSetFloat64 600 "setFloat64", DataViewSetFloat64
594 )); 601 ));
595 } 602 }
596 603
597 SetupDataView(); 604 SetupDataView();
OLDNEW
« no previous file with comments | « no previous file | tools/js2c.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698