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

Side by Side Diff: src/dataview.js

Issue 15943002: v8 typed arrays: add DataView type (Closed)
Patch Set: v8 typed arrays: add DataView type, v2 Created 7 years, 6 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 offset = TO_INT32(byteOffset);
Dmitry Lomov (no reviews) 2013/06/03 13:09:04 Should be TO_POSITIVE_INTEGER. Byte offsets may be
83 if (offset < 0 || offset + 1 > %DataViewGetByteLength(this)) {
84 throw MakeRangeError("invalid_data_view_offset");
85 }
86 return %DataViewGetInt8(this, offset);
87 }
88
89
90 function DataViewGetUint8(byteOffset) {
91 if (%_ArgumentsLength() < 1) {
92 throw MakeTypeError("missing_data_view_argument");
93 }
94 var offset = TO_INT32(byteOffset);
95 if (offset < 0 || offset + 1 > %DataViewGetByteLength(this)) {
96 throw MakeRangeError("invalid_data_view_offset");
97 }
98 return %DataViewGetUint8(this, offset);
99 }
100
101
102 function DataViewGetInt16(byteOffset, littleEndian) {
103 if (%_ArgumentsLength() < 1) {
104 throw MakeTypeError("missing_data_view_argument");
105 }
106 var offset = TO_INT32(byteOffset);
107 if (offset < 0 || offset + 2 > %DataViewGetByteLength(this)) {
108 throw MakeRangeError("invalid_data_view_offset");
109 }
110 return %DataViewGetInt16(this, offset, ToBoolean(littleEndian));
111 }
112
113
114 function DataViewGetUint16(byteOffset, littleEndian) {
115 if (%_ArgumentsLength() < 1) {
116 throw MakeTypeError("missing_data_view_argument");
117 }
118 var offset = TO_INT32(byteOffset);
119 if (offset < 0 || offset + 2 > %DataViewGetByteLength(this)) {
120 throw MakeRangeError("invalid_data_view_offset");
121 }
122 return %DataViewGetUint16(this, offset, ToBoolean(littleEndian));
123 }
124
125
126 function DataViewGetInt32(byteOffset, littleEndian) {
127 if (%_ArgumentsLength() < 1) {
128 throw MakeTypeError("missing_data_view_argument");
129 }
130 var offset = TO_INT32(byteOffset);
131 if (offset < 0 || offset + 4 > %DataViewGetByteLength(this)) {
132 throw MakeRangeError("invalid_data_view_offset");
133 }
134 return %DataViewGetInt32(this, offset, ToBoolean(littleEndian));
135 }
136
137
138 function DataViewGetUint32(byteOffset, littleEndian) {
139 if (%_ArgumentsLength() < 1) {
140 throw MakeTypeError("missing_data_view_argument");
141 }
142 var offset = TO_INT32(byteOffset);
143 if (offset < 0 || offset + 4 > %DataViewGetByteLength(this)) {
144 throw MakeRangeError("invalid_data_view_offset");
145 }
146 return %DataViewGetUint32(this, offset, ToBoolean(littleEndian));
147 }
148
149
150 function DataViewGetFloat32(byteOffset, littleEndian) {
151 if (%_ArgumentsLength() < 1) {
152 throw MakeTypeError("missing_data_view_argument");
153 }
154 var offset = TO_INT32(byteOffset);
155 if (offset < 0 || offset + 4 > %DataViewGetByteLength(this)) {
156 throw MakeRangeError("invalid_data_view_offset");
157 }
158 return %DataViewGetFloat32(this, offset, ToBoolean(littleEndian));
159 }
160
161
162 function DataViewGetFloat64(byteOffset, littleEndian) {
163 if (%_ArgumentsLength() < 1) {
164 throw MakeTypeError("missing_data_view_argument");
165 }
166 var offset = TO_INT32(byteOffset);
167 if (offset < 0 || offset + 8 > %DataViewGetByteLength(this)) {
168 throw MakeRangeError("invalid_data_view_offset");
169 }
170 return %DataViewGetFloat64(this, offset, ToBoolean(littleEndian));
171 }
172
173
174 function DataViewSetInt8(byteOffset, value) {
175 if (%_ArgumentsLength() < 2) {
176 throw MakeTypeError("missing_data_view_argument");
177 }
178 var offset = TO_INT32(byteOffset);
179 if (offset < 0 || offset + 1 > %DataViewGetByteLength(this)) {
180 throw MakeRangeError("invalid_data_view_offset");
181 }
182 return %DataViewSetInt8(this, offset, TO_INT32(value));
183 }
184
185
186 function DataViewSetUint8(byteOffset, value) {
187 if (%_ArgumentsLength() < 2) {
188 throw MakeTypeError("missing_data_view_argument");
189 }
190 var offset = TO_INT32(byteOffset);
191 if (offset < 0 || offset + 1 > %DataViewGetByteLength(this)) {
192 throw MakeRangeError("invalid_data_view_offset");
193 }
194 return %DataViewSetUint8(this, offset, TO_UINT32(value));
195 }
196
197
198 function DataViewSetInt16(byteOffset, value, littleEndian) {
199 if (%_ArgumentsLength() < 2) {
200 throw MakeTypeError("missing_data_view_argument");
201 }
202 var offset = TO_INT32(byteOffset);
203 if (offset < 0 || offset + 2 > %DataViewGetByteLength(this)) {
204 throw MakeRangeError("invalid_data_view_offset");
205 }
206 var le = !IS_UNDEFINED(littleEndian) && ToBoolean(littleEndian);
207 return %DataViewSetInt16(this, offset, TO_INT32(value), le);
208 }
209
210
211 function DataViewSetUint16(byteOffset, value, littleEndian) {
212 if (%_ArgumentsLength() < 2) {
213 throw MakeTypeError("missing_data_view_argument");
214 }
215 var offset = TO_INT32(byteOffset);
216 if (offset < 0 || offset + 2 > %DataViewGetByteLength(this)) {
217 throw MakeRangeError("invalid_data_view_offset");
218 }
219 var le = !IS_UNDEFINED(littleEndian) && ToBoolean(littleEndian);
220 return %DataViewSetUint16(this, offset, TO_UINT32(value), le);
221 }
222
223
224 function DataViewSetInt32(byteOffset, value, littleEndian) {
225 if (%_ArgumentsLength() < 2) {
226 throw MakeTypeError("missing_data_view_argument");
227 }
228 var offset = TO_INT32(byteOffset);
229 if (offset < 0 || offset + 4 > %DataViewGetByteLength(this)) {
230 throw MakeRangeError("invalid_data_view_offset");
231 }
232 var le = !IS_UNDEFINED(littleEndian) && ToBoolean(littleEndian);
233 return %DataViewSetInt32(this, offset, TO_INT32(value), le);
234 }
235
236
237 function DataViewSetUint32(byteOffset, value, littleEndian) {
238 if (%_ArgumentsLength() < 2) {
239 throw MakeTypeError("missing_data_view_argument");
240 }
241 var offset = TO_INT32(byteOffset);
242 if (offset < 0 || offset + 4 > %DataViewGetByteLength(this)) {
243 throw MakeRangeError("invalid_data_view_offset");
244 }
245 var le = !IS_UNDEFINED(littleEndian) && ToBoolean(littleEndian);
246 return %DataViewSetUint32(this, offset, TO_UINT32(value), le);
247 }
248
249
250 function DataViewSetFloat32(byteOffset, value, littleEndian) {
251 if (%_ArgumentsLength() < 2) {
252 throw MakeTypeError("missing_data_view_argument");
253 }
254 var offset = TO_INT32(byteOffset);
255 if (offset < 0 || offset + 4 > %DataViewGetByteLength(this)) {
256 throw MakeRangeError("invalid_data_view_offset");
257 }
258 var le = !IS_UNDEFINED(littleEndian) && ToBoolean(littleEndian);
259 return %DataViewSetFloat32(this, offset, TO_NUMBER_INLINE(value), le);
260 }
261
262
263 function DataViewSetFloat64(byteOffset, value, littleEndian) {
264 if (%_ArgumentsLength() < 2) {
265 throw MakeTypeError("missing_data_view_argument");
266 }
267 var offset = TO_INT32(byteOffset);
268 if (offset < 0 || offset + 8 > %DataViewGetByteLength(this)) {
269 throw MakeRangeError("invalid_data_view_offset");
270 }
271 var le = !IS_UNDEFINED(littleEndian) && ToBoolean(littleEndian);
272 return %DataViewSetFloat64(this, offset, TO_NUMBER_INLINE(value), le);
273 }
274
275
276 function SetUpDataView() {
277 %CheckIsBootstrapping();
278 %SetCode($DataView, DataViewConstructor);
279 %FunctionSetPrototype($DataView, new $Object());
280 %SetProperty($DataView.prototype, "constructor", $DataView, DONT_ENUM);
281 InstallGetter($DataView.prototype, "buffer", DataViewGetBuffer);
282 InstallGetter($DataView.prototype, "byteOffset", DataViewGetByteOffset);
283 InstallGetter($DataView.prototype, "byteLength", DataViewGetByteLength);
284 InstallFunctions($DataView.prototype, DONT_ENUM, $Array(
285 "getInt8", DataViewGetInt8,
286 "getUint8", DataViewGetUint8,
287 "getInt16", DataViewGetInt16,
288 "getUint16", DataViewGetUint16,
289 "getInt32", DataViewGetInt32,
290 "getUint32", DataViewGetUint32,
291 "getFloat32", DataViewGetFloat32,
292 "getFloat64", DataViewGetFloat64,
293 "setInt8", DataViewSetInt8,
294 "setUint8", DataViewSetUint8,
295 "setInt16", DataViewSetInt16,
296 "setUint16", DataViewSetUint16,
297 "setInt32", DataViewSetInt32,
298 "setUint32", DataViewSetUint32,
299 "setFloat32", DataViewSetFloat32,
300 "setFloat64", DataViewSetFloat64
301 ));
302 }
303
304 SetUpDataView();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698