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

Side by Side Diff: src/typedarray.js

Issue 14460008: TypedArray(length) constructor (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Added unit-tests for typed array length 0 Created 7 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 | « no previous file | test/mjsunit/harmony/typedarrays.js » ('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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 // TODO(dslomov): implement inheritance 78 // TODO(dslomov): implement inheritance
79 var result = new $ArrayBuffer(newLen); 79 var result = new $ArrayBuffer(newLen);
80 80
81 %ArrayBufferSliceImpl(this, result, first); 81 %ArrayBufferSliceImpl(this, result, first);
82 return result; 82 return result;
83 } 83 }
84 84
85 // --------------- Typed Arrays --------------------- 85 // --------------- Typed Arrays ---------------------
86 86
87 function CreateTypedArrayConstructor(name, elementSize, arrayId, constructor) { 87 function CreateTypedArrayConstructor(name, elementSize, arrayId, constructor) {
88 return function (buffer, byteOffset, length) { 88 function ConstructByArrayBuffer(obj, buffer, byteOffset, length) {
89 var offset = IS_UNDEFINED(byteOffset)
90 ? 0 : offset = TO_POSITIVE_INTEGER(byteOffset);
91
92 if (offset % elementSize !== 0) {
93 throw MakeRangeError("invalid_typed_array_alignment",
94 "start offset", name, elementSize);
95 }
96 var bufferByteLength = %ArrayBufferGetByteLength(buffer);
97 if (offset >= bufferByteLength) {
98 throw MakeRangeError("invalid_typed_array_offset");
99 }
100
101 var newByteLength;
102 var newLength;
103 if (IS_UNDEFINED(length)) {
104 if (bufferByteLength % elementSize !== 0) {
105 throw MakeRangeError("invalid_typed_array_alignment",
106 "byte length", name, elementSize);
107 }
108 newByteLength = bufferByteLength - offset;
109 newLength = newByteLength / elementSize;
110 } else {
111 var newLength = TO_POSITIVE_INTEGER(length);
112 newByteLength = newLength * elementSize;
113 }
114 if (newByteLength > bufferByteLength) {
115 throw MakeRangeError("invalid_typed_array_length");
116 }
117 %TypedArrayInitialize(obj, arrayId, buffer, offset, newByteLength);
118 }
119
120 function ConstructByLength(obj, length) {
121 var l = IS_UNDEFINED(length) ? 0 : TO_POSITIVE_INTEGER(length);
122 var byteLength = l * elementSize;
123 var buffer = new $ArrayBuffer(byteLength);
124 %TypedArrayInitialize(obj, arrayId, buffer, 0, byteLength);
125 }
126
127 return function (arg1, arg2, arg3) {
89 if (%_IsConstructCall()) { 128 if (%_IsConstructCall()) {
90 if (!IS_ARRAYBUFFER(buffer)) { 129 if (IS_ARRAYBUFFER(arg1)) {
91 throw MakeTypeError("Type error!"); 130 ConstructByArrayBuffer(this, arg1, arg2, arg3);
131 } else {
132 ConstructByLength(this, arg1);
92 } 133 }
93 var offset = IS_UNDEFINED(byteOffset)
94 ? 0 : offset = TO_POSITIVE_INTEGER(byteOffset);
95
96 if (offset % elementSize !== 0) {
97 throw MakeRangeError("invalid_typed_array_alignment",
98 "start offset", name, elementSize);
99 }
100 var bufferByteLength = %ArrayBufferGetByteLength(buffer);
101 if (offset >= bufferByteLength) {
102 throw MakeRangeError("invalid_typed_array_offset");
103 }
104
105 var newByteLength;
106 var newLength;
107 if (IS_UNDEFINED(length)) {
108 if (bufferByteLength % elementSize !== 0) {
109 throw MakeRangeError("invalid_typed_array_alignment",
110 "byte length", name, elementSize);
111 }
112 newByteLength = bufferByteLength - offset;
113 newLength = newByteLength / elementSize;
114 } else {
115 var newLength = TO_POSITIVE_INTEGER(length);
116 newByteLength = newLength * elementSize;
117 }
118 if (newByteLength > bufferByteLength) {
119 throw MakeRangeError("invalid_typed_array_length");
120 }
121 %TypedArrayInitialize(this, arrayId, buffer, offset, newByteLength);
122 } else { 134 } else {
123 return new constructor(buffer, byteOffset, length); 135 return new constructor(arg1, arg2, arg3);
124 } 136 }
125 } 137 }
126 } 138 }
127 139
128 function TypedArrayGetBuffer() { 140 function TypedArrayGetBuffer() {
129 return %TypedArrayGetBuffer(this); 141 return %TypedArrayGetBuffer(this);
130 } 142 }
131 143
132 function TypedArrayGetByteLength() { 144 function TypedArrayGetByteLength() {
133 return %TypedArrayGetByteLength(this); 145 return %TypedArrayGetByteLength(this);
(...skipping 23 matching lines...) Expand all
157 InstallGetter($ArrayBuffer.prototype, "byteLength", ArrayBufferGetByteLength); 169 InstallGetter($ArrayBuffer.prototype, "byteLength", ArrayBufferGetByteLength);
158 170
159 InstallFunctions($ArrayBuffer.prototype, DONT_ENUM, $Array( 171 InstallFunctions($ArrayBuffer.prototype, DONT_ENUM, $Array(
160 "slice", ArrayBufferSlice 172 "slice", ArrayBufferSlice
161 )); 173 ));
162 } 174 }
163 175
164 SetUpArrayBuffer(); 176 SetUpArrayBuffer();
165 177
166 function SetupTypedArray(arrayId, name, constructor, elementSize) { 178 function SetupTypedArray(arrayId, name, constructor, elementSize) {
167 var f = CreateTypedArrayConstructor(name, elementSize, 179 %CheckIsBootstrapping();
168 arrayId, constructor); 180 var fun = CreateTypedArrayConstructor(name, elementSize,
169 %SetCode(constructor, f); 181 arrayId, constructor);
182 %SetCode(constructor, fun);
170 %FunctionSetPrototype(constructor, new $Object()); 183 %FunctionSetPrototype(constructor, new $Object());
171 184
172 %SetProperty(constructor.prototype, 185 %SetProperty(constructor.prototype,
173 "constructor", constructor, DONT_ENUM); 186 "constructor", constructor, DONT_ENUM);
174 %SetProperty(constructor.prototype, 187 %SetProperty(constructor.prototype,
175 "BYTES_PER_ELEMENT", elementSize, 188 "BYTES_PER_ELEMENT", elementSize,
176 READ_ONLY | DONT_ENUM | DONT_DELETE); 189 READ_ONLY | DONT_ENUM | DONT_DELETE);
177 InstallGetter(constructor.prototype, "buffer", TypedArrayGetBuffer); 190 InstallGetter(constructor.prototype, "buffer", TypedArrayGetBuffer);
178 InstallGetter(constructor.prototype, "byteOffset", TypedArrayGetByteOffset); 191 InstallGetter(constructor.prototype, "byteOffset", TypedArrayGetByteOffset);
179 InstallGetter(constructor.prototype, "byteLength", TypedArrayGetByteLength); 192 InstallGetter(constructor.prototype, "byteLength", TypedArrayGetByteLength);
180 InstallGetter(constructor.prototype, "length", TypedArrayGetLength); 193 InstallGetter(constructor.prototype, "length", TypedArrayGetLength);
181 } 194 }
182 195
183 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. 196 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize.
184 SetupTypedArray(1, "Uint8Array", global.Uint8Array, 1); 197 SetupTypedArray(1, "Uint8Array", global.Uint8Array, 1);
185 SetupTypedArray(2, "Int8Array", global.Int8Array, 1); 198 SetupTypedArray(2, "Int8Array", global.Int8Array, 1);
186 SetupTypedArray(3, "Uint16Array", global.Uint16Array, 2); 199 SetupTypedArray(3, "Uint16Array", global.Uint16Array, 2);
187 SetupTypedArray(4, "Int16Array", global.Int16Array, 2); 200 SetupTypedArray(4, "Int16Array", global.Int16Array, 2);
188 SetupTypedArray(5, "Uint32Array", global.Uint32Array, 4); 201 SetupTypedArray(5, "Uint32Array", global.Uint32Array, 4);
189 SetupTypedArray(6, "Int32Array", global.Int32Array, 4); 202 SetupTypedArray(6, "Int32Array", global.Int32Array, 4);
190 SetupTypedArray(7, "Float32Array", global.Float32Array, 4); 203 SetupTypedArray(7, "Float32Array", global.Float32Array, 4);
191 SetupTypedArray(8, "Float64Array", global.Float64Array, 8); 204 SetupTypedArray(8, "Float64Array", global.Float64Array, 8);
192 205
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/harmony/typedarrays.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698