diff --git a/shell/common/gin_helper/dictionary.h b/shell/common/gin_helper/dictionary.h index be0e9fae72b..e51ddd623a4 100644 --- a/shell/common/gin_helper/dictionary.h +++ b/shell/common/gin_helper/dictionary.h @@ -42,16 +42,16 @@ class Dictionary : public gin::Dictionary { // 3. It accepts arbitrary type of key. template bool Get(const K& key, V* out) const { + v8::Isolate* const iso = isolate(); + v8::Local handle = GetHandle(); + v8::Local context = iso->GetCurrentContext(); + v8::Local v8_key = gin::ConvertToV8(iso, key); + v8::Local value; // Check for existence before getting, otherwise this method will always // returns true when T == v8::Local. - v8::Local context = isolate()->GetCurrentContext(); - v8::Local v8_key = gin::ConvertToV8(isolate(), key); - v8::Local value; - v8::Maybe result = GetHandle()->Has(context, v8_key); - if (result.IsJust() && result.FromJust() && - GetHandle()->Get(context, v8_key).ToLocal(&value)) - return gin::ConvertFromV8(isolate(), value, out); - return false; + return handle->Has(context, v8_key).FromMaybe(false) && + handle->Get(context, v8_key).ToLocal(&value) && + gin::ConvertFromV8(iso, value, out); } // Differences from the Set method in gin::Dictionary: @@ -64,7 +64,7 @@ class Dictionary : public gin::Dictionary { v8::Maybe result = GetHandle()->Set(isolate()->GetCurrentContext(), gin::ConvertToV8(isolate(), key), v8_value); - return !result.IsNothing() && result.FromJust(); + return result.FromMaybe(false); } // Like normal Get but put result in an std::optional. @@ -81,28 +81,27 @@ class Dictionary : public gin::Dictionary { template bool GetHidden(std::string_view key, T* out) const { - v8::Local context = isolate()->GetCurrentContext(); - v8::Local privateKey = - v8::Private::ForApi(isolate(), gin::StringToV8(isolate(), key)); + v8::Isolate* const iso = isolate(); + v8::Local handle = GetHandle(); + v8::Local context = iso->GetCurrentContext(); + v8::Local privateKey = MakeHiddenKey(key); v8::Local value; - v8::Maybe result = GetHandle()->HasPrivate(context, privateKey); - if (result.IsJust() && result.FromJust() && - GetHandle()->GetPrivate(context, privateKey).ToLocal(&value)) - return gin::ConvertFromV8(isolate(), value, out); - return false; + return handle->HasPrivate(context, privateKey).FromMaybe(false) && + handle->GetPrivate(context, privateKey).ToLocal(&value) && + gin::ConvertFromV8(iso, value, out); } template bool SetHidden(std::string_view key, T val) { + v8::Isolate* const iso = isolate(); v8::Local v8_value; - if (!gin::TryConvertToV8(isolate(), val, &v8_value)) + if (!gin::TryConvertToV8(iso, val, &v8_value)) return false; - v8::Local context = isolate()->GetCurrentContext(); - v8::Local privateKey = - v8::Private::ForApi(isolate(), gin::StringToV8(isolate(), key)); + v8::Local context = iso->GetCurrentContext(); + v8::Local privateKey = MakeHiddenKey(key); v8::Maybe result = GetHandle()->SetPrivate(context, privateKey, v8_value); - return !result.IsNothing() && result.FromJust(); + return result.FromMaybe(false); } template @@ -110,7 +109,7 @@ class Dictionary : public gin::Dictionary { auto context = isolate()->GetCurrentContext(); auto templ = CallbackTraits::CreateTemplate(isolate(), callback); return GetHandle() - ->Set(context, gin::StringToV8(isolate(), key), + ->Set(context, MakeKey(key), templ->GetFunction(context).ToLocalChecked()) .ToChecked(); } @@ -130,7 +129,7 @@ class Dictionary : public gin::Dictionary { return GetHandle() ->SetNativeDataProperty( - context, gin::StringToV8(isolate(), key), + context, MakeKey(key), [](v8::Local property_name, const v8::PropertyCallbackInfo& info) { AccessorValue acc_value; @@ -153,9 +152,8 @@ class Dictionary : public gin::Dictionary { if (!gin::TryConvertToV8(isolate(), val, &v8_value)) return false; v8::Maybe result = GetHandle()->DefineOwnProperty( - isolate()->GetCurrentContext(), gin::StringToV8(isolate(), key), - v8_value, v8::ReadOnly); - return !result.IsNothing() && result.FromJust(); + isolate()->GetCurrentContext(), MakeKey(key), v8_value, v8::ReadOnly); + return result.FromMaybe(false); } // Note: If we plan to add more Set methods, consider adding an option instead @@ -166,22 +164,21 @@ class Dictionary : public gin::Dictionary { if (!gin::TryConvertToV8(isolate(), val, &v8_value)) return false; v8::Maybe result = GetHandle()->DefineOwnProperty( - isolate()->GetCurrentContext(), gin::StringToV8(isolate(), key), - v8_value, + isolate()->GetCurrentContext(), MakeKey(key), v8_value, static_cast(v8::ReadOnly | v8::DontDelete)); - return !result.IsNothing() && result.FromJust(); + return result.FromMaybe(false); } bool Has(std::string_view key) const { - v8::Maybe result = GetHandle()->Has(isolate()->GetCurrentContext(), - gin::StringToV8(isolate(), key)); - return !result.IsNothing() && result.FromJust(); + v8::Maybe result = + GetHandle()->Has(isolate()->GetCurrentContext(), MakeKey(key)); + return result.FromMaybe(false); } bool Delete(std::string_view key) { - v8::Maybe result = GetHandle()->Delete( - isolate()->GetCurrentContext(), gin::StringToV8(isolate(), key)); - return !result.IsNothing() && result.FromJust(); + v8::Maybe result = + GetHandle()->Delete(isolate()->GetCurrentContext(), MakeKey(key)); + return result.FromMaybe(false); } bool IsEmpty() const { return isolate() == nullptr || GetHandle().IsEmpty(); } @@ -194,6 +191,16 @@ class Dictionary : public gin::Dictionary { private: // DO NOT ADD ANY DATA MEMBER. + + [[nodiscard]] v8::Local MakeKey( + const std::string_view key) const { + return gin::StringToV8(isolate(), key); + } + + [[nodiscard]] v8::Local MakeHiddenKey( + const std::string_view key) const { + return v8::Private::ForApi(isolate(), MakeKey(key)); + } }; } // namespace gin_helper diff --git a/shell/common/gin_helper/persistent_dictionary.h b/shell/common/gin_helper/persistent_dictionary.h index 87d0dc46c55..7bb41835fd7 100644 --- a/shell/common/gin_helper/persistent_dictionary.h +++ b/shell/common/gin_helper/persistent_dictionary.h @@ -32,14 +32,13 @@ class PersistentDictionary { template bool Get(const K& key, V* out) const { + const auto handle = GetHandle(); v8::Local context = isolate_->GetCurrentContext(); v8::Local v8_key = gin::ConvertToV8(isolate_, key); v8::Local value; - v8::Maybe result = GetHandle()->Has(context, v8_key); - if (result.IsJust() && result.FromJust() && - GetHandle()->Get(context, v8_key).ToLocal(&value)) - return gin::ConvertFromV8(isolate_, value, out); - return false; + return handle->Has(context, v8_key).FromMaybe(false) && + handle->Get(context, v8_key).ToLocal(&value) && + gin::ConvertFromV8(isolate_, value, out); } private: