OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium 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 #include "sky/engine/tonic/dart_exception_factory.h" | 5 #include "sky/engine/tonic/dart_exception_factory.h" |
6 | 6 |
7 #include "sky/engine/tonic/dart_converter.h" | 7 #include "sky/engine/tonic/dart_converter.h" |
8 #include "sky/engine/tonic/dart_builtin.h" | 8 #include "sky/engine/tonic/dart_builtin.h" |
9 #include "sky/engine/wtf/text/StringBuilder.h" | |
10 | 9 |
11 namespace blink { | 10 namespace blink { |
12 | 11 |
| 12 // TODO(johnmccutchan): Move this into another file. |
| 13 class StdStringBuilder { |
| 14 public: |
| 15 StdStringBuilder() { |
| 16 } |
| 17 |
| 18 void Append(const char* s, intptr_t length) { |
| 19 if (s == NULL) { |
| 20 return; |
| 21 } |
| 22 for (intptr_t i = 0; i < length; i++) { |
| 23 buffer_.push_back(s[i]); |
| 24 } |
| 25 } |
| 26 |
| 27 void Append(const char* s) { |
| 28 if (s == NULL) { |
| 29 return; |
| 30 } |
| 31 for (intptr_t i = 0; s[i] != '\0'; i++) { |
| 32 buffer_.push_back(s[i]); |
| 33 } |
| 34 } |
| 35 |
| 36 void Append(char ch) { |
| 37 buffer_.push_back(ch); |
| 38 } |
| 39 |
| 40 void Append(std::string s) { |
| 41 if (s.length() == 0) { |
| 42 return; |
| 43 } |
| 44 const char* c_str = s.data(); |
| 45 intptr_t c_str_length = s.size(); |
| 46 Append(c_str, c_str_length); |
| 47 } |
| 48 |
| 49 void AppendNumber(int num) { |
| 50 Append(std::to_string(num)); |
| 51 } |
| 52 |
| 53 void Clear() { |
| 54 buffer_.resize(0); |
| 55 } |
| 56 |
| 57 void ShrinkToFit() { |
| 58 buffer_.shrink_to_fit(); |
| 59 } |
| 60 |
| 61 void Reserve(intptr_t capacity) { |
| 62 buffer_.reserve(capacity); |
| 63 } |
| 64 |
| 65 std::string ToString() { |
| 66 return std::string(buffer_.data(), buffer_.size()); |
| 67 } |
| 68 |
| 69 const char* data() const { |
| 70 return buffer_.data(); |
| 71 } |
| 72 |
| 73 char* data() { |
| 74 return buffer_.data(); |
| 75 } |
| 76 |
| 77 intptr_t size() const { |
| 78 return buffer_.size(); |
| 79 } |
| 80 |
| 81 private: |
| 82 std::vector<char> buffer_; |
| 83 |
| 84 DISALLOW_COPY_AND_ASSIGN(StdStringBuilder); |
| 85 }; |
| 86 |
13 DartExceptionFactory::DartExceptionFactory(DartState* dart_state) | 87 DartExceptionFactory::DartExceptionFactory(DartState* dart_state) |
14 : dart_state_(dart_state) { | 88 : dart_state_(dart_state) { |
15 } | 89 } |
16 | 90 |
17 DartExceptionFactory::~DartExceptionFactory() { | 91 DartExceptionFactory::~DartExceptionFactory() { |
18 } | 92 } |
19 | 93 |
20 Dart_Handle DartExceptionFactory::CreateNullArgumentException(int index) { | 94 Dart_Handle DartExceptionFactory::CreateNullArgumentException(int index) { |
21 StringBuilder message; | 95 StdStringBuilder builder; |
22 message.appendLiteral("Argument "); | 96 builder.Append("Argument "); |
23 message.appendNumber(index); | 97 builder.AppendNumber(index); |
24 message.appendLiteral(" cannot be null."); | 98 builder.Append(" cannot be null."); |
25 return CreateException("ArgumentError", message.toString()); | 99 Dart_Handle message_handle = Dart_NewStringFromUTF8( |
| 100 reinterpret_cast<const uint8_t*>(builder.data()), builder.size()); |
| 101 return CreateException("ArgumentError", message_handle); |
26 } | 102 } |
27 | 103 |
28 Dart_Handle DartExceptionFactory::CreateException(const String& class_name, | 104 Dart_Handle DartExceptionFactory::CreateException(const std::string& class_name, |
29 const String& message) { | 105 const std::string& message) { |
| 106 return CreateException(class_name, StdStringToDart(message)); |
| 107 } |
| 108 |
| 109 Dart_Handle DartExceptionFactory::CreateException(const std::string& class_name, |
| 110 Dart_Handle message) { |
30 if (core_library_.is_empty()) { | 111 if (core_library_.is_empty()) { |
31 Dart_Handle library = DartBuiltin::LookupLibrary("dart:core"); | 112 Dart_Handle library = DartBuiltin::LookupLibrary("dart:core"); |
32 core_library_.Set(dart_state_, library); | 113 core_library_.Set(dart_state_, library); |
33 } | 114 } |
34 | 115 |
35 Dart_Handle exception_class = Dart_GetType( | 116 Dart_Handle exception_class = Dart_GetType( |
36 core_library_.value(), StringToDart(dart_state_, class_name), 0, 0); | 117 core_library_.value(), StdStringToDart(class_name), 0, 0); |
37 Dart_Handle message_handle = StringToDart(dart_state_, message); | 118 return Dart_New(exception_class, Dart_EmptyString(), 1, &message); |
38 return Dart_New(exception_class, Dart_EmptyString(), 1, &message_handle); | |
39 } | 119 } |
40 | 120 |
41 } // namespace blink | 121 } // namespace blink |
OLD | NEW |