1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 21:08:12 +00:00

LibJS: Implement the RequireObjectCoercible abstract operation

Throws an exception if the given value is nullish, returns it otherwise.
We can now gradually replace such manual checks with this function where
applicable.

This also has the advantage that the somewhat useless "ToObject on null
or undefined" will be replaced with "null cannot be converted to an
object" or "undefined cannot be converted to an object". :^)
This commit is contained in:
Linus Groh 2021-06-06 16:10:23 +01:00 committed by Andreas Kling
parent 4ca4c43cbd
commit cd12b2aa57
3 changed files with 16 additions and 1 deletions

View file

@ -1390,4 +1390,16 @@ Object* species_constructor(GlobalObject& global_object, const Object& object, O
vm.throw_exception<TypeError>(global_object, ErrorType::NotAConstructor, species.to_string_without_side_effects());
return nullptr;
}
// 7.2.1 RequireObjectCoercible, https://tc39.es/ecma262/#sec-requireobjectcoercible
Value require_object_coercible(GlobalObject& global_object, Value value)
{
auto& vm = global_object.vm();
if (value.is_nullish()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotObjectCoercible, value.to_string_without_side_effects());
return {};
}
return value;
}
}