| 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; | |
| 16 var $sameValue; | 15 var $sameValue; |
| 17 var $sameValueZero; | 16 var $sameValueZero; |
| 18 var $toNumber; | |
| 19 var $toPositiveInteger; | 17 var $toPositiveInteger; |
| 20 | 18 |
| 21 var harmony_tolength = false; | 19 var harmony_tolength = false; |
| 22 | 20 |
| 23 (function(global, utils) { | 21 (function(global, utils) { |
| 24 | 22 |
| 25 %CheckIsBootstrapping(); | 23 %CheckIsBootstrapping(); |
| 26 | 24 |
| 27 var GlobalArray = global.Array; | 25 var GlobalArray = global.Array; |
| 28 var GlobalBoolean = global.Boolean; | 26 var GlobalBoolean = global.Boolean; |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 // ECMA-262, section 9.2, page 30 | 169 // ECMA-262, section 9.2, page 30 |
| 172 function ToBoolean(x) { | 170 function ToBoolean(x) { |
| 173 if (IS_BOOLEAN(x)) return x; | 171 if (IS_BOOLEAN(x)) return x; |
| 174 if (IS_STRING(x)) return x.length != 0; | 172 if (IS_STRING(x)) return x.length != 0; |
| 175 if (x == null) return false; | 173 if (x == null) return false; |
| 176 if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x)); | 174 if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x)); |
| 177 return true; | 175 return true; |
| 178 } | 176 } |
| 179 | 177 |
| 180 | 178 |
| 181 // ECMA-262, section 9.3, page 31. | |
| 182 function ToNumber(x) { | |
| 183 if (IS_NUMBER(x)) return x; | |
| 184 if (IS_STRING(x)) { | |
| 185 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x) | |
| 186 : %StringToNumber(x); | |
| 187 } | |
| 188 if (IS_BOOLEAN(x)) return x ? 1 : 0; | |
| 189 if (IS_UNDEFINED(x)) return NAN; | |
| 190 // Types that can't be converted to number are caught in DefaultNumber. | |
| 191 return (IS_NULL(x)) ? 0 : ToNumber(DefaultNumber(x)); | |
| 192 } | |
| 193 | |
| 194 function NonNumberToNumber(x) { | |
| 195 if (IS_STRING(x)) { | |
| 196 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x) | |
| 197 : %StringToNumber(x); | |
| 198 } | |
| 199 if (IS_BOOLEAN(x)) return x ? 1 : 0; | |
| 200 if (IS_UNDEFINED(x)) return NAN; | |
| 201 // Types that can't be converted to number are caught in DefaultNumber. | |
| 202 return (IS_NULL(x)) ? 0 : ToNumber(DefaultNumber(x)); | |
| 203 } | |
| 204 | |
| 205 | |
| 206 // ECMA-262, section 9.8, page 35. | |
| 207 function ToString(x) { | |
| 208 if (IS_STRING(x)) return x; | |
| 209 if (IS_NUMBER(x)) return %_NumberToString(x); | |
| 210 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; | |
| 211 if (IS_UNDEFINED(x)) return 'undefined'; | |
| 212 // Types that can't be converted to string are caught in DefaultString. | |
| 213 return (IS_NULL(x)) ? 'null' : ToString(DefaultString(x)); | |
| 214 } | |
| 215 | |
| 216 | |
| 217 // ES5, section 9.12 | 179 // ES5, section 9.12 |
| 218 function SameValue(x, y) { | 180 function SameValue(x, y) { |
| 219 if (typeof x != typeof y) return false; | 181 if (typeof x != typeof y) return false; |
| 220 if (IS_NUMBER(x)) { | 182 if (IS_NUMBER(x)) { |
| 221 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true; | 183 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true; |
| 222 // x is +0 and y is -0 or vice versa. | 184 // x is +0 and y is -0 or vice versa. |
| 223 if (x === 0 && y === 0 && %_IsMinusZero(x) != %_IsMinusZero(y)) { | 185 if (x === 0 && y === 0 && %_IsMinusZero(x) != %_IsMinusZero(y)) { |
| 224 return false; | 186 return false; |
| 225 } | 187 } |
| 226 } | 188 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 247 } | 209 } |
| 248 return target; | 210 return target; |
| 249 } | 211 } |
| 250 | 212 |
| 251 | 213 |
| 252 /* --------------------------------- | 214 /* --------------------------------- |
| 253 - - - U t i l i t i e s - - - | 215 - - - U t i l i t i e s - - - |
| 254 --------------------------------- | 216 --------------------------------- |
| 255 */ | 217 */ |
| 256 | 218 |
| 257 // Returns if the given x is a primitive value - not an object or a | |
| 258 // function. | |
| 259 function IsPrimitive(x) { | |
| 260 // Even though the type of null is "object", null is still | |
| 261 // considered a primitive value. IS_SPEC_OBJECT handles this correctly | |
| 262 // (i.e., it will return false if x is null). | |
| 263 return !IS_SPEC_OBJECT(x); | |
| 264 } | |
| 265 | |
| 266 | 219 |
| 267 // ES6, draft 10-14-14, section 22.1.3.1.1 | 220 // ES6, draft 10-14-14, section 22.1.3.1.1 |
| 268 function IsConcatSpreadable(O) { | 221 function IsConcatSpreadable(O) { |
| 269 if (!IS_SPEC_OBJECT(O)) return false; | 222 if (!IS_SPEC_OBJECT(O)) return false; |
| 270 var spreadable = O[isConcatSpreadableSymbol]; | 223 var spreadable = O[isConcatSpreadableSymbol]; |
| 271 if (IS_UNDEFINED(spreadable)) return IS_ARRAY(O); | 224 if (IS_UNDEFINED(spreadable)) return IS_ARRAY(O); |
| 272 return ToBoolean(spreadable); | 225 return ToBoolean(spreadable); |
| 273 } | 226 } |
| 274 | 227 |
| 275 | 228 |
| 276 // ECMA-262, section 8.6.2.6, page 28. | |
| 277 function DefaultNumber(x) { | |
| 278 var valueOf = x.valueOf; | |
| 279 if (IS_CALLABLE(valueOf)) { | |
| 280 var v = %_Call(valueOf, x); | |
| 281 if (IS_SYMBOL(v)) throw MakeTypeError(kSymbolToNumber); | |
| 282 if (IS_SIMD_VALUE(x)) throw MakeTypeError(kSimdToNumber); | |
| 283 if (IsPrimitive(v)) return v; | |
| 284 } | |
| 285 var toString = x.toString; | |
| 286 if (IS_CALLABLE(toString)) { | |
| 287 var s = %_Call(toString, x); | |
| 288 if (IsPrimitive(s)) return s; | |
| 289 } | |
| 290 throw MakeTypeError(kCannotConvertToPrimitive); | |
| 291 } | |
| 292 | |
| 293 // ECMA-262, section 8.6.2.6, page 28. | |
| 294 function DefaultString(x) { | |
| 295 if (!IS_SYMBOL_WRAPPER(x)) { | |
| 296 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToString); | |
| 297 var toString = x.toString; | |
| 298 if (IS_CALLABLE(toString)) { | |
| 299 var s = %_Call(toString, x); | |
| 300 if (IsPrimitive(s)) return s; | |
| 301 } | |
| 302 | |
| 303 var valueOf = x.valueOf; | |
| 304 if (IS_CALLABLE(valueOf)) { | |
| 305 var v = %_Call(valueOf, x); | |
| 306 if (IsPrimitive(v)) return v; | |
| 307 } | |
| 308 } | |
| 309 throw MakeTypeError(kCannotConvertToPrimitive); | |
| 310 } | |
| 311 | |
| 312 function ToPositiveInteger(x, rangeErrorIndex) { | 229 function ToPositiveInteger(x, rangeErrorIndex) { |
| 313 var i = TO_INTEGER_MAP_MINUS_ZERO(x); | 230 var i = TO_INTEGER_MAP_MINUS_ZERO(x); |
| 314 if (i < 0) throw MakeRangeError(rangeErrorIndex); | 231 if (i < 0) throw MakeRangeError(rangeErrorIndex); |
| 315 return i; | 232 return i; |
| 316 } | 233 } |
| 317 | 234 |
| 318 //---------------------------------------------------------------------------- | 235 //---------------------------------------------------------------------------- |
| 319 | 236 |
| 320 // NOTE: Setting the prototype for Array must take place as early as | 237 // NOTE: Setting the prototype for Array must take place as early as |
| 321 // possible due to code generation for array literals. When | 238 // possible due to code generation for array literals. When |
| 322 // generating code for a array literal a boilerplate array is created | 239 // generating code for a array literal a boilerplate array is created |
| 323 // that is cloned when running the code. It is essential that the | 240 // that is cloned when running the code. It is essential that the |
| 324 // boilerplate gets the right prototype. | 241 // boilerplate gets the right prototype. |
| 325 %FunctionSetPrototype(GlobalArray, new GlobalArray(0)); | 242 %FunctionSetPrototype(GlobalArray, new GlobalArray(0)); |
| 326 | 243 |
| 327 // ---------------------------------------------------------------------------- | 244 // ---------------------------------------------------------------------------- |
| 328 // Exports | 245 // Exports |
| 329 | 246 |
| 330 $NaN = %GetRootNaN(); | 247 $NaN = %GetRootNaN(); |
| 331 $nonNumberToNumber = NonNumberToNumber; | |
| 332 $sameValue = SameValue; | 248 $sameValue = SameValue; |
| 333 $sameValueZero = SameValueZero; | 249 $sameValueZero = SameValueZero; |
| 334 $toNumber = ToNumber; | |
| 335 $toPositiveInteger = ToPositiveInteger; | 250 $toPositiveInteger = ToPositiveInteger; |
| 336 | 251 |
| 337 %InstallToContext([ | 252 %InstallToContext([ |
| 338 "apply_prepare_builtin", APPLY_PREPARE, | 253 "apply_prepare_builtin", APPLY_PREPARE, |
| 339 "concat_iterable_to_array_builtin", CONCAT_ITERABLE_TO_ARRAY, | 254 "concat_iterable_to_array_builtin", CONCAT_ITERABLE_TO_ARRAY, |
| 340 "reflect_apply_prepare_builtin", REFLECT_APPLY_PREPARE, | 255 "reflect_apply_prepare_builtin", REFLECT_APPLY_PREPARE, |
| 341 "reflect_construct_prepare_builtin", REFLECT_CONSTRUCT_PREPARE, | 256 "reflect_construct_prepare_builtin", REFLECT_CONSTRUCT_PREPARE, |
| 342 ]); | 257 ]); |
| 343 | 258 |
| 344 %InstallToContext([ | 259 %InstallToContext([ |
| 345 "concat_iterable_to_array", ConcatIterableToArray, | 260 "concat_iterable_to_array", ConcatIterableToArray, |
| 346 "non_number_to_number", NonNumberToNumber, | |
| 347 "to_number_fun", ToNumber, | |
| 348 ]); | 261 ]); |
| 349 | 262 |
| 350 utils.Export(function(to) { | 263 utils.Export(function(to) { |
| 351 to.ToBoolean = ToBoolean; | 264 to.ToBoolean = ToBoolean; |
| 352 to.ToNumber = ToNumber; | |
| 353 to.ToString = ToString; | |
| 354 }); | 265 }); |
| 355 | 266 |
| 356 }) | 267 }) |
| OLD | NEW |