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 23 matching lines...) Expand all Loading... |
34 var InnerArraySome; | 34 var InnerArraySome; |
35 var InnerArraySort; | 35 var InnerArraySort; |
36 var InnerArrayToLocaleString; | 36 var InnerArrayToLocaleString; |
37 var InternalArray = utils.InternalArray; | 37 var InternalArray = utils.InternalArray; |
38 var IsNaN; | 38 var IsNaN; |
39 var MaxSimple; | 39 var MaxSimple; |
40 var MinSimple; | 40 var MinSimple; |
41 var PackedArrayReverse; | 41 var PackedArrayReverse; |
42 var SpeciesConstructor; | 42 var SpeciesConstructor; |
43 var ToPositiveInteger; | 43 var ToPositiveInteger; |
| 44 var ToIndex; |
44 var iteratorSymbol = utils.ImportNow("iterator_symbol"); | 45 var iteratorSymbol = utils.ImportNow("iterator_symbol"); |
45 var speciesSymbol = utils.ImportNow("species_symbol"); | 46 var speciesSymbol = utils.ImportNow("species_symbol"); |
46 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); | 47 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); |
47 | 48 |
48 macro TYPED_ARRAYS(FUNCTION) | 49 macro TYPED_ARRAYS(FUNCTION) |
49 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. | 50 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. |
50 FUNCTION(1, Uint8Array, 1) | 51 FUNCTION(1, Uint8Array, 1) |
51 FUNCTION(2, Int8Array, 1) | 52 FUNCTION(2, Int8Array, 1) |
52 FUNCTION(3, Uint16Array, 2) | 53 FUNCTION(3, Uint16Array, 2) |
53 FUNCTION(4, Int16Array, 2) | 54 FUNCTION(4, Int16Array, 2) |
(...skipping 28 matching lines...) Expand all Loading... |
82 InnerArrayReduceRight = from.InnerArrayReduceRight; | 83 InnerArrayReduceRight = from.InnerArrayReduceRight; |
83 InnerArraySome = from.InnerArraySome; | 84 InnerArraySome = from.InnerArraySome; |
84 InnerArraySort = from.InnerArraySort; | 85 InnerArraySort = from.InnerArraySort; |
85 InnerArrayToLocaleString = from.InnerArrayToLocaleString; | 86 InnerArrayToLocaleString = from.InnerArrayToLocaleString; |
86 IsNaN = from.IsNaN; | 87 IsNaN = from.IsNaN; |
87 MaxSimple = from.MaxSimple; | 88 MaxSimple = from.MaxSimple; |
88 MinSimple = from.MinSimple; | 89 MinSimple = from.MinSimple; |
89 PackedArrayReverse = from.PackedArrayReverse; | 90 PackedArrayReverse = from.PackedArrayReverse; |
90 SpeciesConstructor = from.SpeciesConstructor; | 91 SpeciesConstructor = from.SpeciesConstructor; |
91 ToPositiveInteger = from.ToPositiveInteger; | 92 ToPositiveInteger = from.ToPositiveInteger; |
| 93 ToIndex = from.ToIndex; |
92 }); | 94 }); |
93 | 95 |
94 // --------------- Typed Arrays --------------------- | 96 // --------------- Typed Arrays --------------------- |
95 | 97 |
96 function TypedArrayDefaultConstructor(typedArray) { | 98 function TypedArrayDefaultConstructor(typedArray) { |
97 switch (%_ClassOf(typedArray)) { | 99 switch (%_ClassOf(typedArray)) { |
98 macro TYPED_ARRAY_CONSTRUCTOR_CASE(ARRAY_ID, NAME, ELEMENT_SIZE) | 100 macro TYPED_ARRAY_CONSTRUCTOR_CASE(ARRAY_ID, NAME, ELEMENT_SIZE) |
99 case "NAME": | 101 case "NAME": |
100 return GlobalNAME; | 102 return GlobalNAME; |
101 endmacro | 103 endmacro |
(...skipping 24 matching lines...) Expand all Loading... |
126 function TypedArraySpeciesCreate(exemplar, arg0, arg1, arg2, conservative) { | 128 function TypedArraySpeciesCreate(exemplar, arg0, arg1, arg2, conservative) { |
127 var defaultConstructor = TypedArrayDefaultConstructor(exemplar); | 129 var defaultConstructor = TypedArrayDefaultConstructor(exemplar); |
128 var constructor = SpeciesConstructor(exemplar, defaultConstructor, | 130 var constructor = SpeciesConstructor(exemplar, defaultConstructor, |
129 conservative); | 131 conservative); |
130 return TypedArrayCreate(constructor, arg0, arg1, arg2); | 132 return TypedArrayCreate(constructor, arg0, arg1, arg2); |
131 } | 133 } |
132 | 134 |
133 macro TYPED_ARRAY_CONSTRUCTOR(ARRAY_ID, NAME, ELEMENT_SIZE) | 135 macro TYPED_ARRAY_CONSTRUCTOR(ARRAY_ID, NAME, ELEMENT_SIZE) |
134 function NAMEConstructByArrayBuffer(obj, buffer, byteOffset, length) { | 136 function NAMEConstructByArrayBuffer(obj, buffer, byteOffset, length) { |
135 if (!IS_UNDEFINED(byteOffset)) { | 137 if (!IS_UNDEFINED(byteOffset)) { |
136 byteOffset = ToPositiveInteger(byteOffset, kInvalidTypedArrayLength); | 138 byteOffset = ToIndex(byteOffset, kInvalidTypedArrayLength); |
137 } | 139 } |
138 if (!IS_UNDEFINED(length)) { | 140 if (!IS_UNDEFINED(length)) { |
139 length = ToPositiveInteger(length, kInvalidTypedArrayLength); | 141 length = ToIndex(length, kInvalidTypedArrayLength); |
| 142 } |
| 143 if (length > %_MaxSmi()) { |
| 144 // Note: this is not per spec, but rather a constraint of our current |
| 145 // representation (which uses smi's). |
| 146 throw %make_range_error(kInvalidTypedArrayLength); |
140 } | 147 } |
141 | 148 |
142 var bufferByteLength = %_ArrayBufferGetByteLength(buffer); | 149 var bufferByteLength = %_ArrayBufferGetByteLength(buffer); |
143 var offset; | 150 var offset; |
144 if (IS_UNDEFINED(byteOffset)) { | 151 if (IS_UNDEFINED(byteOffset)) { |
145 offset = 0; | 152 offset = 0; |
146 } else { | 153 } else { |
147 offset = byteOffset; | 154 offset = byteOffset; |
148 | 155 |
149 if (offset % ELEMENT_SIZE !== 0) { | 156 if (offset % ELEMENT_SIZE !== 0) { |
150 throw %make_range_error(kInvalidTypedArrayAlignment, | 157 throw %make_range_error(kInvalidTypedArrayAlignment, |
151 "start offset", "NAME", ELEMENT_SIZE); | 158 "start offset", "NAME", ELEMENT_SIZE); |
152 } | 159 } |
153 if (offset > bufferByteLength) { | |
154 throw %make_range_error(kInvalidTypedArrayOffset); | |
155 } | |
156 } | 160 } |
157 | 161 |
158 var newByteLength; | 162 var newByteLength; |
159 var newLength; | |
160 if (IS_UNDEFINED(length)) { | 163 if (IS_UNDEFINED(length)) { |
161 if (bufferByteLength % ELEMENT_SIZE !== 0) { | 164 if (bufferByteLength % ELEMENT_SIZE !== 0) { |
162 throw %make_range_error(kInvalidTypedArrayAlignment, | 165 throw %make_range_error(kInvalidTypedArrayAlignment, |
163 "byte length", "NAME", ELEMENT_SIZE); | 166 "byte length", "NAME", ELEMENT_SIZE); |
164 } | 167 } |
165 newByteLength = bufferByteLength - offset; | 168 newByteLength = bufferByteLength - offset; |
166 newLength = newByteLength / ELEMENT_SIZE; | 169 if (newByteLength < 0) { |
| 170 throw %make_range_error(kInvalidTypedArrayAlignment, |
| 171 "byte length", "NAME", ELEMENT_SIZE); |
| 172 } |
167 } else { | 173 } else { |
168 var newLength = length; | 174 newByteLength = length * ELEMENT_SIZE; |
169 newByteLength = newLength * ELEMENT_SIZE; | 175 if (offset + newByteLength > bufferByteLength) { |
170 } | 176 throw %make_range_error(kInvalidTypedArrayLength); |
171 if ((offset + newByteLength > bufferByteLength) | 177 } |
172 || (newLength > %_MaxSmi())) { | |
173 throw %make_range_error(kInvalidTypedArrayLength); | |
174 } | 178 } |
175 %_TypedArrayInitialize(obj, ARRAY_ID, buffer, offset, newByteLength, true); | 179 %_TypedArrayInitialize(obj, ARRAY_ID, buffer, offset, newByteLength, true); |
176 } | 180 } |
177 | 181 |
178 function NAMEConstructByLength(obj, length) { | 182 function NAMEConstructByLength(obj, length) { |
179 var l = IS_UNDEFINED(length) ? | 183 var l = IS_UNDEFINED(length) ? |
180 0 : ToPositiveInteger(length, kInvalidTypedArrayLength); | 184 0 : ToIndex(length, kInvalidTypedArrayLength); |
181 if (l > %_MaxSmi()) { | 185 if (length > %_MaxSmi()) { |
| 186 // Note: this is not per spec, but rather a constraint of our current |
| 187 // representation (which uses smi's). |
182 throw %make_range_error(kInvalidTypedArrayLength); | 188 throw %make_range_error(kInvalidTypedArrayLength); |
183 } | 189 } |
184 var byteLength = l * ELEMENT_SIZE; | 190 var byteLength = l * ELEMENT_SIZE; |
185 if (byteLength > %_TypedArrayMaxSizeInHeap()) { | 191 if (byteLength > %_TypedArrayMaxSizeInHeap()) { |
186 var buffer = new GlobalArrayBuffer(byteLength); | 192 var buffer = new GlobalArrayBuffer(byteLength); |
187 %_TypedArrayInitialize(obj, ARRAY_ID, buffer, 0, byteLength, true); | 193 %_TypedArrayInitialize(obj, ARRAY_ID, buffer, 0, byteLength, true); |
188 } else { | 194 } else { |
189 %_TypedArrayInitialize(obj, ARRAY_ID, null, 0, byteLength, true); | 195 %_TypedArrayInitialize(obj, ARRAY_ID, null, 0, byteLength, true); |
190 } | 196 } |
191 } | 197 } |
(...skipping 731 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
923 FUNCTION(Float64) | 929 FUNCTION(Float64) |
924 endmacro | 930 endmacro |
925 | 931 |
926 | 932 |
927 macro DATA_VIEW_GETTER_SETTER(TYPENAME) | 933 macro DATA_VIEW_GETTER_SETTER(TYPENAME) |
928 function DataViewGetTYPENAMEJS(offset, little_endian) { | 934 function DataViewGetTYPENAMEJS(offset, little_endian) { |
929 if (!IS_DATAVIEW(this)) { | 935 if (!IS_DATAVIEW(this)) { |
930 throw %make_type_error(kIncompatibleMethodReceiver, | 936 throw %make_type_error(kIncompatibleMethodReceiver, |
931 'DataView.getTYPENAME', this); | 937 'DataView.getTYPENAME', this); |
932 } | 938 } |
933 if (arguments.length < 1) throw %make_type_error(kInvalidArgument); | 939 offset = IS_UNDEFINED(offset) ? 0 : ToIndex(offset, kInvalidDataViewAccessorOf
fset); |
934 offset = ToPositiveInteger(offset, kInvalidDataViewAccessorOffset); | |
935 return %DataViewGetTYPENAME(this, offset, !!little_endian); | 940 return %DataViewGetTYPENAME(this, offset, !!little_endian); |
936 } | 941 } |
937 %FunctionSetLength(DataViewGetTYPENAMEJS, 1); | 942 %FunctionSetLength(DataViewGetTYPENAMEJS, 1); |
938 | 943 |
939 function DataViewSetTYPENAMEJS(offset, value, little_endian) { | 944 function DataViewSetTYPENAMEJS(offset, value, little_endian) { |
940 if (!IS_DATAVIEW(this)) { | 945 if (!IS_DATAVIEW(this)) { |
941 throw %make_type_error(kIncompatibleMethodReceiver, | 946 throw %make_type_error(kIncompatibleMethodReceiver, |
942 'DataView.setTYPENAME', this); | 947 'DataView.setTYPENAME', this); |
943 } | 948 } |
944 if (arguments.length < 2) throw %make_type_error(kInvalidArgument); | 949 offset = IS_UNDEFINED(offset) ? 0 : ToIndex(offset, kInvalidDataViewAccessorOf
fset); |
945 offset = ToPositiveInteger(offset, kInvalidDataViewAccessorOffset); | |
946 %DataViewSetTYPENAME(this, offset, TO_NUMBER(value), !!little_endian); | 950 %DataViewSetTYPENAME(this, offset, TO_NUMBER(value), !!little_endian); |
947 } | 951 } |
948 %FunctionSetLength(DataViewSetTYPENAMEJS, 2); | 952 %FunctionSetLength(DataViewSetTYPENAMEJS, 2); |
949 endmacro | 953 endmacro |
950 | 954 |
951 DATA_VIEW_TYPES(DATA_VIEW_GETTER_SETTER) | 955 DATA_VIEW_TYPES(DATA_VIEW_GETTER_SETTER) |
952 | 956 |
953 utils.InstallFunctions(GlobalDataView.prototype, DONT_ENUM, [ | 957 utils.InstallFunctions(GlobalDataView.prototype, DONT_ENUM, [ |
954 "getInt8", DataViewGetInt8JS, | 958 "getInt8", DataViewGetInt8JS, |
955 "setInt8", DataViewSetInt8JS, | 959 "setInt8", DataViewSetInt8JS, |
(...skipping 14 matching lines...) Expand all Loading... |
970 "setUint32", DataViewSetUint32JS, | 974 "setUint32", DataViewSetUint32JS, |
971 | 975 |
972 "getFloat32", DataViewGetFloat32JS, | 976 "getFloat32", DataViewGetFloat32JS, |
973 "setFloat32", DataViewSetFloat32JS, | 977 "setFloat32", DataViewSetFloat32JS, |
974 | 978 |
975 "getFloat64", DataViewGetFloat64JS, | 979 "getFloat64", DataViewGetFloat64JS, |
976 "setFloat64", DataViewSetFloat64JS | 980 "setFloat64", DataViewSetFloat64JS |
977 ]); | 981 ]); |
978 | 982 |
979 }) | 983 }) |
OLD | NEW |