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/v8-value-copier.h" | 5 #include "src/inspector/v8-value-copier.h" |
6 | 6 |
7 namespace v8_inspector { | 7 namespace v8_inspector { |
8 | 8 |
9 namespace { | 9 namespace { |
10 | 10 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 } | 66 } |
67 return result; | 67 return result; |
68 } | 68 } |
69 | 69 |
70 v8::Isolate* m_isolate; | 70 v8::Isolate* m_isolate; |
71 v8::Local<v8::Context> m_from; | 71 v8::Local<v8::Context> m_from; |
72 v8::Local<v8::Context> m_to; | 72 v8::Local<v8::Context> m_to; |
73 int m_calls; | 73 int m_calls; |
74 }; | 74 }; |
75 | 75 |
| 76 protocol::Response toProtocolValue(v8::Local<v8::Context> context, |
| 77 v8::Local<v8::Value> value, int maxDepth, |
| 78 std::unique_ptr<protocol::Value>* result) { |
| 79 using protocol::Response; |
| 80 if (value.IsEmpty()) { |
| 81 UNREACHABLE(); |
| 82 return Response::InternalError(); |
| 83 } |
| 84 |
| 85 if (!maxDepth) return Response::Error("Object reference chain is too long"); |
| 86 maxDepth--; |
| 87 |
| 88 if (value->IsNull() || value->IsUndefined()) { |
| 89 *result = protocol::Value::null(); |
| 90 return Response::OK(); |
| 91 } |
| 92 if (value->IsBoolean()) { |
| 93 *result = |
| 94 protocol::FundamentalValue::create(value.As<v8::Boolean>()->Value()); |
| 95 return Response::OK(); |
| 96 } |
| 97 if (value->IsNumber()) { |
| 98 double doubleValue = value.As<v8::Number>()->Value(); |
| 99 int intValue = static_cast<int>(doubleValue); |
| 100 if (intValue == doubleValue) { |
| 101 *result = protocol::FundamentalValue::create(intValue); |
| 102 return Response::OK(); |
| 103 } |
| 104 *result = protocol::FundamentalValue::create(doubleValue); |
| 105 return Response::OK(); |
| 106 } |
| 107 if (value->IsString()) { |
| 108 *result = |
| 109 protocol::StringValue::create(toProtocolString(value.As<v8::String>())); |
| 110 return Response::OK(); |
| 111 } |
| 112 if (value->IsArray()) { |
| 113 v8::Local<v8::Array> array = value.As<v8::Array>(); |
| 114 std::unique_ptr<protocol::ListValue> inspectorArray = |
| 115 protocol::ListValue::create(); |
| 116 uint32_t length = array->Length(); |
| 117 for (uint32_t i = 0; i < length; i++) { |
| 118 v8::Local<v8::Value> value; |
| 119 if (!array->Get(context, i).ToLocal(&value)) |
| 120 return Response::InternalError(); |
| 121 std::unique_ptr<protocol::Value> element; |
| 122 Response response = toProtocolValue(context, value, maxDepth, &element); |
| 123 if (!response.isSuccess()) return response; |
| 124 inspectorArray->pushValue(std::move(element)); |
| 125 } |
| 126 *result = std::move(inspectorArray); |
| 127 return Response::OK(); |
| 128 } |
| 129 if (value->IsObject()) { |
| 130 std::unique_ptr<protocol::DictionaryValue> jsonObject = |
| 131 protocol::DictionaryValue::create(); |
| 132 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value); |
| 133 v8::Local<v8::Array> propertyNames; |
| 134 if (!object->GetPropertyNames(context).ToLocal(&propertyNames)) |
| 135 return Response::InternalError(); |
| 136 uint32_t length = propertyNames->Length(); |
| 137 for (uint32_t i = 0; i < length; i++) { |
| 138 v8::Local<v8::Value> name; |
| 139 if (!propertyNames->Get(context, i).ToLocal(&name)) |
| 140 return Response::InternalError(); |
| 141 // FIXME(yurys): v8::Object should support GetOwnPropertyNames |
| 142 if (name->IsString()) { |
| 143 v8::Maybe<bool> hasRealNamedProperty = object->HasRealNamedProperty( |
| 144 context, v8::Local<v8::String>::Cast(name)); |
| 145 if (!hasRealNamedProperty.IsJust() || !hasRealNamedProperty.FromJust()) |
| 146 continue; |
| 147 } |
| 148 v8::Local<v8::String> propertyName; |
| 149 if (!name->ToString(context).ToLocal(&propertyName)) continue; |
| 150 v8::Local<v8::Value> property; |
| 151 if (!object->Get(context, name).ToLocal(&property)) |
| 152 return Response::InternalError(); |
| 153 std::unique_ptr<protocol::Value> propertyValue; |
| 154 Response response = |
| 155 toProtocolValue(context, property, maxDepth, &propertyValue); |
| 156 if (!response.isSuccess()) return response; |
| 157 jsonObject->setValue(toProtocolString(propertyName), |
| 158 std::move(propertyValue)); |
| 159 } |
| 160 *result = std::move(jsonObject); |
| 161 return Response::OK(); |
| 162 } |
| 163 return Response::Error("Object couldn't be returned by value"); |
| 164 } |
| 165 |
76 } // namespace | 166 } // namespace |
77 | 167 |
78 v8::MaybeLocal<v8::Value> copyValueFromDebuggerContext( | 168 v8::MaybeLocal<v8::Value> copyValueFromDebuggerContext( |
79 v8::Isolate* isolate, v8::Local<v8::Context> debuggerContext, | 169 v8::Isolate* isolate, v8::Local<v8::Context> debuggerContext, |
80 v8::Local<v8::Context> toContext, v8::Local<v8::Value> value) { | 170 v8::Local<v8::Context> toContext, v8::Local<v8::Value> value) { |
81 V8ValueCopier copier; | 171 V8ValueCopier copier; |
82 copier.m_isolate = isolate; | 172 copier.m_isolate = isolate; |
83 copier.m_from = debuggerContext; | 173 copier.m_from = debuggerContext; |
84 copier.m_to = toContext; | 174 copier.m_to = toContext; |
85 copier.m_calls = 0; | 175 copier.m_calls = 0; |
(...skipping 14 matching lines...) Expand all Loading... |
100 v8::Maybe<bool> createDataProperty(v8::Local<v8::Context> context, | 190 v8::Maybe<bool> createDataProperty(v8::Local<v8::Context> context, |
101 v8::Local<v8::Array> array, int index, | 191 v8::Local<v8::Array> array, int index, |
102 v8::Local<v8::Value> value) { | 192 v8::Local<v8::Value> value) { |
103 v8::TryCatch tryCatch(context->GetIsolate()); | 193 v8::TryCatch tryCatch(context->GetIsolate()); |
104 v8::Isolate::DisallowJavascriptExecutionScope throwJs( | 194 v8::Isolate::DisallowJavascriptExecutionScope throwJs( |
105 context->GetIsolate(), | 195 context->GetIsolate(), |
106 v8::Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE); | 196 v8::Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE); |
107 return array->CreateDataProperty(context, index, value); | 197 return array->CreateDataProperty(context, index, value); |
108 } | 198 } |
109 | 199 |
| 200 protocol::Response toProtocolValue(v8::Local<v8::Context> context, |
| 201 v8::Local<v8::Value> value, |
| 202 std::unique_ptr<protocol::Value>* result) { |
| 203 return toProtocolValue(context, value, 1000, result); |
| 204 } |
| 205 |
110 } // namespace v8_inspector | 206 } // namespace v8_inspector |
OLD | NEW |