mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:57:34 +00:00
LibJS: Replace some is_nullish() checks with require_object_coercible()
This commit is contained in:
parent
cd12b2aa57
commit
3cfd9f51f7
2 changed files with 13 additions and 22 deletions
|
@ -36,10 +36,10 @@ static StringObject* typed_this(VM& vm, GlobalObject& global_object)
|
||||||
|
|
||||||
static String ak_string_from(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);
|
auto this_value = require_object_coercible(global_object, vm.this_value(global_object));
|
||||||
if (!this_object)
|
if (vm.exception())
|
||||||
return {};
|
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)
|
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)
|
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::symbol_iterator)
|
||||||
{
|
{
|
||||||
auto this_object = vm.this_value(global_object);
|
auto this_object = require_object_coercible(global_object, vm.this_value(global_object));
|
||||||
if (this_object.is_nullish()) {
|
if (vm.exception())
|
||||||
vm.throw_exception<TypeError>(global_object, ErrorType::ToObjectNullOrUndefined);
|
|
||||||
return {};
|
return {};
|
||||||
}
|
|
||||||
|
|
||||||
auto string = this_object.to_string(global_object);
|
auto string = this_object.to_string(global_object);
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
return {};
|
return {};
|
||||||
|
@ -665,11 +662,9 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::symbol_iterator)
|
||||||
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::match)
|
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::match)
|
||||||
{
|
{
|
||||||
// https://tc39.es/ecma262/#sec-string.prototype.match
|
// https://tc39.es/ecma262/#sec-string.prototype.match
|
||||||
auto this_object = vm.this_value(global_object);
|
auto this_object = require_object_coercible(global_object, vm.this_value(global_object));
|
||||||
if (this_object.is_nullish()) {
|
if (vm.exception())
|
||||||
vm.throw_exception<TypeError>(global_object, ErrorType::ToObjectNullOrUndefined);
|
|
||||||
return {};
|
return {};
|
||||||
}
|
|
||||||
auto regexp = vm.argument(0);
|
auto regexp = vm.argument(0);
|
||||||
if (!regexp.is_nullish()) {
|
if (!regexp.is_nullish()) {
|
||||||
if (auto* matcher = get_method(global_object, regexp, vm.well_known_symbol_match()))
|
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)
|
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::replace)
|
||||||
{
|
{
|
||||||
// https://tc39.es/ecma262/#sec-string.prototype.replace
|
// https://tc39.es/ecma262/#sec-string.prototype.replace
|
||||||
auto this_object = vm.this_value(global_object);
|
auto this_object = require_object_coercible(global_object, vm.this_value(global_object));
|
||||||
if (this_object.is_nullish()) {
|
if (vm.exception())
|
||||||
vm.throw_exception<TypeError>(global_object, ErrorType::ToObjectNullOrUndefined);
|
|
||||||
return {};
|
return {};
|
||||||
}
|
|
||||||
|
|
||||||
auto search_value = vm.argument(0);
|
auto search_value = vm.argument(0);
|
||||||
auto replace_value = vm.argument(1);
|
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)
|
static Value create_html(GlobalObject& global_object, Value string, const String& tag, const String& attribute, Value value)
|
||||||
{
|
{
|
||||||
auto& vm = global_object.vm();
|
auto& vm = global_object.vm();
|
||||||
if (string.is_nullish()) {
|
require_object_coercible(global_object, string);
|
||||||
vm.throw_exception<TypeError>(global_object, ErrorType::ToObjectNullOrUndefined);
|
if (vm.exception())
|
||||||
return {};
|
return {};
|
||||||
}
|
|
||||||
auto str = string.to_string(global_object);
|
auto str = string.to_string(global_object);
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
return {};
|
return {};
|
||||||
|
|
|
@ -30,10 +30,10 @@ test("casts |this| to string", () => {
|
||||||
|
|
||||||
expect(() => {
|
expect(() => {
|
||||||
String.prototype[Symbol.iterator].call(null);
|
String.prototype[Symbol.iterator].call(null);
|
||||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
}).toThrowWithMessage(TypeError, "null cannot be converted to an object");
|
||||||
expect(() => {
|
expect(() => {
|
||||||
String.prototype[Symbol.iterator].call(undefined);
|
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", () => {
|
test("utf8 compatible", () => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue