John McCutchan | 080c33e | 2015-07-16 12:35:35 -0700 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "tonic/dart_exception_factory.h" |
| 6 | |
| 7 | #include "tonic/dart_converter.h" |
| 8 | #include "tonic/dart_builtin.h" |
| 9 | |
John McCutchan | deace8f | 2015-07-21 08:01:40 -0700 | [diff] [blame] | 10 | namespace tonic { |
John McCutchan | 080c33e | 2015-07-16 12:35:35 -0700 | [diff] [blame] | 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 | |
| 87 | DartExceptionFactory::DartExceptionFactory(DartState* dart_state) |
| 88 | : dart_state_(dart_state) { |
| 89 | } |
| 90 | |
| 91 | DartExceptionFactory::~DartExceptionFactory() { |
| 92 | } |
| 93 | |
| 94 | Dart_Handle DartExceptionFactory::CreateNullArgumentException(int index) { |
| 95 | StdStringBuilder builder; |
| 96 | builder.Append("Argument "); |
| 97 | builder.AppendNumber(index); |
| 98 | builder.Append(" cannot be null."); |
| 99 | Dart_Handle message_handle = Dart_NewStringFromUTF8( |
| 100 | reinterpret_cast<const uint8_t*>(builder.data()), builder.size()); |
| 101 | return CreateException("ArgumentError", message_handle); |
| 102 | } |
| 103 | |
| 104 | Dart_Handle DartExceptionFactory::CreateException(const std::string& class_name, |
| 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) { |
| 111 | if (core_library_.is_empty()) { |
| 112 | Dart_Handle library = DartBuiltin::LookupLibrary("dart:core"); |
| 113 | core_library_.Set(dart_state_, library); |
| 114 | } |
| 115 | |
| 116 | Dart_Handle exception_class = Dart_GetType( |
| 117 | core_library_.value(), StdStringToDart(class_name), 0, 0); |
| 118 | return Dart_New(exception_class, Dart_EmptyString(), 1, &message); |
| 119 | } |
| 120 | |
John McCutchan | deace8f | 2015-07-21 08:01:40 -0700 | [diff] [blame] | 121 | } // namespace tonic |