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

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: Rebase again. 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
« no previous file with comments | « src/proxy.js ('k') | src/runtime/runtime.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 423 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 return this < x.length; 434 return this < x.length;
435 } 435 }
436 return %HasElement(x, this); 436 return %HasElement(x, this);
437 } 437 }
438 return %HasProperty(x, this); 438 return %HasProperty(x, this);
439 } 439 }
440 440
441 441
442 function CALL_NON_FUNCTION() { 442 function CALL_NON_FUNCTION() {
443 var delegate = %GetFunctionDelegate(this); 443 var delegate = %GetFunctionDelegate(this);
444 if (!IS_FUNCTION(delegate)) {
445 var callsite = %RenderCallSite();
446 if (callsite == "") callsite = typeof this;
447 throw %make_type_error(kCalledNonCallable, callsite);
448 }
449 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength()); 444 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
450 } 445 }
451 446
452 447
453 function CALL_NON_FUNCTION_AS_CONSTRUCTOR() { 448 function CALL_NON_FUNCTION_AS_CONSTRUCTOR() {
454 var delegate = %GetConstructorDelegate(this); 449 var delegate = %GetConstructorDelegate(this);
455 if (!IS_FUNCTION(delegate)) {
456 var callsite = %RenderCallSite();
457 if (callsite == "") callsite = typeof this;
458 throw %make_type_error(kCalledNonCallable, callsite);
459 }
460 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength()); 450 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
461 } 451 }
462 452
463 453
464 function CALL_FUNCTION_PROXY() { 454 function CALL_FUNCTION_PROXY() {
465 var arity = %_ArgumentsLength() - 1; 455 var arity = %_ArgumentsLength() - 1;
466 var proxy = %_Arguments(arity); // The proxy comes in as an additional arg. 456 var proxy = %_Arguments(arity); // The proxy comes in as an additional arg.
467 var trap = %GetCallTrap(proxy); 457 var trap = %GetCallTrap(proxy);
468 return %Apply(trap, this, arguments, 0, arity); 458 return %Apply(trap, this, arguments, 0, arity);
469 } 459 }
470 460
471 461
472 function CALL_FUNCTION_PROXY_AS_CONSTRUCTOR () { 462 function CALL_FUNCTION_PROXY_AS_CONSTRUCTOR () {
473 var proxy = this; 463 var proxy = this;
474 var trap = %GetConstructTrap(proxy); 464 var trap = %GetConstructTrap(proxy);
475 return %Apply(trap, this, arguments, 0, %_ArgumentsLength()); 465 return %Apply(trap, this, arguments, 0, %_ArgumentsLength());
476 } 466 }
477 467
478 468
479 function APPLY_PREPARE(args) { 469 function APPLY_PREPARE(args) {
480 var length; 470 var length;
481 // First check whether length is a positive Smi and args is an 471 // First check whether length is a positive Smi and args is an
482 // array. This is the fast case. If this fails, we do the slow case 472 // array. This is the fast case. If this fails, we do the slow case
483 // that takes care of more eventualities. 473 // that takes care of more eventualities.
484 if (IS_ARRAY(args)) { 474 if (IS_ARRAY(args)) {
485 length = args.length; 475 length = args.length;
486 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength && 476 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength &&
487 IS_SPEC_FUNCTION(this)) { 477 IS_CALLABLE(this)) {
488 return length; 478 return length;
489 } 479 }
490 } 480 }
491 481
492 length = (args == null) ? 0 : TO_UINT32(args.length); 482 length = (args == null) ? 0 : TO_UINT32(args.length);
493 483
494 // We can handle any number of apply arguments if the stack is 484 // We can handle any number of apply arguments if the stack is
495 // big enough, but sanity check the value to avoid overflow when 485 // big enough, but sanity check the value to avoid overflow when
496 // multiplying with pointer size. 486 // multiplying with pointer size.
497 if (length > kSafeArgumentsLength) throw %make_range_error(kStackOverflow); 487 if (length > kSafeArgumentsLength) throw %make_range_error(kStackOverflow);
498 488
499 if (!IS_SPEC_FUNCTION(this)) { 489 if (!IS_CALLABLE(this)) {
500 throw %make_type_error(kApplyNonFunction, %to_string_fun(this), typeof this) ; 490 throw %make_type_error(kApplyNonFunction, %to_string_fun(this),
491 typeof this);
501 } 492 }
502 493
503 // Make sure the arguments list has the right type. 494 // Make sure the arguments list has the right type.
504 if (args != null && !IS_SPEC_OBJECT(args)) { 495 if (args != null && !IS_SPEC_OBJECT(args)) {
505 throw %make_type_error(kWrongArgs, "Function.prototype.apply"); 496 throw %make_type_error(kWrongArgs, "Function.prototype.apply");
506 } 497 }
507 498
508 // Return the length which is the number of arguments to copy to the 499 // Return the length which is the number of arguments to copy to the
509 // stack. It is guaranteed to be a small integer at this point. 500 // stack. It is guaranteed to be a small integer at this point.
510 return length; 501 return length;
511 } 502 }
512 503
513 504
514 function REFLECT_APPLY_PREPARE(args) { 505 function REFLECT_APPLY_PREPARE(args) {
515 var length; 506 var length;
516 // First check whether length is a positive Smi and args is an 507 // First check whether length is a positive Smi and args is an
517 // array. This is the fast case. If this fails, we do the slow case 508 // array. This is the fast case. If this fails, we do the slow case
518 // that takes care of more eventualities. 509 // that takes care of more eventualities.
519 if (IS_ARRAY(args)) { 510 if (IS_ARRAY(args)) {
520 length = args.length; 511 length = args.length;
521 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength && 512 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength &&
522 IS_SPEC_FUNCTION(this)) { 513 IS_CALLABLE(this)) {
523 return length; 514 return length;
524 } 515 }
525 } 516 }
526 517
527 if (!IS_SPEC_FUNCTION(this)) { 518 if (!IS_CALLABLE(this)) {
528 throw %make_type_error(kCalledNonCallable, %to_string_fun(this)); 519 throw %make_type_error(kCalledNonCallable, %to_string_fun(this));
529 } 520 }
530 521
531 if (!IS_SPEC_OBJECT(args)) { 522 if (!IS_SPEC_OBJECT(args)) {
532 throw %make_type_error(kWrongArgs, "Reflect.apply"); 523 throw %make_type_error(kWrongArgs, "Reflect.apply");
533 } 524 }
534 525
535 length = %to_length_fun(args.length); 526 length = %to_length_fun(args.length);
536 527
537 // We can handle any number of apply arguments if the stack is 528 // We can handle any number of apply arguments if the stack is
538 // big enough, but sanity check the value to avoid overflow when 529 // big enough, but sanity check the value to avoid overflow when
539 // multiplying with pointer size. 530 // multiplying with pointer size.
540 if (length > kSafeArgumentsLength) throw %make_range_error(kStackOverflow); 531 if (length > kSafeArgumentsLength) throw %make_range_error(kStackOverflow);
541 532
542 // Return the length which is the number of arguments to copy to the 533 // Return the length which is the number of arguments to copy to the
543 // stack. It is guaranteed to be a small integer at this point. 534 // stack. It is guaranteed to be a small integer at this point.
544 return length; 535 return length;
545 } 536 }
546 537
547 538
548 function REFLECT_CONSTRUCT_PREPARE( 539 function REFLECT_CONSTRUCT_PREPARE(
549 args, newTarget) { 540 args, newTarget) {
550 var length; 541 var length;
551 var ctorOk = IS_SPEC_FUNCTION(this) && %IsConstructor(this); 542 var ctorOk = IS_CALLABLE(this) && %IsConstructor(this);
552 var newTargetOk = IS_SPEC_FUNCTION(newTarget) && %IsConstructor(newTarget); 543 var newTargetOk = IS_CALLABLE(newTarget) && %IsConstructor(newTarget);
553 544
554 // First check whether length is a positive Smi and args is an 545 // First check whether length is a positive Smi and args is an
555 // array. This is the fast case. If this fails, we do the slow case 546 // array. This is the fast case. If this fails, we do the slow case
556 // that takes care of more eventualities. 547 // that takes care of more eventualities.
557 if (IS_ARRAY(args)) { 548 if (IS_ARRAY(args)) {
558 length = args.length; 549 length = args.length;
559 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength && 550 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength &&
560 ctorOk && newTargetOk) { 551 ctorOk && newTargetOk) {
561 return length; 552 return length;
562 } 553 }
563 } 554 }
564 555
565 if (!ctorOk) { 556 if (!ctorOk) {
566 if (!IS_SPEC_FUNCTION(this)) { 557 if (!IS_CALLABLE(this)) {
567 throw %make_type_error(kCalledNonCallable, %to_string_fun(this)); 558 throw %make_type_error(kCalledNonCallable, %to_string_fun(this));
568 } else { 559 } else {
569 throw %make_type_error(kNotConstructor, %to_string_fun(this)); 560 throw %make_type_error(kNotConstructor, %to_string_fun(this));
570 } 561 }
571 } 562 }
572 563
573 if (!newTargetOk) { 564 if (!newTargetOk) {
574 if (!IS_SPEC_FUNCTION(newTarget)) { 565 if (!IS_CALLABLE(newTarget)) {
575 throw %make_type_error(kCalledNonCallable, %to_string_fun(newTarget)); 566 throw %make_type_error(kCalledNonCallable, %to_string_fun(newTarget));
576 } else { 567 } else {
577 throw %make_type_error(kNotConstructor, %to_string_fun(newTarget)); 568 throw %make_type_error(kNotConstructor, %to_string_fun(newTarget));
578 } 569 }
579 } 570 }
580 571
581 if (!IS_SPEC_OBJECT(args)) { 572 if (!IS_SPEC_OBJECT(args)) {
582 throw %make_type_error(kWrongArgs, "Reflect.construct"); 573 throw %make_type_error(kWrongArgs, "Reflect.construct");
583 } 574 }
584 575
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
744 if (!IS_SPEC_OBJECT(O)) return false; 735 if (!IS_SPEC_OBJECT(O)) return false;
745 var spreadable = O[isConcatSpreadableSymbol]; 736 var spreadable = O[isConcatSpreadableSymbol];
746 if (IS_UNDEFINED(spreadable)) return IS_ARRAY(O); 737 if (IS_UNDEFINED(spreadable)) return IS_ARRAY(O);
747 return ToBoolean(spreadable); 738 return ToBoolean(spreadable);
748 } 739 }
749 740
750 741
751 // ECMA-262, section 8.6.2.6, page 28. 742 // ECMA-262, section 8.6.2.6, page 28.
752 function DefaultNumber(x) { 743 function DefaultNumber(x) {
753 var valueOf = x.valueOf; 744 var valueOf = x.valueOf;
754 if (IS_SPEC_FUNCTION(valueOf)) { 745 if (IS_CALLABLE(valueOf)) {
755 var v = %_CallFunction(x, valueOf); 746 var v = %_CallFunction(x, valueOf);
756 if (IS_SYMBOL(v)) throw MakeTypeError(kSymbolToNumber); 747 if (IS_SYMBOL(v)) throw MakeTypeError(kSymbolToNumber);
757 if (IS_SIMD_VALUE(x)) throw MakeTypeError(kSimdToNumber); 748 if (IS_SIMD_VALUE(x)) throw MakeTypeError(kSimdToNumber);
758 if (IsPrimitive(v)) return v; 749 if (IsPrimitive(v)) return v;
759 } 750 }
760 var toString = x.toString; 751 var toString = x.toString;
761 if (IS_SPEC_FUNCTION(toString)) { 752 if (IS_CALLABLE(toString)) {
762 var s = %_CallFunction(x, toString); 753 var s = %_CallFunction(x, toString);
763 if (IsPrimitive(s)) return s; 754 if (IsPrimitive(s)) return s;
764 } 755 }
765 throw MakeTypeError(kCannotConvertToPrimitive); 756 throw MakeTypeError(kCannotConvertToPrimitive);
766 } 757 }
767 758
768 // ECMA-262, section 8.6.2.6, page 28. 759 // ECMA-262, section 8.6.2.6, page 28.
769 function DefaultString(x) { 760 function DefaultString(x) {
770 if (!IS_SYMBOL_WRAPPER(x)) { 761 if (!IS_SYMBOL_WRAPPER(x)) {
771 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToString); 762 if (IS_SYMBOL(x)) throw MakeTypeError(kSymbolToString);
772 var toString = x.toString; 763 var toString = x.toString;
773 if (IS_SPEC_FUNCTION(toString)) { 764 if (IS_CALLABLE(toString)) {
774 var s = %_CallFunction(x, toString); 765 var s = %_CallFunction(x, toString);
775 if (IsPrimitive(s)) return s; 766 if (IsPrimitive(s)) return s;
776 } 767 }
777 768
778 var valueOf = x.valueOf; 769 var valueOf = x.valueOf;
779 if (IS_SPEC_FUNCTION(valueOf)) { 770 if (IS_CALLABLE(valueOf)) {
780 var v = %_CallFunction(x, valueOf); 771 var v = %_CallFunction(x, valueOf);
781 if (IsPrimitive(v)) return v; 772 if (IsPrimitive(v)) return v;
782 } 773 }
783 } 774 }
784 throw MakeTypeError(kCannotConvertToPrimitive); 775 throw MakeTypeError(kCannotConvertToPrimitive);
785 } 776 }
786 777
787 function ToPositiveInteger(x, rangeErrorIndex) { 778 function ToPositiveInteger(x, rangeErrorIndex) {
788 var i = TO_INTEGER_MAP_MINUS_ZERO(x); 779 var i = TO_INTEGER_MAP_MINUS_ZERO(x);
789 if (i < 0) throw MakeRangeError(rangeErrorIndex); 780 if (i < 0) throw MakeRangeError(rangeErrorIndex);
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
868 859
869 utils.Export(function(to) { 860 utils.Export(function(to) {
870 to.ToBoolean = ToBoolean; 861 to.ToBoolean = ToBoolean;
871 to.ToLength = ToLength; 862 to.ToLength = ToLength;
872 to.ToNumber = ToNumber; 863 to.ToNumber = ToNumber;
873 to.ToPrimitive = ToPrimitive; 864 to.ToPrimitive = ToPrimitive;
874 to.ToString = ToString; 865 to.ToString = ToString;
875 }); 866 });
876 867
877 }) 868 })
OLDNEW
« no previous file with comments | « src/proxy.js ('k') | src/runtime/runtime.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698