From 2f6eec13225ec526b4adf77dfd31c15933986859 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 9 Jul 2021 14:57:39 -0400 Subject: [PATCH] LibJS: Implement RegExp.prototype. according to the spec The flag getters (e.g. RegExp.prototype.unicode) must specially handle when "this" object is the RegExp.prototype object. --- .../LibJS/Runtime/RegExpPrototype.cpp | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp b/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp index 0e4b1b27cf..8f5880a50c 100644 --- a/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp @@ -230,14 +230,22 @@ static Value regexp_exec(GlobalObject& global_object, Object& regexp_object, Str // 22.2.5.9 get RegExp.prototype.multiline, https://tc39.es/ecma262/#sec-get-regexp.prototype.multiline // 22.2.5.14 get RegExp.prototype.sticky, https://tc39.es/ecma262/#sec-get-regexp.prototype.sticky // 22.2.5.17 get RegExp.prototype.unicode, https://tc39.es/ecma262/#sec-get-regexp.prototype.unicode -#define __JS_ENUMERATE(flagName, flag_name, flag_char, ECMAScriptFlagName) \ - JS_DEFINE_NATIVE_GETTER(RegExpPrototype::flag_name) \ - { \ - auto regexp_object = regexp_object_from(vm, global_object); \ - if (!regexp_object) \ - return {}; \ - \ - return Value(regexp_object->declared_options().has_flag_set(ECMAScriptFlags::ECMAScriptFlagName)); \ +#define __JS_ENUMERATE(flagName, flag_name, flag_char, ECMAScriptFlagName) \ + JS_DEFINE_NATIVE_GETTER(RegExpPrototype::flag_name) \ + { \ + auto* regexp_object = this_object_from(vm, global_object); \ + if (!regexp_object) \ + return {}; \ + \ + if (!is(regexp_object)) { \ + if (same_value(regexp_object, global_object.regexp_prototype())) \ + return js_undefined(); \ + vm.throw_exception(global_object, ErrorType::NotA, "RegExp"); \ + return {}; \ + } \ + \ + auto flags = static_cast(regexp_object)->declared_options(); \ + return Value(flags.has_flag_set(ECMAScriptFlags::ECMAScriptFlagName)); \ } JS_ENUMERATE_REGEXP_FLAGS #undef __JS_ENUMERATE