0

Allow ScriptPromiseResolver<V8EnumClass>::Resolve(V8EnumClass::Enum)

This simplifies callsites from:
resolver->Resolve(V8EnumClass(V8EnumClass::Enum::kEnumValue));

to:
resolver->Resolve(V8EnumClass::Enum::kEnumValue);

Use this pattern in gamepad_haptic_actuator.cc as a demonstration

Change-Id: Id1dd886611c611343104d1f5ba0463a103630521
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6410845
Reviewed-by: Andrey Kosyakov <caseq@chromium.org>
Commit-Queue: Nate Chapin <japhet@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1439786}
This commit is contained in:
Nate Chapin 2025-03-28 16:52:20 -07:00 committed by Chromium LUCI CQ
parent 993f0c0908
commit e2fb2c5ecb
2 changed files with 17 additions and 7 deletions
third_party/blink/renderer

@ -281,6 +281,18 @@ class ScriptPromiseResolver final : public ScriptPromiseResolverBase {
MakeGarbageCollected<IDLResolvedType>(value));
}
// This Resolve() method allows a Promise expecting to be resolved with an
// enum type to be resolved with an enum value of that type rather than having
// to explicitly construct the enum type.
template <typename T = IDLResolvedType>
requires std::derived_from<IDLResolvedType, bindings::EnumerationBase>
void Resolve(T::Enum value) {
if (!PrepareToResolveOrReject<kResolving>()) {
return;
}
ResolveOrReject<IDLResolvedType>(T(value));
}
// A promise may be resolved with another promise if they are the same type.
void Resolve(ScriptPromise<IDLResolvedType> promise) {
if (!PrepareToResolveOrReject<kResolving>()) {

@ -36,18 +36,16 @@ GamepadHapticEffectType EffectTypeFromEnum(
NOTREACHED();
}
V8GamepadHapticsResult ResultToV8(GamepadHapticsResult result) {
V8GamepadHapticsResult::Enum ResultToV8(GamepadHapticsResult result) {
switch (result) {
case GamepadHapticsResult::GamepadHapticsResultComplete:
return V8GamepadHapticsResult(V8GamepadHapticsResult::Enum::kComplete);
return V8GamepadHapticsResult::Enum::kComplete;
case GamepadHapticsResult::GamepadHapticsResultPreempted:
return V8GamepadHapticsResult(V8GamepadHapticsResult::Enum::kPreempted);
return V8GamepadHapticsResult::Enum::kPreempted;
case GamepadHapticsResult::GamepadHapticsResultInvalidParameter:
return V8GamepadHapticsResult(
V8GamepadHapticsResult::Enum::kInvalidParameter);
return V8GamepadHapticsResult::Enum::kInvalidParameter;
case GamepadHapticsResult::GamepadHapticsResultNotSupported:
return V8GamepadHapticsResult(
V8GamepadHapticsResult::Enum::kNotSupported);
return V8GamepadHapticsResult::Enum::kNotSupported;
default:
NOTREACHED();
}