From 5cf9f40c32042138b25badd4fb846601352ad915 Mon Sep 17 00:00:00 2001 From: Eugene Lazutkin Date: Fri, 20 Oct 2023 13:58:05 -0500 Subject: [PATCH] Fixes #191. --- lib/new.cc | 40 +++++++++++++++++++++++++--------------- tests/test_general.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 15 deletions(-) diff --git a/lib/new.cc b/lib/new.cc index 9f06c30..fc9ff91 100644 --- a/lib/new.cc +++ b/lib/new.cc @@ -235,6 +235,7 @@ NAN_METHOD(WrappedRE2::New) bool hasIndices = false; auto context = Nan::GetCurrentContext(); + bool needFlags = true; if (info.Length() > 1) { @@ -279,6 +280,7 @@ NAN_METHOD(WrappedRE2::New) } } size = 0; + needFlags = false; } bool needConversion = true; @@ -303,14 +305,18 @@ NAN_METHOD(WrappedRE2::New) source = escapeRegExp(data, size); - v8::RegExp::Flags flags = re->GetFlags(); - global = bool(flags & v8::RegExp::kGlobal); - ignoreCase = bool(flags & v8::RegExp::kIgnoreCase); - multiline = bool(flags & v8::RegExp::kMultiline); - dotAll = bool(flags & v8::RegExp::kDotAll); - unicode = bool(flags & v8::RegExp::kUnicode); - sticky = bool(flags & v8::RegExp::kSticky); - hasIndices = bool(flags & v8::RegExp::kHasIndices); + if (needFlags) + { + v8::RegExp::Flags flags = re->GetFlags(); + global = bool(flags & v8::RegExp::kGlobal); + ignoreCase = bool(flags & v8::RegExp::kIgnoreCase); + multiline = bool(flags & v8::RegExp::kMultiline); + dotAll = bool(flags & v8::RegExp::kDotAll); + unicode = bool(flags & v8::RegExp::kUnicode); + sticky = bool(flags & v8::RegExp::kSticky); + hasIndices = bool(flags & v8::RegExp::kHasIndices); + needFlags = false; + } } else if (info[0]->IsObject() && !info[0]->IsString()) { @@ -331,13 +337,17 @@ NAN_METHOD(WrappedRE2::New) source = re2->source; - global = re2->global; - ignoreCase = re2->ignoreCase; - multiline = re2->multiline; - dotAll = re2->dotAll; - unicode = true; - sticky = re2->sticky; - hasIndices = re2->hasIndices; + if (needFlags) + { + global = re2->global; + ignoreCase = re2->ignoreCase; + multiline = re2->multiline; + dotAll = re2->dotAll; + unicode = true; + sticky = re2->sticky; + hasIndices = re2->hasIndices; + needFlags = false; + } } } else if (info[0]->IsString()) diff --git a/tests/test_general.js b/tests/test_general.js index 3a9487e..2165e0a 100644 --- a/tests/test_general.js +++ b/tests/test_general.js @@ -278,6 +278,45 @@ unit.add(module, [ re = new RE2("a", "smigyu"); eval(t.TEST("re.flags === 'gimsuy'")); + }, + function test_flags_2nd(t) { + "use strict"; + + var re = new RE2(/a/, "u"); + eval(t.TEST("re.flags === 'u'")); + + re = new RE2(/a/gm, "iu"); + eval(t.TEST("re.flags === 'iu'")); + + re = new RE2(/a/ig, "mu"); + eval(t.TEST("re.flags === 'mu'")); + + re = new RE2(/a/g, "gu"); + eval(t.TEST("re.flags === 'gu'")); + + re = new RE2(/a/m, "yu"); + eval(t.TEST("re.flags === 'uy'")); + + re = new RE2(/a/, "yiu"); + eval(t.TEST("re.flags === 'iuy'")); + + re = new RE2(/a/gim, "yigu"); + eval(t.TEST("re.flags === 'giuy'")); + + re = new RE2(/a/gm, "miu"); + eval(t.TEST("re.flags === 'imu'")); + + re = new RE2(/a/i, "ygu"); + eval(t.TEST("re.flags === 'guy'")); + + re = new RE2(/a/g, "myu"); + eval(t.TEST("re.flags === 'muy'")); + + re = new RE2(/a/, "migyu"); + eval(t.TEST("re.flags === 'gimuy'")); + + re = new RE2(/a/s, "smigyu"); + eval(t.TEST("re.flags === 'gimsuy'")); } ]);