1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-20 18:05:07 +00:00

LibJS: Stop using Optional<Value> in favor of Value's empty state

JS::Value already has the empty state ({} or Value() gives you one.)
Use this instead of wrapping Value in Optional in some places.
I've also added Value::value_or(Value) so you can easily provide a
fallback value when one is not present.
This commit is contained in:
Andreas Kling 2020-04-25 18:43:34 +02:00
parent 5adf4901df
commit 35aea2e454
14 changed files with 60 additions and 48 deletions

View file

@ -136,8 +136,8 @@ Value CallExpression::execute(Interpreter& interpreter) const
if (is_new_expression()) {
new_object = Object::create_empty(interpreter, interpreter.global_object());
auto prototype = function.get("prototype");
if (prototype.has_value() && prototype.value().is_object())
new_object->set_prototype(&prototype.value().as_object());
if (prototype.is_object())
new_object->set_prototype(&prototype.as_object());
call_frame.this_value = new_object;
result = function.construct(interpreter);
} else {
@ -704,10 +704,10 @@ void ForStatement::dump(int indent) const
Value Identifier::execute(Interpreter& interpreter) const
{
auto variable = interpreter.get_variable(string());
if (!variable.has_value())
auto value = interpreter.get_variable(string());
if (value.is_empty())
return interpreter.throw_exception<ReferenceError>(String::format("'%s' not known", string().characters()));
return variable.value();
return value;
}
void Identifier::dump(int indent) const
@ -1022,11 +1022,7 @@ Value MemberExpression::execute(Interpreter& interpreter) const
auto* object_result = object_value.to_object(interpreter.heap());
if (interpreter.exception())
return {};
auto result = object_result->get(computed_property_name(interpreter));
if (result.has_value()) {
ASSERT(!result.value().is_empty());
}
return result.value_or(js_undefined());
return object_result->get(computed_property_name(interpreter)).value_or(js_undefined());
}
Value StringLiteral::execute(Interpreter& interpreter) const