| OLD | NEW |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project 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 "src/inspector/string-util.h" | 5 #include "src/inspector/string-util.h" |
| 6 | 6 |
| 7 #include "src/inspector/protocol/Protocol.h" | 7 #include "src/inspector/protocol/Protocol.h" |
| 8 | 8 |
| 9 namespace v8_inspector { | 9 namespace v8_inspector { |
| 10 | 10 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 } | 104 } |
| 105 | 105 |
| 106 std::unique_ptr<protocol::Value> parseJSON(const String16& string) { | 106 std::unique_ptr<protocol::Value> parseJSON(const String16& string) { |
| 107 if (!string.length()) return nullptr; | 107 if (!string.length()) return nullptr; |
| 108 return protocol::parseJSON(string.characters16(), | 108 return protocol::parseJSON(string.characters16(), |
| 109 static_cast<int>(string.length())); | 109 static_cast<int>(string.length())); |
| 110 } | 110 } |
| 111 | 111 |
| 112 } // namespace protocol | 112 } // namespace protocol |
| 113 | 113 |
| 114 std::unique_ptr<protocol::Value> toProtocolValue(protocol::String* errorString, | |
| 115 v8::Local<v8::Context> context, | |
| 116 v8::Local<v8::Value> value, | |
| 117 int maxDepth) { | |
| 118 if (value.IsEmpty()) { | |
| 119 UNREACHABLE(); | |
| 120 return nullptr; | |
| 121 } | |
| 122 | |
| 123 if (!maxDepth) { | |
| 124 *errorString = "Object reference chain is too long"; | |
| 125 return nullptr; | |
| 126 } | |
| 127 maxDepth--; | |
| 128 | |
| 129 if (value->IsNull() || value->IsUndefined()) return protocol::Value::null(); | |
| 130 if (value->IsBoolean()) | |
| 131 return protocol::FundamentalValue::create(value.As<v8::Boolean>()->Value()); | |
| 132 if (value->IsNumber()) { | |
| 133 double doubleValue = value.As<v8::Number>()->Value(); | |
| 134 int intValue = static_cast<int>(doubleValue); | |
| 135 if (intValue == doubleValue) | |
| 136 return protocol::FundamentalValue::create(intValue); | |
| 137 return protocol::FundamentalValue::create(doubleValue); | |
| 138 } | |
| 139 if (value->IsString()) | |
| 140 return protocol::StringValue::create( | |
| 141 toProtocolString(value.As<v8::String>())); | |
| 142 if (value->IsArray()) { | |
| 143 v8::Local<v8::Array> array = value.As<v8::Array>(); | |
| 144 std::unique_ptr<protocol::ListValue> inspectorArray = | |
| 145 protocol::ListValue::create(); | |
| 146 uint32_t length = array->Length(); | |
| 147 for (uint32_t i = 0; i < length; i++) { | |
| 148 v8::Local<v8::Value> value; | |
| 149 if (!array->Get(context, i).ToLocal(&value)) { | |
| 150 *errorString = "Internal error"; | |
| 151 return nullptr; | |
| 152 } | |
| 153 std::unique_ptr<protocol::Value> element = | |
| 154 toProtocolValue(errorString, context, value, maxDepth); | |
| 155 if (!element) return nullptr; | |
| 156 inspectorArray->pushValue(std::move(element)); | |
| 157 } | |
| 158 return std::move(inspectorArray); | |
| 159 } | |
| 160 if (value->IsObject()) { | |
| 161 std::unique_ptr<protocol::DictionaryValue> jsonObject = | |
| 162 protocol::DictionaryValue::create(); | |
| 163 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value); | |
| 164 v8::Local<v8::Array> propertyNames; | |
| 165 if (!object->GetPropertyNames(context).ToLocal(&propertyNames)) { | |
| 166 *errorString = "Internal error"; | |
| 167 return nullptr; | |
| 168 } | |
| 169 uint32_t length = propertyNames->Length(); | |
| 170 for (uint32_t i = 0; i < length; i++) { | |
| 171 v8::Local<v8::Value> name; | |
| 172 if (!propertyNames->Get(context, i).ToLocal(&name)) { | |
| 173 *errorString = "Internal error"; | |
| 174 return nullptr; | |
| 175 } | |
| 176 // FIXME(yurys): v8::Object should support GetOwnPropertyNames | |
| 177 if (name->IsString()) { | |
| 178 v8::Maybe<bool> hasRealNamedProperty = object->HasRealNamedProperty( | |
| 179 context, v8::Local<v8::String>::Cast(name)); | |
| 180 if (!hasRealNamedProperty.IsJust() || !hasRealNamedProperty.FromJust()) | |
| 181 continue; | |
| 182 } | |
| 183 v8::Local<v8::String> propertyName; | |
| 184 if (!name->ToString(context).ToLocal(&propertyName)) continue; | |
| 185 v8::Local<v8::Value> property; | |
| 186 if (!object->Get(context, name).ToLocal(&property)) { | |
| 187 *errorString = "Internal error"; | |
| 188 return nullptr; | |
| 189 } | |
| 190 std::unique_ptr<protocol::Value> propertyValue = | |
| 191 toProtocolValue(errorString, context, property, maxDepth); | |
| 192 if (!propertyValue) return nullptr; | |
| 193 jsonObject->setValue(toProtocolString(propertyName), | |
| 194 std::move(propertyValue)); | |
| 195 } | |
| 196 return std::move(jsonObject); | |
| 197 } | |
| 198 *errorString = "Object couldn't be returned by value"; | |
| 199 return nullptr; | |
| 200 } | |
| 201 | |
| 202 // static | 114 // static |
| 203 std::unique_ptr<StringBuffer> StringBuffer::create(const StringView& string) { | 115 std::unique_ptr<StringBuffer> StringBuffer::create(const StringView& string) { |
| 204 String16 owner = toString16(string); | 116 String16 owner = toString16(string); |
| 205 return StringBufferImpl::adopt(owner); | 117 return StringBufferImpl::adopt(owner); |
| 206 } | 118 } |
| 207 | 119 |
| 208 // static | 120 // static |
| 209 std::unique_ptr<StringBufferImpl> StringBufferImpl::adopt(String16& string) { | 121 std::unique_ptr<StringBufferImpl> StringBufferImpl::adopt(String16& string) { |
| 210 return wrapUnique(new StringBufferImpl(string)); | 122 return wrapUnique(new StringBufferImpl(string)); |
| 211 } | 123 } |
| 212 | 124 |
| 213 StringBufferImpl::StringBufferImpl(String16& string) { | 125 StringBufferImpl::StringBufferImpl(String16& string) { |
| 214 m_owner.swap(string); | 126 m_owner.swap(string); |
| 215 m_string = toStringView(m_owner); | 127 m_string = toStringView(m_owner); |
| 216 } | 128 } |
| 217 | 129 |
| 218 } // namespace v8_inspector | 130 } // namespace v8_inspector |
| OLD | NEW |