Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: src/runtime.js

Issue 1316933002: [es6] Initial steps towards a correct implementation of IsCallable. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: ia32, arm and arm64 ports. Misc cleanups. Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 419 matching lines...) Expand 10 before | Expand all | Expand 10 after
430 return this < x.length; 430 return this < x.length;
431 } 431 }
432 return %HasElement(x, this); 432 return %HasElement(x, this);
433 } 433 }
434 return %HasProperty(x, %to_name(this)); 434 return %HasProperty(x, %to_name(this));
435 } 435 }
436 436
437 437
438 function CALL_NON_FUNCTION() { 438 function CALL_NON_FUNCTION() {
439 var delegate = %GetFunctionDelegate(this); 439 var delegate = %GetFunctionDelegate(this);
440 if (!IS_FUNCTION(delegate)) {
441 var callsite = %RenderCallSite();
442 if (callsite == "") callsite = typeof this;
443 throw %make_type_error(kCalledNonCallable, callsite);
444 }
445 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength()); 440 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
446 } 441 }
447 442
448 443
449 function CALL_NON_FUNCTION_AS_CONSTRUCTOR() { 444 function CALL_NON_FUNCTION_AS_CONSTRUCTOR() {
450 var delegate = %GetConstructorDelegate(this); 445 var delegate = %GetConstructorDelegate(this);
451 if (!IS_FUNCTION(delegate)) {
452 var callsite = %RenderCallSite();
453 if (callsite == "") callsite = typeof this;
454 throw %make_type_error(kCalledNonCallable, callsite);
455 }
456 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength()); 446 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
457 } 447 }
458 448
459 449
460 function CALL_FUNCTION_PROXY() { 450 function CALL_FUNCTION_PROXY() {
461 var arity = %_ArgumentsLength() - 1; 451 var arity = %_ArgumentsLength() - 1;
462 var proxy = %_Arguments(arity); // The proxy comes in as an additional arg. 452 var proxy = %_Arguments(arity); // The proxy comes in as an additional arg.
463 var trap = %GetCallTrap(proxy); 453 var trap = %GetCallTrap(proxy);
464 return %Apply(trap, this, arguments, 0, arity); 454 return %Apply(trap, this, arguments, 0, arity);
465 } 455 }
466 456
467 457
468 function CALL_FUNCTION_PROXY_AS_CONSTRUCTOR () { 458 function CALL_FUNCTION_PROXY_AS_CONSTRUCTOR () {
469 var proxy = this; 459 var proxy = this;
470 var trap = %GetConstructTrap(proxy); 460 var trap = %GetConstructTrap(proxy);
471 return %Apply(trap, this, arguments, 0, %_ArgumentsLength()); 461 return %Apply(trap, this, arguments, 0, %_ArgumentsLength());
472 } 462 }
473 463
474 464
475 function APPLY_PREPARE(args) { 465 function APPLY_PREPARE(args) {
476 var length; 466 var length;
477 // First check whether length is a positive Smi and args is an 467 // First check whether length is a positive Smi and args is an
478 // array. This is the fast case. If this fails, we do the slow case 468 // array. This is the fast case. If this fails, we do the slow case
479 // that takes care of more eventualities. 469 // that takes care of more eventualities.
480 if (IS_ARRAY(args)) { 470 if (IS_ARRAY(args)) {
481 length = args.length; 471 length = args.length;
482 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength && 472 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength &&
483 IS_SPEC_FUNCTION(this)) { 473 IS_CALLABLE(this)) {
484 return length; 474 return length;
485 } 475 }
486 } 476 }
487 477
488 length = (args == null) ? 0 : TO_UINT32(args.length); 478 length = (args == null) ? 0 : TO_UINT32(args.length);
489 479
490 // We can handle any number of apply arguments if the stack is 480 // We can handle any number of apply arguments if the stack is
491 // big enough, but sanity check the value to avoid overflow when 481 // big enough, but sanity check the value to avoid overflow when
492 // multiplying with pointer size. 482 // multiplying with pointer size.
493 if (length > kSafeArgumentsLength) throw %make_range_error(kStackOverflow); 483 if (length > kSafeArgumentsLength) throw %make_range_error(kStackOverflow);
494 484
495 if (!IS_SPEC_FUNCTION(this)) { 485 if (!IS_CALLABLE(this)) {
496 throw %make_type_error(kApplyNonFunction, %to_string_fun(this), typeof this) ; 486 throw %make_type_error(kApplyNonFunction, %to_string_fun(this),
487 typeof this);
497 } 488 }
498 489
499 // Make sure the arguments list has the right type. 490 // Make sure the arguments list has the right type.
500 if (args != null && !IS_SPEC_OBJECT(args)) { 491 if (args != null && !IS_SPEC_OBJECT(args)) {
501 throw %make_type_error(kWrongArgs, "Function.prototype.apply"); 492 throw %make_type_error(kWrongArgs, "Function.prototype.apply");
502 } 493 }
503 494
504 // Return the length which is the number of arguments to copy to the 495 // Return the length which is the number of arguments to copy to the
505 // stack. It is guaranteed to be a small integer at this point. 496 // stack. It is guaranteed to be a small integer at this point.
506 return length; 497 return length;
507 } 498 }
508 499
509 500
510 function REFLECT_APPLY_PREPARE(args) { 501 function REFLECT_APPLY_PREPARE(args) {
511 var length; 502 var length;
512 // First check whether length is a positive Smi and args is an 503 // First check whether length is a positive Smi and args is an
513 // array. This is the fast case. If this fails, we do the slow case 504 // array. This is the fast case. If this fails, we do the slow case
514 // that takes care of more eventualities. 505 // that takes care of more eventualities.
515 if (IS_ARRAY(args)) { 506 if (IS_ARRAY(args)) {
516 length = args.length; 507 length = args.length;
517 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength && 508 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength &&
518 IS_SPEC_FUNCTION(this)) { 509 IS_CALLABLE(this)) {
519 return length; 510 return length;
520 } 511 }
521 } 512 }
522 513
523 if (!IS_SPEC_FUNCTION(this)) { 514 if (!IS_CALLABLE(this)) {
524 throw %make_type_error(kCalledNonCallable, %to_string_fun(this)); 515 throw %make_type_error(kCalledNonCallable, %to_string_fun(this));
525 } 516 }
526 517
527 if (!IS_SPEC_OBJECT(args)) { 518 if (!IS_SPEC_OBJECT(args)) {
528 throw %make_type_error(kWrongArgs, "Reflect.apply"); 519 throw %make_type_error(kWrongArgs, "Reflect.apply");
529 } 520 }
530 521
531 length = %to_length_fun(args.length); 522 length = %to_length_fun(args.length);
532 523
533 // We can handle any number of apply arguments if the stack is 524 // We can handle any number of apply arguments if the stack is
534 // big enough, but sanity check the value to avoid overflow when 525 // big enough, but sanity check the value to avoid overflow when
535 // multiplying with pointer size. 526 // multiplying with pointer size.
536 if (length > kSafeArgumentsLength) throw %make_range_error(kStackOverflow); 527 if (length > kSafeArgumentsLength) throw %make_range_error(kStackOverflow);
537 528
538 // Return the length which is the number of arguments to copy to the 529 // Return the length which is the number of arguments to copy to the
539 // stack. It is guaranteed to be a small integer at this point. 530 // stack. It is guaranteed to be a small integer at this point.
540 return length; 531 return length;
541 } 532 }
542 533
543 534
544 function REFLECT_CONSTRUCT_PREPARE( 535 function REFLECT_CONSTRUCT_PREPARE(
545 args, newTarget) { 536 args, newTarget) {
546 var length; 537 var length;
547 var ctorOk = IS_SPEC_FUNCTION(this) && %IsConstructor(this); 538 var ctorOk = IS_CALLABLE(this) && %IsConstructor(this);
548 var newTargetOk = IS_SPEC_FUNCTION(newTarget) && %IsConstructor(newTarget); 539 var newTargetOk = IS_CALLABLE(newTarget) && %IsConstructor(newTarget);
549 540
550 // First check whether length is a positive Smi and args is an 541 // First check whether length is a positive Smi and args is an
551 // array. This is the fast case. If this fails, we do the slow case 542 // array. This is the fast case. If this fails, we do the slow case
552 // that takes care of more eventualities. 543 // that takes care of more eventualities.
553 if (IS_ARRAY(args)) { 544 if (IS_ARRAY(args)) {
554 length = args.length; 545 length = args.length;
555 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength && 546 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength &&
556 ctorOk && newTargetOk) { 547 ctorOk && newTargetOk) {
557 return length; 548 return length;
558 } 549 }
559 } 550 }
560 551
561 if (!ctorOk) { 552 if (!ctorOk) {
562 if (!IS_SPEC_FUNCTION(this)) { 553 if (!IS_CALLABLE(this)) {
563 throw %make_type_error(kCalledNonCallable, %to_string_fun(this)); 554 throw %make_type_error(kCalledNonCallable, %to_string_fun(this));
564 } else { 555 } else {
565 throw %make_type_error(kNotConstructor, %to_string_fun(this)); 556 throw %make_type_error(kNotConstructor, %to_string_fun(this));
566 } 557 }
567 } 558 }
568 559
569 if (!newTargetOk) { 560 if (!newTargetOk) {
570 if (!IS_SPEC_FUNCTION(newTarget)) { 561 if (!IS_CALLABLE(newTarget)) {
571 throw %make_type_error(kCalledNonCallable, %to_string_fun(newTarget)); 562 throw %make_type_error(kCalledNonCallable, %to_string_fun(newTarget));
572 } else { 563 } else {
573 throw %make_type_error(kNotConstructor, %to_string_fun(newTarget)); 564 throw %make_type_error(kNotConstructor, %to_string_fun(newTarget));
574 } 565 }
575 } 566 }
576 567
577 if (!IS_SPEC_OBJECT(args)) { 568 if (!IS_SPEC_OBJECT(args)) {
578 throw %make_type_error(kWrongArgs, "Reflect.construct"); 569 throw %make_type_error(kWrongArgs, "Reflect.construct");
579 } 570 }
580 571
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
764 if (!IS_SPEC_OBJECT(O)) return false; 755 if (!IS_SPEC_OBJECT(O)) return false;
765 var spreadable = O[symbolIsConcatSpreadable]; 756 var spreadable = O[symbolIsConcatSpreadable];
766 if (IS_UNDEFINED(spreadable)) return IS_ARRAY(O); 757 if (IS_UNDEFINED(spreadable)) return IS_ARRAY(O);
767 return ToBoolean(spreadable); 758 return ToBoolean(spreadable);
768 } 759 }
769 760
770 761
771 // ECMA-262, section 8.6.2.6, page 28. 762 // ECMA-262, section 8.6.2.6, page 28.
772 function DefaultNumber(x) { 763 function DefaultNumber(x) {
773 var valueOf = x.valueOf; 764 var valueOf = x.valueOf;
774 if (IS_SPEC_FUNCTION(valueOf)) { 765 if (IS_CALLABLE(valueOf)) {
775 var v = %_CallFunction(x, valueOf); 766 var v = %_CallFunction(x, valueOf);
776 if (IS_SYMBOL(v)) throw MakeTypeError(kSymbolToNumber); 767 if (IS_SYMBOL(v)) throw MakeTypeError(kSymbolToNumber);
777 if (IS_SIMD_VALUE(x)) throw MakeTypeError(kSimdToNumber); 768 if (IS_SIMD_VALUE(x)) throw MakeTypeError(kSimdToNumber);
778 if (IsPrimitive(v)) return v; 769 if (IsPrimitive(v)) return v;
779 } 770 }
780 var toString = x.toString; 771 var toString = x.toString;
781 if (IS_SPEC_FUNCTION(toString)) { 772 if (IS_CALLABLE(toString)) {
782 var s = %_CallFunction(x, toString); 773 var s = %_CallFunction(x, toString);
783 if (IsPrimitive(s)) return s; 774 if (IsPrimitive(s)) return s;
784 } 775 }
785 throw MakeTypeError(kCannotConvertToPrimitive); 776 throw MakeTypeError(kCannotConvertToPrimitive);
786 } 777 }
787 778
788 // ECMA-262, section 8.6.2.6, page 28. 779 // ECMA-262, section 8.6.2.6, page 28.
789 function DefaultString(x) { 780 function DefaultString(x) {
790 if (!IS_SYMBOL_WRAPPER(x)) { 781 if (!IS_SYMBOL_WRAPPER(x)) {
791 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToString); 782 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToString);
792 var toString = x.toString; 783 var toString = x.toString;
793 if (IS_SPEC_FUNCTION(toString)) { 784 if (IS_CALLABLE(toString)) {
794 var s = %_CallFunction(x, toString); 785 var s = %_CallFunction(x, toString);
795 if (IsPrimitive(s)) return s; 786 if (IsPrimitive(s)) return s;
796 } 787 }
797 788
798 var valueOf = x.valueOf; 789 var valueOf = x.valueOf;
799 if (IS_SPEC_FUNCTION(valueOf)) { 790 if (IS_CALLABLE(valueOf)) {
800 var v = %_CallFunction(x, valueOf); 791 var v = %_CallFunction(x, valueOf);
801 if (IsPrimitive(v)) return v; 792 if (IsPrimitive(v)) return v;
802 } 793 }
803 } 794 }
804 throw MakeTypeError(kCannotConvertToPrimitive); 795 throw MakeTypeError(kCannotConvertToPrimitive);
805 } 796 }
806 797
807 function ToPositiveInteger(x, rangeErrorIndex) { 798 function ToPositiveInteger(x, rangeErrorIndex) {
808 var i = TO_INTEGER_MAP_MINUS_ZERO(x); 799 var i = TO_INTEGER_MAP_MINUS_ZERO(x);
809 if (i < 0) throw MakeRangeError(rangeErrorIndex); 800 if (i < 0) throw MakeRangeError(rangeErrorIndex);
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
895 utils.Export(function(to) { 886 utils.Export(function(to) {
896 to.ToBoolean = ToBoolean; 887 to.ToBoolean = ToBoolean;
897 to.ToLength = ToLength; 888 to.ToLength = ToLength;
898 to.ToName = ToName; 889 to.ToName = ToName;
899 to.ToNumber = ToNumber; 890 to.ToNumber = ToNumber;
900 to.ToPrimitive = ToPrimitive; 891 to.ToPrimitive = ToPrimitive;
901 to.ToString = ToString; 892 to.ToString = ToString;
902 }); 893 });
903 894
904 }) 895 })
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698