// Copyright 2018 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_D8_ASYNC_HOOKS_WRAPPER_H_ #define V8_D8_ASYNC_HOOKS_WRAPPER_H_ #include #include "include/v8-function-callback.h" #include "include/v8-local-handle.h" #include "include/v8-promise.h" #include "src/objects/objects.h" namespace v8 { class Function; class Isolate; class ObjectTemplate; class Value; using async_id_t = double; struct AsyncContext { async_id_t execution_async_id; async_id_t trigger_async_id; }; class AsyncHooksWrap { public: explicit AsyncHooksWrap(Isolate* isolate) : isolate_(isolate), enabled_(false) {} void Enable(); void Disable(); bool IsEnabled() const { return enabled_; } inline v8::Local init_function() const; inline void set_init_function(v8::Local value); inline v8::Local before_function() const; inline void set_before_function(v8::Local value); inline v8::Local after_function() const; inline void set_after_function(v8::Local value); inline v8::Local promiseResolve_function() const; inline void set_promiseResolve_function(v8::Local value); private: Isolate* isolate_; Persistent init_function_; Persistent before_function_; Persistent after_function_; Persistent promiseResolve_function_; bool enabled_; }; class AsyncHooks { public: explicit AsyncHooks(Isolate* isolate); ~AsyncHooks(); async_id_t GetExecutionAsyncId() const; async_id_t GetTriggerAsyncId() const; Local CreateHook(const v8::FunctionCallbackInfo& args); Persistent async_hook_ctor; private: base::RecursiveMutex async_wraps_mutex_; std::vector> async_wraps_; Isolate* isolate_; Persistent async_hooks_templ; Persistent async_id_smb; Persistent trigger_id_smb; static void ShellPromiseHook(PromiseHookType type, Local promise, Local parent); static void PromiseHookDispatch(PromiseHookType type, Local promise, Local parent, const AsyncHooksWrap& wrap, AsyncHooks* hooks); std::stack asyncContexts; async_id_t current_async_id; }; } // namespace v8 #endif // V8_D8_ASYNC_HOOKS_WRAPPER_H_