#pragma once #include "transferable.h" #include #include #include #include namespace ivm { /** * Holds a reference to a function in an arbitrary isolate */ class CallbackHandle : public TransferableHandle { public: template explicit CallbackHandle(Args&&... args) : data{std::forward(args)...} {} static auto Definition() -> v8::Local; static auto New(v8::Local fn, v8::MaybeLocal maybe_options) -> std::unique_ptr; auto TransferOut() -> std::unique_ptr final; void Release(); struct InvokeData { enum class Apply { Async, Ignored, Sync }; RemoteHandle callback; RemoteHandle context; Apply apply{}; }; struct Data : public InvokeData { Data() = default; template Data(std::optional name, int length, Args&&... args) : InvokeData{std::forward(args)...}, name{std::move(name)}, length{length} {} std::optional name; int length = 0; }; private: Data data; }; /** * Internal transferable handle for callback */ class CallbackTransferable : public Transferable { public: explicit CallbackTransferable(CallbackHandle::Data& data); explicit CallbackTransferable(v8::Local data); CallbackTransferable(v8::Local fn, v8::Local context); auto TransferIn() -> v8::Local final; private: static void Invoke(const v8::FunctionCallbackInfo& info); CallbackHandle::Data data; }; } // namespace ivm