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