Commit 19f626f0 authored by jgruber's avatar jgruber Committed by Commit bot

[regexp] Handle unmatched groups in callable replacers

BUG=v8:5437

Review-Url: https://codereview.chromium.org/2776263003
Cr-Commit-Position: refs/heads/master@{#44194}
parent dca166b0
......@@ -1125,7 +1125,7 @@ Handle<JSObject> ConstructNamedCaptureGroupsObject(
DCHECK(1 <= capture_ix && capture_ix <= capture_count);
Handle<Object> capture_value(f_get_capture(capture_ix), isolate);
DCHECK(capture_value->IsString());
DCHECK(capture_value->IsUndefined(isolate) || capture_value->IsString());
JSObject::AddProperty(groups, capture_name, capture_value, NONE);
}
......
......@@ -108,6 +108,9 @@ assertEquals("a", /(?<__proto__>a)/u.exec("a").groups.__proto__);
return `${snd}${fst}`;
});
assertEquals("bacd", result);
assertEquals("undefinedbcd", "abcd".replace(/(.)|(.)/u,
(match, fst, snd, offset, str) => snd));
}
// @@replace with a callable replacement argument (global, named captures).
......@@ -138,6 +141,10 @@ assertEquals("a", /(?<__proto__>a)/u.exec("a").groups.__proto__);
return `${groups.snd}${groups.fst}`;
});
assertEquals("badc", result);
assertEquals("undefinedundefinedundefinedundefined",
"abcd".replace(/(?<fst>.)|(?<snd>.)/gu,
(match, fst, snd, offset, str, groups) => groups.snd));
}
// @@replace with a callable replacement argument (non-global, named captures).
......@@ -154,6 +161,10 @@ assertEquals("a", /(?<__proto__>a)/u.exec("a").groups.__proto__);
return `${groups.snd}${groups.fst}`;
});
assertEquals("bacd", result);
assertEquals("undefinedbcd",
"abcd".replace(/(?<fst>.)|(?<snd>.)/u,
(match, fst, snd, offset, str, groups) => groups.snd));
}
function toSlowMode(re) {
......@@ -190,6 +201,10 @@ function toSlowMode(re) {
return `${groups.snd}${groups.fst}`;
});
assertEquals("badc", result);
assertEquals("undefinedundefinedundefinedundefined",
"abcd".replace(toSlowMode(/(?<fst>.)|(?<snd>.)/gu),
(match, fst, snd, offset, str, groups) => groups.snd));
}
// @@replace with a callable replacement argument (slow, non-global,
......@@ -207,6 +222,10 @@ function toSlowMode(re) {
return `${groups.snd}${groups.fst}`;
});
assertEquals("bacd", result);
assertEquals("undefinedbcd",
"abcd".replace(toSlowMode(/(?<fst>.)|(?<snd>.)/u),
(match, fst, snd, offset, str, groups) => groups.snd));
}
// @@replace with a string replacement argument (no named captures).
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment