Skip to content

Commit

Permalink
Do not use ImmutableList.copyOf whilst finding the innermost starlark…
Browse files Browse the repository at this point in the history
… function of a StarlarkThread.

RELNOTES: None.
PiperOrigin-RevId: 514374572
Change-Id: I719489ebb78fd8c9a230d09a962e1208dd01eda9
  • Loading branch information
zhengwei143 authored and copybara-github committed Mar 6, 2023
1 parent 8681437 commit f4c5a95
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/main/java/net/starlark/java/eval/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,9 @@ public static Module create() {
*/
@Nullable
public static Module ofInnermostEnclosingStarlarkFunction(StarlarkThread thread) {
for (Debug.Frame fr : thread.getDebugCallStack().reverse()) {
if (fr.getFunction() instanceof StarlarkFunction) {
return ((StarlarkFunction) fr.getFunction()).getModule();
}
StarlarkFunction fn = thread.getInnermostEnclosingStarlarkFunction();
if (fn != null) {
return fn.getModule();
}
return null;
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/net/starlark/java/eval/StarlarkThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,17 @@ ImmutableList<Debug.Frame> getDebugCallStack() {
return ImmutableList.copyOf(callstack);
}

@Nullable
StarlarkFunction getInnermostEnclosingStarlarkFunction() {
for (int i = callstack.size() - 1; i >= 0; i--) {
Debug.Frame fr = callstack.get(i);
if (fr.getFunction() instanceof StarlarkFunction) {
return (StarlarkFunction) fr.getFunction();
}
}
return null;
}

/** Returns the size of the callstack. This is needed for the debugger. */
int getCallStackSize() {
return callstack.size();
Expand Down

0 comments on commit f4c5a95

Please sign in to comment.