OLD | NEW |
---|---|
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 (function(global, utils) { | 5 (function(global, utils) { |
6 | 6 |
7 "use strict"; | 7 "use strict"; |
8 | 8 |
9 %CheckIsBootstrapping(); | 9 %CheckIsBootstrapping(); |
10 | 10 |
(...skipping 28 matching lines...) Expand all Loading... | |
39 var InnerArrayToLocaleString; | 39 var InnerArrayToLocaleString; |
40 var InternalArray = utils.InternalArray; | 40 var InternalArray = utils.InternalArray; |
41 var IsNaN; | 41 var IsNaN; |
42 var MakeRangeError; | 42 var MakeRangeError; |
43 var MakeTypeError; | 43 var MakeTypeError; |
44 var MaxSimple; | 44 var MaxSimple; |
45 var MinSimple; | 45 var MinSimple; |
46 var PackedArrayReverse; | 46 var PackedArrayReverse; |
47 var SpeciesConstructor; | 47 var SpeciesConstructor; |
48 var ToPositiveInteger; | 48 var ToPositiveInteger; |
49 var ToIndex; | |
49 var iteratorSymbol = utils.ImportNow("iterator_symbol"); | 50 var iteratorSymbol = utils.ImportNow("iterator_symbol"); |
50 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); | 51 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); |
51 | 52 |
52 macro TYPED_ARRAYS(FUNCTION) | 53 macro TYPED_ARRAYS(FUNCTION) |
53 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. | 54 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. |
54 FUNCTION(1, Uint8Array, 1) | 55 FUNCTION(1, Uint8Array, 1) |
55 FUNCTION(2, Int8Array, 1) | 56 FUNCTION(2, Int8Array, 1) |
56 FUNCTION(3, Uint16Array, 2) | 57 FUNCTION(3, Uint16Array, 2) |
57 FUNCTION(4, Int16Array, 2) | 58 FUNCTION(4, Int16Array, 2) |
58 FUNCTION(5, Uint32Array, 4) | 59 FUNCTION(5, Uint32Array, 4) |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
91 InnerArraySort = from.InnerArraySort; | 92 InnerArraySort = from.InnerArraySort; |
92 InnerArrayToLocaleString = from.InnerArrayToLocaleString; | 93 InnerArrayToLocaleString = from.InnerArrayToLocaleString; |
93 IsNaN = from.IsNaN; | 94 IsNaN = from.IsNaN; |
94 MakeRangeError = from.MakeRangeError; | 95 MakeRangeError = from.MakeRangeError; |
95 MakeTypeError = from.MakeTypeError; | 96 MakeTypeError = from.MakeTypeError; |
96 MaxSimple = from.MaxSimple; | 97 MaxSimple = from.MaxSimple; |
97 MinSimple = from.MinSimple; | 98 MinSimple = from.MinSimple; |
98 PackedArrayReverse = from.PackedArrayReverse; | 99 PackedArrayReverse = from.PackedArrayReverse; |
99 SpeciesConstructor = from.SpeciesConstructor; | 100 SpeciesConstructor = from.SpeciesConstructor; |
100 ToPositiveInteger = from.ToPositiveInteger; | 101 ToPositiveInteger = from.ToPositiveInteger; |
102 ToIndex = from.ToIndex; | |
101 }); | 103 }); |
102 | 104 |
103 // --------------- Typed Arrays --------------------- | 105 // --------------- Typed Arrays --------------------- |
104 | 106 |
105 function TypedArrayDefaultConstructor(typedArray) { | 107 function TypedArrayDefaultConstructor(typedArray) { |
106 switch (%_ClassOf(typedArray)) { | 108 switch (%_ClassOf(typedArray)) { |
107 macro TYPED_ARRAY_CONSTRUCTOR_CASE(ARRAY_ID, NAME, ELEMENT_SIZE) | 109 macro TYPED_ARRAY_CONSTRUCTOR_CASE(ARRAY_ID, NAME, ELEMENT_SIZE) |
108 case "NAME": | 110 case "NAME": |
109 return GlobalNAME; | 111 return GlobalNAME; |
110 endmacro | 112 endmacro |
(...skipping 24 matching lines...) Expand all Loading... | |
135 function TypedArraySpeciesCreate(exemplar, arg0, arg1, arg2, conservative) { | 137 function TypedArraySpeciesCreate(exemplar, arg0, arg1, arg2, conservative) { |
136 var defaultConstructor = TypedArrayDefaultConstructor(exemplar); | 138 var defaultConstructor = TypedArrayDefaultConstructor(exemplar); |
137 var constructor = SpeciesConstructor(exemplar, defaultConstructor, | 139 var constructor = SpeciesConstructor(exemplar, defaultConstructor, |
138 conservative); | 140 conservative); |
139 return TypedArrayCreate(constructor, arg0, arg1, arg2); | 141 return TypedArrayCreate(constructor, arg0, arg1, arg2); |
140 } | 142 } |
141 | 143 |
142 macro TYPED_ARRAY_CONSTRUCTOR(ARRAY_ID, NAME, ELEMENT_SIZE) | 144 macro TYPED_ARRAY_CONSTRUCTOR(ARRAY_ID, NAME, ELEMENT_SIZE) |
143 function NAMEConstructByArrayBuffer(obj, buffer, byteOffset, length) { | 145 function NAMEConstructByArrayBuffer(obj, buffer, byteOffset, length) { |
144 if (!IS_UNDEFINED(byteOffset)) { | 146 if (!IS_UNDEFINED(byteOffset)) { |
145 byteOffset = ToPositiveInteger(byteOffset, kInvalidTypedArrayLength); | 147 byteOffset = ToIndex(byteOffset, kInvalidTypedArrayLength); |
146 } | 148 } |
147 if (!IS_UNDEFINED(length)) { | 149 if (!IS_UNDEFINED(length)) { |
148 length = ToPositiveInteger(length, kInvalidTypedArrayLength); | 150 length = ToIndex(length, kInvalidTypedArrayLength); |
151 } | |
152 if (length > %_MaxSmi()) { // note: this is not per spec; we just don't want t o allocate excessively large arrays | |
adamk
2016/06/23 22:04:28
Drive-by comment, here and elsewhere: please limit
| |
153 throw MakeRangeError(kInvalidTypedArrayLength); | |
149 } | 154 } |
150 | 155 |
151 var bufferByteLength = %_ArrayBufferGetByteLength(buffer); | 156 var bufferByteLength = %_ArrayBufferGetByteLength(buffer); |
152 var offset; | 157 var offset; |
153 if (IS_UNDEFINED(byteOffset)) { | 158 if (IS_UNDEFINED(byteOffset)) { |
154 offset = 0; | 159 offset = 0; |
155 } else { | 160 } else { |
156 offset = byteOffset; | 161 offset = byteOffset; |
157 | 162 |
158 if (offset % ELEMENT_SIZE !== 0) { | 163 if (offset % ELEMENT_SIZE !== 0) { |
159 throw MakeRangeError(kInvalidTypedArrayAlignment, | 164 throw MakeRangeError(kInvalidTypedArrayAlignment, |
160 "start offset", "NAME", ELEMENT_SIZE); | 165 "start offset", "NAME", ELEMENT_SIZE); |
161 } | 166 } |
162 if (offset > bufferByteLength) { | |
163 throw MakeRangeError(kInvalidTypedArrayOffset); | |
164 } | |
165 } | 167 } |
166 | 168 |
167 var newByteLength; | 169 var newByteLength; |
168 var newLength; | |
169 if (IS_UNDEFINED(length)) { | 170 if (IS_UNDEFINED(length)) { |
170 if (bufferByteLength % ELEMENT_SIZE !== 0) { | 171 if (bufferByteLength % ELEMENT_SIZE !== 0) { |
171 throw MakeRangeError(kInvalidTypedArrayAlignment, | 172 throw MakeRangeError(kInvalidTypedArrayAlignment, |
172 "byte length", "NAME", ELEMENT_SIZE); | 173 "byte length", "NAME", ELEMENT_SIZE); |
173 } | 174 } |
174 newByteLength = bufferByteLength - offset; | 175 newByteLength = bufferByteLength - offset; |
175 newLength = newByteLength / ELEMENT_SIZE; | 176 if (newByteLength < 0) { |
177 throw MakeRangeError(kInvalidTypedArrayAlignment, | |
178 "byte length", "NAME", ELEMENT_SIZE); | |
179 } | |
176 } else { | 180 } else { |
177 var newLength = length; | 181 newByteLength = length * ELEMENT_SIZE; |
178 newByteLength = newLength * ELEMENT_SIZE; | 182 if (offset + newByteLength > bufferByteLength) { |
179 } | 183 throw MakeRangeError(kInvalidTypedArrayAlignment, |
180 if ((offset + newByteLength > bufferByteLength) | 184 "byte length", "NAME", ELEMENT_SIZE); |
181 || (newLength > %_MaxSmi())) { | 185 } |
182 throw MakeRangeError(kInvalidTypedArrayLength); | |
183 } | 186 } |
184 %_TypedArrayInitialize(obj, ARRAY_ID, buffer, offset, newByteLength, true); | 187 %_TypedArrayInitialize(obj, ARRAY_ID, buffer, offset, newByteLength, true); |
185 } | 188 } |
186 | 189 |
187 function NAMEConstructByLength(obj, length) { | 190 function NAMEConstructByLength(obj, length) { |
188 var l = IS_UNDEFINED(length) ? | 191 var l = IS_UNDEFINED(length) ? |
189 0 : ToPositiveInteger(length, kInvalidTypedArrayLength); | 192 0 : ToIndex(length, kInvalidTypedArrayLength); |
190 if (l > %_MaxSmi()) { | 193 if (length > %_MaxSmi()) { // note: this is not per spec; we just don't want t o allocate excessively large arrays |
191 throw MakeRangeError(kInvalidTypedArrayLength); | 194 throw MakeRangeError(kInvalidTypedArrayLength); |
192 } | 195 } |
193 var byteLength = l * ELEMENT_SIZE; | 196 var byteLength = l * ELEMENT_SIZE; |
194 if (byteLength > %_TypedArrayMaxSizeInHeap()) { | 197 if (byteLength > %_TypedArrayMaxSizeInHeap()) { |
195 var buffer = new GlobalArrayBuffer(byteLength); | 198 var buffer = new GlobalArrayBuffer(byteLength); |
196 %_TypedArrayInitialize(obj, ARRAY_ID, buffer, 0, byteLength, true); | 199 %_TypedArrayInitialize(obj, ARRAY_ID, buffer, 0, byteLength, true); |
197 } else { | 200 } else { |
198 %_TypedArrayInitialize(obj, ARRAY_ID, null, 0, byteLength, true); | 201 %_TypedArrayInitialize(obj, ARRAY_ID, null, 0, byteLength, true); |
199 } | 202 } |
200 } | 203 } |
(...skipping 651 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
852 FUNCTION(Float64) | 855 FUNCTION(Float64) |
853 endmacro | 856 endmacro |
854 | 857 |
855 | 858 |
856 macro DATA_VIEW_GETTER_SETTER(TYPENAME) | 859 macro DATA_VIEW_GETTER_SETTER(TYPENAME) |
857 function DataViewGetTYPENAMEJS(offset, little_endian) { | 860 function DataViewGetTYPENAMEJS(offset, little_endian) { |
858 if (!IS_DATAVIEW(this)) { | 861 if (!IS_DATAVIEW(this)) { |
859 throw MakeTypeError(kIncompatibleMethodReceiver, | 862 throw MakeTypeError(kIncompatibleMethodReceiver, |
860 'DataView.getTYPENAME', this); | 863 'DataView.getTYPENAME', this); |
861 } | 864 } |
862 if (arguments.length < 1) throw MakeTypeError(kInvalidArgument); | 865 offset = IS_UNDEFINED(offset) ? 0 : ToIndex(offset, kInvalidDataViewAccessorOf fset); |
863 offset = ToPositiveInteger(offset, kInvalidDataViewAccessorOffset); | |
864 return %DataViewGetTYPENAME(this, offset, !!little_endian); | 866 return %DataViewGetTYPENAME(this, offset, !!little_endian); |
865 } | 867 } |
866 %FunctionSetLength(DataViewGetTYPENAMEJS, 1); | 868 %FunctionSetLength(DataViewGetTYPENAMEJS, 1); |
867 | 869 |
868 function DataViewSetTYPENAMEJS(offset, value, little_endian) { | 870 function DataViewSetTYPENAMEJS(offset, value, little_endian) { |
869 if (!IS_DATAVIEW(this)) { | 871 if (!IS_DATAVIEW(this)) { |
870 throw MakeTypeError(kIncompatibleMethodReceiver, | 872 throw MakeTypeError(kIncompatibleMethodReceiver, |
871 'DataView.setTYPENAME', this); | 873 'DataView.setTYPENAME', this); |
872 } | 874 } |
873 if (arguments.length < 2) throw MakeTypeError(kInvalidArgument); | 875 offset = IS_UNDEFINED(offset) ? 0 : ToIndex(offset, kInvalidDataViewAccessorOf fset); |
874 offset = ToPositiveInteger(offset, kInvalidDataViewAccessorOffset); | |
875 %DataViewSetTYPENAME(this, offset, TO_NUMBER(value), !!little_endian); | 876 %DataViewSetTYPENAME(this, offset, TO_NUMBER(value), !!little_endian); |
876 } | 877 } |
877 %FunctionSetLength(DataViewSetTYPENAMEJS, 2); | 878 %FunctionSetLength(DataViewSetTYPENAMEJS, 2); |
878 endmacro | 879 endmacro |
879 | 880 |
880 DATA_VIEW_TYPES(DATA_VIEW_GETTER_SETTER) | 881 DATA_VIEW_TYPES(DATA_VIEW_GETTER_SETTER) |
881 | 882 |
882 utils.InstallFunctions(GlobalDataView.prototype, DONT_ENUM, [ | 883 utils.InstallFunctions(GlobalDataView.prototype, DONT_ENUM, [ |
883 "getInt8", DataViewGetInt8JS, | 884 "getInt8", DataViewGetInt8JS, |
884 "setInt8", DataViewSetInt8JS, | 885 "setInt8", DataViewSetInt8JS, |
(...skipping 14 matching lines...) Expand all Loading... | |
899 "setUint32", DataViewSetUint32JS, | 900 "setUint32", DataViewSetUint32JS, |
900 | 901 |
901 "getFloat32", DataViewGetFloat32JS, | 902 "getFloat32", DataViewGetFloat32JS, |
902 "setFloat32", DataViewSetFloat32JS, | 903 "setFloat32", DataViewSetFloat32JS, |
903 | 904 |
904 "getFloat64", DataViewGetFloat64JS, | 905 "getFloat64", DataViewGetFloat64JS, |
905 "setFloat64", DataViewSetFloat64JS | 906 "setFloat64", DataViewSetFloat64JS |
906 ]); | 907 ]); |
907 | 908 |
908 }) | 909 }) |
OLD | NEW |