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 "include/libplatform/libplatform.h" | |
6 #include "include/v8.h" | |
7 | |
8 #include "src/base/platform/platform.h" | |
9 #include "src/flags.h" | |
10 #include "src/utils.h" | |
11 #include "src/vector.h" | |
12 | |
13 #include "test/inspector/inspector-impl.h" | |
14 #include "test/inspector/task-runner.h" | |
15 | |
16 namespace { | |
17 | |
18 void Exit() { | |
19 fflush(stdout); | |
20 fflush(stderr); | |
21 _exit(0); | |
22 } | |
23 | |
24 class UtilsExtension : public v8::Extension { | |
25 public: | |
26 UtilsExtension() | |
27 : v8::Extension("v8_inspector/utils", | |
28 "native function print(); native function quit();") {} | |
29 virtual v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate( | |
30 v8::Isolate* isolate, v8::Local<v8::String> name) { | |
31 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | |
32 if (name->Equals(context, v8::String::NewFromUtf8( | |
33 isolate, "print", v8::NewStringType::kNormal) | |
34 .ToLocalChecked()) | |
35 .FromJust()) { | |
36 return v8::FunctionTemplate::New(isolate, UtilsExtension::Print); | |
37 } else if (name->Equals(context, | |
38 v8::String::NewFromUtf8(isolate, "quit", | |
39 v8::NewStringType::kNormal) | |
40 .ToLocalChecked()) | |
41 .FromJust()) { | |
42 return v8::FunctionTemplate::New(isolate, UtilsExtension::Quit); | |
43 } | |
44 return v8::Local<v8::FunctionTemplate>(); | |
45 } | |
46 | |
47 private: | |
48 static void Print(const v8::FunctionCallbackInfo<v8::Value>& args) { | |
49 for (int i = 0; i < args.Length(); i++) { | |
50 v8::HandleScope handle_scope(args.GetIsolate()); | |
51 if (i != 0) { | |
52 printf(" "); | |
53 } | |
54 | |
55 // Explicitly catch potential exceptions in toString(). | |
56 v8::TryCatch try_catch(args.GetIsolate()); | |
57 v8::Local<v8::Value> arg = args[i]; | |
58 v8::Local<v8::String> str_obj; | |
59 | |
60 if (arg->IsSymbol()) { | |
61 arg = v8::Local<v8::Symbol>::Cast(arg)->Name(); | |
62 } | |
63 if (!arg->ToString(args.GetIsolate()->GetCurrentContext()) | |
64 .ToLocal(&str_obj)) { | |
65 try_catch.ReThrow(); | |
66 return; | |
67 } | |
68 | |
69 v8::String::Utf8Value str(str_obj); | |
70 int n = | |
71 static_cast<int>(fwrite(*str, sizeof(**str), str.length(), stdout)); | |
72 if (n != str.length()) { | |
73 printf("Error in fwrite\n"); | |
74 Quit(args); | |
75 } | |
76 } | |
77 printf("\n"); | |
78 fflush(stdout); | |
79 } | |
80 | |
81 static void Quit(const v8::FunctionCallbackInfo<v8::Value>& args) { Exit(); } | |
82 }; | |
83 | |
84 class SetTimeoutTask : public TaskRunner::Task { | |
85 public: | |
86 SetTimeoutTask(v8::Isolate* isolate, v8::Local<v8::Function> function) | |
87 : function_(isolate, function) {} | |
88 virtual ~SetTimeoutTask() {} | |
89 | |
90 bool is_inspector_task() final { return false; } | |
91 | |
92 void Run(v8::Isolate* isolate, | |
93 const v8::Global<v8::Context>& global_context) override { | |
94 v8::MicrotasksScope microtasks_scope(isolate, | |
95 v8::MicrotasksScope::kRunMicrotasks); | |
96 v8::HandleScope handle_scope(isolate); | |
97 v8::Local<v8::Context> context = global_context.Get(isolate); | |
98 v8::Context::Scope context_scope(context); | |
99 | |
100 v8::Local<v8::Function> function = function_.Get(isolate); | |
101 v8::MaybeLocal<v8::Value> result; | |
102 v8_inspector::V8Inspector* inspector = | |
103 InspectorClientImpl::InspectorFromContext(context); | |
104 if (inspector) inspector->willExecuteScript(context, function->ScriptId()); | |
105 result = function->Call(context, context->Global(), 0, nullptr); | |
106 if (inspector) inspector->didExecuteScript(context); | |
107 } | |
108 | |
109 private: | |
110 v8::Global<v8::Function> function_; | |
111 }; | |
112 | |
113 class SetTimeoutExtension : public v8::Extension { | |
114 public: | |
115 SetTimeoutExtension() | |
116 : v8::Extension("v8_inspector/setTimeout", | |
117 "native function setTimeout();") {} | |
118 | |
119 virtual v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate( | |
120 v8::Isolate* isolate, v8::Local<v8::String> name) { | |
121 return v8::FunctionTemplate::New(isolate, SetTimeoutExtension::SetTimeout); | |
122 } | |
123 | |
124 private: | |
125 static void SetTimeout(const v8::FunctionCallbackInfo<v8::Value>& args) { | |
126 v8::Isolate* isolate = args.GetIsolate(); | |
127 if (args.Length() != 2 || !args[1]->IsNumber() || !args[0]->IsFunction() || | |
128 args[1].As<v8::Number>()->Value() != 0.0) { | |
129 fprintf(stderr, | |
130 "Internal error: only setTimeout(function, 0) is supported."); | |
131 Exit(); | |
132 } | |
133 v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext(); | |
134 TaskRunner::FromContext(context)->Append(new SetTimeoutTask( | |
135 args.GetIsolate(), v8::Local<v8::Function>::Cast(args[0]))); | |
136 } | |
137 }; | |
138 | |
139 v8_inspector::String16 ToString16(const v8_inspector::StringView& string) { | |
140 if (string.is8Bit()) | |
141 return v8_inspector::String16( | |
142 reinterpret_cast<const char*>(string.characters8()), string.length()); | |
143 return v8_inspector::String16( | |
144 reinterpret_cast<const uint16_t*>(string.characters16()), | |
145 string.length()); | |
146 } | |
147 | |
148 class FrontendChannelImpl : public InspectorClientImpl::FrontendChannel { | |
149 public: | |
150 explicit FrontendChannelImpl(TaskRunner* frontend_task_runner) | |
151 : frontend_task_runner_(frontend_task_runner) {} | |
152 virtual ~FrontendChannelImpl() {} | |
153 | |
154 void SendMessageToFrontend(const v8_inspector::StringView& message) final { | |
155 v8_inspector::String16Builder script; | |
156 script.append("InspectorTest.dispatchMessage("); | |
157 script.append(ToString16(message)); | |
158 script.append(")"); | |
159 frontend_task_runner_->Append(new ExecuteStringTask(script.toString())); | |
160 } | |
161 | |
162 private: | |
163 TaskRunner* frontend_task_runner_; | |
164 }; | |
165 | |
166 } // namespace | |
167 | |
168 int main(int argc, char* argv[]) { | |
169 v8::V8::InitializeICUDefaultLocation(argv[0]); | |
170 v8::Platform* platform = v8::platform::CreateDefaultPlatform(); | |
171 v8::V8::InitializePlatform(platform); | |
172 v8::internal::FlagList::SetFlagsFromCommandLine(&argc, argv, true); | |
173 v8::V8::InitializeExternalStartupData(argv[0]); | |
174 v8::V8::Initialize(); | |
175 | |
176 SetTimeoutExtension set_timeout_extension; | |
177 v8::RegisterExtension(&set_timeout_extension); | |
178 UtilsExtension utils_extension; | |
179 v8::RegisterExtension(&utils_extension); | |
180 SendMessageToBackendExtension send_message_to_backend_extension; | |
181 v8::RegisterExtension(&send_message_to_backend_extension); | |
182 | |
183 v8::base::Semaphore ready_semaphore(0); | |
184 | |
185 const char* backend_extensions[] = {"v8_inspector/setTimeout"}; | |
186 v8::ExtensionConfiguration backend_configuration( | |
187 arraysize(backend_extensions), backend_extensions); | |
188 TaskRunner backend_runner(&backend_configuration, &ready_semaphore); | |
189 ready_semaphore.Wait(); | |
190 SendMessageToBackendExtension::set_backend_task_runner(&backend_runner); | |
191 | |
192 const char* frontend_extensions[] = {"v8_inspector/utils", | |
193 "v8_inspector/frontend"}; | |
194 v8::ExtensionConfiguration frontend_configuration( | |
195 arraysize(frontend_extensions), frontend_extensions); | |
196 TaskRunner frontend_runner(&frontend_configuration, &ready_semaphore); | |
197 ready_semaphore.Wait(); | |
198 | |
199 FrontendChannelImpl frontend_channel(&frontend_runner); | |
200 InspectorClientImpl inspector_client(&backend_runner, &frontend_channel, | |
201 &ready_semaphore); | |
202 ready_semaphore.Wait(); | |
203 | |
204 for (int i = 1; i < argc; ++i) { | |
205 if (argv[i][0] == '-') break; | |
206 | |
207 bool exists = false; | |
208 v8::internal::Vector<const char> chars = | |
209 v8::internal::ReadFile(argv[i], &exists, true); | |
210 if (!exists) { | |
211 fprintf(stderr, "Internal error: script file doesn't exists: %s\n", | |
212 argv[i]); | |
213 Exit(); | |
214 } | |
215 v8_inspector::String16 source = | |
216 v8_inspector::String16::fromUTF8(chars.start(), chars.length()); | |
217 frontend_runner.Append(new ExecuteStringTask(source)); | |
218 } | |
219 | |
220 frontend_runner.Join(); | |
221 return 0; | |
222 } | |
OLD | NEW |