| 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 "include/dart_api.h" | 7 #include "include/dart_api.h" |
| 8 #include "platform/assert.h" | 8 #include "platform/assert.h" |
| 9 #include "vm/assembler.h" | 9 #include "vm/assembler.h" |
| 10 #include "vm/bigint_operations.h" | 10 #include "vm/bigint_operations.h" |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 if (is_setter) { | 180 if (is_setter) { |
| 181 // Setters need to end with '='. | 181 // Setters need to end with '='. |
| 182 const String& suffix = String::Handle(Symbols::Equals()); | 182 const String& suffix = String::Handle(Symbols::Equals()); |
| 183 return String::Concat(result, suffix); | 183 return String::Concat(result, suffix); |
| 184 } | 184 } |
| 185 | 185 |
| 186 return result.raw(); | 186 return result.raw(); |
| 187 } | 187 } |
| 188 | 188 |
| 189 | 189 |
| 190 template<typename type> |
| 191 static bool IsSpecialCharacter(type value) { |
| 192 return ((value == '"') || |
| 193 (value == '\n') || |
| 194 (value == '\f') || |
| 195 (value == '\b') || |
| 196 (value == '\t') || |
| 197 (value == '\v') || |
| 198 (value == '\r')); |
| 199 } |
| 200 |
| 201 |
| 202 template<typename type> |
| 203 static type SpecialCharacter(type value) { |
| 204 if (value == '"') { |
| 205 return '"'; |
| 206 } else if (value == '\n') { |
| 207 return 'n'; |
| 208 } else if (value == '\f') { |
| 209 return 'f'; |
| 210 } else if (value == '\b') { |
| 211 return 'b'; |
| 212 } else if (value == '\t') { |
| 213 return 't'; |
| 214 } else if (value == '\v') { |
| 215 return 'v'; |
| 216 } else if (value == '\r') { |
| 217 return 'r'; |
| 218 } |
| 219 UNREACHABLE(); |
| 220 return '\0'; |
| 221 } |
| 222 |
| 223 |
| 190 void Object::InitOnce() { | 224 void Object::InitOnce() { |
| 191 // TODO(iposva): NoGCScope needs to be added here. | 225 // TODO(iposva): NoGCScope needs to be added here. |
| 192 ASSERT(class_class() == null_); | 226 ASSERT(class_class() == null_); |
| 193 // Initialize the static vtable values. | 227 // Initialize the static vtable values. |
| 194 { | 228 { |
| 195 Object fake_object; | 229 Object fake_object; |
| 196 Smi fake_smi; | 230 Smi fake_smi; |
| 197 Object::handle_vtable_ = fake_object.vtable(); | 231 Object::handle_vtable_ = fake_object.vtable(); |
| 198 Smi::handle_vtable_ = fake_smi.vtable(); | 232 Smi::handle_vtable_ = fake_smi.vtable(); |
| 199 } | 233 } |
| (...skipping 4621 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4821 GrowableObjectArray::Handle(GrowableObjectArray::New(data.Length())); | 4855 GrowableObjectArray::Handle(GrowableObjectArray::New(data.Length())); |
| 4822 const String& private_key = String::Handle(PrivateKey()); | 4856 const String& private_key = String::Handle(PrivateKey()); |
| 4823 intptr_t private_len = private_key.Length(); | 4857 intptr_t private_len = private_key.Length(); |
| 4824 | 4858 |
| 4825 String& blank = String::Handle(String::New(" ")); | 4859 String& blank = String::Handle(String::New(" ")); |
| 4826 String& newline = String::Handle(String::New("\n")); | 4860 String& newline = String::Handle(String::New("\n")); |
| 4827 String& two_newlines = String::Handle(String::New("\n\n")); | 4861 String& two_newlines = String::Handle(String::New("\n\n")); |
| 4828 String& double_quotes = String::Handle(String::New("\"")); | 4862 String& double_quotes = String::Handle(String::New("\"")); |
| 4829 String& dollar = String::Handle(String::New("$")); | 4863 String& dollar = String::Handle(String::New("$")); |
| 4830 String& two_spaces = String::Handle(String::New(" ")); | 4864 String& two_spaces = String::Handle(String::New(" ")); |
| 4865 String& raw_string = String::Handle(String::New("@")); |
| 4831 | 4866 |
| 4832 Token::Kind curr = iterator.CurrentTokenKind(); | 4867 Token::Kind curr = iterator.CurrentTokenKind(); |
| 4833 Token::Kind prev = Token::kILLEGAL; | 4868 Token::Kind prev = Token::kILLEGAL; |
| 4834 // Handles used in the loop. | 4869 // Handles used in the loop. |
| 4835 Object& obj = Object::Handle(); | 4870 Object& obj = Object::Handle(); |
| 4836 String& literal = String::Handle(); | 4871 String& literal = String::Handle(); |
| 4837 // Current indentation level. | 4872 // Current indentation level. |
| 4838 int indent = 0; | 4873 int indent = 0; |
| 4839 | 4874 |
| 4840 while (curr != Token::kEOS) { | 4875 while (curr != Token::kEOS) { |
| 4841 // Remember current values for this token. | 4876 // Remember current values for this token. |
| 4842 obj = iterator.CurrentToken(); | 4877 obj = iterator.CurrentToken(); |
| 4843 literal = iterator.MakeLiteralToken(obj); | 4878 literal = iterator.MakeLiteralToken(obj); |
| 4844 // Advance to be able to use next token kind. | 4879 // Advance to be able to use next token kind. |
| 4845 iterator.Advance(); | 4880 iterator.Advance(); |
| 4846 Token::Kind next = iterator.CurrentTokenKind(); | 4881 Token::Kind next = iterator.CurrentTokenKind(); |
| 4847 | 4882 |
| 4848 // Handle the current token. | 4883 // Handle the current token. |
| 4849 if (curr == Token::kSTRING) { | 4884 if (curr == Token::kSTRING) { |
| 4850 bool escape_quotes = false; | 4885 bool is_raw_string = false; |
| 4886 bool escape_characters = false; |
| 4851 for (intptr_t i = 0; i < literal.Length(); i++) { | 4887 for (intptr_t i = 0; i < literal.Length(); i++) { |
| 4852 if (literal.CharAt(i) == '"') { | 4888 if (IsSpecialCharacter(literal.CharAt(i))) { |
| 4853 escape_quotes = true; | 4889 escape_characters = true; |
| 4854 break; | 4890 } |
| 4891 // TODO(4995): Temp solution for raw strings, this will break |
| 4892 // if we saw a string that is not a raw string but has back slashes |
| 4893 // in it. |
| 4894 if ((literal.CharAt(i) == '\\')) { |
| 4895 if ((next != Token::kINTERPOL_VAR) && |
| 4896 (next != Token::kINTERPOL_START) && |
| 4897 (prev != Token::kINTERPOL_VAR) && |
| 4898 (prev != Token::kINTERPOL_END)) { |
| 4899 is_raw_string = true; |
| 4900 } else { |
| 4901 escape_characters = true; |
| 4902 } |
| 4855 } | 4903 } |
| 4856 } | 4904 } |
| 4857 if ((prev != Token::kINTERPOL_VAR) && (prev != Token::kINTERPOL_END)) { | 4905 if ((prev != Token::kINTERPOL_VAR) && (prev != Token::kINTERPOL_END)) { |
| 4906 if (is_raw_string) { |
| 4907 literals.Add(raw_string); |
| 4908 } |
| 4858 literals.Add(double_quotes); | 4909 literals.Add(double_quotes); |
| 4859 } | 4910 } |
| 4860 if (escape_quotes) { | 4911 if (escape_characters) { |
| 4861 literal = String::EscapeDoubleQuotes(literal); | 4912 literal = String::EscapeSpecialCharacters(literal, is_raw_string); |
| 4862 literals.Add(literal); | 4913 literals.Add(literal); |
| 4863 } else { | 4914 } else { |
| 4864 literals.Add(literal); | 4915 literals.Add(literal); |
| 4865 } | 4916 } |
| 4866 if ((next != Token::kINTERPOL_VAR) && (next != Token::kINTERPOL_START)) { | 4917 if ((next != Token::kINTERPOL_VAR) && (next != Token::kINTERPOL_START)) { |
| 4867 literals.Add(double_quotes); | 4918 literals.Add(double_quotes); |
| 4868 } | 4919 } |
| 4869 } else if (curr == Token::kINTERPOL_VAR) { | 4920 } else if (curr == Token::kINTERPOL_VAR) { |
| 4870 literals.Add(dollar); | 4921 literals.Add(dollar); |
| 4922 if (literal.CharAt(0) == Scanner::kPrivateIdentifierStart) { |
| 4923 literal = String::SubString(literal, 0, literal.Length() - private_len); |
| 4924 } |
| 4871 literals.Add(literal); | 4925 literals.Add(literal); |
| 4872 } else if (curr == Token::kIDENT) { | 4926 } else if (curr == Token::kIDENT) { |
| 4873 if (literal.CharAt(0) == Scanner::kPrivateIdentifierStart) { | 4927 if (literal.CharAt(0) == Scanner::kPrivateIdentifierStart) { |
| 4874 literal = String::SubString(literal, 0, literal.Length() - private_len); | 4928 literal = String::SubString(literal, 0, literal.Length() - private_len); |
| 4875 } | 4929 } |
| 4876 literals.Add(literal); | 4930 literals.Add(literal); |
| 4877 } else { | 4931 } else { |
| 4878 literals.Add(literal); | 4932 literals.Add(literal); |
| 4879 } | 4933 } |
| 4880 // Determine the separation text based on this current token. | 4934 // Determine the separation text based on this current token. |
| (...skipping 4315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9196 const ExternalFourByteString& fourstr = | 9250 const ExternalFourByteString& fourstr = |
| 9197 ExternalFourByteString::Cast(src); | 9251 ExternalFourByteString::Cast(src); |
| 9198 NoGCScope no_gc; | 9252 NoGCScope no_gc; |
| 9199 String::Copy(dst, dst_offset, fourstr.CharAddr(0) + src_offset, len); | 9253 String::Copy(dst, dst_offset, fourstr.CharAddr(0) + src_offset, len); |
| 9200 } | 9254 } |
| 9201 } | 9255 } |
| 9202 } | 9256 } |
| 9203 } | 9257 } |
| 9204 | 9258 |
| 9205 | 9259 |
| 9206 RawString* String::EscapeDoubleQuotes(const String& str) { | 9260 RawString* String::EscapeSpecialCharacters(const String& str, bool raw_str) { |
| 9207 if (str.IsOneByteString()) { | 9261 if (str.IsOneByteString()) { |
| 9208 const OneByteString& onestr = OneByteString::Cast(str); | 9262 const OneByteString& onestr = OneByteString::Cast(str); |
| 9209 return onestr.EscapeDoubleQuotes(); | 9263 return onestr.EscapeSpecialCharacters(raw_str); |
| 9210 } | 9264 } |
| 9211 if (str.IsTwoByteString()) { | 9265 if (str.IsTwoByteString()) { |
| 9212 const TwoByteString& twostr = TwoByteString::Cast(str); | 9266 const TwoByteString& twostr = TwoByteString::Cast(str); |
| 9213 return twostr.EscapeDoubleQuotes(); | 9267 return twostr.EscapeSpecialCharacters(raw_str); |
| 9214 } | 9268 } |
| 9215 ASSERT(str.IsFourByteString()); | 9269 ASSERT(str.IsFourByteString()); |
| 9216 const FourByteString& fourstr = FourByteString::Cast(str); | 9270 const FourByteString& fourstr = FourByteString::Cast(str); |
| 9217 return fourstr.EscapeDoubleQuotes(); | 9271 return fourstr.EscapeSpecialCharacters(raw_str); |
| 9218 } | 9272 } |
| 9219 | 9273 |
| 9220 | 9274 |
| 9221 RawString* String::NewFormatted(const char* format, ...) { | 9275 RawString* String::NewFormatted(const char* format, ...) { |
| 9222 va_list args; | 9276 va_list args; |
| 9223 va_start(args, format); | 9277 va_start(args, format); |
| 9224 intptr_t len = OS::VSNPrint(NULL, 0, format, args); | 9278 intptr_t len = OS::VSNPrint(NULL, 0, format, args); |
| 9225 va_end(args); | 9279 va_end(args); |
| 9226 | 9280 |
| 9227 Zone* zone = Isolate::Current()->current_zone(); | 9281 Zone* zone = Isolate::Current()->current_zone(); |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9375 return Transform(CaseMapping::ToUpper, str, space); | 9429 return Transform(CaseMapping::ToUpper, str, space); |
| 9376 } | 9430 } |
| 9377 | 9431 |
| 9378 | 9432 |
| 9379 RawString* String::ToLowerCase(const String& str, Heap::Space space) { | 9433 RawString* String::ToLowerCase(const String& str, Heap::Space space) { |
| 9380 // TODO(cshapiro): create a fast-path for OneByteString instances. | 9434 // TODO(cshapiro): create a fast-path for OneByteString instances. |
| 9381 return Transform(CaseMapping::ToLower, str, space); | 9435 return Transform(CaseMapping::ToLower, str, space); |
| 9382 } | 9436 } |
| 9383 | 9437 |
| 9384 | 9438 |
| 9385 RawOneByteString* OneByteString::EscapeDoubleQuotes() const { | 9439 RawOneByteString* OneByteString::EscapeSpecialCharacters(bool raw_str) const { |
| 9386 intptr_t len = Length(); | 9440 intptr_t len = Length(); |
| 9387 if (len > 0) { | 9441 if (len > 0) { |
| 9388 intptr_t num_quotes = 0; | 9442 intptr_t num_escapes = 0; |
| 9389 intptr_t index = 0; | 9443 intptr_t index = 0; |
| 9390 for (intptr_t i = 0; i < len; i++) { | 9444 for (intptr_t i = 0; i < len; i++) { |
| 9391 if (*CharAddr(i) == '"') { | 9445 if (IsSpecialCharacter(*CharAddr(i)) || |
| 9392 num_quotes += 1; | 9446 (!raw_str && (*CharAddr(i) == '\\'))) { |
| 9447 num_escapes += 1; |
| 9393 } | 9448 } |
| 9394 } | 9449 } |
| 9395 const OneByteString& dststr = OneByteString::Handle( | 9450 const OneByteString& dststr = OneByteString::Handle( |
| 9396 OneByteString::New(len + num_quotes, Heap::kNew)); | 9451 OneByteString::New(len + num_escapes, Heap::kNew)); |
| 9397 for (intptr_t i = 0; i < len; i++) { | 9452 for (intptr_t i = 0; i < len; i++) { |
| 9398 if (*CharAddr(i) == '"') { | 9453 if (IsSpecialCharacter(*CharAddr(i))) { |
| 9399 *(dststr.CharAddr(index)) = '\\'; | 9454 *(dststr.CharAddr(index)) = '\\'; |
| 9400 *(dststr.CharAddr(index + 1)) = '"'; | 9455 *(dststr.CharAddr(index + 1)) = SpecialCharacter(*CharAddr(i)); |
| 9456 index += 2; |
| 9457 } else if (!raw_str && (*CharAddr(i) == '\\')) { |
| 9458 *(dststr.CharAddr(index)) = '\\'; |
| 9459 *(dststr.CharAddr(index + 1)) = '\\'; |
| 9401 index += 2; | 9460 index += 2; |
| 9402 } else { | 9461 } else { |
| 9403 *(dststr.CharAddr(index)) = *CharAddr(i); | 9462 *(dststr.CharAddr(index)) = *CharAddr(i); |
| 9404 index += 1; | 9463 index += 1; |
| 9405 } | 9464 } |
| 9406 } | 9465 } |
| 9407 return dststr.raw(); | 9466 return dststr.raw(); |
| 9408 } | 9467 } |
| 9409 return OneByteString::null(); | 9468 return OneByteString::null(); |
| 9410 } | 9469 } |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9573 } | 9632 } |
| 9574 return result.raw(); | 9633 return result.raw(); |
| 9575 } | 9634 } |
| 9576 | 9635 |
| 9577 | 9636 |
| 9578 const char* OneByteString::ToCString() const { | 9637 const char* OneByteString::ToCString() const { |
| 9579 return String::ToCString(); | 9638 return String::ToCString(); |
| 9580 } | 9639 } |
| 9581 | 9640 |
| 9582 | 9641 |
| 9583 RawTwoByteString* TwoByteString::EscapeDoubleQuotes() const { | 9642 RawTwoByteString* TwoByteString::EscapeSpecialCharacters(bool raw_str) const { |
| 9584 intptr_t len = Length(); | 9643 intptr_t len = Length(); |
| 9585 if (len > 0) { | 9644 if (len > 0) { |
| 9586 intptr_t num_quotes = 0; | 9645 intptr_t num_escapes = 0; |
| 9587 intptr_t index = 0; | 9646 intptr_t index = 0; |
| 9588 for (intptr_t i = 0; i < len; i++) { | 9647 for (intptr_t i = 0; i < len; i++) { |
| 9589 if (*CharAddr(i) == '"') { | 9648 if (IsSpecialCharacter(*CharAddr(i)) || |
| 9590 num_quotes += 1; | 9649 (!raw_str && (*CharAddr(i) == '\\'))) { |
| 9650 num_escapes += 1; |
| 9591 } | 9651 } |
| 9592 } | 9652 } |
| 9593 const TwoByteString& dststr = TwoByteString::Handle( | 9653 const TwoByteString& dststr = TwoByteString::Handle( |
| 9594 TwoByteString::New(len + num_quotes, Heap::kNew)); | 9654 TwoByteString::New(len + num_escapes, Heap::kNew)); |
| 9595 for (intptr_t i = 0; i < len; i++) { | 9655 for (intptr_t i = 0; i < len; i++) { |
| 9596 if (*CharAddr(i) == '"') { | 9656 if (IsSpecialCharacter(*CharAddr(i))) { |
| 9597 *(dststr.CharAddr(index)) = '\\'; | 9657 *(dststr.CharAddr(index)) = '\\'; |
| 9598 *(dststr.CharAddr(index + 1)) = '"'; | 9658 *(dststr.CharAddr(index + 1)) = SpecialCharacter(*CharAddr(i)); |
| 9659 index += 2; |
| 9660 } else if (!raw_str && (*CharAddr(i) == '\\')) { |
| 9661 *(dststr.CharAddr(index)) = '\\'; |
| 9662 *(dststr.CharAddr(index + 1)) = '\\'; |
| 9599 index += 2; | 9663 index += 2; |
| 9600 } else { | 9664 } else { |
| 9601 *(dststr.CharAddr(index)) = *CharAddr(i); | 9665 *(dststr.CharAddr(index)) = *CharAddr(i); |
| 9602 index += 1; | 9666 index += 1; |
| 9603 } | 9667 } |
| 9604 } | 9668 } |
| 9605 return dststr.raw(); | 9669 return dststr.raw(); |
| 9606 } | 9670 } |
| 9607 return TwoByteString::null(); | 9671 return TwoByteString::null(); |
| 9608 } | 9672 } |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9705 } | 9769 } |
| 9706 return result.raw(); | 9770 return result.raw(); |
| 9707 } | 9771 } |
| 9708 | 9772 |
| 9709 | 9773 |
| 9710 const char* TwoByteString::ToCString() const { | 9774 const char* TwoByteString::ToCString() const { |
| 9711 return String::ToCString(); | 9775 return String::ToCString(); |
| 9712 } | 9776 } |
| 9713 | 9777 |
| 9714 | 9778 |
| 9715 RawFourByteString* FourByteString::EscapeDoubleQuotes() const { | 9779 RawFourByteString* FourByteString::EscapeSpecialCharacters(bool raw_str) const { |
| 9716 intptr_t len = Length(); | 9780 intptr_t len = Length(); |
| 9717 if (len > 0) { | 9781 if (len > 0) { |
| 9718 intptr_t num_quotes = 0; | 9782 intptr_t num_escapes = 0; |
| 9719 intptr_t index = 0; | 9783 intptr_t index = 0; |
| 9720 for (intptr_t i = 0; i < len; i++) { | 9784 for (intptr_t i = 0; i < len; i++) { |
| 9721 if (*CharAddr(i) == '"') { | 9785 if (IsSpecialCharacter(*CharAddr(i)) || |
| 9722 num_quotes += 1; | 9786 (!raw_str && (*CharAddr(i) == '\\'))) { |
| 9787 num_escapes += 1; |
| 9723 } | 9788 } |
| 9724 } | 9789 } |
| 9725 const FourByteString& dststr = FourByteString::Handle( | 9790 const FourByteString& dststr = FourByteString::Handle( |
| 9726 FourByteString::New(len + num_quotes, Heap::kNew)); | 9791 FourByteString::New(len + num_escapes, Heap::kNew)); |
| 9727 for (intptr_t i = 0; i < len; i++) { | 9792 for (intptr_t i = 0; i < len; i++) { |
| 9728 if (*CharAddr(i) == '"') { | 9793 if (IsSpecialCharacter(*CharAddr(i))) { |
| 9729 *(dststr.CharAddr(index)) = '\\'; | 9794 *(dststr.CharAddr(index)) = '\\'; |
| 9730 *(dststr.CharAddr(index + 1)) = '"'; | 9795 *(dststr.CharAddr(index + 1)) = SpecialCharacter(*CharAddr(i)); |
| 9796 index += 2; |
| 9797 } else if (!raw_str && (*CharAddr(i) == '\\')) { |
| 9798 *(dststr.CharAddr(index)) = '\\'; |
| 9799 *(dststr.CharAddr(index + 1)) = '\\'; |
| 9731 index += 2; | 9800 index += 2; |
| 9732 } else { | 9801 } else { |
| 9733 *(dststr.CharAddr(index)) = *CharAddr(i); | 9802 *(dststr.CharAddr(index)) = *CharAddr(i); |
| 9734 index += 1; | 9803 index += 1; |
| 9735 } | 9804 } |
| 9736 } | 9805 } |
| 9737 return dststr.raw(); | 9806 return dststr.raw(); |
| 9738 } | 9807 } |
| 9739 return FourByteString::null(); | 9808 return FourByteString::null(); |
| 9740 } | 9809 } |
| (...skipping 1461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 11202 } | 11271 } |
| 11203 return result.raw(); | 11272 return result.raw(); |
| 11204 } | 11273 } |
| 11205 | 11274 |
| 11206 | 11275 |
| 11207 const char* WeakProperty::ToCString() const { | 11276 const char* WeakProperty::ToCString() const { |
| 11208 return "_WeakProperty"; | 11277 return "_WeakProperty"; |
| 11209 } | 11278 } |
| 11210 | 11279 |
| 11211 } // namespace dart | 11280 } // namespace dart |
| OLD | NEW |