0

[v8][api] Don't use v8::Object::SetAccessor() in ModuleSystem

... and use SetLazyDataProperty() instead. SetAccessor() will be
deprecated soon.

Given that SetLazyDataProperty() does property reconfiguration
underneath it's no longer necessary to do it manually in
ModuleSystem::LazyFieldGetter().

Bug: 336325111
Change-Id: I64dbf9cc5481c9346233c9872c4407fca206cf29
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5477788
Reviewed-by: Devlin Cronin <rdevlin.cronin@chromium.org>
Commit-Queue: Igor Sheludko <ishell@chromium.org>
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1291802}
This commit is contained in:
Igor Sheludko
2024-04-24 11:08:38 +00:00
committed by Chromium LUCI CQ
parent eefbee8a9f
commit 993878ad8e

@ -481,30 +481,8 @@ void ModuleSystem::LazyFieldGetter(
// that the extension doesn't have permission to use them.
CHECK(!new_field.IsEmpty());
// Delete the getter and set this field to |new_field| so the same object is
// returned every time a certain API is accessed.
v8::Local<v8::Value> val = info.This();
if (val->IsObject()) {
v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(val);
auto maybe_deleted = object->Delete(context, property);
if (!maybe_deleted.IsJust()) {
// In theory, deletion should never result in throwing an error. But
// crazier things have happened.
NOTREACHED();
return;
}
if (!maybe_deleted.FromJust()) {
// Deletion can *fail* in certain cases, such as when the script does
// Object.freeze(chrome).
return;
}
auto maybe_set = object->CreateDataProperty(context, property, new_field);
// Setting a new value can fail in multiple scenarios. Bail out if it does.
if (!maybe_set.IsJust() || !maybe_set.FromJust())
return;
} else {
NOTREACHED();
}
// v8::Object::SetLazyDataProperty() machinery will reconfigure the property
// to a regular data property with |new_field| value.
info.GetReturnValue().Set(new_field);
}
@ -525,9 +503,9 @@ void ModuleSystem::SetLazyField(v8::Local<v8::Object> object,
ToV8StringUnsafe(GetIsolate(), module_name.c_str()));
SetPrivateProperty(context, parameters, kModuleField,
ToV8StringUnsafe(GetIsolate(), module_field.c_str()));
auto maybe = object->SetAccessor(
auto maybe = object->SetLazyDataProperty(
context, ToV8StringUnsafe(GetIsolate(), field.c_str()),
&ModuleSystem::LazyFieldGetter, nullptr, parameters);
&ModuleSystem::LazyFieldGetter, parameters);
CHECK(v8_helpers::IsTrue(maybe));
}