Skip to content

Commit

Permalink
LibJS: Convert Value::get() to ThrowCompletionOr
Browse files Browse the repository at this point in the history
  • Loading branch information
IdanHo authored and linusg committed Oct 18, 2021
1 parent 72b409a commit c15a3b0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
4 changes: 1 addition & 3 deletions Userland/Libraries/LibJS/Runtime/PromiseConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ static Value get_promise_resolve(GlobalObject& global_object, Value constructor)
VERIFY(constructor.is_constructor());
auto& vm = global_object.vm();

auto promise_resolve = constructor.get(global_object, vm.names.resolve);
if (vm.exception())
return {};
auto promise_resolve = TRY_OR_DISCARD(constructor.get(global_object, vm.names.resolve));
if (!promise_resolve.is_function()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, promise_resolve.to_string_without_side_effects());
return {};
Expand Down
14 changes: 5 additions & 9 deletions Userland/Libraries/LibJS/Runtime/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -733,16 +733,16 @@ ThrowCompletionOr<double> Value::to_integer_or_infinity(GlobalObject& global_obj
}

// 7.3.3 GetV ( V, P ), https://tc39.es/ecma262/#sec-getv
Value Value::get(GlobalObject& global_object, PropertyName const& property_name) const
ThrowCompletionOr<Value> Value::get(GlobalObject& global_object, PropertyName const& property_name) const
{
// 1. Assert: IsPropertyKey(P) is true.
VERIFY(property_name.is_valid());

// 2. Let O be ? ToObject(V).
auto* object = TRY_OR_DISCARD(to_object(global_object));
auto* object = TRY(to_object(global_object));

// 3. Return ? O.[[Get]](P, V).
return TRY_OR_DISCARD(object->internal_get(property_name, *this));
return TRY(object->internal_get(property_name, *this));
}

// 7.3.10 GetMethod ( V, P ), https://tc39.es/ecma262/#sec-getmethod
Expand All @@ -754,9 +754,7 @@ ThrowCompletionOr<FunctionObject*> Value::get_method(GlobalObject& global_object
VERIFY(property_name.is_valid());

// 2. Let func be ? GetV(V, P).
auto function = get(global_object, property_name);
if (auto* exception = vm.exception())
return throw_completion(exception->value());
auto function = TRY(get(global_object, property_name));

// 3. If func is either undefined or null, return undefined.
if (function.is_nullish())
Expand Down Expand Up @@ -1485,9 +1483,7 @@ TriState is_less_than(GlobalObject& global_object, bool left_first, Value lhs, V
ThrowCompletionOr<Value> Value::invoke_internal(GlobalObject& global_object, JS::PropertyName const& property_name, Optional<MarkedValueList> arguments)
{
auto& vm = global_object.vm();
auto property = get(global_object, property_name);
if (auto* exception = vm.exception())
return throw_completion(exception->value());
auto property = TRY(get(global_object, property_name));
if (!property.is_function())
return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, property.to_string_without_side_effects());

Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibJS/Runtime/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ class Value {
ThrowCompletionOr<double> to_integer_or_infinity(GlobalObject&) const;
bool to_boolean() const;

Value get(GlobalObject&, PropertyName const&) const;
ThrowCompletionOr<Value> get(GlobalObject&, PropertyName const&) const;
ThrowCompletionOr<FunctionObject*> get_method(GlobalObject&, PropertyName const&) const;

String to_string_without_side_effects() const;
Expand Down

0 comments on commit c15a3b0

Please sign in to comment.