// Copyright 2020 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_TEST_INSPECTOR_TASKS_H_ #define V8_TEST_INSPECTOR_TASKS_H_ #include #include "include/v8-context.h" #include "include/v8-function.h" #include "include/v8-inspector.h" #include "include/v8-microtask-queue.h" #include "include/v8-primitive.h" #include "src/base/platform/semaphore.h" #include "test/inspector/isolate-data.h" #include "test/inspector/task-runner.h" #include "test/inspector/utils.h" namespace v8 { namespace internal { void RunSyncTask(TaskRunner* task_runner, std::function callback); void RunSimpleAsyncTask(TaskRunner* task_runner, std::function task, v8::Local callback); class SendMessageToBackendTask : public TaskRunner::Task { public: SendMessageToBackendTask(int session_id, const std::vector& message) : session_id_(session_id), message_(message) {} bool is_priority_task() final { return true; } private: void Run(InspectorIsolateData* data) override { v8_inspector::StringView message_view(message_.data(), message_.size()); data->SendMessage(session_id_, message_view); } int session_id_; std::vector message_; }; inline void RunAsyncTask(TaskRunner* task_runner, const v8_inspector::StringView& task_name, std::unique_ptr task) { class AsyncTask : public TaskRunner::Task { public: explicit AsyncTask(std::unique_ptr inner) : inner_(std::move(inner)) {} ~AsyncTask() override = default; AsyncTask(const AsyncTask&) = delete; AsyncTask& operator=(const AsyncTask&) = delete; bool is_priority_task() override { return inner_->is_priority_task(); } void Run(InspectorIsolateData* data) override { data->AsyncTaskStarted(inner_.get()); inner_->Run(data); data->AsyncTaskFinished(inner_.get()); } private: std::unique_ptr inner_; }; task_runner->data()->AsyncTaskScheduled(task_name, task.get(), false); task_runner->Append(std::make_unique(std::move(task))); } class ExecuteStringTask : public TaskRunner::Task { public: ExecuteStringTask(v8::Isolate* isolate, int context_group_id, const std::vector& expression, v8::Local name, v8::Local line_offset, v8::Local column_offset, v8::Local is_module) : expression_(expression), name_(ToVector(isolate, name)), line_offset_(line_offset.As()->Value()), column_offset_(column_offset.As()->Value()), is_module_(is_module->Value()), context_group_id_(context_group_id) {} ExecuteStringTask(const std::string& expression, int context_group_id) : expression_utf8_(expression), context_group_id_(context_group_id) {} ~ExecuteStringTask() override = default; ExecuteStringTask(const ExecuteStringTask&) = delete; ExecuteStringTask& operator=(const ExecuteStringTask&) = delete; bool is_priority_task() override { return false; } void Run(InspectorIsolateData* data) override; private: std::vector expression_; std::string expression_utf8_; std::vector name_; int32_t line_offset_ = 0; int32_t column_offset_ = 0; bool is_module_ = false; int context_group_id_; }; class SetTimeoutTask : public TaskRunner::Task { public: SetTimeoutTask(int context_group_id, v8::Isolate* isolate, v8::Local function) : function_(isolate, function), context_group_id_(context_group_id) {} ~SetTimeoutTask() override = default; bool is_priority_task() final { return false; } private: void Run(InspectorIsolateData* data) override { v8::HandleScope handle_scope(data->isolate()); v8::Local context = data->GetDefaultContext(context_group_id_); v8::MicrotasksScope microtasks_scope(context, v8::MicrotasksScope::kRunMicrotasks); v8::Context::Scope context_scope(context); v8::Local function = function_.Get(data->isolate()); v8::MaybeLocal result; result = function->Call(context, context->Global(), 0, nullptr); } v8::Global function_; int context_group_id_; }; class SetTimeoutExtension : public InspectorIsolateData::SetupGlobalTask { public: void Run(v8::Isolate* isolate, v8::Local global) override { global->Set( ToV8String(isolate, "setTimeout"), v8::FunctionTemplate::New(isolate, &SetTimeoutExtension::SetTimeout)); } private: static void SetTimeout(const v8::FunctionCallbackInfo& info) { if (info.Length() != 2 || !info[1]->IsNumber() || (!info[0]->IsFunction() && !info[0]->IsString()) || info[1].As()->Value() != 0.0) { return; } v8::Isolate* isolate = info.GetIsolate(); v8::Local context = isolate->GetCurrentContext(); InspectorIsolateData* data = InspectorIsolateData::FromContext(context); int context_group_id = data->GetContextGroupId(context); const char* task_name = "setTimeout"; v8_inspector::StringView task_name_view( reinterpret_cast(task_name), strlen(task_name)); if (info[0]->IsFunction()) { RunAsyncTask(data->task_runner(), task_name_view, std::make_unique( context_group_id, isolate, v8::Local::Cast(info[0]))); } else { RunAsyncTask( data->task_runner(), task_name_view, std::make_unique( isolate, context_group_id, ToVector(isolate, info[0].As()), v8::String::Empty(isolate), v8::Integer::New(isolate, 0), v8::Integer::New(isolate, 0), v8::Boolean::New(isolate, false))); } } }; } // namespace internal } // namespace v8 #endif // V8_TEST_INSPECTOR_TASKS_H_