OLD | NEW |
1 // Copyright 2017 the V8 project authors. All rights reserved. | 1 // Copyright 2017 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 "test/inspector/isolate-data.h" | 5 #include "test/inspector/isolate-data.h" |
6 | 6 |
7 #include "test/inspector/inspector-impl.h" | 7 #include "test/inspector/inspector-impl.h" |
8 #include "test/inspector/task-runner.h" | 8 #include "test/inspector/task-runner.h" |
9 | 9 |
10 namespace { | 10 namespace { |
11 | 11 |
12 const int kIsolateDataIndex = 2; | 12 const int kIsolateDataIndex = 2; |
| 13 const int kContextGroupIdIndex = 3; |
13 | 14 |
14 v8::internal::Vector<uint16_t> ToVector(v8::Local<v8::String> str) { | 15 v8::internal::Vector<uint16_t> ToVector(v8::Local<v8::String> str) { |
15 v8::internal::Vector<uint16_t> buffer = | 16 v8::internal::Vector<uint16_t> buffer = |
16 v8::internal::Vector<uint16_t>::New(str->Length()); | 17 v8::internal::Vector<uint16_t>::New(str->Length()); |
17 str->Write(buffer.start(), 0, str->Length()); | 18 str->Write(buffer.start(), 0, str->Length()); |
18 return buffer; | 19 return buffer; |
19 } | 20 } |
20 | 21 |
21 } // namespace | 22 } // namespace |
22 | 23 |
23 IsolateData::IsolateData(TaskRunner* task_runner, | 24 IsolateData::IsolateData(TaskRunner* task_runner, |
24 IsolateData::SetupGlobalTasks setup_global_tasks, | 25 IsolateData::SetupGlobalTasks setup_global_tasks, |
25 v8::StartupData* startup_data) | 26 v8::StartupData* startup_data, |
| 27 InspectorClientImpl::FrontendChannel* channel) |
26 : task_runner_(task_runner), | 28 : task_runner_(task_runner), |
27 setup_global_tasks_(std::move(setup_global_tasks)) { | 29 setup_global_tasks_(std::move(setup_global_tasks)) { |
28 v8::Isolate::CreateParams params; | 30 v8::Isolate::CreateParams params; |
29 params.array_buffer_allocator = | 31 params.array_buffer_allocator = |
30 v8::ArrayBuffer::Allocator::NewDefaultAllocator(); | 32 v8::ArrayBuffer::Allocator::NewDefaultAllocator(); |
31 params.snapshot_blob = startup_data; | 33 params.snapshot_blob = startup_data; |
32 isolate_ = v8::Isolate::New(params); | 34 isolate_ = v8::Isolate::New(params); |
33 isolate_->SetMicrotasksPolicy(v8::MicrotasksPolicy::kScoped); | 35 isolate_->SetMicrotasksPolicy(v8::MicrotasksPolicy::kScoped); |
| 36 if (channel) |
| 37 inspector_.reset(new InspectorClientImpl(isolate_, task_runner, channel)); |
34 } | 38 } |
35 | 39 |
36 IsolateData* IsolateData::FromContext(v8::Local<v8::Context> context) { | 40 IsolateData* IsolateData::FromContext(v8::Local<v8::Context> context) { |
37 return static_cast<IsolateData*>( | 41 return static_cast<IsolateData*>( |
38 context->GetAlignedPointerFromEmbedderData(kIsolateDataIndex)); | 42 context->GetAlignedPointerFromEmbedderData(kIsolateDataIndex)); |
39 } | 43 } |
40 | 44 |
41 int IsolateData::CreateContextGroup() { | 45 int IsolateData::CreateContextGroup() { |
| 46 v8::HandleScope handle_scope(isolate_); |
42 v8::Local<v8::ObjectTemplate> global_template = | 47 v8::Local<v8::ObjectTemplate> global_template = |
43 v8::ObjectTemplate::New(isolate_); | 48 v8::ObjectTemplate::New(isolate_); |
44 for (auto it = setup_global_tasks_.begin(); it != setup_global_tasks_.end(); | 49 for (auto it = setup_global_tasks_.begin(); it != setup_global_tasks_.end(); |
45 ++it) { | 50 ++it) { |
46 (*it)->Run(isolate_, global_template); | 51 (*it)->Run(isolate_, global_template); |
47 } | 52 } |
48 v8::Local<v8::Context> context = | 53 v8::Local<v8::Context> context = |
49 v8::Context::New(isolate_, nullptr, global_template); | 54 v8::Context::New(isolate_, nullptr, global_template); |
50 context->SetAlignedPointerInEmbedderData(kIsolateDataIndex, this); | 55 context->SetAlignedPointerInEmbedderData(kIsolateDataIndex, this); |
51 int context_group_id = ++last_context_group_id_; | 56 int context_group_id = ++last_context_group_id_; |
| 57 // Should be 2-byte aligned. |
| 58 context->SetAlignedPointerInEmbedderData( |
| 59 kContextGroupIdIndex, reinterpret_cast<void*>(context_group_id * 2)); |
52 contexts_[context_group_id].Reset(isolate_, context); | 60 contexts_[context_group_id].Reset(isolate_, context); |
| 61 if (inspector_) inspector_->ContextCreated(context, context_group_id); |
53 return context_group_id; | 62 return context_group_id; |
54 } | 63 } |
55 | 64 |
56 v8::Local<v8::Context> IsolateData::GetContext(int context_group_id) { | 65 v8::Local<v8::Context> IsolateData::GetContext(int context_group_id) { |
57 return contexts_[context_group_id].Get(isolate_); | 66 return contexts_[context_group_id].Get(isolate_); |
58 } | 67 } |
59 | 68 |
| 69 int IsolateData::GetContextGroupId(v8::Local<v8::Context> context) { |
| 70 return static_cast<int>( |
| 71 reinterpret_cast<intptr_t>( |
| 72 context->GetAlignedPointerFromEmbedderData(kContextGroupIdIndex)) / |
| 73 2); |
| 74 } |
| 75 |
60 void IsolateData::RegisterModule(v8::Local<v8::Context> context, | 76 void IsolateData::RegisterModule(v8::Local<v8::Context> context, |
61 v8::internal::Vector<uint16_t> name, | 77 v8::internal::Vector<uint16_t> name, |
62 v8::ScriptCompiler::Source* source) { | 78 v8::ScriptCompiler::Source* source) { |
63 v8::Local<v8::Module> module; | 79 v8::Local<v8::Module> module; |
64 if (!v8::ScriptCompiler::CompileModule(isolate(), source).ToLocal(&module)) | 80 if (!v8::ScriptCompiler::CompileModule(isolate(), source).ToLocal(&module)) |
65 return; | 81 return; |
66 if (!module->Instantiate(context, &IsolateData::ModuleResolveCallback)) | 82 if (!module->Instantiate(context, &IsolateData::ModuleResolveCallback)) |
67 return; | 83 return; |
68 v8::Local<v8::Value> result; | 84 v8::Local<v8::Value> result; |
69 if (!module->Evaluate(context).ToLocal(&result)) return; | 85 if (!module->Evaluate(context).ToLocal(&result)) return; |
70 modules_[name] = v8::Global<v8::Module>(isolate_, module); | 86 modules_[name] = v8::Global<v8::Module>(isolate_, module); |
71 } | 87 } |
72 | 88 |
73 v8::MaybeLocal<v8::Module> IsolateData::ModuleResolveCallback( | 89 v8::MaybeLocal<v8::Module> IsolateData::ModuleResolveCallback( |
74 v8::Local<v8::Context> context, v8::Local<v8::String> specifier, | 90 v8::Local<v8::Context> context, v8::Local<v8::String> specifier, |
75 v8::Local<v8::Module> referrer) { | 91 v8::Local<v8::Module> referrer) { |
76 std::string str = *v8::String::Utf8Value(specifier); | 92 std::string str = *v8::String::Utf8Value(specifier); |
77 IsolateData* data = IsolateData::FromContext(context); | 93 IsolateData* data = IsolateData::FromContext(context); |
78 return data->modules_[ToVector(specifier)].Get(data->isolate_); | 94 return data->modules_[ToVector(specifier)].Get(data->isolate_); |
79 } | 95 } |
OLD | NEW |