Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 39 additions & 18 deletions rhino/src/main/java/org/mozilla/javascript/regexp/NativeRegExp.java
Original file line number Diff line number Diff line change
Expand Up @@ -1865,6 +1865,10 @@ private static REProgState popProgState(REGlobalData gData) {
return state;
}

private static REProgState peekProgState(REGlobalData gData) {
return gData.stateStackTop;
}

private static void pushBackTrackState(REGlobalData gData, byte op, int pc) {
REProgState state = gData.stateStackTop;
gData.backTrackStackTop =
Expand Down Expand Up @@ -2739,14 +2743,15 @@ && simpleMatch(gData, input, op, program, pc, end, false, false)
{
int nextpc, nextop;
do {
REProgState state = popProgState(gData);
REProgState state = peekProgState(gData);
if (!result) {
// Failed, see if we have enough children.
if (state.min == 0) result = true;
continuationPc = state.continuationPc;
continuationOp = state.continuationOp;
pc += 2 * INDEX_LEN; /* <parencount> & <parenindex> */
pc += getOffset(program, pc);
popProgState(gData);
break switchStatement;
}
if (state.min == 0 && (gData.cp == state.index || state.max == 0)) {
Expand All @@ -2757,6 +2762,7 @@ && simpleMatch(gData, input, op, program, pc, end, false, false)
continuationOp = state.continuationOp;
pc += 2 * INDEX_LEN;
pc += getOffset(program, pc);
popProgState(gData);
break switchStatement;
}
int new_min = state.min, new_max = state.max;
Expand All @@ -2768,6 +2774,7 @@ && simpleMatch(gData, input, op, program, pc, end, false, false)
continuationOp = state.continuationOp;
pc += 2 * INDEX_LEN;
pc += getOffset(program, pc);
popProgState(gData);
break switchStatement;
}
nextpc = pc + 3 * INDEX_LEN;
Expand All @@ -2791,31 +2798,45 @@ && simpleMatch(gData, input, op, program, pc, end, false, false)
continuationOp = state.continuationOp;
pc += 2 * INDEX_LEN; /* <parencount> & <parenindex> */
pc += getOffset(program, pc);
popProgState(gData);
break switchStatement;
}
result = true;
nextpc = match;
}
continuationOp = REOP_REPEAT;
continuationPc = pc;
pushProgState(
gData,
new_min,
new_max,
startcp,
matchBackward,
null,
state.continuationOp,
state.continuationPc);
if (new_min == 0) {
pushBackTrackState(
int opAfterQuant = program[pc + getOffset(program, pc + 2 * INDEX_LEN) + 2 * INDEX_LEN];
if ((opAfterQuant == REOP_END || (opAfterQuant == REOP_EOL && !gData.multiline)) && gData.parens == null) {
// simply update new_min, new_max and start cp
// of the top of the stack
gData.stateStackTop.min = new_min;
gData.stateStackTop.max = new_max;
gData.stateStackTop.index = startcp;
} else {
pushProgState(
gData,
REOP_REPEAT,
pc,
new_min,
new_max,
startcp,
matchBackward,
null,
state.continuationOp,
state.continuationPc);
}
if (new_min == 0) {
if ((opAfterQuant == REOP_END || (opAfterQuant == REOP_EOL && !gData.multiline)) && gData.parens == null) {
gData.backTrackStackTop.cp = startcp;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment: The backTrackStackTop is setup for greedy quantifiers to point to the REPEAT opcode, where success and failure are decided.

} else {
pushBackTrackState(
gData,
REOP_REPEAT,
pc,
startcp,
state.continuationOp,
state.continuationPc);
}
}
int parenCount = getIndex(program, pc);
int parenIndex = getIndex(program, pc + INDEX_LEN);
for (int k = 0; k < parenCount; k++) {
Expand Down Expand Up @@ -3952,9 +3973,9 @@ class REProgState {

final REProgState previous; // previous state in stack

final int min; /* current quantifier min */
final int max; /* current quantifier max */
final int index; /* progress in text */
int min; /* current quantifier min */
int max; /* current quantifier max */
int index; /* progress in text */
final int continuationOp;
final int continuationPc;
final REBackTrackData backTrack; // used by ASSERT_ to recover state
Expand All @@ -3979,7 +4000,7 @@ class REBackTrackData {

final int op; /* operator */
final int pc; /* bytecode pointer */
final int cp; /* char buffer index */
int cp; /* char buffer index */
final int continuationOp; /* continuation op */
final int continuationPc; /* continuation pc */
final long[] parens; /* parenthesis captures */
Expand Down
Loading