Skip to content

Commit eefbed1

Browse files
committed
Simplify some templates
1 parent d7722d2 commit eefbed1

4 files changed

Lines changed: 13 additions & 8 deletions

File tree

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/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)