Skip to content

Evaluate an expression against the globals a program left (#680) - #681

Open
bertysentry wants to merge 4 commits into
mainfrom
claude/issues-677-680-fixes-qo7j56
Open

bertysentry wants to merge 4 commits into
mainfrom
claude/issues-677-680-fixes-qo7j56

Conversation

@bertysentry

@bertysentry bertysentry commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #680.

What

AVM.evalPersistingGlobals(expression, input) evaluates a compiled expression against the globals that the AVM retains, typically those a program left with executePersistingGlobals():

avm.executePersistingGlobals(prepare, textSource);        // /^Name: / { name = $2 }  /^T / { temp[$2] = $3 }
avm.evalPersistingGlobals(label, cpuRow);                 // name " " temp[$1]  ->  "srv 40"
avm.evalPersistingGlobals(doubled, diskRow);              // temp[$1] * 2       ->  62
  • The expression reads the retained globals by name, scalars and arrays, also through SYMTAB. A name it uses and that no run has set is uninitialized.
  • What the expression assigns stays (same rule as executePersistingGlobals()), also when the evaluation fails after the assignment. Every global that the expression read or assigned is retained afterwards, as a persisting execution retains every global of its program (a name that was only read is retained uninitialized).
  • Settings-level variables are assigned again before each evaluation to the variables of the same name that the expression uses, in place of their retained value, as they are before a persisting execution.
  • The special variables (NR, NF, FS, ...) start from their defaults, as with eval(). ARGV, ARGC and ENVIRON are not retained.
  • clearPersistentGlobals() ends it, as today. A plain eval() / execute() still resets everything.

There is also an overload with per-call overrides, evalPersistingGlobals(expression, input, Map), to mirror eval(expression, input, Map). It was not asked for in the issue: say so if you would rather not have it.

How

The retained globals are not copied. The expression runs on a small frame of its own layout:

  1. RuntimeStack.bindGlobalFrame(frame) keeps the retained globals aside and makes the frame the globals that the tuples address by offset. Only the slots of the user-defined globals of the expression are filled, from the retained values of the same name.
  2. After the evaluation (in a finally), unbindGlobalFrame() puts the retained globals back, and the slots of the expression are stored back by name (retainGlobalVariable, which appends a slot for a name that was used for the first time).

Which slots of an expression are bound is computed once per expression and per AVM (a WeakHashMap, identity keys). The cost of an evaluation therefore depends on the number of variables the expression uses, not on the number of retained globals: a rough timing loop on my side gave about 1.1 to 1.2 µs per evaluation with 5 retained globals as with 200, against about 1.0 µs for a plain eval() of a comparable field-only expression. Not a JMH measurement.

Also in this PR

After the evaluation, the offsets of the expression no longer address the retained globals, so the layout is uninstalled and getVariable() reads the retained globals by name. That made two existing problems visible, which are fixed here with the same reset:

  • getVariable(name) after clearPersistentGlobals() threw a NullPointerException.
  • getVariable(name) after restorePersistentMemory() read the slot of another variable, or threw an ArrayIndexOutOfBoundsException.

Two more changes follow from reading retained globals by name:

  • getVariable() and SYMTAB also give a retained global that the running script does not name, and SYMTAB keeps its type (an array cannot be replaced with a scalar). This changes executePersistingGlobals() too: a program used to see in SYMTAB only the globals it named itself.
  • getGlobalVariableNames() keeps answering the names of the last script after its layout is uninstalled.

Tests and docs

  • AwkEvalPersistingGlobalsTest (29 tests) and RuntimeStackGlobalFrameTest (8 tests). The tests that run a script and then expressions on its globals go through AwkTestSupport, which gains evalPersistingGlobals(expression, record); the others run no script and call the AVM methods directly.
  • java-advanced.md (new section), java-compile.md (table row), behavior-changes.md (Unreleased bullet).
  • mvn clean verify site: BUILD SUCCESS, 1056 unit tests pass, Checkstyle / PMD / SpotBugs at 0.

Notes

🤖 Generated with Claude Code

https://claude.ai/code/session_012oN69B3jk7sAZtL24ycAgx

AVM.evalPersistingGlobals(expression, input) evaluates a compiled
expression against the globals that the AVM retains, typically those
left by executePersistingGlobals(): scalars and arrays, read by name.
What the expression assigns stays.

The retained globals are not copied. The expression runs on a frame of
its own layout, where only the variables it uses are bound to the
retained values of the same name, and stored back afterwards: the cost
of an evaluation does not depend on how many globals are retained.

getVariable() now reads a retained global by name when the last script
has no slot for it, and no longer fails after clearPersistentGlobals()
or restorePersistentMemory(), whose globals the offsets of the last
script did not address any more.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012oN69B3jk7sAZtL24ycAgx
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-29T12:16:44.980447Z 978575f Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e50133b2b7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/main/java/io/jawk/backend/AVM.java Resolved
Comment thread src/main/java/io/jawk/backend/AVM.java Resolved
- Tests: the cases that run a script, then expressions on its globals,
  go through AwkTestSupport, which gains evalPersistingGlobals(expression,
  record). The other tests exercise the AVM methods around a persisting
  evaluation and run no script: they start from a restored snapshot.
- SYMTAB also holds the retained globals that the running script does
  not name, linked to their values, for a persisting evaluation as for a
  persisting execution.
- getGlobalVariableNames() keeps answering the names of the last script
  after its layout is uninstalled.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012oN69B3jk7sAZtL24ycAgx

Copy link
Copy Markdown
Contributor Author

@codex please review again


Generated by Claude Code

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3f6b565264

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/main/java/io/jawk/backend/AVM.java Resolved
- A global that an expression leaves uninitialized is retained too: it
  may have been assigned (x = unset), and what an expression assigns
  stays. Only a slot that was never read nor assigned adds nothing, as a
  persisting execution retains every global of its program.
- SYMTAB refuses to replace a retained array with a scalar, or a retained
  scalar with an array, also when the running script does not name that
  global: its type is then the type of its retained value.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012oN69B3jk7sAZtL24ycAgx

Copy link
Copy Markdown
Contributor Author

@codex please review again


Generated by Claude Code

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: babae61408

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

List<Integer> offsets = new ArrayList<>();
for (Map.Entry<String, Integer> entry : offsetsByName.entrySet()) {
frameLength = Math.max(frameLength, entry.getValue().intValue() + 1);
if (isPersistentEligibleGlobal(entry.getKey())) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Honor the expression's POSIX mode when retaining globals

When an expression was compiled in POSIX mode but the shared AwkSettings is switched back to non-POSIX before creating or using the AVM, this eligibility check treats ERRNO and ARGIND as managed specials even though the compiled expression addresses them as ordinary global slots. For example, a POSIX-compiled (ERRNO = 5) evaluates successfully, but ERRNO is omitted from this binding and its assigned value is discarded during unbinding. Determine eligibility from compiledExpression.isPosix() rather than the AVM's current mutable settings.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Traced, and left as it is in this PR. The observation is right: isManagedSpecialVariable() reads settings.isPosix(), the mode of the AVM, so an expression compiled with --posix and evaluated on a non-POSIX AVM does not get its ERRNO / ARGIND slots bound or stored back.

But this is not specific to evalPersistingGlobals(). Everything that decides whether these two names are special uses the mode of the AVM, not the mode of the compiled script: executePersistingGlobals() (collectPersistentGlobalValues() drops them the same way in that mixed setup), snapshotPersistentMemory(), getVariable(), assignVariable() and SYMTAB. Using compiledExpression.isPosix() in the binding alone would retain an ERRNO that the snapshot then drops and that getVariable("ERRNO") answers from the JRT instead, which is less consistent than today. Making the mode follow the compiled script everywhere is a change to existing behavior beyond #680.

Leaving this thread open for @bertysentry to decide whether that mixed-mode case deserves its own issue.


Generated by Claude Code

Keeps both Unreleased bullets of behavior-changes.md: #680 and #679.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012oN69B3jk7sAZtL24ycAgx

Copy link
Copy Markdown
Contributor Author

Merged main into the branch (978575f) after #682 and #683 landed. The only conflict was in behavior-changes.md, where both #679 and #680 add a bullet at the top of "Unreleased": both are kept. No other change.

@codex please review again


Generated by Claude Code

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 978575f585

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +3774 to +3778
if (array == null && isRetainedGlobal(name)) {
// the script does not name this retained global: its value tells its type
Object retained = runtimeStack.getGlobalVariable(name);
if (retained instanceof Map) {
array = Boolean.TRUE;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve types for unmaterialized retained globals

The fresh case not covered by the earlier SYMTAB fix is a declared global whose slot was never materialized: after a persisting program such as BEGIN { if (0) temp[1] = 1 }, temp is known statically to be an array but its retained value is null. A later expression such as (SYMTAB[$1] = $2) with $1 == "temp" reaches this branch, cannot infer the retained type from the value, and therefore replaces the array global with a scalar; the next expression or program that uses temp[...] then fails. Retain the prior layout's scalar/array metadata alongside the value rather than relying only on instanceof Map.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Traced, and not changed in this PR. The sequence is right: after BEGIN { if (0) temp[1] = 1 }, temp is retained with no value, so (SYMTAB[$1] = $2) with $1 == "temp" makes it a scalar, and a later script that uses temp[...] fails.

But the retained globals have never carried the static type of the script that declared them: a retained global is typed by its value only. The same already holds between two persisting executions on main: after that program, executePersistingGlobals() of BEGIN { temp = 5 } makes temp a scalar, and running the first program again then fails on temp[...]. An unassigned retained global is untyped, and the first script that assigns it gives it its type, which is also how gawk treats a variable that was never used. The SYMTAB path of this PR follows that rule.

Keeping the scalar/array declaration of every script next to the retained values (and deciding what happens when two scripts declare the same unassigned name differently) would be a change to the persistent memory itself, beyond #680.

Leaving this thread open for the maintainer to decide whether that is wanted.


Generated by Claude Code

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Evaluate an expression against the globals a program left

2 participants