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

Side by Side Diff: runtime/vm/object.cc

Issue 10695137: Provide better error message when passing wrong number of arguments. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 5 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 | Annotate | Revision Log
« no previous file with comments | « runtime/vm/object.h ('k') | runtime/vm/parser.cc » ('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 (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/object.h" 5 #include "vm/object.h"
6 6
7 #include "platform/assert.h" 7 #include "platform/assert.h"
8 #include "vm/assembler.h" 8 #include "vm/assembler.h"
9 #include "vm/bigint_operations.h" 9 #include "vm/bigint_operations.h"
10 #include "vm/bootstrap.h" 10 #include "vm/bootstrap.h"
(...skipping 3672 matching lines...) Expand 10 before | Expand all | Expand 10 after
3683 void Function::set_is_native(bool value) const { 3683 void Function::set_is_native(bool value) const {
3684 raw_ptr()->is_native_ = value; 3684 raw_ptr()->is_native_ = value;
3685 } 3685 }
3686 3686
3687 3687
3688 intptr_t Function::NumberOfParameters() const { 3688 intptr_t Function::NumberOfParameters() const {
3689 return num_fixed_parameters() + num_optional_parameters(); 3689 return num_fixed_parameters() + num_optional_parameters();
3690 } 3690 }
3691 3691
3692 3692
3693 intptr_t Function::NumberOfImplicitParameters() const {
3694 if (kind() == RawFunction::kConstructor) {
3695 if (is_static()) {
3696 ASSERT(IsFactory());
3697 return 1; // Type arguments.
3698 } else {
3699 ASSERT(IsConstructor());
3700 return 2; // Instance, phase.
3701 }
3702 }
3703 if (!is_static()) {
3704 return 1; // Receiver.
3705 }
3706 return 0; // No implicit parameters.
3707 }
3708
3709
3693 bool Function::AreValidArgumentCounts(int num_arguments, 3710 bool Function::AreValidArgumentCounts(int num_arguments,
3694 int num_named_arguments) const { 3711 int num_named_arguments,
3712 String* error_message) const {
3695 if (num_arguments > NumberOfParameters()) { 3713 if (num_arguments > NumberOfParameters()) {
3714 if (error_message != NULL) {
3715 const intptr_t kMessageBufferSize = 64;
3716 char message_buffer[kMessageBufferSize];
3717 // Hide implicit parameters to the user.
3718 const intptr_t num_hidden_params = NumberOfImplicitParameters();
3719 OS::SNPrint(message_buffer,
3720 kMessageBufferSize,
3721 "%d passed, %s%d expected",
3722 num_arguments - num_hidden_params,
3723 num_optional_parameters() > 0 ? "at most " : "",
3724 NumberOfParameters() - num_hidden_params);
3725 *error_message = String::New(message_buffer);
3726 }
3696 return false; // Too many arguments. 3727 return false; // Too many arguments.
3697 } 3728 }
3698 const int num_positional_args = num_arguments - num_named_arguments; 3729 const int num_positional_args = num_arguments - num_named_arguments;
3699 if (num_positional_args < num_fixed_parameters()) { 3730 if (num_positional_args < num_fixed_parameters()) {
3731 if (error_message != NULL) {
3732 const intptr_t kMessageBufferSize = 64;
3733 char message_buffer[kMessageBufferSize];
3734 // Hide implicit parameters to the user.
3735 const intptr_t num_hidden_params = NumberOfImplicitParameters();
3736 OS::SNPrint(message_buffer,
3737 kMessageBufferSize,
3738 "%d %spassed, %d expected",
3739 num_positional_args - num_hidden_params,
3740 num_optional_parameters() > 0 ? "positional " : "",
3741 num_fixed_parameters() - num_hidden_params);
3742 *error_message = String::New(message_buffer);
3743 }
3700 return false; // Too few arguments. 3744 return false; // Too few arguments.
3701 } 3745 }
3702 return true; 3746 return true;
3703 } 3747 }
3704 3748
3705 3749
3706 // TODO(regis): Return some sort of diagnosis information so that we
3707 // can improve the error messages generated by the parser.
3708 bool Function::AreValidArguments(int num_arguments, 3750 bool Function::AreValidArguments(int num_arguments,
3709 const Array& argument_names) const { 3751 const Array& argument_names,
3752 String* error_message) const {
3710 const int num_named_arguments = 3753 const int num_named_arguments =
3711 argument_names.IsNull() ? 0 : argument_names.Length(); 3754 argument_names.IsNull() ? 0 : argument_names.Length();
3712 if (!AreValidArgumentCounts(num_arguments, num_named_arguments)) { 3755 if (!AreValidArgumentCounts(num_arguments,
3756 num_named_arguments,
3757 error_message)) {
3713 return false; 3758 return false;
3714 } 3759 }
3715 // Verify that all argument names are valid parameter names. 3760 // Verify that all argument names are valid parameter names.
3716 String& argument_name = String::Handle(); 3761 String& argument_name = String::Handle();
3717 String& parameter_name = String::Handle(); 3762 String& parameter_name = String::Handle();
3718 for (int i = 0; i < num_named_arguments; i++) { 3763 for (int i = 0; i < num_named_arguments; i++) {
3719 argument_name ^= argument_names.At(i); 3764 argument_name ^= argument_names.At(i);
3720 ASSERT(argument_name.IsSymbol()); 3765 ASSERT(argument_name.IsSymbol());
3721 bool found = false; 3766 bool found = false;
3722 const int num_positional_args = num_arguments - num_named_arguments; 3767 const int num_positional_args = num_arguments - num_named_arguments;
3723 const int num_parameters = NumberOfParameters(); 3768 const int num_parameters = NumberOfParameters();
3724 for (int j = num_positional_args; !found && (j < num_parameters); j++) { 3769 for (int j = num_positional_args; !found && (j < num_parameters); j++) {
3725 parameter_name ^= ParameterNameAt(j); 3770 parameter_name ^= ParameterNameAt(j);
3726 ASSERT(argument_name.IsSymbol()); 3771 ASSERT(argument_name.IsSymbol());
3727 if (argument_name.Equals(parameter_name)) { 3772 if (argument_name.Equals(parameter_name)) {
3728 found = true; 3773 found = true;
3729 } 3774 }
3730 } 3775 }
3731 if (!found) { 3776 if (!found) {
3777 if (error_message != NULL) {
3778 const intptr_t kMessageBufferSize = 64;
3779 char message_buffer[kMessageBufferSize];
3780 OS::SNPrint(message_buffer,
3781 kMessageBufferSize,
3782 "no optional formal parameter named '%s'",
3783 argument_name.ToCString());
3784 *error_message = String::New(message_buffer);
3785 }
3732 return false; 3786 return false;
3733 } 3787 }
3734 } 3788 }
3735 return true; 3789 return true;
3736 } 3790 }
3737 3791
3738 3792
3739 // Helper allocating a C string buffer in the zone, printing the fully qualified 3793 // Helper allocating a C string buffer in the zone, printing the fully qualified
3740 // name of a function in it, and replacing ':' by '_' to make sure the 3794 // name of a function in it, and replacing ':' by '_' to make sure the
3741 // constructed name is a valid C++ identifier for debugging purpose. 3795 // constructed name is a valid C++ identifier for debugging purpose.
(...skipping 6913 matching lines...) Expand 10 before | Expand all | Expand 10 after
10655 const String& str = String::Handle(pattern()); 10709 const String& str = String::Handle(pattern());
10656 const char* format = "JSRegExp: pattern=%s flags=%s"; 10710 const char* format = "JSRegExp: pattern=%s flags=%s";
10657 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags()); 10711 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags());
10658 char* chars = reinterpret_cast<char*>( 10712 char* chars = reinterpret_cast<char*>(
10659 Isolate::Current()->current_zone()->Allocate(len + 1)); 10713 Isolate::Current()->current_zone()->Allocate(len + 1));
10660 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags()); 10714 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags());
10661 return chars; 10715 return chars;
10662 } 10716 }
10663 10717
10664 } // namespace dart 10718 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/object.h ('k') | runtime/vm/parser.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698