LibJS: Throw on a regex searchString in String.startsWith

As is required by the specification:
"Let isRegExp be ? IsRegExp(searchString). If isRegExp is true,
throw a TypeError exception."
This commit is contained in:
Idan Horowitz 2021-04-20 15:26:47 +03:00 committed by Linus Groh
parent b1e5330e64
commit 63af67ea01
Notes: sideshowbarker 2024-07-18 19:21:07 +09:00

View file

@ -180,7 +180,17 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::starts_with)
if (string.is_null())
return {};
auto search_string = vm.argument(0).to_string(global_object);
auto search_string_value = vm.argument(0);
bool search_is_regexp = search_string_value.is_regexp(global_object);
if (vm.exception())
return {};
if (search_is_regexp) {
vm.throw_exception<TypeError>(global_object, ErrorType::IsNotA, "searchString", "string, but a regular expression");
return {};
}
auto search_string = search_string_value.to_string(global_object);
if (vm.exception())
return {};