OLD | NEW |
---|---|
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 Loading... | |
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 char* message_buffer, | |
3713 intptr_t message_buffer_size) const { | |
3695 if (num_arguments > NumberOfParameters()) { | 3714 if (num_arguments > NumberOfParameters()) { |
3715 if (message_buffer_size > 0) { | |
srdjan
2012/07/12 00:06:37
|| message_buffer == NULL
regis
2012/07/12 17:37:36
OS::SNPrint will work with a NULL buffer, as long
| |
3716 // Hide implicit parameters to the user. | |
3717 const intptr_t num_hidden_params = NumberOfImplicitParameters(); | |
3718 OS::SNPrint(message_buffer, | |
3719 message_buffer_size, | |
3720 "%d passed, %s%d expected", | |
3721 num_arguments - num_hidden_params, | |
3722 num_optional_parameters() > 0 ? "at most " : "", | |
3723 NumberOfParameters() - num_hidden_params); | |
3724 } | |
3696 return false; // Too many arguments. | 3725 return false; // Too many arguments. |
3697 } | 3726 } |
3698 const int num_positional_args = num_arguments - num_named_arguments; | 3727 const int num_positional_args = num_arguments - num_named_arguments; |
3699 if (num_positional_args < num_fixed_parameters()) { | 3728 if (num_positional_args < num_fixed_parameters()) { |
3729 if (message_buffer_size > 0) { | |
srdjan
2012/07/12 00:06:37
ditto
regis
2012/07/12 17:37:36
ditto
| |
3730 // Hide implicit parameters to the user. | |
3731 const intptr_t num_hidden_params = NumberOfImplicitParameters(); | |
3732 OS::SNPrint(message_buffer, | |
3733 message_buffer_size, | |
3734 "%d %spassed, %d expected", | |
3735 num_positional_args - num_hidden_params, | |
3736 num_optional_parameters() > 0 ? "positional " : "", | |
3737 num_fixed_parameters() - num_hidden_params); | |
3738 } | |
3700 return false; // Too few arguments. | 3739 return false; // Too few arguments. |
3701 } | 3740 } |
3702 return true; | 3741 return true; |
3703 } | 3742 } |
3704 | 3743 |
3705 | 3744 |
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, | 3745 bool Function::AreValidArguments(int num_arguments, |
3709 const Array& argument_names) const { | 3746 const Array& argument_names, |
3747 char* message_buffer, | |
3748 intptr_t message_buffer_size) const { | |
3710 const int num_named_arguments = | 3749 const int num_named_arguments = |
3711 argument_names.IsNull() ? 0 : argument_names.Length(); | 3750 argument_names.IsNull() ? 0 : argument_names.Length(); |
3712 if (!AreValidArgumentCounts(num_arguments, num_named_arguments)) { | 3751 if (!AreValidArgumentCounts(num_arguments, |
3752 num_named_arguments, | |
3753 message_buffer, | |
3754 message_buffer_size)) { | |
3713 return false; | 3755 return false; |
3714 } | 3756 } |
3715 // Verify that all argument names are valid parameter names. | 3757 // Verify that all argument names are valid parameter names. |
3716 String& argument_name = String::Handle(); | 3758 String& argument_name = String::Handle(); |
3717 String& parameter_name = String::Handle(); | 3759 String& parameter_name = String::Handle(); |
3718 for (int i = 0; i < num_named_arguments; i++) { | 3760 for (int i = 0; i < num_named_arguments; i++) { |
3719 argument_name ^= argument_names.At(i); | 3761 argument_name ^= argument_names.At(i); |
3720 ASSERT(argument_name.IsSymbol()); | 3762 ASSERT(argument_name.IsSymbol()); |
3721 bool found = false; | 3763 bool found = false; |
3722 const int num_positional_args = num_arguments - num_named_arguments; | 3764 const int num_positional_args = num_arguments - num_named_arguments; |
3723 const int num_parameters = NumberOfParameters(); | 3765 const int num_parameters = NumberOfParameters(); |
3724 for (int j = num_positional_args; !found && (j < num_parameters); j++) { | 3766 for (int j = num_positional_args; !found && (j < num_parameters); j++) { |
3725 parameter_name ^= ParameterNameAt(j); | 3767 parameter_name ^= ParameterNameAt(j); |
3726 ASSERT(argument_name.IsSymbol()); | 3768 ASSERT(argument_name.IsSymbol()); |
3727 if (argument_name.Equals(parameter_name)) { | 3769 if (argument_name.Equals(parameter_name)) { |
3728 found = true; | 3770 found = true; |
3729 } | 3771 } |
3730 } | 3772 } |
3731 if (!found) { | 3773 if (!found) { |
3774 if (message_buffer_size > 0) { | |
srdjan
2012/07/12 00:06:37
|| (message_buffer == NULL)
regis
2012/07/12 17:37:36
ditto
| |
3775 OS::SNPrint(message_buffer, | |
3776 message_buffer_size, | |
3777 "no optional formal parameter named '%s'", | |
3778 argument_name.ToCString()); | |
3779 } | |
3732 return false; | 3780 return false; |
3733 } | 3781 } |
3734 } | 3782 } |
3735 return true; | 3783 return true; |
3736 } | 3784 } |
3737 | 3785 |
3738 | 3786 |
3739 // Helper allocating a C string buffer in the zone, printing the fully qualified | 3787 // 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 | 3788 // name of a function in it, and replacing ':' by '_' to make sure the |
3741 // constructed name is a valid C++ identifier for debugging purpose. | 3789 // constructed name is a valid C++ identifier for debugging purpose. |
(...skipping 6913 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
10655 const String& str = String::Handle(pattern()); | 10703 const String& str = String::Handle(pattern()); |
10656 const char* format = "JSRegExp: pattern=%s flags=%s"; | 10704 const char* format = "JSRegExp: pattern=%s flags=%s"; |
10657 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags()); | 10705 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags()); |
10658 char* chars = reinterpret_cast<char*>( | 10706 char* chars = reinterpret_cast<char*>( |
10659 Isolate::Current()->current_zone()->Allocate(len + 1)); | 10707 Isolate::Current()->current_zone()->Allocate(len + 1)); |
10660 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags()); | 10708 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags()); |
10661 return chars; | 10709 return chars; |
10662 } | 10710 } |
10663 | 10711 |
10664 } // namespace dart | 10712 } // namespace dart |
OLD | NEW |