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

Side by Side Diff: src/dataview.js

Issue 15943002: v8 typed arrays: add DataView type (Closed)
Patch Set: 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
OLDNEW
(Empty)
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 "use strict";
29
30 // This file relies on the fact that the following declaration has been made
31 // in runtime.js:
32 // var $Array = global.Array;
33
34 var $DataView = global.DataView;
35
36
37 function DataViewConstructor(buffer, byteOffset, byteLength) {
38 if (%_IsConstructCall()) {
39 if (!IS_ARRAYBUFFER(buffer)) {
40 throw MakeTypeError("not_array_buffer");
41 }
42 var bufferByteLength = %ArrayBufferGetByteLength(buffer);
43 var offset = IS_UNDEFINED(byteOffset) ? 0 : TO_POSITIVE_INTEGER(byteOffset);
44 if (offset > bufferByteLength) {
45 throw MakeRangeError("invalid_data_view_offset");
46 }
47 var length;
48 if (IS_UNDEFINED(byteLength)) {
49 length = bufferByteLength - offset;
50 } else {
51 length = TO_POSITIVE_INTEGER(byteLength);
52 }
53 if (offset + length > bufferByteLength) {
54 throw MakeRangeError("invalid_data_view_length");
55 }
56 %DataViewInitialize(this, buffer, offset, length);
57 } else {
58 return new $DataView(buffer, byteOffset, byteLength);
59 }
60 }
61
62
63 function DataViewGetBuffer() {
64 return %DataViewGetBuffer(this);
65 }
66
67
68 function DataViewGetByteLength() {
69 return %DataViewGetByteLength(this);
70 }
71
72
73 function DataViewGetByteOffset() {
74 return %DataViewGetByteOffset(this);
75 }
76
77
78 function DataViewGetInt8(byteOffset) {
79 if (%_ArgumentsLength() < 1) {
80 throw MakeTypeError("missing_data_view_argument");
81 }
82 var buffer = %DataViewGetBuffer(this);
83 var offset = TO_INT32(byteOffset);
84 if (offset < 0 || offset + 1 > %ArrayBufferGetByteLength(buffer)) {
85 throw MakeRangeError("invalid_data_view_offset");
86 }
87 return %DataViewGetInt8(this, offset, false);
bnoordhuis 2013/05/23 22:59:04 I've been trying to figure out if it's possible to
88 }
89
90
91 function DataViewGetUint8(byteOffset) {
92 if (%_ArgumentsLength() < 1) {
93 throw MakeTypeError("missing_data_view_argument");
94 }
95 var buffer = %DataViewGetBuffer(this);
96 var offset = TO_INT32(byteOffset);
97 if (offset < 0 || offset + 1 > %ArrayBufferGetByteLength(buffer)) {
98 throw MakeRangeError("invalid_data_view_offset");
99 }
100 return %DataViewGetUint8(this, offset, false);
101 }
102
103
104 function DataViewGetInt16(byteOffset, littleEndian) {
105 if (%_ArgumentsLength() < 1) {
106 throw MakeTypeError("missing_data_view_argument");
107 }
108 var buffer = %DataViewGetBuffer(this);
109 var offset = TO_INT32(byteOffset);
110 if (offset < 0 || offset + 2 > %ArrayBufferGetByteLength(buffer)) {
111 throw MakeRangeError("invalid_data_view_offset");
112 }
113 return %DataViewGetInt16(this, offset, ToBoolean(littleEndian));
114 }
115
116
117 function DataViewGetUint16(byteOffset, littleEndian) {
118 if (%_ArgumentsLength() < 1) {
119 throw MakeTypeError("missing_data_view_argument");
120 }
121 var buffer = %DataViewGetBuffer(this);
122 var offset = TO_INT32(byteOffset);
123 if (offset < 0 || offset + 2 > %ArrayBufferGetByteLength(buffer)) {
124 throw MakeRangeError("invalid_data_view_offset");
125 }
126 return %DataViewGetUint16(this, offset, ToBoolean(littleEndian));
127 }
128
129
130 function DataViewGetInt32(byteOffset, littleEndian) {
131 if (%_ArgumentsLength() < 1) {
132 throw MakeTypeError("missing_data_view_argument");
133 }
134 var buffer = %DataViewGetBuffer(this);
135 var offset = TO_INT32(byteOffset);
136 if (offset < 0 || offset + 4 > %ArrayBufferGetByteLength(buffer)) {
137 throw MakeRangeError("invalid_data_view_offset");
138 }
139 return %DataViewGetInt32(this, offset, ToBoolean(littleEndian));
140 }
141
142
143 function DataViewGetUint32(byteOffset, littleEndian) {
144 if (%_ArgumentsLength() < 1) {
145 throw MakeTypeError("missing_data_view_argument");
146 }
147 var buffer = %DataViewGetBuffer(this);
148 var offset = TO_INT32(byteOffset);
149 if (offset < 0 || offset + 4 > %ArrayBufferGetByteLength(buffer)) {
150 throw MakeRangeError("invalid_data_view_offset");
151 }
152 return %DataViewGetUint32(this, offset, ToBoolean(littleEndian));
153 }
154
155
156 function DataViewGetFloat32(byteOffset, littleEndian) {
157 if (%_ArgumentsLength() < 1) {
158 throw MakeTypeError("missing_data_view_argument");
159 }
160 var buffer = %DataViewGetBuffer(this);
161 var offset = TO_INT32(byteOffset);
162 if (offset < 0 || offset + 4 > %ArrayBufferGetByteLength(buffer)) {
163 throw MakeRangeError("invalid_data_view_offset");
164 }
165 return %DataViewGetFloat32(this, offset, ToBoolean(littleEndian));
166 }
167
168
169 function DataViewGetFloat64(byteOffset, littleEndian) {
170 if (%_ArgumentsLength() < 1) {
171 throw MakeTypeError("missing_data_view_argument");
172 }
173 var buffer = %DataViewGetBuffer(this);
174 var offset = TO_INT32(byteOffset);
175 if (offset < 0 || offset + 8 > %ArrayBufferGetByteLength(buffer)) {
176 throw MakeRangeError("invalid_data_view_offset");
177 }
178 return %DataViewGetFloat64(this, offset, ToBoolean(littleEndian));
179 }
180
181
182 function DataViewSetInt8(byteOffset, value) {
183 if (%_ArgumentsLength() < 2) {
184 throw MakeTypeError("missing_data_view_argument");
185 }
186 var buffer = %DataViewGetBuffer(this);
187 var offset = TO_INT32(byteOffset);
188 if (offset < 0 || offset + 1 > %ArrayBufferGetByteLength(buffer)) {
189 throw MakeRangeError("invalid_data_view_offset");
190 }
191 return %DataViewSetInt8(this, offset, TO_INT32(value), false);
192 }
193
194
195 function DataViewSetUint8(byteOffset, value) {
196 if (%_ArgumentsLength() < 2) {
197 throw MakeTypeError("missing_data_view_argument");
198 }
199 var buffer = %DataViewGetBuffer(this);
200 var offset = TO_INT32(byteOffset);
201 if (offset < 0 || offset + 1 > %ArrayBufferGetByteLength(buffer)) {
202 throw MakeRangeError("invalid_data_view_offset");
203 }
204 return %DataViewSetUint8(this, offset, TO_UINT32(value), false);
205 }
206
207
208 function DataViewSetInt16(byteOffset, value, littleEndian) {
209 if (%_ArgumentsLength() < 2) {
210 throw MakeTypeError("missing_data_view_argument");
211 }
212 var buffer = %DataViewGetBuffer(this);
213 var offset = TO_INT32(byteOffset);
214 if (offset < 0 || offset + 2 > %ArrayBufferGetByteLength(buffer)) {
215 throw MakeRangeError("invalid_data_view_offset");
216 }
217 var le = !IS_UNDEFINED(littleEndian) && ToBoolean(littleEndian);
218 return %DataViewSetInt16(this, offset, TO_INT32(value), le);
219 }
220
221
222 function DataViewSetUint16(byteOffset, value, littleEndian) {
223 if (%_ArgumentsLength() < 2) {
224 throw MakeTypeError("missing_data_view_argument");
225 }
226 var buffer = %DataViewGetBuffer(this);
227 var offset = TO_INT32(byteOffset);
228 if (offset < 0 || offset + 2 > %ArrayBufferGetByteLength(buffer)) {
229 throw MakeRangeError("invalid_data_view_offset");
230 }
231 var le = !IS_UNDEFINED(littleEndian) && ToBoolean(littleEndian);
232 return %DataViewSetUint16(this, offset, TO_UINT32(value), le);
233 }
234
235
236 function DataViewSetInt32(byteOffset, value, littleEndian) {
237 if (%_ArgumentsLength() < 2) {
238 throw MakeTypeError("missing_data_view_argument");
239 }
240 var buffer = %DataViewGetBuffer(this);
241 var offset = TO_INT32(byteOffset);
242 if (offset < 0 || offset + 4 > %ArrayBufferGetByteLength(buffer)) {
243 throw MakeRangeError("invalid_data_view_offset");
244 }
245 var le = !IS_UNDEFINED(littleEndian) && ToBoolean(littleEndian);
246 return %DataViewSetInt32(this, offset, TO_INT32(value), le);
247 }
248
249
250 function DataViewSetUint32(byteOffset, value, littleEndian) {
251 if (%_ArgumentsLength() < 2) {
252 throw MakeTypeError("missing_data_view_argument");
253 }
254 var buffer = %DataViewGetBuffer(this);
255 var offset = TO_INT32(byteOffset);
256 if (offset < 0 || offset + 4 > %ArrayBufferGetByteLength(buffer)) {
257 throw MakeRangeError("invalid_data_view_offset");
258 }
259 var le = !IS_UNDEFINED(littleEndian) && ToBoolean(littleEndian);
260 return %DataViewSetUint32(this, offset, TO_UINT32(value), le);
261 }
262
263
264 function DataViewSetFloat32(byteOffset, value, littleEndian) {
265 if (%_ArgumentsLength() < 2) {
266 throw MakeTypeError("missing_data_view_argument");
267 }
268 var buffer = %DataViewGetBuffer(this);
269 var offset = TO_INT32(byteOffset);
270 if (offset < 0 || offset + 4 > %ArrayBufferGetByteLength(buffer)) {
271 throw MakeRangeError("invalid_data_view_offset");
272 }
273 var le = !IS_UNDEFINED(littleEndian) && ToBoolean(littleEndian);
274 return %DataViewSetFloat32(this, offset, TO_NUMBER_INLINE(value), le);
275 }
276
277
278 function DataViewSetFloat64(byteOffset, value, littleEndian) {
279 if (%_ArgumentsLength() < 2) {
280 throw MakeTypeError("missing_data_view_argument");
281 }
282 var buffer = %DataViewGetBuffer(this);
283 var offset = TO_INT32(byteOffset);
284 if (offset < 0 || offset + 8 > %ArrayBufferGetByteLength(buffer)) {
285 throw MakeRangeError("invalid_data_view_offset");
286 }
287 var le = !IS_UNDEFINED(littleEndian) && ToBoolean(littleEndian);
288 return %DataViewSetFloat64(this, offset, TO_NUMBER_INLINE(value), le);
289 }
290
291
292 function SetUpDataView() {
293 %CheckIsBootstrapping();
294 %SetCode($DataView, DataViewConstructor);
295 %FunctionSetPrototype($DataView, new $Object());
296 %SetProperty($DataView.prototype, "constructor", $DataView, DONT_ENUM);
297 InstallGetter($DataView.prototype, "buffer", DataViewGetBuffer);
298 InstallGetter($DataView.prototype, "byteOffset", DataViewGetByteOffset);
299 InstallGetter($DataView.prototype, "byteLength", DataViewGetByteLength);
300 InstallFunctions($DataView.prototype, DONT_ENUM, $Array(
301 "getInt8", DataViewGetInt8,
302 "getUint8", DataViewGetUint8,
303 "getInt16", DataViewGetInt16,
304 "getUint16", DataViewGetUint16,
305 "getInt32", DataViewGetInt32,
306 "getUint32", DataViewGetUint32,
307 "getFloat32", DataViewGetFloat32,
308 "getFloat64", DataViewGetFloat64,
309 "setInt8", DataViewSetInt8,
310 "setUint8", DataViewSetUint8,
311 "setInt16", DataViewSetInt16,
312 "setUint16", DataViewSetUint16,
313 "setInt32", DataViewSetInt32,
314 "setUint32", DataViewSetUint32,
315 "setFloat32", DataViewSetFloat32,
316 "setFloat64", DataViewSetFloat64
317 ));
318 }
319
320 SetUpDataView();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698