OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "src/inspector/V8ValueCopier.h" | |
6 | |
7 namespace v8_inspector { | |
8 | |
9 namespace { | |
10 | |
11 static int kMaxDepth = 20; | |
12 static int kMaxCalls = 1000; | |
13 | |
14 class V8ValueCopier { | |
15 public: | |
16 v8::MaybeLocal<v8::Value> copy(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> copied; | |
41 if (!copy(item, depth + 1).ToLocal(&copied)) | |
42 return v8::MaybeLocal<v8::Value>(); | |
43 if (!createDataProperty(m_to, result, i, copied).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> copied; | |
64 if (!copy(property, depth + 1).ToLocal(&copied)) | |
65 return v8::MaybeLocal<v8::Value>(); | |
66 if (!createDataProperty(m_to, result, v8::Local<v8::String>::Cast(na
me), copied).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> copyValueFromDebuggerContext(v8::Isolate* isolate, v8:
:Local<v8::Context> debuggerContext, v8::Local<v8::Context> toContext, v8::Local
<v8::Value> value) | |
81 { | |
82 V8ValueCopier copier; | |
83 copier.m_isolate = isolate; | |
84 copier.m_from = debuggerContext; | |
85 copier.m_to = toContext; | |
86 copier.m_calls = 0; | |
87 return copier.copy(value, 0); | |
88 } | |
89 | |
90 v8::Maybe<bool> createDataProperty(v8::Local<v8::Context> context, v8::Local<v8:
:Object> object, v8::Local<v8::Name> key, v8::Local<v8::Value> value) | |
91 { | |
92 v8::TryCatch tryCatch(context->GetIsolate()); | |
93 v8::Isolate::DisallowJavascriptExecutionScope throwJs(context->GetIsolate(),
v8::Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE); | |
94 return object->CreateDataProperty(context, key, value); | |
95 } | |
96 | |
97 v8::Maybe<bool> createDataProperty(v8::Local<v8::Context> context, v8::Local<v8:
:Array> array, int index, v8::Local<v8::Value> value) | |
98 { | |
99 v8::TryCatch tryCatch(context->GetIsolate()); | |
100 v8::Isolate::DisallowJavascriptExecutionScope throwJs(context->GetIsolate(),
v8::Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE); | |
101 return array->CreateDataProperty(context, index, value); | |
102 } | |
103 | |
104 } // namespace v8_inspector | |
OLD | NEW |