Skip to content

Commit 35ab20a

Browse files
authored
Merge pull request #5595 from cloudflare/jasnell/eliminate-duplicate-v8-value-templates
2 parents 6cc580c + eefbed1 commit 35ab20a

File tree

6 files changed

+20
-15
lines changed

6 files changed

+20
-15
lines changed

src/workerd/jsg/jsg.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ kj::String stringifyHandle(v8::Local<v8::Value> value);
4242

4343
namespace v8 {
4444
// Allows v8 handles to be passed to kj::str() as well as KJ_LOG and related macros.
45-
template <typename T, typename = kj::EnableIf<kj::canConvert<T*, v8::Value*>()>>
45+
template <workerd::jsg::V8Value T>
4646
kj::String KJ_STRINGIFY(v8::Local<T> value) {
4747
return workerd::jsg::stringifyHandle(value);
4848
}

src/workerd/jsg/jsvalue.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -713,18 +713,21 @@ inline kj::String KJ_STRINGIFY(const JsValue& value) {
713713
return value.toString(jsg::Lock::current());
714714
}
715715

716+
template <typename T>
717+
concept JsValueType = std::is_assignable_v<JsValue, T>;
718+
716719
template <typename TypeWrapper>
717720
struct JsValueWrapper {
718721
#define TYPES_TO_WRAP(V) \
719722
V(Value) \
720723
JS_TYPE_CLASSES(V)
721724

722-
template <typename T, typename = kj::EnableIf<std::is_assignable_v<JsValue, T>>>
725+
template <JsValueType T>
723726
static constexpr const std::type_info& getName(T*) {
724727
return typeid(T);
725728
}
726729

727-
template <typename T, typename = kj::EnableIf<std::is_assignable_v<JsValue, T>>>
730+
template <JsValueType T>
728731
static constexpr const std::type_info& getName(JsRef<T>*) {
729732
return typeid(T);
730733
}
@@ -742,7 +745,7 @@ struct JsValueWrapper {
742745
TYPES_TO_WRAP(V)
743746
#undef V
744747

745-
template <typename T, typename = kj::EnableIf<std::is_assignable_v<JsValue, T>>>
748+
template <JsValueType T>
746749
kj::Maybe<T> tryUnwrap(Lock& js,
747750
v8::Local<v8::Context> context,
748751
v8::Local<v8::Value> handle,
@@ -763,7 +766,7 @@ struct JsValueWrapper {
763766
}
764767
}
765768

766-
template <typename T, typename = kj::EnableIf<std::is_assignable_v<JsValue, T>>>
769+
template <JsValueType T>
767770
kj::Maybe<JsRef<T>> tryUnwrap(Lock& js,
768771
v8::Local<v8::Context> context,
769772
v8::Local<v8::Value> handle,

src/workerd/jsg/type-wrapper.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ concept ValueLessParameter =
4141
// This is just a trivial pass-through.
4242
class V8HandleWrapper {
4343
public:
44-
template <typename T, typename = kj::EnableIf<kj::canConvert<T, v8::Value>()>>
44+
template <V8Value T>
4545
static constexpr const std::type_info& getName(v8::Local<T>*) {
4646
return typeid(T);
4747
}
4848

49-
template <typename T, typename = kj::EnableIf<kj::canConvert<T, v8::Value>()>>
49+
template <V8Value T>
5050
v8::Local<T> wrap(jsg::Lock& js,
5151
v8::Local<v8::Context> context,
5252
kj::Maybe<v8::Local<v8::Object>> creator,
@@ -111,12 +111,12 @@ class V8HandleWrapper {
111111
#undef JSG_DEFINE_TRY_UNWRAP
112112
#undef JSG_FOR_EACH_V8_VALUE_SUBCLASS
113113

114-
template <typename T, typename = kj::EnableIf<kj::canConvert<T, v8::Value>()>>
114+
template <V8Value T>
115115
static constexpr const std::type_info& getName(v8::Global<T>*) {
116116
return typeid(T);
117117
}
118118

119-
template <typename T, typename = kj::EnableIf<kj::canConvert<T, v8::Value>()>>
119+
template <V8Value T>
120120
v8::Local<T> wrap(jsg::Lock& js,
121121
v8::Local<v8::Context> context,
122122
kj::Maybe<v8::Local<v8::Object>> creator,
@@ -132,12 +132,12 @@ class V8HandleWrapper {
132132
return v8::Global<v8::Value>(js.v8Isolate, handle);
133133
}
134134

135-
template <typename T, typename = kj::EnableIf<kj::canConvert<T, v8::Value>()>>
135+
template <V8Value T>
136136
static constexpr const std::type_info& getName(V8Ref<T>*) {
137137
return typeid(T);
138138
}
139139

140-
template <typename T, typename = kj::EnableIf<kj::canConvert<T, v8::Value>()>>
140+
template <V8Value T>
141141
v8::Local<T> wrap(jsg::Lock& js,
142142
v8::Local<v8::Context> context,
143143
kj::Maybe<v8::Local<v8::Object>> creator,

src/workerd/jsg/util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,9 @@ inline bool isFinite(double value) {
482482
return !(kj::isNaN(value) || value == kj::inf() || value == -kj::inf());
483483
}
484484

485+
template <typename T>
486+
concept StrictlyBool = kj::isSameType<T, bool>();
487+
485488
// ======================================================================================
486489

487490
class Lock;

src/workerd/jsg/value.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ namespace workerd::jsg {
2424

2525
// =======================================================================================
2626
// Primitives (numbers, booleans)
27-
2827
// TypeWrapper mixin for numbers and booleans.
2928
//
3029
// This wrapper has extra wrap() overloads that take an isolate instead of a
@@ -375,15 +374,15 @@ class PrimitiveWrapper {
375374
return "boolean";
376375
}
377376

378-
template <typename T, typename = kj::EnableIf<kj::isSameType<T, bool>()>>
377+
template <StrictlyBool T>
379378
v8::Local<v8::Boolean> wrap(
380379
Lock& js, v8::Local<v8::Context> context, kj::Maybe<v8::Local<v8::Object>> creator, T value) {
381380
// The template is needed to prevent this overload from being chosen for arbitrary types that
382381
// can convert to bool, such as pointers.
383382
return wrap(js.v8Isolate, creator, value);
384383
}
385384

386-
template <typename T, typename = kj::EnableIf<kj::isSameType<T, bool>()>>
385+
template <StrictlyBool T>
387386
v8::Local<v8::Boolean> wrap(
388387
v8::Isolate* isolate, kj::Maybe<v8::Local<v8::Object>> creator, T value) {
389388
// The template is needed to prevent this overload from being chosen for arbitrary types that

src/workerd/jsg/web-idl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ template <typename K, typename V>
100100
constexpr bool isRecordType<Dict<V, K>> = true;
101101

102102
template <typename T>
103-
constexpr bool isBooleanType = kj::isSameType<T, bool>() || kj::isSameType<T, NonCoercible<bool>>();
103+
constexpr bool isBooleanType = StrictlyBool<T> || kj::isSameType<T, NonCoercible<bool>>();
104104

105105
template <typename T>
106106
constexpr bool isIntegerType = kj::isSameType<T, int8_t>() || kj::isSameType<T, int16_t>() ||

0 commit comments

Comments
 (0)