OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 // This files contains runtime support implemented in JavaScript. | 5 // This files contains runtime support implemented in JavaScript. |
6 | 6 |
7 // CAUTION: Some of the functions specified in this file are called | 7 // CAUTION: Some of the functions specified in this file are called |
8 // directly from compiled code. These are the functions with names in | 8 // directly from compiled code. These are the functions with names in |
9 // ALL CAPS. The compiled code passes the first argument in 'this'. | 9 // ALL CAPS. The compiled code passes the first argument in 'this'. |
10 | 10 |
11 | 11 |
12 // The following declarations are shared with other native JS files. | 12 // The following declarations are shared with other native JS files. |
13 // They are all declared at this one spot to avoid redeclaration errors. | 13 // They are all declared at this one spot to avoid redeclaration errors. |
14 var $NaN; | 14 var $NaN; |
15 var $nonNumberToNumber; | 15 var $nonNumberToNumber; |
16 var $sameValue; | 16 var $sameValue; |
17 var $sameValueZero; | 17 var $sameValueZero; |
18 var $toInteger; | |
19 var $toLength; | |
20 var $toNumber; | 18 var $toNumber; |
21 var $toPositiveInteger; | 19 var $toPositiveInteger; |
22 | 20 |
23 var harmony_tolength = false; | 21 var harmony_tolength = false; |
24 | 22 |
25 (function(global, utils) { | 23 (function(global, utils) { |
26 | 24 |
27 %CheckIsBootstrapping(); | 25 %CheckIsBootstrapping(); |
28 | 26 |
29 var GlobalArray = global.Array; | 27 var GlobalArray = global.Array; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 length = args.length; | 88 length = args.length; |
91 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength) { | 89 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength) { |
92 return length; | 90 return length; |
93 } | 91 } |
94 } | 92 } |
95 | 93 |
96 if (!IS_SPEC_OBJECT(args)) { | 94 if (!IS_SPEC_OBJECT(args)) { |
97 throw %make_type_error(kWrongArgs, "Reflect.apply"); | 95 throw %make_type_error(kWrongArgs, "Reflect.apply"); |
98 } | 96 } |
99 | 97 |
100 length = %to_length_fun(args.length); | 98 length = TO_LENGTH(args.length); |
101 | 99 |
102 // We can handle any number of apply arguments if the stack is | 100 // We can handle any number of apply arguments if the stack is |
103 // big enough, but sanity check the value to avoid overflow when | 101 // big enough, but sanity check the value to avoid overflow when |
104 // multiplying with pointer size. | 102 // multiplying with pointer size. |
105 if (length > kSafeArgumentsLength) throw %make_range_error(kStackOverflow); | 103 if (length > kSafeArgumentsLength) throw %make_range_error(kStackOverflow); |
106 | 104 |
107 // Return the length which is the number of arguments to copy to the | 105 // Return the length which is the number of arguments to copy to the |
108 // stack. It is guaranteed to be a small integer at this point. | 106 // stack. It is guaranteed to be a small integer at this point. |
109 return length; | 107 return length; |
110 } | 108 } |
(...skipping 29 matching lines...) Expand all Loading... |
140 throw %make_type_error(kCalledNonCallable, TO_STRING(newTarget)); | 138 throw %make_type_error(kCalledNonCallable, TO_STRING(newTarget)); |
141 } else { | 139 } else { |
142 throw %make_type_error(kNotConstructor, TO_STRING(newTarget)); | 140 throw %make_type_error(kNotConstructor, TO_STRING(newTarget)); |
143 } | 141 } |
144 } | 142 } |
145 | 143 |
146 if (!IS_SPEC_OBJECT(args)) { | 144 if (!IS_SPEC_OBJECT(args)) { |
147 throw %make_type_error(kWrongArgs, "Reflect.construct"); | 145 throw %make_type_error(kWrongArgs, "Reflect.construct"); |
148 } | 146 } |
149 | 147 |
150 length = %to_length_fun(args.length); | 148 length = TO_LENGTH(args.length); |
151 | 149 |
152 // We can handle any number of apply arguments if the stack is | 150 // We can handle any number of apply arguments if the stack is |
153 // big enough, but sanity check the value to avoid overflow when | 151 // big enough, but sanity check the value to avoid overflow when |
154 // multiplying with pointer size. | 152 // multiplying with pointer size. |
155 if (length > kSafeArgumentsLength) throw %make_range_error(kStackOverflow); | 153 if (length > kSafeArgumentsLength) throw %make_range_error(kStackOverflow); |
156 | 154 |
157 // Return the length which is the number of arguments to copy to the | 155 // Return the length which is the number of arguments to copy to the |
158 // stack. It is guaranteed to be a small integer at this point. | 156 // stack. It is guaranteed to be a small integer at this point. |
159 return length; | 157 return length; |
160 } | 158 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 function ToString(x) { | 207 function ToString(x) { |
210 if (IS_STRING(x)) return x; | 208 if (IS_STRING(x)) return x; |
211 if (IS_NUMBER(x)) return %_NumberToString(x); | 209 if (IS_NUMBER(x)) return %_NumberToString(x); |
212 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; | 210 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; |
213 if (IS_UNDEFINED(x)) return 'undefined'; | 211 if (IS_UNDEFINED(x)) return 'undefined'; |
214 // Types that can't be converted to string are caught in DefaultString. | 212 // Types that can't be converted to string are caught in DefaultString. |
215 return (IS_NULL(x)) ? 'null' : ToString(DefaultString(x)); | 213 return (IS_NULL(x)) ? 'null' : ToString(DefaultString(x)); |
216 } | 214 } |
217 | 215 |
218 | 216 |
219 // ECMA-262, section 9.4, page 34. | |
220 function ToInteger(x) { | |
221 if (%_IsSmi(x)) return x; | |
222 return %NumberToInteger(ToNumber(x)); | |
223 } | |
224 | |
225 | |
226 // ES6, draft 08-24-14, section 7.1.15 | |
227 function ToLength(arg) { | |
228 arg = ToInteger(arg); | |
229 if (arg < 0) return 0; | |
230 return arg < kMaxSafeInteger ? arg : kMaxSafeInteger; | |
231 } | |
232 | |
233 | |
234 // ES5, section 9.12 | 217 // ES5, section 9.12 |
235 function SameValue(x, y) { | 218 function SameValue(x, y) { |
236 if (typeof x != typeof y) return false; | 219 if (typeof x != typeof y) return false; |
237 if (IS_NUMBER(x)) { | 220 if (IS_NUMBER(x)) { |
238 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true; | 221 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true; |
239 // x is +0 and y is -0 or vice versa. | 222 // x is +0 and y is -0 or vice versa. |
240 if (x === 0 && y === 0 && %_IsMinusZero(x) != %_IsMinusZero(y)) { | 223 if (x === 0 && y === 0 && %_IsMinusZero(x) != %_IsMinusZero(y)) { |
241 return false; | 224 return false; |
242 } | 225 } |
243 } | 226 } |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 // boilerplate gets the right prototype. | 324 // boilerplate gets the right prototype. |
342 %FunctionSetPrototype(GlobalArray, new GlobalArray(0)); | 325 %FunctionSetPrototype(GlobalArray, new GlobalArray(0)); |
343 | 326 |
344 // ---------------------------------------------------------------------------- | 327 // ---------------------------------------------------------------------------- |
345 // Exports | 328 // Exports |
346 | 329 |
347 $NaN = %GetRootNaN(); | 330 $NaN = %GetRootNaN(); |
348 $nonNumberToNumber = NonNumberToNumber; | 331 $nonNumberToNumber = NonNumberToNumber; |
349 $sameValue = SameValue; | 332 $sameValue = SameValue; |
350 $sameValueZero = SameValueZero; | 333 $sameValueZero = SameValueZero; |
351 $toInteger = ToInteger; | |
352 $toLength = ToLength; | |
353 $toNumber = ToNumber; | 334 $toNumber = ToNumber; |
354 $toPositiveInteger = ToPositiveInteger; | 335 $toPositiveInteger = ToPositiveInteger; |
355 | 336 |
356 %InstallToContext([ | 337 %InstallToContext([ |
357 "apply_prepare_builtin", APPLY_PREPARE, | 338 "apply_prepare_builtin", APPLY_PREPARE, |
358 "concat_iterable_to_array_builtin", CONCAT_ITERABLE_TO_ARRAY, | 339 "concat_iterable_to_array_builtin", CONCAT_ITERABLE_TO_ARRAY, |
359 "reflect_apply_prepare_builtin", REFLECT_APPLY_PREPARE, | 340 "reflect_apply_prepare_builtin", REFLECT_APPLY_PREPARE, |
360 "reflect_construct_prepare_builtin", REFLECT_CONSTRUCT_PREPARE, | 341 "reflect_construct_prepare_builtin", REFLECT_CONSTRUCT_PREPARE, |
361 ]); | 342 ]); |
362 | 343 |
363 %InstallToContext([ | 344 %InstallToContext([ |
364 "concat_iterable_to_array", ConcatIterableToArray, | 345 "concat_iterable_to_array", ConcatIterableToArray, |
365 "non_number_to_number", NonNumberToNumber, | 346 "non_number_to_number", NonNumberToNumber, |
366 "to_integer_fun", ToInteger, | |
367 "to_length_fun", ToLength, | |
368 "to_number_fun", ToNumber, | 347 "to_number_fun", ToNumber, |
369 ]); | 348 ]); |
370 | 349 |
371 utils.Export(function(to) { | 350 utils.Export(function(to) { |
372 to.ToBoolean = ToBoolean; | 351 to.ToBoolean = ToBoolean; |
373 to.ToLength = ToLength; | |
374 to.ToNumber = ToNumber; | 352 to.ToNumber = ToNumber; |
375 to.ToString = ToString; | 353 to.ToString = ToString; |
376 }); | 354 }); |
377 | 355 |
378 }) | 356 }) |
OLD | NEW |