Introduce a flag to disable string operator +

If allow_string_plus is set to false, the compiler will report an error
when it finds a string literal followed by +. Invoking the + operator on a
string value throws a noSuchMethod exception.

This is temporary code that we'll eliminate once the + operator on
strings is completely removed.
Review URL: https://chromiumcodereview.appspot.com//9960084

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@6423 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
hausner@google.com
2012-04-11 18:23:22 +00:00
parent d1d3235013
commit 2fc3bb679d
4 changed files with 36 additions and 3 deletions
+21
View File
@@ -117,6 +117,27 @@ DEFINE_NATIVE_ENTRY(String_concat, 2) {
}
// TODO(hausner): Remove obsolete String_plus.
DECLARE_FLAG(bool, allow_string_plus);
DEFINE_NATIVE_ENTRY(String_plus, 2) {
const String& receiver = String::CheckedHandle(arguments->At(0));
GET_NATIVE_ARGUMENT(String, b, arguments->At(1));
if (!FLAG_allow_string_plus) {
// Throw a noSuchMethod exception if operator + is not supported.
const String& func_name = String::Handle(String::New("+"));
const Array& func_args = Array::Handle(Array::New(1));
func_args.SetAt(0, b);
GrowableArray<const Object*> dart_arguments(3);
dart_arguments.Add(&receiver);
dart_arguments.Add(&func_name);
dart_arguments.Add(&func_args);
Exceptions::ThrowByType(Exceptions::kNoSuchMethod, dart_arguments);
}
const String& result = String::Handle(String::Concat(receiver, b));
arguments->SetReturn(result);
}
DEFINE_NATIVE_ENTRY(String_toLowerCase, 1) {
const String& receiver = String::CheckedHandle(arguments->At(0));
ASSERT(!receiver.IsNull());
+7 -1
View File
@@ -232,12 +232,18 @@ class StringBase {
return buffer.add(this.substring(startIndex)).toString();
}
/**
* TODO(hausner): remove support for operator + and native method plus().
*/
String plus(String other) native "String_plus";
/**
* Convert argument obj to string and concat it with this string.
* Returns concatenated string.
* TODO(hausner): remove support for +.
*/
String operator +(Object obj) {
return this.concat(obj.toString());
return this.plus(obj.toString());
}
/**
+1
View File
@@ -76,6 +76,7 @@ namespace dart {
V(String_charAt, 2) \
V(String_charCodeAt, 2) \
V(String_concat, 2) \
V(String_plus, 2) \
V(String_toLowerCase, 1) \
V(String_toUpperCase, 1) \
V(Strings_concatAll, 1) \
+7 -2
View File
@@ -26,6 +26,7 @@ DEFINE_FLAG(bool, enable_type_checks, false, "Enable type checks.");
DEFINE_FLAG(bool, trace_parser, false, "Trace parser operations.");
DEFINE_FLAG(bool, warning_as_error, false, "Treat warnings as errors.");
DEFINE_FLAG(bool, silent_warnings, false, "Silence warnings.");
DEFINE_FLAG(bool, allow_string_plus, true, "Allow + operator on strings.");
static void CheckedModeHandler(bool value) {
FLAG_enable_asserts = value;
@@ -5702,8 +5703,12 @@ AstNode* Parser::ParseBinaryExpr(int min_preced) {
if (left_operand->IsLiteralNode()) {
LiteralNode* lit = left_operand->AsLiteralNode();
if (lit->literal().IsString()) {
str_concat = new StringConcatNode(lit->token_index());
str_concat->AddExpr(lit);
if (FLAG_allow_string_plus) {
str_concat = new StringConcatNode(lit->token_index());
str_concat->AddExpr(lit);
} else {
ErrorMsg(op_pos, "operator + on strings no longer allowed");
}
}
} else if (left_operand->IsStringConcatNode()) {
str_concat = left_operand->AsStringConcatNode();