Skip to content

Commit

Permalink
More idiomatic loop, without continues and unconditional break at bot…
Browse files Browse the repository at this point in the history
…tom.
  • Loading branch information
szegedi authored and gbrail committed Jul 4, 2024
1 parent a3297d0 commit 955af15
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions rhino/src/main/java/org/mozilla/javascript/Interpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1774,23 +1774,21 @@ private static Object interpretLoop(Context cx, CallFrame frame, Object throwabl
calleeScope =
ScriptableObject.getTopLevelScope(frame.scope);
}
// Iteratively peel known function types that can be reduced:
// arrows, lambdas, bound functions, call/apply, and
// no-such-method-handler in order to make a best-effort to keep
// them in this interpreter loop so continuations keep working.
// The loop initializer and condition are formulated so that
// they short-circuit the loop if the function is already an
// interpreted function, which should be the majority of cases.
// Iteratively reduce known function types: arrows, lambdas,
// bound functions, call/apply, and no-such-method-handler in
// order to make a best-effort to keep them in this interpreter
// loop so continuations keep working. The loop initializer and
// condition are formulated so that they short-circuit the loop
// if the function is already an interpreted function, which
// should be the majority of cases.
for (boolean notInt = !(fun instanceof InterpretedFunction);
notInt; ) {
if (fun instanceof ArrowFunction) {
ArrowFunction afun = (ArrowFunction) fun;
fun = afun.getTargetFunction();
funThisObj = afun.getCallThis(cx);
continue;
} else if (fun instanceof LambdaFunction) {
fun = ((LambdaFunction) fun).getTarget();
continue;
} else if (fun instanceof BoundFunction) {
BoundFunction bfun = (BoundFunction) fun;
fun = bfun.getTargetFunction();
Expand Down Expand Up @@ -1818,7 +1816,6 @@ private static Object interpretLoop(Context cx, CallFrame frame, Object throwabl
boundArgs, 0, stack, stackTop + 2, blen);
indexReg += blen;
}
continue;
} else if (fun instanceof IdFunctionObject) {
IdFunctionObject ifun = (IdFunctionObject) fun;
// Bug 405654 -- make the best effort to keep
Expand Down Expand Up @@ -1878,7 +1875,10 @@ private static Object interpretLoop(Context cx, CallFrame frame, Object throwabl
indexReg--;
}
}
continue;
} else {
// Some other IdFunctionObject we don't know how to
// reduce.
break;
}
} else if (fun instanceof NoSuchMethodShim) {
NoSuchMethodShim nsmfun = (NoSuchMethodShim) fun;
Expand All @@ -1895,15 +1895,13 @@ private static Object interpretLoop(Context cx, CallFrame frame, Object throwabl
stack[stackTop + 3] =
cx.newArray(calleeScope, elements);
indexReg = 2;
continue;
} else if (fun == null) {
throw ScriptRuntime.notFunctionError(null, null);
} else {
// Current function is something that we can't reduce
// further.
break;
}

// We reached here without any of the continue statements
// triggering. Current function is something that we can't
// peel back further.
break;
}

if (fun instanceof InterpretedFunction) {
Expand Down

0 comments on commit 955af15

Please sign in to comment.