From ee66eaa1b03581a90ab24f3e27fab757a97a9fc6 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Fri, 27 Nov 2020 23:04:01 +0000 Subject: [PATCH] LibJS: Make RegExp.prototype.flags spec-compliant This should be using the individual flag boolean properties rather than the [[OriginalFlags]] internal slot. Use an enumerator macro here for brevity, this will be useful for other things as well. :^) --- Libraries/LibJS/Forward.h | 8 +++++ Libraries/LibJS/Runtime/RegExpPrototype.cpp | 35 +++++++++++-------- .../builtins/RegExp/RegExp.prototype.flags.js | 10 ++++++ 3 files changed, 38 insertions(+), 15 deletions(-) create mode 100644 Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.flags.js diff --git a/Libraries/LibJS/Forward.h b/Libraries/LibJS/Forward.h index 3e85cd6b015..3ad96d44592 100644 --- a/Libraries/LibJS/Forward.h +++ b/Libraries/LibJS/Forward.h @@ -92,6 +92,14 @@ __JS_ENUMERATE(toPrimitive, to_primitive) \ __JS_ENUMERATE(toStringTag, to_string_tag) +#define JS_ENUMERATE_REGEXP_FLAGS \ + __JS_ENUMERATE(global, global, g, Global) \ + __JS_ENUMERATE(ignoreCase, ignore_case, i, Insensitive) \ + __JS_ENUMERATE(multiline, multiline, m, Multiline) \ + __JS_ENUMERATE(dotAll, dot_all, s, SingleLine) \ + __JS_ENUMERATE(unicode, unicode, u, Unicode) \ + __JS_ENUMERATE(sticky, sticky, y, Sticky) + namespace JS { class ASTNode; diff --git a/Libraries/LibJS/Runtime/RegExpPrototype.cpp b/Libraries/LibJS/Runtime/RegExpPrototype.cpp index 165bde164d5..d4eb4bd882f 100644 --- a/Libraries/LibJS/Runtime/RegExpPrototype.cpp +++ b/Libraries/LibJS/Runtime/RegExpPrototype.cpp @@ -62,6 +62,16 @@ RegExpPrototype::~RegExpPrototype() { } +static Object* this_object_from(VM& vm, GlobalObject& global_object) +{ + auto this_value = vm.this_value(global_object); + if (!this_value.is_object()) { + vm.throw_exception(global_object, ErrorType::NotAnObject, this_value.to_string_without_side_effects()); + return {}; + } + return &this_value.as_object(); +} + static RegExpObject* regexp_object_from(VM& vm, GlobalObject& global_object) { auto* this_object = vm.this_value(global_object).to_object(global_object); @@ -85,25 +95,20 @@ JS_DEFINE_NATIVE_GETTER(RegExpPrototype::dot_all) JS_DEFINE_NATIVE_GETTER(RegExpPrototype::flags) { - auto regexp_object = regexp_object_from(vm, global_object); - if (!regexp_object) + auto this_object = this_object_from(vm, global_object); + if (!this_object) return {}; - auto flags = regexp_object->declared_options(); StringBuilder builder(8); - if (flags.has_flag_set(ECMAScriptFlags::Global)) - builder.append('g'); - if (flags.has_flag_set(ECMAScriptFlags::Insensitive)) - builder.append('i'); - if (flags.has_flag_set(ECMAScriptFlags::Multiline)) - builder.append('m'); - if (flags.has_flag_set(ECMAScriptFlags::SingleLine)) - builder.append('s'); - if (flags.has_flag_set(ECMAScriptFlags::Unicode)) - builder.append('u'); - if (flags.has_flag_set(ECMAScriptFlags::Sticky)) - builder.append('y'); +#define __JS_ENUMERATE(flagName, flag_name, flag_char, ECMAScriptFlagName) \ + auto flag_##flag_name = this_object->get(vm.names.flagName).value_or(js_undefined()); \ + if (vm.exception()) \ + return {}; \ + if (flag_##flag_name.to_boolean()) \ + builder.append(#flag_char); + JS_ENUMERATE_REGEXP_FLAGS +#undef __JS_ENUMERATE return js_string(vm, builder.to_string()); } diff --git a/Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.flags.js b/Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.flags.js new file mode 100644 index 00000000000..b6445d0b222 --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.flags.js @@ -0,0 +1,10 @@ +test("basic functionality", () => { + expect(/foo/.flags).toBe(""); + expect(/foo/g.flags).toBe("g"); + expect(/foo/i.flags).toBe("i"); + expect(/foo/m.flags).toBe("m"); + expect(/foo/s.flags).toBe("s"); + expect(/foo/u.flags).toBe("u"); + expect(/foo/y.flags).toBe("y"); + expect(/foo/sgimyu.flags).toBe("gimsuy"); +});