Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "platform/v8_inspector/DebuggerContextAdapter.h" | |
| 6 | |
| 7 namespace v8_inspector { | |
| 8 | |
| 9 namespace { | |
| 10 | |
| 11 static int kMaxDepth = 20; | |
| 12 static int kMaxCalls = 1000; | |
| 13 | |
| 14 class DebuggerContextAdapter { | |
| 15 public: | |
| 16 v8::MaybeLocal<v8::Value> adapt(v8::Local<v8::Value> value, int depth) | |
| 17 { | |
| 18 if (++m_calls > kMaxCalls || depth > kMaxDepth) | |
| 19 return v8::MaybeLocal<v8::Value>(); | |
| 20 | |
| 21 if (value.IsEmpty()) | |
| 22 return v8::MaybeLocal<v8::Value>(); | |
| 23 if (value->IsNull() || value->IsUndefined() || value->IsBoolean() || val ue->IsString() || value->IsNumber()) | |
| 24 return value; | |
| 25 if (!value->IsObject()) | |
| 26 return v8::MaybeLocal<v8::Value>(); | |
| 27 v8::Local<v8::Object> object = value.As<v8::Object>(); | |
| 28 if (object->CreationContext() != m_from) | |
| 29 return value; | |
| 30 | |
| 31 if (object->IsArray()) { | |
| 32 v8::Local<v8::Array> array = object.As<v8::Array>(); | |
| 33 v8::Local<v8::Array> result = v8::Array::New(m_isolate, array->Lengt h()); | |
| 34 if (!result->SetPrototype(m_to, v8::Null(m_isolate)).FromMaybe(false )) | |
| 35 return v8::MaybeLocal<v8::Value>(); | |
| 36 for (size_t i = 0; i < array->Length(); ++i) { | |
| 37 v8::Local<v8::Value> item; | |
| 38 if (!array->Get(m_from, i).ToLocal(&item)) | |
| 39 return v8::MaybeLocal<v8::Value>(); | |
| 40 v8::Local<v8::Value> adapted; | |
| 41 if (!adapt(item, depth + 1).ToLocal(&adapted)) | |
| 42 return v8::MaybeLocal<v8::Value>(); | |
| 43 if (!result->Set(m_to, i, adapted).FromMaybe(false)) | |
| 44 return v8::MaybeLocal<v8::Value>(); | |
| 45 } | |
| 46 return result; | |
| 47 } | |
| 48 | |
| 49 | |
| 50 v8::Local<v8::Object> result = v8::Object::New(m_isolate); | |
| 51 if (!result->SetPrototype(m_to, v8::Null(m_isolate)).FromMaybe(false)) | |
| 52 return v8::MaybeLocal<v8::Value>(); | |
| 53 v8::Local<v8::Array> properties; | |
| 54 if (!object->GetOwnPropertyNames(m_from).ToLocal(&properties)) | |
| 55 return v8::MaybeLocal<v8::Value>(); | |
| 56 for (size_t i = 0; i < properties->Length(); ++i) { | |
| 57 v8::Local<v8::Value> name; | |
| 58 if (!properties->Get(m_from, i).ToLocal(&name) || !name->IsString()) | |
| 59 return v8::MaybeLocal<v8::Value>(); | |
| 60 v8::Local<v8::Value> property; | |
| 61 if (!object->Get(m_from, name).ToLocal(&property)) | |
| 62 return v8::MaybeLocal<v8::Value>(); | |
| 63 v8::Local<v8::Value> adapted; | |
| 64 if (!adapt(property, depth + 1).ToLocal(&adapted)) | |
| 65 return v8::MaybeLocal<v8::Value>(); | |
| 66 if (!result->Set(m_to, name, adapted).FromMaybe(false)) | |
| 67 return v8::MaybeLocal<v8::Value>(); | |
| 68 } | |
| 69 return result; | |
| 70 } | |
| 71 | |
| 72 v8::Isolate* m_isolate; | |
| 73 v8::Local<v8::Context> m_from; | |
| 74 v8::Local<v8::Context> m_to; | |
| 75 int m_calls; | |
| 76 }; | |
| 77 | |
| 78 } // namespace | |
| 79 | |
| 80 v8::MaybeLocal<v8::Value> adaptValueFromDebuggerContext(v8::Isolate* isolate, v8 ::Local<v8::Context> debuggerContext, v8::Local<v8::Context> toContext, v8::Loca l<v8::Value> value) | |
|
pfeldman
2016/08/17 01:33:28
adoptValue
dgozman
2016/08/17 01:49:09
Went with copyValue.
| |
| 81 { | |
| 82 DebuggerContextAdapter adapter; | |
| 83 adapter.m_isolate = isolate; | |
| 84 adapter.m_from = debuggerContext; | |
| 85 adapter.m_to = toContext; | |
| 86 adapter.m_calls = 0; | |
| 87 return adapter.adapt(value, 0); | |
| 88 } | |
| 89 | |
| 90 } // namespace v8_inspector | |
| OLD | NEW |