LibJS: Add the String.prototype.search method

This is only the String.prototype side of search, for full
functionality Symbol.search has to be implemented in our RegExp
built-in object.
This commit is contained in:
Idan Horowitz 2021-06-16 17:49:46 +03:00 committed by Linus Groh
parent bad2108d3c
commit 25a292f534
Notes: sideshowbarker 2024-07-18 12:10:29 +09:00
2 changed files with 24 additions and 0 deletions

View file

@ -81,6 +81,7 @@ void StringPrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.match, match, 1, attr);
define_native_function(vm.names.matchAll, match_all, 1, attr);
define_native_function(vm.names.replace, replace, 2, attr);
define_native_function(vm.names.search, search, 1, attr);
define_native_function(vm.names.anchor, anchor, 1, attr);
define_native_function(vm.names.big, big, 0, attr);
define_native_function(vm.names.blink, blink, 0, attr);
@ -802,6 +803,28 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::replace)
return js_string(vm, builder.build());
}
// 22.1.3.19 String.prototype.search ( regexp ), https://tc39.es/ecma262/#sec-string.prototype.search
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::search)
{
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* searcher = get_method(global_object, regexp, vm.well_known_symbol_search()))
return vm.call(*searcher, regexp, this_object);
if (vm.exception())
return {};
}
auto s = this_object.to_string(global_object);
if (vm.exception())
return {};
auto rx = regexp_create(global_object, regexp, js_undefined());
if (!rx)
return {};
return rx->invoke(vm.well_known_symbol_search(), js_string(vm, s));
}
// B.2.3.2.1 CreateHTML ( string, tag, attribute, value ), https://tc39.es/ecma262/#sec-createhtml
static Value create_html(GlobalObject& global_object, Value string, const String& tag, const String& attribute, Value value)
{

View file

@ -46,6 +46,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(match);
JS_DECLARE_NATIVE_FUNCTION(match_all);
JS_DECLARE_NATIVE_FUNCTION(replace);
JS_DECLARE_NATIVE_FUNCTION(search);
JS_DECLARE_NATIVE_FUNCTION(anchor);
JS_DECLARE_NATIVE_FUNCTION(big);
JS_DECLARE_NATIVE_FUNCTION(blink);