From 63af67ea01089d069b353d9270c7b4843307d2e7 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Tue, 20 Apr 2021 15:26:47 +0300 Subject: [PATCH] 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." --- Userland/Libraries/LibJS/Runtime/StringPrototype.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp index 35cf826764..159f895713 100644 --- a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -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(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 {};