LibJS: Replace some is_nullish() checks with require_object_coercible()

This commit is contained in:
Linus Groh 2021-06-06 16:27:38 +01:00 committed by Andreas Kling
parent cd12b2aa57
commit 3cfd9f51f7
Notes: sideshowbarker 2024-07-18 12:45:39 +09:00
2 changed files with 13 additions and 22 deletions

View File

@ -36,10 +36,10 @@ static StringObject* typed_this(VM& vm, GlobalObject& global_object)
static String ak_string_from(VM& vm, GlobalObject& global_object)
{
auto* this_object = vm.this_value(global_object).to_object(global_object);
if (!this_object)
auto this_value = require_object_coercible(global_object, vm.this_value(global_object));
if (vm.exception())
return {};
return Value(this_object).to_string(global_object);
return this_value.to_string(global_object);
}
static Optional<size_t> split_match(const String& haystack, size_t start, const String& needle)
@ -650,12 +650,9 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::at)
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::symbol_iterator)
{
auto this_object = vm.this_value(global_object);
if (this_object.is_nullish()) {
vm.throw_exception<TypeError>(global_object, ErrorType::ToObjectNullOrUndefined);
auto this_object = require_object_coercible(global_object, vm.this_value(global_object));
if (vm.exception())
return {};
}
auto string = this_object.to_string(global_object);
if (vm.exception())
return {};
@ -665,11 +662,9 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::symbol_iterator)
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::match)
{
// https://tc39.es/ecma262/#sec-string.prototype.match
auto this_object = vm.this_value(global_object);
if (this_object.is_nullish()) {
vm.throw_exception<TypeError>(global_object, ErrorType::ToObjectNullOrUndefined);
auto this_object = require_object_coercible(global_object, vm.this_value(global_object));
if (vm.exception())
return {};
}
auto regexp = vm.argument(0);
if (!regexp.is_nullish()) {
if (auto* matcher = get_method(global_object, regexp, vm.well_known_symbol_match()))
@ -687,12 +682,9 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::match)
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::replace)
{
// https://tc39.es/ecma262/#sec-string.prototype.replace
auto this_object = vm.this_value(global_object);
if (this_object.is_nullish()) {
vm.throw_exception<TypeError>(global_object, ErrorType::ToObjectNullOrUndefined);
auto this_object = require_object_coercible(global_object, vm.this_value(global_object));
if (vm.exception())
return {};
}
auto search_value = vm.argument(0);
auto replace_value = vm.argument(1);
@ -741,10 +733,9 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::replace)
static Value create_html(GlobalObject& global_object, Value string, const String& tag, const String& attribute, Value value)
{
auto& vm = global_object.vm();
if (string.is_nullish()) {
vm.throw_exception<TypeError>(global_object, ErrorType::ToObjectNullOrUndefined);
require_object_coercible(global_object, string);
if (vm.exception())
return {};
}
auto str = string.to_string(global_object);
if (vm.exception())
return {};

View File

@ -30,10 +30,10 @@ test("casts |this| to string", () => {
expect(() => {
String.prototype[Symbol.iterator].call(null);
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
}).toThrowWithMessage(TypeError, "null cannot be converted to an object");
expect(() => {
String.prototype[Symbol.iterator].call(undefined);
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
}).toThrowWithMessage(TypeError, "undefined cannot be converted to an object");
});
test("utf8 compatible", () => {