1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 22:27:42 +00:00

LibJS: Don't use Handle<Value> for JS::Object private fields

There's no reason to use handles here, we can just mark private element
values from objects that store them.
This commit is contained in:
Andreas Kling 2023-12-09 11:05:25 +01:00
parent 6a4e3d9002
commit 463931384d
4 changed files with 22 additions and 14 deletions

View file

@ -201,13 +201,13 @@ ThrowCompletionOr<ClassElement::ClassValue> ClassMethod::class_element_evaluatio
switch (kind()) { switch (kind()) {
case Kind::Method: case Kind::Method:
set_function_name(); set_function_name();
return ClassValue { PrivateElement { private_name, PrivateElement::Kind::Method, make_handle(method_value) } }; return ClassValue { PrivateElement { private_name, PrivateElement::Kind::Method, method_value } };
case Kind::Getter: case Kind::Getter:
set_function_name("get"); set_function_name("get");
return ClassValue { PrivateElement { private_name, PrivateElement::Kind::Accessor, make_handle(Value(Accessor::create(vm, &method_function, nullptr))) } }; return ClassValue { PrivateElement { private_name, PrivateElement::Kind::Accessor, Value(Accessor::create(vm, &method_function, nullptr)) } };
case Kind::Setter: case Kind::Setter:
set_function_name("set"); set_function_name("set");
return ClassValue { PrivateElement { private_name, PrivateElement::Kind::Accessor, make_handle(Value(Accessor::create(vm, nullptr, &method_function))) } }; return ClassValue { PrivateElement { private_name, PrivateElement::Kind::Accessor, Value(Accessor::create(vm, nullptr, &method_function)) } };
default: default:
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
@ -392,11 +392,11 @@ ThrowCompletionOr<ECMAScriptFunctionObject*> ClassExpression::create_class_const
if (existing.key == private_element.key) { if (existing.key == private_element.key) {
VERIFY(existing.kind == PrivateElement::Kind::Accessor); VERIFY(existing.kind == PrivateElement::Kind::Accessor);
VERIFY(private_element.kind == PrivateElement::Kind::Accessor); VERIFY(private_element.kind == PrivateElement::Kind::Accessor);
auto& accessor = private_element.value.value().as_accessor(); auto& accessor = private_element.value.as_accessor();
if (!accessor.getter()) if (!accessor.getter())
existing.value.value().as_accessor().set_setter(accessor.setter()); existing.value.as_accessor().set_setter(accessor.setter());
else else
existing.value.value().as_accessor().set_getter(accessor.getter()); existing.value.as_accessor().set_getter(accessor.getter());
added_to_existing = true; added_to_existing = true;
} }
} }

View file

@ -538,6 +538,9 @@ void ECMAScriptFunctionObject::visit_edges(Visitor& visitor)
visitor.visit(property_key_ptr->as_symbol()); visitor.visit(property_key_ptr->as_symbol());
} }
for (auto& private_element : m_private_methods)
visitor.visit(private_element.value);
m_script_or_module.visit( m_script_or_module.visit(
[](Empty) {}, [](Empty) {},
[&](auto& script_or_module) { [&](auto& script_or_module) {

View file

@ -519,7 +519,7 @@ ThrowCompletionOr<void> Object::private_field_add(PrivateName const& name, Value
m_private_elements = make<Vector<PrivateElement>>(); m_private_elements = make<Vector<PrivateElement>>();
// 4. Append PrivateElement { [[Key]]: P, [[Kind]]: field, [[Value]]: value } to O.[[PrivateElements]]. // 4. Append PrivateElement { [[Key]]: P, [[Kind]]: field, [[Value]]: value } to O.[[PrivateElements]].
m_private_elements->empend(name, PrivateElement::Kind::Field, make_handle(value)); m_private_elements->empend(name, PrivateElement::Kind::Field, value);
// 5. Return unused. // 5. Return unused.
return {}; return {};
@ -570,14 +570,14 @@ ThrowCompletionOr<Value> Object::private_get(PrivateName const& name)
// 3. If entry.[[Kind]] is either field or method, then // 3. If entry.[[Kind]] is either field or method, then
if (entry->kind != PrivateElement::Kind::Accessor) { if (entry->kind != PrivateElement::Kind::Accessor) {
// a. Return entry.[[Value]]. // a. Return entry.[[Value]].
return value.value(); return value;
} }
// Assert: entry.[[Kind]] is accessor. // Assert: entry.[[Kind]] is accessor.
VERIFY(value.value().is_accessor()); VERIFY(value.is_accessor());
// 6. Let getter be entry.[[Get]]. // 6. Let getter be entry.[[Get]].
auto* getter = value.value().as_accessor().getter(); auto* getter = value.as_accessor().getter();
// 5. If entry.[[Get]] is undefined, throw a TypeError exception. // 5. If entry.[[Get]] is undefined, throw a TypeError exception.
if (!getter) if (!getter)
@ -602,7 +602,7 @@ ThrowCompletionOr<void> Object::private_set(PrivateName const& name, Value value
// 3. If entry.[[Kind]] is field, then // 3. If entry.[[Kind]] is field, then
if (entry->kind == PrivateElement::Kind::Field) { if (entry->kind == PrivateElement::Kind::Field) {
// a. Set entry.[[Value]] to value. // a. Set entry.[[Value]] to value.
entry->value = make_handle(value); entry->value = value;
return {}; return {};
} }
// 4. Else if entry.[[Kind]] is method, then // 4. Else if entry.[[Kind]] is method, then
@ -617,10 +617,10 @@ ThrowCompletionOr<void> Object::private_set(PrivateName const& name, Value value
VERIFY(entry->kind == PrivateElement::Kind::Accessor); VERIFY(entry->kind == PrivateElement::Kind::Accessor);
auto& accessor = entry->value; auto& accessor = entry->value;
VERIFY(accessor.value().is_accessor()); VERIFY(accessor.is_accessor());
// c. Let setter be entry.[[Set]]. // c. Let setter be entry.[[Set]].
auto* setter = accessor.value().as_accessor().setter(); auto* setter = accessor.as_accessor().setter();
// b. If entry.[[Set]] is undefined, throw a TypeError exception. // b. If entry.[[Set]] is undefined, throw a TypeError exception.
if (!setter) if (!setter)
@ -1386,6 +1386,11 @@ void Object::visit_edges(Cell::Visitor& visitor)
m_indexed_properties.for_each_value([&visitor](auto& value) { m_indexed_properties.for_each_value([&visitor](auto& value) {
visitor.visit(value); visitor.visit(value);
}); });
if (m_private_elements) {
for (auto& private_element : *m_private_elements)
visitor.visit(private_element.value);
}
} }
// 7.1.1.1 OrdinaryToPrimitive ( O, hint ), https://tc39.es/ecma262/#sec-ordinarytoprimitive // 7.1.1.1 OrdinaryToPrimitive ( O, hint ), https://tc39.es/ecma262/#sec-ordinarytoprimitive

View file

@ -37,7 +37,7 @@ struct PrivateElement {
PrivateName key; PrivateName key;
Kind kind { Kind::Field }; Kind kind { Kind::Field };
Handle<Value> value; Value value;
}; };
// Non-standard: This is information optionally returned by object property access functions. // Non-standard: This is information optionally returned by object property access functions.