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 speciesSymbol = utils.ImportNow("species_symbol"); | 51 var speciesSymbol = utils.ImportNow("species_symbol"); |
51 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); | 52 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); |
52 | 53 |
53 macro TYPED_ARRAYS(FUNCTION) | 54 macro TYPED_ARRAYS(FUNCTION) |
54 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. | 55 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. |
55 FUNCTION(1, Uint8Array, 1) | 56 FUNCTION(1, Uint8Array, 1) |
56 FUNCTION(2, Int8Array, 1) | 57 FUNCTION(2, Int8Array, 1) |
57 FUNCTION(3, Uint16Array, 2) | 58 FUNCTION(3, Uint16Array, 2) |
58 FUNCTION(4, Int16Array, 2) | 59 FUNCTION(4, Int16Array, 2) |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 InnerArraySort = from.InnerArraySort; | 93 InnerArraySort = from.InnerArraySort; |
93 InnerArrayToLocaleString = from.InnerArrayToLocaleString; | 94 InnerArrayToLocaleString = from.InnerArrayToLocaleString; |
94 IsNaN = from.IsNaN; | 95 IsNaN = from.IsNaN; |
95 MakeRangeError = from.MakeRangeError; | 96 MakeRangeError = from.MakeRangeError; |
96 MakeTypeError = from.MakeTypeError; | 97 MakeTypeError = from.MakeTypeError; |
97 MaxSimple = from.MaxSimple; | 98 MaxSimple = from.MaxSimple; |
98 MinSimple = from.MinSimple; | 99 MinSimple = from.MinSimple; |
99 PackedArrayReverse = from.PackedArrayReverse; | 100 PackedArrayReverse = from.PackedArrayReverse; |
100 SpeciesConstructor = from.SpeciesConstructor; | 101 SpeciesConstructor = from.SpeciesConstructor; |
101 ToPositiveInteger = from.ToPositiveInteger; | 102 ToPositiveInteger = from.ToPositiveInteger; |
103 ToIndex = from.ToIndex; | |
102 }); | 104 }); |
103 | 105 |
104 // --------------- Typed Arrays --------------------- | 106 // --------------- Typed Arrays --------------------- |
105 | 107 |
106 function TypedArrayDefaultConstructor(typedArray) { | 108 function TypedArrayDefaultConstructor(typedArray) { |
107 switch (%_ClassOf(typedArray)) { | 109 switch (%_ClassOf(typedArray)) { |
108 macro TYPED_ARRAY_CONSTRUCTOR_CASE(ARRAY_ID, NAME, ELEMENT_SIZE) | 110 macro TYPED_ARRAY_CONSTRUCTOR_CASE(ARRAY_ID, NAME, ELEMENT_SIZE) |
109 case "NAME": | 111 case "NAME": |
110 return GlobalNAME; | 112 return GlobalNAME; |
111 endmacro | 113 endmacro |
(...skipping 24 matching lines...) Expand all Loading... | |
136 function TypedArraySpeciesCreate(exemplar, arg0, arg1, arg2, conservative) { | 138 function TypedArraySpeciesCreate(exemplar, arg0, arg1, arg2, conservative) { |
137 var defaultConstructor = TypedArrayDefaultConstructor(exemplar); | 139 var defaultConstructor = TypedArrayDefaultConstructor(exemplar); |
138 var constructor = SpeciesConstructor(exemplar, defaultConstructor, | 140 var constructor = SpeciesConstructor(exemplar, defaultConstructor, |
139 conservative); | 141 conservative); |
140 return TypedArrayCreate(constructor, arg0, arg1, arg2); | 142 return TypedArrayCreate(constructor, arg0, arg1, arg2); |
141 } | 143 } |
142 | 144 |
143 macro TYPED_ARRAY_CONSTRUCTOR(ARRAY_ID, NAME, ELEMENT_SIZE) | 145 macro TYPED_ARRAY_CONSTRUCTOR(ARRAY_ID, NAME, ELEMENT_SIZE) |
144 function NAMEConstructByArrayBuffer(obj, buffer, byteOffset, length) { | 146 function NAMEConstructByArrayBuffer(obj, buffer, byteOffset, length) { |
145 if (!IS_UNDEFINED(byteOffset)) { | 147 if (!IS_UNDEFINED(byteOffset)) { |
146 byteOffset = ToPositiveInteger(byteOffset, kInvalidTypedArrayLength); | 148 byteOffset = ToIndex(byteOffset, kInvalidTypedArrayLength); |
147 } | 149 } |
148 if (!IS_UNDEFINED(length)) { | 150 if (!IS_UNDEFINED(length)) { |
149 length = ToPositiveInteger(length, kInvalidTypedArrayLength); | 151 length = ToIndex(length, kInvalidTypedArrayLength); |
152 } | |
153 if (length > %_MaxSmi()) { // note: this is not per spec; we just don't want t o allocate excessively large arrays | |
Dan Ehrenberg
2016/06/29 19:13:43
Line length, also see note below
| |
154 throw MakeRangeError(kInvalidTypedArrayLength); | |
150 } | 155 } |
151 | 156 |
152 var bufferByteLength = %_ArrayBufferGetByteLength(buffer); | 157 var bufferByteLength = %_ArrayBufferGetByteLength(buffer); |
153 var offset; | 158 var offset; |
154 if (IS_UNDEFINED(byteOffset)) { | 159 if (IS_UNDEFINED(byteOffset)) { |
155 offset = 0; | 160 offset = 0; |
156 } else { | 161 } else { |
157 offset = byteOffset; | 162 offset = byteOffset; |
158 | 163 |
159 if (offset % ELEMENT_SIZE !== 0) { | 164 if (offset % ELEMENT_SIZE !== 0) { |
160 throw MakeRangeError(kInvalidTypedArrayAlignment, | 165 throw MakeRangeError(kInvalidTypedArrayAlignment, |
161 "start offset", "NAME", ELEMENT_SIZE); | 166 "start offset", "NAME", ELEMENT_SIZE); |
162 } | 167 } |
163 if (offset > bufferByteLength) { | |
164 throw MakeRangeError(kInvalidTypedArrayOffset); | |
165 } | |
166 } | 168 } |
167 | 169 |
168 var newByteLength; | 170 var newByteLength; |
169 var newLength; | |
170 if (IS_UNDEFINED(length)) { | 171 if (IS_UNDEFINED(length)) { |
171 if (bufferByteLength % ELEMENT_SIZE !== 0) { | 172 if (bufferByteLength % ELEMENT_SIZE !== 0) { |
172 throw MakeRangeError(kInvalidTypedArrayAlignment, | 173 throw MakeRangeError(kInvalidTypedArrayAlignment, |
173 "byte length", "NAME", ELEMENT_SIZE); | 174 "byte length", "NAME", ELEMENT_SIZE); |
174 } | 175 } |
175 newByteLength = bufferByteLength - offset; | 176 newByteLength = bufferByteLength - offset; |
176 newLength = newByteLength / ELEMENT_SIZE; | 177 if (newByteLength < 0) { |
178 throw MakeRangeError(kInvalidTypedArrayAlignment, | |
179 "byte length", "NAME", ELEMENT_SIZE); | |
180 } | |
177 } else { | 181 } else { |
178 var newLength = length; | 182 newByteLength = length * ELEMENT_SIZE; |
179 newByteLength = newLength * ELEMENT_SIZE; | 183 if (offset + newByteLength > bufferByteLength) { |
180 } | 184 throw MakeRangeError(kInvalidTypedArrayAlignment, |
181 if ((offset + newByteLength > bufferByteLength) | 185 "byte length", "NAME", ELEMENT_SIZE); |
182 || (newLength > %_MaxSmi())) { | 186 } |
183 throw MakeRangeError(kInvalidTypedArrayLength); | |
184 } | 187 } |
185 %_TypedArrayInitialize(obj, ARRAY_ID, buffer, offset, newByteLength, true); | 188 %_TypedArrayInitialize(obj, ARRAY_ID, buffer, offset, newByteLength, true); |
186 } | 189 } |
187 | 190 |
188 function NAMEConstructByLength(obj, length) { | 191 function NAMEConstructByLength(obj, length) { |
189 var l = IS_UNDEFINED(length) ? | 192 var l = IS_UNDEFINED(length) ? |
190 0 : ToPositiveInteger(length, kInvalidTypedArrayLength); | 193 0 : ToIndex(length, kInvalidTypedArrayLength); |
191 if (l > %_MaxSmi()) { | 194 if (length > %_MaxSmi()) { // note: this is not per spec; we just don't want t o allocate excessively large arrays |
Dan Ehrenberg
2016/06/29 19:13:43
Line length.
Also, "we don't want to" is "we are
bakkot
2016/06/29 19:25:09
Addressed.
| |
192 throw MakeRangeError(kInvalidTypedArrayLength); | 195 throw MakeRangeError(kInvalidTypedArrayLength); |
193 } | 196 } |
194 var byteLength = l * ELEMENT_SIZE; | 197 var byteLength = l * ELEMENT_SIZE; |
195 if (byteLength > %_TypedArrayMaxSizeInHeap()) { | 198 if (byteLength > %_TypedArrayMaxSizeInHeap()) { |
196 var buffer = new GlobalArrayBuffer(byteLength); | 199 var buffer = new GlobalArrayBuffer(byteLength); |
197 %_TypedArrayInitialize(obj, ARRAY_ID, buffer, 0, byteLength, true); | 200 %_TypedArrayInitialize(obj, ARRAY_ID, buffer, 0, byteLength, true); |
198 } else { | 201 } else { |
199 %_TypedArrayInitialize(obj, ARRAY_ID, null, 0, byteLength, true); | 202 %_TypedArrayInitialize(obj, ARRAY_ID, null, 0, byteLength, true); |
200 } | 203 } |
201 } | 204 } |
(...skipping 657 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
859 FUNCTION(Float64) | 862 FUNCTION(Float64) |
860 endmacro | 863 endmacro |
861 | 864 |
862 | 865 |
863 macro DATA_VIEW_GETTER_SETTER(TYPENAME) | 866 macro DATA_VIEW_GETTER_SETTER(TYPENAME) |
864 function DataViewGetTYPENAMEJS(offset, little_endian) { | 867 function DataViewGetTYPENAMEJS(offset, little_endian) { |
865 if (!IS_DATAVIEW(this)) { | 868 if (!IS_DATAVIEW(this)) { |
866 throw MakeTypeError(kIncompatibleMethodReceiver, | 869 throw MakeTypeError(kIncompatibleMethodReceiver, |
867 'DataView.getTYPENAME', this); | 870 'DataView.getTYPENAME', this); |
868 } | 871 } |
869 if (arguments.length < 1) throw MakeTypeError(kInvalidArgument); | 872 offset = IS_UNDEFINED(offset) ? 0 : ToIndex(offset, kInvalidDataViewAccessorOf fset); |
870 offset = ToPositiveInteger(offset, kInvalidDataViewAccessorOffset); | |
871 return %DataViewGetTYPENAME(this, offset, !!little_endian); | 873 return %DataViewGetTYPENAME(this, offset, !!little_endian); |
872 } | 874 } |
873 %FunctionSetLength(DataViewGetTYPENAMEJS, 1); | 875 %FunctionSetLength(DataViewGetTYPENAMEJS, 1); |
874 | 876 |
875 function DataViewSetTYPENAMEJS(offset, value, little_endian) { | 877 function DataViewSetTYPENAMEJS(offset, value, little_endian) { |
876 if (!IS_DATAVIEW(this)) { | 878 if (!IS_DATAVIEW(this)) { |
877 throw MakeTypeError(kIncompatibleMethodReceiver, | 879 throw MakeTypeError(kIncompatibleMethodReceiver, |
878 'DataView.setTYPENAME', this); | 880 'DataView.setTYPENAME', this); |
879 } | 881 } |
880 if (arguments.length < 2) throw MakeTypeError(kInvalidArgument); | 882 offset = IS_UNDEFINED(offset) ? 0 : ToIndex(offset, kInvalidDataViewAccessorOf fset); |
881 offset = ToPositiveInteger(offset, kInvalidDataViewAccessorOffset); | |
882 %DataViewSetTYPENAME(this, offset, TO_NUMBER(value), !!little_endian); | 883 %DataViewSetTYPENAME(this, offset, TO_NUMBER(value), !!little_endian); |
883 } | 884 } |
884 %FunctionSetLength(DataViewSetTYPENAMEJS, 2); | 885 %FunctionSetLength(DataViewSetTYPENAMEJS, 2); |
885 endmacro | 886 endmacro |
886 | 887 |
887 DATA_VIEW_TYPES(DATA_VIEW_GETTER_SETTER) | 888 DATA_VIEW_TYPES(DATA_VIEW_GETTER_SETTER) |
888 | 889 |
889 utils.InstallFunctions(GlobalDataView.prototype, DONT_ENUM, [ | 890 utils.InstallFunctions(GlobalDataView.prototype, DONT_ENUM, [ |
890 "getInt8", DataViewGetInt8JS, | 891 "getInt8", DataViewGetInt8JS, |
891 "setInt8", DataViewSetInt8JS, | 892 "setInt8", DataViewSetInt8JS, |
(...skipping 14 matching lines...) Expand all Loading... | |
906 "setUint32", DataViewSetUint32JS, | 907 "setUint32", DataViewSetUint32JS, |
907 | 908 |
908 "getFloat32", DataViewGetFloat32JS, | 909 "getFloat32", DataViewGetFloat32JS, |
909 "setFloat32", DataViewSetFloat32JS, | 910 "setFloat32", DataViewSetFloat32JS, |
910 | 911 |
911 "getFloat64", DataViewGetFloat64JS, | 912 "getFloat64", DataViewGetFloat64JS, |
912 "setFloat64", DataViewSetFloat64JS | 913 "setFloat64", DataViewSetFloat64JS |
913 ]); | 914 ]); |
914 | 915 |
915 }) | 916 }) |
OLD | NEW |