#pragma once // Fast API implementation for workerd // // This file provides utilities to support V8 Fast API Calls, which allow optimized // method calls from JavaScript to C++ without going through the V8 API. Fast API // Calls perform type checks in the compiler instead of on the embedder side and // are subject to strict limitations - they cannot allocate on the JS heap or // trigger JS execution. // // The implementation provides concepts and helpers to determine which types and methods // are compatible with Fast API, handling both primitive types that can be passed directly // and wrapped objects that require conversion between JavaScript and C++. // // We don't add FastOneByteString because any GC call before FastOneByteString being copied // will corrupt the string data and cause catastrophic failures with almost zero stack trace. // For more information, see https://github.com/cloudflare/workerd/pull/4625. #include #include #include #include #include #include namespace workerd::jsg { class DOMString; class USVString; class Lock; template class Promise; template constexpr bool isFunctionCallbackInfo = false; template constexpr bool isFunctionCallbackInfo> = true; template constexpr bool isKjPromise = false; template constexpr bool isKjPromise> = true; template constexpr bool isJsgPromise = false; template constexpr bool isJsgPromise> = true; // These types are passed by fast api as is and do not require wrapping/unwrapping. template concept FastApiPrimitive = kj::isSameType() || kj::isSameType() || kj::isSameType() || kj::isSameType() || kj::isSameType() || kj::isSameType() || kj::isSameType() || kj::isSameType(); // Helper to determine if a type can be used as a parameter in V8 Fast API template concept FastApiParam = !isFunctionCallbackInfo>> && !isKjPromise>> && !isJsgPromise>>; // Helper to determine if a type can be used as a return value in a V8 Fast API template concept FastApiReturnParam = FastApiPrimitive; // Helper to determine if a method can be used with V8 fast API template concept FastApiMethod = FastApiReturnParam && (FastApiParam && ...); // Helper to determine if a method pointer type is compatible with Fast API template constexpr bool isFastApiCompatible = false; // Specialization for non-static member functions template constexpr bool isFastApiCompatible = FastApiMethod; // Specialization for const non-static member functions template constexpr bool isFastApiCompatible = FastApiMethod; template constexpr bool isFastApiCompatible = FastApiMethod; template constexpr bool isFastApiCompatible = FastApiMethod; // Specialization for static functions template constexpr bool isFastApiCompatible = FastApiMethod; template constexpr bool isFastApiCompatible = FastApiMethod; template struct FastApiJSGToV8 { using value = v8::Local; }; template requires FastApiPrimitive>> struct FastApiJSGToV8 { using value = kj::RemoveConst>; }; } // namespace workerd::jsg