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 if (++m_calls > kMaxCalls || depth > kMaxDepth) | |
18 return v8::MaybeLocal<v8::Value>(); | |
19 | |
20 if (value.IsEmpty()) return v8::MaybeLocal<v8::Value>(); | |
21 if (value->IsNull() || value->IsUndefined() || value->IsBoolean() || | |
22 value->IsString() || value->IsNumber()) | |
23 return value; | |
24 if (!value->IsObject()) return v8::MaybeLocal<v8::Value>(); | |
25 v8::Local<v8::Object> object = value.As<v8::Object>(); | |
26 if (object->CreationContext() != m_from) return value; | |
27 | |
28 if (object->IsArray()) { | |
29 v8::Local<v8::Array> array = object.As<v8::Array>(); | |
30 v8::Local<v8::Array> result = v8::Array::New(m_isolate, array->Length()); | |
31 if (!result->SetPrototype(m_to, v8::Null(m_isolate)).FromMaybe(false)) | |
32 return v8::MaybeLocal<v8::Value>(); | |
33 for (size_t i = 0; i < array->Length(); ++i) { | |
34 v8::Local<v8::Value> item; | |
35 if (!array->Get(m_from, i).ToLocal(&item)) | |
36 return v8::MaybeLocal<v8::Value>(); | |
37 v8::Local<v8::Value> copied; | |
38 if (!copy(item, depth + 1).ToLocal(&copied)) | |
39 return v8::MaybeLocal<v8::Value>(); | |
40 if (!createDataProperty(m_to, result, i, copied).FromMaybe(false)) | |
41 return v8::MaybeLocal<v8::Value>(); | |
42 } | |
43 return result; | |
44 } | |
45 | |
46 v8::Local<v8::Object> result = v8::Object::New(m_isolate); | |
47 if (!result->SetPrototype(m_to, v8::Null(m_isolate)).FromMaybe(false)) | |
48 return v8::MaybeLocal<v8::Value>(); | |
49 v8::Local<v8::Array> properties; | |
50 if (!object->GetOwnPropertyNames(m_from).ToLocal(&properties)) | |
51 return v8::MaybeLocal<v8::Value>(); | |
52 for (size_t i = 0; i < properties->Length(); ++i) { | |
53 v8::Local<v8::Value> name; | |
54 if (!properties->Get(m_from, i).ToLocal(&name) || !name->IsString()) | |
55 return v8::MaybeLocal<v8::Value>(); | |
56 v8::Local<v8::Value> property; | |
57 if (!object->Get(m_from, name).ToLocal(&property)) | |
58 return v8::MaybeLocal<v8::Value>(); | |
59 v8::Local<v8::Value> copied; | |
60 if (!copy(property, depth + 1).ToLocal(&copied)) | |
61 return v8::MaybeLocal<v8::Value>(); | |
62 if (!createDataProperty(m_to, result, v8::Local<v8::String>::Cast(name), | |
63 copied) | |
64 .FromMaybe(false)) | |
65 return v8::MaybeLocal<v8::Value>(); | |
66 } | |
67 return result; | |
68 } | |
69 | |
70 v8::Isolate* m_isolate; | |
71 v8::Local<v8::Context> m_from; | |
72 v8::Local<v8::Context> m_to; | |
73 int m_calls; | |
74 }; | |
75 | |
76 } // namespace | |
77 | |
78 v8::MaybeLocal<v8::Value> copyValueFromDebuggerContext( | |
79 v8::Isolate* isolate, v8::Local<v8::Context> debuggerContext, | |
80 v8::Local<v8::Context> toContext, v8::Local<v8::Value> value) { | |
81 V8ValueCopier copier; | |
82 copier.m_isolate = isolate; | |
83 copier.m_from = debuggerContext; | |
84 copier.m_to = toContext; | |
85 copier.m_calls = 0; | |
86 return copier.copy(value, 0); | |
87 } | |
88 | |
89 v8::Maybe<bool> createDataProperty(v8::Local<v8::Context> context, | |
90 v8::Local<v8::Object> object, | |
91 v8::Local<v8::Name> key, | |
92 v8::Local<v8::Value> value) { | |
93 v8::TryCatch tryCatch(context->GetIsolate()); | |
94 v8::Isolate::DisallowJavascriptExecutionScope throwJs( | |
95 context->GetIsolate(), | |
96 v8::Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE); | |
97 return object->CreateDataProperty(context, key, value); | |
98 } | |
99 | |
100 v8::Maybe<bool> createDataProperty(v8::Local<v8::Context> context, | |
101 v8::Local<v8::Array> array, int index, | |
102 v8::Local<v8::Value> value) { | |
103 v8::TryCatch tryCatch(context->GetIsolate()); | |
104 v8::Isolate::DisallowJavascriptExecutionScope throwJs( | |
105 context->GetIsolate(), | |
106 v8::Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE); | |
107 return array->CreateDataProperty(context, index, value); | |
108 } | |
109 | |
110 } // namespace v8_inspector | |
OLD | NEW |