From d7722d2455b52aedc83ad33c9a377f444e439026 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Wed, 26 Nov 2025 13:20:47 -0800 Subject: [PATCH 1/2] Eliminate duplicate v8::Value templates Use a common concept V8Value. --- src/workerd/jsg/jsg.h | 2 +- src/workerd/jsg/type-wrapper.h | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/workerd/jsg/jsg.h b/src/workerd/jsg/jsg.h index 72fddddcc52..53f08f4b585 100644 --- a/src/workerd/jsg/jsg.h +++ b/src/workerd/jsg/jsg.h @@ -42,7 +42,7 @@ kj::String stringifyHandle(v8::Local value); namespace v8 { // Allows v8 handles to be passed to kj::str() as well as KJ_LOG and related macros. -template ()>> +template kj::String KJ_STRINGIFY(v8::Local value) { return workerd::jsg::stringifyHandle(value); } diff --git a/src/workerd/jsg/type-wrapper.h b/src/workerd/jsg/type-wrapper.h index 62ce9c8fd8f..7d334552f6b 100644 --- a/src/workerd/jsg/type-wrapper.h +++ b/src/workerd/jsg/type-wrapper.h @@ -41,12 +41,12 @@ concept ValueLessParameter = // This is just a trivial pass-through. class V8HandleWrapper { public: - template ()>> + template static constexpr const std::type_info& getName(v8::Local*) { return typeid(T); } - template ()>> + template v8::Local wrap(jsg::Lock& js, v8::Local context, kj::Maybe> creator, @@ -111,12 +111,12 @@ class V8HandleWrapper { #undef JSG_DEFINE_TRY_UNWRAP #undef JSG_FOR_EACH_V8_VALUE_SUBCLASS - template ()>> + template static constexpr const std::type_info& getName(v8::Global*) { return typeid(T); } - template ()>> + template v8::Local wrap(jsg::Lock& js, v8::Local context, kj::Maybe> creator, @@ -132,12 +132,12 @@ class V8HandleWrapper { return v8::Global(js.v8Isolate, handle); } - template ()>> + template static constexpr const std::type_info& getName(V8Ref*) { return typeid(T); } - template ()>> + template v8::Local wrap(jsg::Lock& js, v8::Local context, kj::Maybe> creator, From eefbed1bb5580df8c325313e0d38213aa4e03e02 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Wed, 26 Nov 2025 13:35:30 -0800 Subject: [PATCH 2/2] Simplify some templates --- src/workerd/jsg/jsvalue.h | 11 +++++++---- src/workerd/jsg/util.h | 3 +++ src/workerd/jsg/value.h | 5 ++--- src/workerd/jsg/web-idl.h | 2 +- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/workerd/jsg/jsvalue.h b/src/workerd/jsg/jsvalue.h index db7aa3c98ec..cfd3fd0fd77 100644 --- a/src/workerd/jsg/jsvalue.h +++ b/src/workerd/jsg/jsvalue.h @@ -713,18 +713,21 @@ inline kj::String KJ_STRINGIFY(const JsValue& value) { return value.toString(jsg::Lock::current()); } +template +concept JsValueType = std::is_assignable_v; + template struct JsValueWrapper { #define TYPES_TO_WRAP(V) \ V(Value) \ JS_TYPE_CLASSES(V) - template >> + template static constexpr const std::type_info& getName(T*) { return typeid(T); } - template >> + template static constexpr const std::type_info& getName(JsRef*) { return typeid(T); } @@ -742,7 +745,7 @@ struct JsValueWrapper { TYPES_TO_WRAP(V) #undef V - template >> + template kj::Maybe tryUnwrap(Lock& js, v8::Local context, v8::Local handle, @@ -763,7 +766,7 @@ struct JsValueWrapper { } } - template >> + template kj::Maybe> tryUnwrap(Lock& js, v8::Local context, v8::Local handle, diff --git a/src/workerd/jsg/util.h b/src/workerd/jsg/util.h index 7b3fd773589..f2c2158b3ed 100644 --- a/src/workerd/jsg/util.h +++ b/src/workerd/jsg/util.h @@ -482,6 +482,9 @@ inline bool isFinite(double value) { return !(kj::isNaN(value) || value == kj::inf() || value == -kj::inf()); } +template +concept StrictlyBool = kj::isSameType(); + // ====================================================================================== class Lock; diff --git a/src/workerd/jsg/value.h b/src/workerd/jsg/value.h index 4f2e97cb838..048664cb63a 100644 --- a/src/workerd/jsg/value.h +++ b/src/workerd/jsg/value.h @@ -24,7 +24,6 @@ namespace workerd::jsg { // ======================================================================================= // Primitives (numbers, booleans) - // TypeWrapper mixin for numbers and booleans. // // This wrapper has extra wrap() overloads that take an isolate instead of a @@ -375,7 +374,7 @@ class PrimitiveWrapper { return "boolean"; } - template ()>> + template v8::Local wrap( Lock& js, v8::Local context, kj::Maybe> creator, T value) { // The template is needed to prevent this overload from being chosen for arbitrary types that @@ -383,7 +382,7 @@ class PrimitiveWrapper { return wrap(js.v8Isolate, creator, value); } - template ()>> + template v8::Local wrap( v8::Isolate* isolate, kj::Maybe> creator, T value) { // The template is needed to prevent this overload from being chosen for arbitrary types that diff --git a/src/workerd/jsg/web-idl.h b/src/workerd/jsg/web-idl.h index 5d055ca1d89..b2523984a7a 100644 --- a/src/workerd/jsg/web-idl.h +++ b/src/workerd/jsg/web-idl.h @@ -100,7 +100,7 @@ template constexpr bool isRecordType> = true; template -constexpr bool isBooleanType = kj::isSameType() || kj::isSameType>(); +constexpr bool isBooleanType = StrictlyBool || kj::isSameType>(); template constexpr bool isIntegerType = kj::isSameType() || kj::isSameType() ||