OLD | NEW |
---|---|
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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
75 } | 75 } |
76 | 76 |
77 var newLen = fin - first; | 77 var newLen = fin - first; |
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 --------------------- | |
86 | |
87 function CreateTypedArrayConstructor(name, elementSize, arrayId, proto) { | |
rossberg
2013/04/15 13:11:03
The 'proto' argument is misnamed: it's not the pro
Dmitry Lomov (no reviews)
2013/04/15 15:40:58
Done.
| |
88 return function (buffer, byteOffset, length) { | |
89 if (%_IsConstructCall()) { | |
90 if (!IS_ARRAYBUFFER(buffer)) { | |
91 throw MakeTypeError("Type error!"); | |
92 } | |
93 var offset; | |
94 if (IS_UNDEFINED(byteOffset)) { | |
rossberg
2013/04/15 13:11:03
You can use ?: here
Dmitry Lomov (no reviews)
2013/04/15 15:40:58
Done.
| |
95 offset = 0; | |
96 } else { | |
97 offset = TO_POSITIVE_INTEGER(byteOffset); | |
98 } | |
99 if (offset % elementSize !== 0) { | |
100 throw MakeRangeError("invalid_typed_array_alignment", | |
101 "start offset", name, elementSize); | |
102 } | |
103 var bufferByteLength = %ArrayBufferGetByteLength(buffer); | |
104 if (offset >= bufferByteLength) { | |
105 throw MakeRangeError("invalid_typed_array_offset"); | |
106 } | |
107 | |
108 var newByteLength; | |
109 var newLength; | |
110 if (IS_UNDEFINED(length)) { | |
111 if (bufferByteLength % elementSize !== 0) { | |
112 throw MakeRangeError("invalid_typed_array_alignment", | |
113 "byte length", name, elementSize); | |
114 } | |
115 newByteLength = bufferByteLength - offset; | |
116 newLength = newByteLength / elementSize; | |
117 } else { | |
118 var newLength = TO_POSITIVE_INTEGER(length); | |
119 newByteLength = newLength * elementSize; | |
120 } | |
121 if (newByteLength > bufferByteLength) { | |
122 throw MakeRangeError("invalid_typed_array_length"); | |
123 } | |
124 %TypedArrayInitialize(this, arrayId, buffer, offset, newLength, newByteLen gth); | |
rossberg
2013/04/15 13:11:03
Line too long
Dmitry Lomov (no reviews)
2013/04/15 15:40:58
Done.
| |
125 } else { | |
126 return new proto(buffer, byteOffset, length); | |
127 } | |
128 } | |
129 } | |
130 | |
131 function TypedArrayGetBuffer() { | |
132 return %TypedArrayGetBuffer(this); | |
133 } | |
134 | |
135 function TypedArrayGetByteLength() { | |
136 return %TypedArrayGetByteLength(this); | |
137 } | |
138 | |
139 function TypedArrayGetByteOffset() { | |
140 return %TypedArrayGetByteOffset(this); | |
141 } | |
142 | |
143 function TypedArrayGetLength() { | |
144 return %TypedArrayGetLength(this); | |
145 } | |
146 | |
147 | |
148 var typedArrayDescs = | |
149 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. | |
150 [{name:"Uint8Array", | |
rossberg
2013/04/15 13:11:03
Style: put in a space after ':'
| |
151 protoObject:global.__Uint8Array, | |
rossberg
2013/04/15 13:11:03
s/protoObject/constructor/g
| |
152 arrayId:1, | |
153 elementSize:1}, | |
154 | |
155 {name:"Int8Array", | |
156 protoObject:global.__Int8Array, | |
157 arrayId:2, | |
158 elementSize:1}, | |
159 | |
160 {name:"Uint16Array", | |
161 protoObject:global.__Uint16Array, | |
162 arrayId:3, | |
163 elementSize:2}, | |
164 | |
165 {name:"Int16Array", | |
166 protoObject:global.__Int16Array, | |
167 arrayId:4, | |
168 elementSize:2}, | |
169 | |
170 {name:"Uint32Array", | |
171 protoObject:global.__Uint32Array, | |
172 arrayId:5, | |
173 elementSize:4}, | |
174 | |
175 {name:"Int32Array", | |
176 protoObject:global.__Int32Array, | |
177 arrayId:6, | |
178 elementSize:4}, | |
179 | |
180 {name:"Float32Array", | |
181 protoObject:global.__Float32Array, | |
182 arrayId:7, | |
183 elementSize:4}, | |
184 | |
185 {name:"Float64Array", | |
186 protoObject:global.__Float64Array, | |
187 arrayId:8, | |
188 elementSize:8} | |
189 ]; | |
85 | 190 |
86 // ------------------------------------------------------------------- | 191 // ------------------------------------------------------------------- |
87 | 192 |
88 function SetUpArrayBuffer() { | 193 function SetUpArrayBuffer() { |
89 %CheckIsBootstrapping(); | 194 %CheckIsBootstrapping(); |
90 | 195 |
91 // Set up the Uint16Array constructor function. | 196 // Set up the ArrayBuffer constructor function. |
92 %SetCode($ArrayBuffer, ArrayBufferConstructor); | 197 %SetCode($ArrayBuffer, ArrayBufferConstructor); |
198 %FunctionSetPrototype($ArrayBuffer, new $Object()); | |
93 | 199 |
94 // Set up the constructor property on the ArrayBuffer prototype object. | 200 // Set up the constructor property on the ArrayBuffer prototype object. |
95 %SetProperty($ArrayBuffer.prototype, "constructor", $ArrayBuffer, DONT_ENUM); | 201 %SetProperty($ArrayBuffer.prototype, "constructor", $ArrayBuffer, DONT_ENUM); |
96 | 202 |
97 InstallGetter($ArrayBuffer.prototype, "byteLength", ArrayBufferGetByteLength); | 203 InstallGetter($ArrayBuffer.prototype, "byteLength", ArrayBufferGetByteLength); |
98 | 204 |
99 InstallFunctions($ArrayBuffer.prototype, DONT_ENUM, $Array( | 205 InstallFunctions($ArrayBuffer.prototype, DONT_ENUM, $Array( |
100 "slice", ArrayBufferSlice | 206 "slice", ArrayBufferSlice |
101 )); | 207 )); |
208 for (var i = 0; i < typedArrayDescs.length; i++) { | |
rossberg
2013/04/15 13:11:03
Hm, I'd prefer separating initialization of thyped
Dmitry Lomov (no reviews)
2013/04/15 15:40:58
Done.
| |
209 var desc = typedArrayDescs[i]; | |
210 var f = CreateTypedArrayConstructor(desc.name, desc.elementSize, | |
211 desc.arrayId, desc.protoObject); | |
212 %SetCode(desc.protoObject, f); | |
213 %FunctionSetPrototype(desc.protoObject, new $Object()); | |
214 | |
215 %SetProperty(desc.protoObject.prototype, | |
216 "constructor", desc.protoObject, DONT_ENUM); | |
217 %SetProperty(desc.protoObject.prototype, | |
218 "BYTES_PER_ELEMENT", desc.elementSize, | |
219 READ_ONLY | DONT_ENUM | DONT_DELETE); | |
220 InstallGetter(desc.protoObject.prototype, "buffer", TypedArrayGetBuffer); | |
221 InstallGetter(desc.protoObject.prototype, "byteOffset", TypedArrayGetByteOff set); | |
rossberg
2013/04/15 13:11:03
Line too long (and below)
Dmitry Lomov (no reviews)
2013/04/15 15:40:58
Done.
| |
222 InstallGetter(desc.protoObject.prototype, "byteLength", TypedArrayGetByteLen gth); | |
223 InstallGetter(desc.protoObject.prototype, "length", TypedArrayGetLength); | |
224 } | |
102 } | 225 } |
103 | 226 |
104 SetUpArrayBuffer(); | 227 SetUpArrayBuffer(); |
OLD | NEW |