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