// Copyright (c) 2017-2022 Cloudflare, Inc. // Licensed under the Apache 2.0 license found in the LICENSE file or at: // https://opensource.org/licenses/Apache-2.0 #pragma once #include #include namespace workerd { template class PromiseWrapper { public: template static constexpr const char* getName(kj::Promise*) { return "Promise"; } // Explicitly disallow V8 handles, which are not safe for the KJ event loop to own directly. template static constexpr const char* getName(kj::Promise>*) = delete; template static constexpr const char* getName(kj::Promise>*) = delete; template static constexpr const char* getName(kj::Promise>*) { return "Promise"; } // For some reason, this wasn't needed for jsg::V8Ref... template v8::Local wrap(jsg::Lock& js, v8::Local context, kj::Maybe> creator, kj::Promise promise) { auto jsPromise = IoContext::current().awaitIoLegacy(js, kj::mv(promise)); return static_cast(*this).wrap(js, context, kj::mv(creator), kj::mv(jsPromise)); } template kj::Maybe> tryUnwrap(jsg::Lock& js, v8::Local context, v8::Local handle, kj::Promise*, kj::Maybe> parentObject) { auto& wrapper = static_cast(*this); auto jsPromise = KJ_UNWRAP_OR_RETURN( wrapper.tryUnwrap(js, context, handle, (jsg::Promise*)nullptr, parentObject), kj::none); return IoContext::current().awaitJs(js, kj::mv(jsPromise)); } template v8::Local newContext(v8::Isolate* isolate, kj::Promise value) = delete; template v8::Local getTemplate(v8::Isolate* isolate, kj::Promise*) = delete; }; } // namespace workerd