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 |
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 } | 417 } |
418 throw %make_type_error(kStrongImplicitConversion); | 418 throw %make_type_error(kStrongImplicitConversion); |
419 } | 419 } |
420 | 420 |
421 | 421 |
422 /* ----------------------------- | 422 /* ----------------------------- |
423 - - - H e l p e r s - - - | 423 - - - H e l p e r s - - - |
424 ----------------------------- | 424 ----------------------------- |
425 */ | 425 */ |
426 | 426 |
427 function CALL_NON_FUNCTION() { | |
428 var delegate = %GetFunctionDelegate(this); | |
429 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength()); | |
430 } | |
431 | |
432 | |
433 function CALL_NON_FUNCTION_AS_CONSTRUCTOR() { | 427 function CALL_NON_FUNCTION_AS_CONSTRUCTOR() { |
434 var delegate = %GetConstructorDelegate(this); | 428 var delegate = %GetConstructorDelegate(this); |
435 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength()); | 429 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength()); |
436 } | 430 } |
437 | 431 |
438 | 432 |
439 function CALL_FUNCTION_PROXY() { | |
440 var arity = %_ArgumentsLength() - 1; | |
441 var proxy = %_Arguments(arity); // The proxy comes in as an additional arg. | |
442 var trap = %GetCallTrap(proxy); | |
443 return %Apply(trap, this, arguments, 0, arity); | |
444 } | |
445 | |
446 | |
447 function CALL_FUNCTION_PROXY_AS_CONSTRUCTOR () { | 433 function CALL_FUNCTION_PROXY_AS_CONSTRUCTOR () { |
448 var proxy = this; | 434 var proxy = this; |
449 var trap = %GetConstructTrap(proxy); | 435 var trap = %GetConstructTrap(proxy); |
450 return %Apply(trap, this, arguments, 0, %_ArgumentsLength()); | 436 return %Apply(trap, this, arguments, 0, %_ArgumentsLength()); |
451 } | 437 } |
452 | 438 |
453 | 439 |
454 function APPLY_PREPARE(args) { | 440 function APPLY_PREPARE(args) { |
455 var length; | 441 var length; |
| 442 |
| 443 // First check that the receiver is callable. |
| 444 if (!IS_CALLABLE(this)) { |
| 445 throw %make_type_error(kApplyNonFunction, %to_string_fun(this), |
| 446 typeof this); |
| 447 } |
| 448 |
456 // First check whether length is a positive Smi and args is an | 449 // First check whether length is a positive Smi and args is an |
457 // array. This is the fast case. If this fails, we do the slow case | 450 // array. This is the fast case. If this fails, we do the slow case |
458 // that takes care of more eventualities. | 451 // that takes care of more eventualities. |
459 if (IS_ARRAY(args)) { | 452 if (IS_ARRAY(args)) { |
460 length = args.length; | 453 length = args.length; |
461 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength && | 454 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength) { |
462 IS_CALLABLE(this)) { | |
463 return length; | 455 return length; |
464 } | 456 } |
465 } | 457 } |
466 | 458 |
467 length = (args == null) ? 0 : TO_UINT32(args.length); | 459 length = (args == null) ? 0 : TO_UINT32(args.length); |
468 | 460 |
469 // We can handle any number of apply arguments if the stack is | 461 // We can handle any number of apply arguments if the stack is |
470 // big enough, but sanity check the value to avoid overflow when | 462 // big enough, but sanity check the value to avoid overflow when |
471 // multiplying with pointer size. | 463 // multiplying with pointer size. |
472 if (length > kSafeArgumentsLength) throw %make_range_error(kStackOverflow); | 464 if (length > kSafeArgumentsLength) throw %make_range_error(kStackOverflow); |
473 | 465 |
474 if (!IS_CALLABLE(this)) { | |
475 throw %make_type_error(kApplyNonFunction, %to_string_fun(this), | |
476 typeof this); | |
477 } | |
478 | |
479 // Make sure the arguments list has the right type. | 466 // Make sure the arguments list has the right type. |
480 if (args != null && !IS_SPEC_OBJECT(args)) { | 467 if (args != null && !IS_SPEC_OBJECT(args)) { |
481 throw %make_type_error(kWrongArgs, "Function.prototype.apply"); | 468 throw %make_type_error(kWrongArgs, "Function.prototype.apply"); |
482 } | 469 } |
483 | 470 |
484 // Return the length which is the number of arguments to copy to the | 471 // Return the length which is the number of arguments to copy to the |
485 // stack. It is guaranteed to be a small integer at this point. | 472 // stack. It is guaranteed to be a small integer at this point. |
486 return length; | 473 return length; |
487 } | 474 } |
488 | 475 |
489 | 476 |
490 function REFLECT_APPLY_PREPARE(args) { | 477 function REFLECT_APPLY_PREPARE(args) { |
491 var length; | 478 var length; |
| 479 |
| 480 // First check that the receiver is callable. |
| 481 if (!IS_CALLABLE(this)) { |
| 482 throw %make_type_error(kApplyNonFunction, %to_string_fun(this), |
| 483 typeof this); |
| 484 } |
| 485 |
492 // First check whether length is a positive Smi and args is an | 486 // First check whether length is a positive Smi and args is an |
493 // array. This is the fast case. If this fails, we do the slow case | 487 // array. This is the fast case. If this fails, we do the slow case |
494 // that takes care of more eventualities. | 488 // that takes care of more eventualities. |
495 if (IS_ARRAY(args)) { | 489 if (IS_ARRAY(args)) { |
496 length = args.length; | 490 length = args.length; |
497 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength && | 491 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength) { |
498 IS_CALLABLE(this)) { | |
499 return length; | 492 return length; |
500 } | 493 } |
501 } | 494 } |
502 | 495 |
503 if (!IS_CALLABLE(this)) { | |
504 throw %make_type_error(kCalledNonCallable, %to_string_fun(this)); | |
505 } | |
506 | |
507 if (!IS_SPEC_OBJECT(args)) { | 496 if (!IS_SPEC_OBJECT(args)) { |
508 throw %make_type_error(kWrongArgs, "Reflect.apply"); | 497 throw %make_type_error(kWrongArgs, "Reflect.apply"); |
509 } | 498 } |
510 | 499 |
511 length = %to_length_fun(args.length); | 500 length = %to_length_fun(args.length); |
512 | 501 |
513 // We can handle any number of apply arguments if the stack is | 502 // We can handle any number of apply arguments if the stack is |
514 // big enough, but sanity check the value to avoid overflow when | 503 // big enough, but sanity check the value to avoid overflow when |
515 // multiplying with pointer size. | 504 // multiplying with pointer size. |
516 if (length > kSafeArgumentsLength) throw %make_range_error(kStackOverflow); | 505 if (length > kSafeArgumentsLength) throw %make_range_error(kStackOverflow); |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
795 "add_builtin", ADD, | 784 "add_builtin", ADD, |
796 "add_strong_builtin", ADD_STRONG, | 785 "add_strong_builtin", ADD_STRONG, |
797 "apply_prepare_builtin", APPLY_PREPARE, | 786 "apply_prepare_builtin", APPLY_PREPARE, |
798 "bit_and_builtin", BIT_AND, | 787 "bit_and_builtin", BIT_AND, |
799 "bit_and_strong_builtin", BIT_AND_STRONG, | 788 "bit_and_strong_builtin", BIT_AND_STRONG, |
800 "bit_or_builtin", BIT_OR, | 789 "bit_or_builtin", BIT_OR, |
801 "bit_or_strong_builtin", BIT_OR_STRONG, | 790 "bit_or_strong_builtin", BIT_OR_STRONG, |
802 "bit_xor_builtin", BIT_XOR, | 791 "bit_xor_builtin", BIT_XOR, |
803 "bit_xor_strong_builtin", BIT_XOR_STRONG, | 792 "bit_xor_strong_builtin", BIT_XOR_STRONG, |
804 "call_function_proxy_as_constructor_builtin", CALL_FUNCTION_PROXY_AS_CONSTRUCT
OR, | 793 "call_function_proxy_as_constructor_builtin", CALL_FUNCTION_PROXY_AS_CONSTRUCT
OR, |
805 "call_function_proxy_builtin", CALL_FUNCTION_PROXY, | |
806 "call_non_function_as_constructor_builtin", CALL_NON_FUNCTION_AS_CONSTRUCTOR, | 794 "call_non_function_as_constructor_builtin", CALL_NON_FUNCTION_AS_CONSTRUCTOR, |
807 "call_non_function_builtin", CALL_NON_FUNCTION, | |
808 "compare_builtin", COMPARE, | 795 "compare_builtin", COMPARE, |
809 "compare_strong_builtin", COMPARE_STRONG, | 796 "compare_strong_builtin", COMPARE_STRONG, |
810 "concat_iterable_to_array_builtin", CONCAT_ITERABLE_TO_ARRAY, | 797 "concat_iterable_to_array_builtin", CONCAT_ITERABLE_TO_ARRAY, |
811 "div_builtin", DIV, | 798 "div_builtin", DIV, |
812 "div_strong_builtin", DIV_STRONG, | 799 "div_strong_builtin", DIV_STRONG, |
813 "equals_builtin", EQUALS, | 800 "equals_builtin", EQUALS, |
814 "mod_builtin", MOD, | 801 "mod_builtin", MOD, |
815 "mod_strong_builtin", MOD_STRONG, | 802 "mod_strong_builtin", MOD_STRONG, |
816 "mul_builtin", MUL, | 803 "mul_builtin", MUL, |
817 "mul_strong_builtin", MUL_STRONG, | 804 "mul_strong_builtin", MUL_STRONG, |
(...skipping 25 matching lines...) Expand all Loading... |
843 | 830 |
844 utils.Export(function(to) { | 831 utils.Export(function(to) { |
845 to.ToBoolean = ToBoolean; | 832 to.ToBoolean = ToBoolean; |
846 to.ToLength = ToLength; | 833 to.ToLength = ToLength; |
847 to.ToNumber = ToNumber; | 834 to.ToNumber = ToNumber; |
848 to.ToPrimitive = ToPrimitive; | 835 to.ToPrimitive = ToPrimitive; |
849 to.ToString = ToString; | 836 to.ToString = ToString; |
850 }); | 837 }); |
851 | 838 |
852 }) | 839 }) |
OLD | NEW |