Scoped REPL evaluator for retained PTC
保留状态 PTC 的 scoped REPL 求值器
`persistent-in-memory` runs every `run_code` call of one Agent in one global scope through node:repl ([best-effort persistent PTC](../../implemented/architecture/2026-09-23-best-effort-persistent-ptc.md)). When a later call declares a `const`, `let`, or `class` name again, V8 rejects the program before it runs with `SyntaxError: Identifier 'x' has already been declared`, and no statement executes. Model guidance can
English
Problem
persistent-in-memory runs every run_code call of one Agent in one global scope through node:repl (best-effort persistent PTC). When a later call declares a const, let, or class name again, V8 rejects the program before it runs with SyntaxError: Identifier 'x' has already been declared, and no statement executes. Model guidance can only ask for no redeclarations, and models rarely track every name across many calls.
Rewriting declarations as var removes the error, but every call then shares one binding, so a later declaration changes the value that earlier functions, closures, and background tasks read:
// call 1
var value = 1
var read = () => value
// call 2
var value = 2 // no error
// call 3
read() // 2: call 2 changed the read written by call 1
Five requirements must hold together:
- A later call may declare an earlier name again with any keyword without an error.
- A redeclaration applies to that call and later calls; earlier functions, closures, and still-running code keep their original values.
- Syntax matches the existing REPL: top-level
awaitworks, the final expression is the result, and top-levelreturnis not supported. - Errors in asynchronous callbacks are handled as in the existing REPL: the process keeps running and variables are retained.
- No scope analysis and no rewriting of declarations or references in model code; acorn only lists top-level declarations and finds the last statement.
Decision
The scoped REPL evaluator is split into a kernel and an evaluator:
| File | Contents |
|---|---|
packages/ptc-runtime/ptc-runtime-node/src/evaluators/scoped-repl.ts | Kernel: the ScopedReplScopes scope chain, which owns binding state, copies and fallback values, redeclarations, and Script compilation; plus the acorn scan, top-level declarations and keywords, top-level return check, final expression, reserved-name check, syntax-error layout, and source generation |
packages/ptc-runtime/ptc-runtime-node/src/evaluators/scoped-repl-evaluator.ts | Evaluator: exports createScopedReplEvaluator(options) implementing the existing ProgramEvaluator interface and connects the kernel to the existing modes: context and options, dynamic imports and the import handler, starting programs from a microtask and reading completions, the domain, and the injected reportRedeclarations |
packages/ptc-runtime/ptc-runtime-node/tests/scoped-repl.spec.ts | Kernel tests only; runs on its own and covers scanning, source generation, and scope layering across consecutive programs in a plain vm context |
packages/ptc-runtime/ptc-runtime-node/tests/scoped-repl-evaluator.spec.ts | Integration tests: completions, environment, modules and imports, errors, and asynchronous failures; every option tested on and off |
The scoped-repl format connects the evaluator to the existing execution modes; see Integration.
Exact behavior
Syntax and results
- A program is a list of statements and supports top-level
await. - When the last non-empty top-level statement is an expression statement, its value is the result. A Promise value is not awaited: the Promise itself is the result, so the host reports
program completion must be lossless JSON, as with the existing REPL. ThefinalExpressionoption controls this and defaults to on; when off, the final value is discarded, results come only from printing, and model guidance must change accordingly.run_codepassesdiscardCompletion: truefor TypeScript runtimes, so the host discards completion values and the model displays output withinspect(...); callers that keep completions, such as workflow-ptc, still take the final expression's value as the result. - When the last statement is a declaration,
if,for, block, or another statement, there is no result. - Top-level
return, including inside top-levelif, loops, and blocks, reportsSyntaxError: Illegal return statement, and no statement of that call runs. Throughrun_code, the host's TypeScript erasure rejects it first withReturn statement is not allowed here, as with the existing REPL. - A static
importorexportreports acorn'sSyntaxError: 'import' and 'export' may appear only with 'sourceType: module'; acorn also rejects top-levelusingandawait using, which remain available inside blocks and functions. - A program that starts with
#!reportsSyntaxError: Hashbang lines are not supportedand does not run: a hashbang is valid only at the very start of a Script, and the generated prefix precedes the program. - Output is the printed text, such as
inspect(...), plus the final expression's value when the caller keeps completions.
Across calls
- Each call starts by copying every top-level name left by earlier calls into a same-named declaration; objects are shared by reference, so changes inside an object are visible on both sides.
- A call may declare an earlier name again with any keyword, including a
var→let→const→class→functionsequence. A name the call declares withlet,const,class, orfunctionis not copied, and the call's declaration wins; a name it declares again only withvaris still copied, so thatvarstarts from the earlier value. - Assigning to an earlier name without declaring it changes this call's copy: later calls see the new value, and earlier functions keep the old one. This includes names declared with
const, which are read-only only in the call that declares them. - After a function is redefined, earlier callers still call the old function.
- Assignments that earlier code, such as timers and callbacks, makes to top-level variables after a call starts are not visible to later calls; keep shared mutable state in objects.
- Earlier functions cannot see names declared later; referencing them reports
ReferenceError. - When a call declares a name again with
let,const, orclass, every reference in that call resolves to the new declaration, including references written before it, and the old value is unreachable: reading the name before its declaration runs reports the nativeReferenceError: Cannot access 'x' before initialization. Such code is a program error and is not handled. Avarredeclaration starts from the earlier value until it assigns a new one, sovar count = count + 1adds to the earlier value; afunctionname holds the new function from the start of the call. - When a call fails partway: declarations completed before the throw are kept; an existing name redeclared but not yet initialized falls back to its earlier value in later calls; an uninitialized new name is dropped.
- A
vardeclared inside a block, includingtry/catch/finally,if/else, loop bodies,switch, and labeled statements, hoists to the top level of the call and counts as the call's declaration like a top-levelvar: it carries to later calls and may reuse a name that an earlier call declared with any keyword. Avarinside a nested function or class does not count. - A
varredeclaration that does not run, after a throw or in a branch that is not taken, keeps the earlier value, and so doesvar x;without an initializer.
Environment
These are the defaults; one option controls each of the first two items, see Context and options.
- Strict mode: assigning an undeclared name reports
ReferenceError; intentional globals useglobalThis.x. With the option off, strict mode is not forced: a program that starts with"use strict"runs strict, and other programs run sloppy. - No code generation from strings: the context's own
evalandnew FunctionreportEvalError, and compiling WebAssembly from bytes reportsCompileError. This is no isolation: main-realm functions copied into the context, such assetTimeout.constructor, still generate code in the main realm. - Modules load only through dynamic
import(), whose relative specifiers resolve from the working directory of the environment's first call and keep doing so after later calls change directory; there is norequire,module, or built-in module global such asfsandpath. Relative file access still follows the current directory. - Top-level
thisisundefined, orglobalThiswhen a program runs sloppy; top-levelargumentsis an empty arguments object with length 0. - Top-level
varandfunctiondeclarations do not becomeglobalThisproperties. - Node globals such as
setTimeout,Buffer,fetch,URL, andstructuredCloneare available.
Errors
- Runtime error lines and columns are native positions in the model's code, and functions defined in an earlier call show that call's filename, for example
<directory>/.ptc-repl-1.js:2:9. On the line of the final expression, columns after the insertion point shift right by 17, which is left as is. - Syntax errors that acorn finds are reported with acorn's message and position, laid out like V8's: filename and line, source line, and caret. Two messages use V8's wording: top-level
returnreportsIllegal return statement, and an error positioned at the end of the code reportsUnexpected end of input. - Errors that acorn accepts and V8 rejects during compilation report V8's native message; with the
strictoption on, strict-mode syntax violations such aswithare reported this way. - These are the evaluator's own reports. Through
run_code, the host's TypeScript erasure parses the whole program first and reports most syntax errors itself, as with the existing REPL; only code that the erasure accepts and the scan rejects, such as reserved names, staticimport, or top-levelusing, shows the layout above. - Runtime errors report the error name and stack, without an
Uncaughtprefix and without evaluator frames.
Asynchronous failures and lifecycle
- Errors thrown by timers, callbacks, Promise continuations, and EventEmitters that a program starts, and unhandled rejections, do not end the process, and variables are retained. Exception: when a callback throws a frozen or non-extensible object, Node's domain fails to add its properties before notifying the listener, and the process ends, as with node:repl; rejecting with such an object is unaffected.
- If an evaluation is active, it fails with
Uncaughtfollowed by the error text; otherwise the error is discarded. The error text is the error'sstack, orname: messagewhen the stack is not a string; a value that is not an error is formatted withutil.inspectafter removing thedomainanddomainThrownproperties the domain added; a failure while formatting reportsprogram threw an unrenderable value. An error belongs to the evaluation that created its callback, so a timer from an earlier call failing during a later call is not charged to the later call. - Output printed after an evaluation ends is discarded; timeout, cancellation, and reset keep the existing host behavior (retained PTC interruption).
Comparison with node:repl and vm-script
✓ means supported, ✗ means not supported, and "—" means not applicable: vm-script without persistence starts a new process for every call and has no cross-call state. "Persistent vm-script" is vm-script with scriptPersistence: true (#7992), and "both vm-script columns" means it and vm-script without persistence. The "ours" column is the default behavior, and its parenthesis names the columns it matches; the Environment table splits it into strict mode on, the default, and off. The Across calls table adds an implementation expected to support cross-call behavior, called the "expected implementation" in parentheses and notes.
Syntax and results
| Capability | node:repl (existing REPL evaluator) | Ours | vm-script (no persistence) | vm-script (persistent, #7992) | Notes |
|---|---|---|---|---|---|
Top-level return, including inside if blocks | ✗ | ✗ (matches node:repl, vm-script, persistent vm-script) | ✗ | ✗ | All report Illegal return statement |
| Final expression as the result | ✓ | ✓ (default; matches node:repl, vm-script, persistent vm-script) | ✓ | ✓ | Ours can turn it off with finalExpression |
| Awaiting a final Promise | ✗ | ✗ (matches node:repl) | ✓ | ✓ | Without awaiting, the host reports a non-JSON completion |
A result when ending with if, for, or a block | ✓ | ✗ (matches node:repl, vm-script, and persistent vm-script when the code contains await) | ✓ | ✓ | node:repl and both vm-script columns have a result only without await |
Top-level await | ✓ | ✓ (matches node:repl, vm-script, persistent vm-script) | ✓ | ✓ | node:repl and both vm-script columns rewrite the whole program; ours rewrites only the final expression |
Static import | ✗ | ✗ (matches node:repl, vm-script, persistent vm-script) | ✗ | ✗ | Ours reports acorn's message |
Top-level using | ✗ | ✗ (matches node:repl, vm-script, persistent vm-script) | ✗ | ✗ | Available inside blocks and functions |
A program that starts with #! | ✓ | ✗ (matches none of node:repl, vm-script, persistent vm-script) | ✓ | ✓ | Ours reports Hashbang lines are not supported |
Two function declarations of one name, or a var and a function of one name, in one call | ✓ | ✓ (matches node:repl, vm-script, persistent vm-script) | ✓ | ✓ | |
Dynamic import() | ✓ | ✓ (matches node:repl, vm-script, persistent vm-script) | ✓ | ✓ |
Across calls
| Capability | node:repl | An implementation expected to support cross-call behavior | Ours | vm-script | Persistent vm-script | Notes |
|---|---|---|---|---|---|---|
| Variables retained across calls | ✓ | ✓ | ✓ (matches node:repl, the expected implementation, persistent vm-script) | ✗ | ✓ | node:repl and persistent vm-script share one scope; ours copies; the expected implementation copies as well |
| Redeclaration with any keyword | ✗ | ✓ | ✓ (matches the expected implementation) | — | ✗ | In node:repl and persistent vm-script, 21 of the 25 keyword pairs report already declared, and only var and function may replace each other; ours reserves a redeclaration notice that prints nothing; the expected implementation prints an extra warning when a const is declared again as const |
| Earlier functions unaffected by a later assignment without declaration | ✗ | ✓ | ✓ (matches the expected implementation) | — | ✗ | |
Assigning to an earlier const without declaring it again | ✗ | ✓ | ✓ (matches the expected implementation) | — | ✗ | node:repl and persistent vm-script report TypeError; ours prints nothing; the expected implementation prints a warning first |
A var redeclaration reads the earlier value of its name (var n = n + 1) | ✓ | ✗ | ✓ (matches node:repl, persistent vm-script) | — | ✓ | node:repl and persistent vm-script only when the earlier declaration is also var, and report already declared after let; ours also after let or const; the expected implementation yields NaN |
| Earlier callers keep calling the old function after a redefinition | ✗ | ✓ | ✓ (matches the expected implementation) | — | ✗ | |
| Later calls see values earlier code assigns afterwards | ✓ | ✗ | ✗ (matches the expected implementation) | — | ✓ | A timer-incremented variable read twice 30 ms apart: node:repl 23, 50; persistent vm-script 22, 44; ours 0, 0; the expected implementation 15, 15 |
| Earlier functions see names declared later | ✓ | ✗ | ✗ (matches the expected implementation) | — | ✓ | Ours reports ReferenceError |
| Changes inside an object visible on both sides | ✓ | ✓ | ✓ (matches node:repl, the expected implementation, persistent vm-script) | — | ✓ | |
| A name stays usable after its initializer throws | ✗ | ✓ | ✓ (matches the expected implementation) | — | ✗ | In node:repl and persistent vm-script a new name stays unusable, and declaring it again reports already declared; ours falls back to the earlier value for existing names and drops new names |
| Declarations completed before a throw are kept | ✓ | ✓ | ✓ (matches node:repl, the expected implementation, persistent vm-script) | — | ✓ | |
A var redeclaration not reached before a throw keeps the old value | ✓ | ✓ | ✓ (matches node:repl, the expected implementation, persistent vm-script) | — | ✓ | Ours copies the earlier value into the call's var, which also keeps it through an untaken branch; the expected implementation inserts marker code to achieve this |
A new var declared in a block is carried to later calls | ✓ | ✗ | ✓ (matches node:repl, persistent vm-script) | — | ✓ | Ours collects block-level var declarations |
A block-level var with the same name as an earlier let is accepted | ✗ | ✗ | ✓ (matches none of node:repl, the expected implementation, persistent vm-script) | — | ✗ | node:repl and persistent vm-script report already declared; ours treats it as the call's new declaration; the expected implementation reports already declared as well |
Environment
| Capability | node:repl | Ours (strict, default) | Ours (non-strict) | vm-script | Persistent vm-script | Notes |
|---|---|---|---|---|---|---|
| Strict mode | ✗ | ✓ (matches none of node:repl, vm-script, persistent vm-script) | ✗ (matches node:repl, vm-script, persistent vm-script) | ✗ | ✗ | With the option off, a program's own leading "use strict" still applies |
| Assigning an undeclared name creates a global | ✓ | ✗ (matches none of node:repl, vm-script, persistent vm-script) | ✓ (matches node:repl, vm-script, persistent vm-script) | ✓ | ✓ | Without strict mode the global is shared by every call and earlier function and is not copied |
Top-level var and function become globalThis properties | ✓ | ✗ (matches none of node:repl, vm-script, persistent vm-script) | ✗ (matches none of node:repl, vm-script, persistent vm-script) | ✓ | ✓ | |
Top-level this is globalThis | ✓ | ✗ (matches none of node:repl, vm-script, persistent vm-script) | ✓ (matches node:repl, vm-script, persistent vm-script) | ✓ | ✓ | Ours is undefined in strict mode |
Top-level arguments | ✗ | ✓ (matches none of node:repl, vm-script, persistent vm-script) | ✓ (matches none of node:repl, vm-script, persistent vm-script) | ✗ | ✗ | Ours is an empty arguments object |
eval and new Function | ✓ | ✗ (default; matches none of node:repl, vm-script, persistent vm-script) | ✗ (default; matches none of node:repl, vm-script, persistent vm-script) | ✓ | ✓ | Can be turned on; limits only the context's own functions, while copied main-realm functions still generate code |
| WebAssembly compilation | ✓ | ✗ (default; matches none of node:repl, vm-script, persistent vm-script) | ✗ (default; matches none of node:repl, vm-script, persistent vm-script) | ✓ | ✓ | Same option as eval |
Node globals (setTimeout, Buffer, fetch, URL, structuredClone) | ✓ | ✓ (matches node:repl, vm-script, persistent vm-script) | ✓ (matches node:repl, vm-script, persistent vm-script) | ✓ | ✓ | |
require, module, and built-in module globals such as fs | ✓ | ✗ (matches vm-script, persistent vm-script) | ✗ (matches vm-script, persistent vm-script) | ✗ | ✗ | Only import() |
Relative import() follows later directory changes | ✓ | ✗ (matches none of node:repl, vm-script, persistent vm-script) | ✗ (matches none of node:repl, vm-script, persistent vm-script) | ✓ | ✓ | Ours keeps the directory of the environment's first call |
_ and _error | ✓ | ✗ (matches vm-script, persistent vm-script) | ✗ (matches vm-script, persistent vm-script) | ✗ | ✗ | |
Code starting with { a: 1 } is an object literal | ✗ | ✗ (matches node:repl, vm-script, persistent vm-script) | ✗ (matches node:repl, vm-script, persistent vm-script) | ✗ | ✗ | node:repl treats it as an object; the existing REPL evaluator forces a block |
Errors
| Capability | node:repl | Ours | vm-script | Persistent vm-script | Notes |
|---|---|---|---|---|---|
Accurate error positions (code without await) | ✓ | ✓ (matches node:repl, vm-script, persistent vm-script) | ✓ | ✓ | Ours excepts the final expression's line |
Accurate first-line positions in code with await | ✗ | ✓ (matches none of node:repl, vm-script, persistent vm-script) | ✗ | ✗ | node:repl and both vm-script columns report column 22 as column 54 |
| Syntax errors show the source line and caret | ✓ | ✓ (matches node:repl, vm-script, persistent vm-script) | ✓ | ✓ | Ours lays out acorn's position; this is evaluator behavior, and through run_code the host's TypeScript erasure reports most syntax errors first, matching the node:repl column |
| Syntax error messages are V8's wording | ✓ | ✗ (matches none of node:repl, vm-script, persistent vm-script) | ✓ | ✓ | Ours uses acorn's message, and V8's wording for errors acorn accepts and V8 rejects |
| Incomplete code gets a clear message | ✗ | ✓ (matches vm-script, persistent vm-script) | ✓ | ✓ | node:repl reports an empty message; through run_code the host's erasure reports it first as well |
| Errors carry the error name and stack | ✓ | ✓ (matches node:repl, vm-script, persistent vm-script) | ✓ | ✓ | |
Uncaught prefix on synchronous and awaited failures | ✓ | ✗ (matches vm-script, persistent vm-script) | ✗ | ✗ | |
Uncaught prefix on asynchronous callback failures | ✓ | ✓ (matches node:repl) | — | — | Both vm-script columns end the process instead of reporting |
Asynchronous failures and lifecycle
| Capability | node:repl | Ours | vm-script | Persistent vm-script | Notes |
|---|---|---|---|---|---|
| A callback failure after an evaluation keeps the process and variables | ✓ | ✓ (matches node:repl) | — | ✗ | Persistent vm-script exits, the next call reports worker-exit, and every variable is lost; without persistence the process has already closed; a callback throwing a frozen object ends the process in node:repl and ours |
| A callback failure during an evaluation fails only that evaluation and keeps variables | ✓ | ✓ (matches node:repl) | ✗ | ✗ | Both vm-script columns exit with worker-exit, and persistent vm-script loses its variables |
| Output printed after an evaluation ends is discarded | ✓ | ✓ (matches node:repl, persistent vm-script) | — | ✓ | |
| Variables can survive a timeout or cancellation | ✓ | ✓ (matches node:repl, persistent vm-script) | — | ✓ | Requires the program to acknowledge the stop |
Sources: the node:repl, vm-script, and persistent vm-script columns come from running master's evaluators directly (Node v24.18.0). Persistent vm-script reuses one evaluator; its single-call rows equal vm-script's, and its cross-call rows and the two asynchronous rows were measured separately, the asynchronous ones through a real child process. scoped-repl.spec.ts tests the cross-call items of the "ours" column and scoped-repl-evaluator.spec.ts the rest. The two asynchronous rows of vm-script without persistence are inferred from the current implementation; node:repl, persistent vm-script, and ours share the host's timeout, cancellation, and reset behavior. The expected-implementation column was measured on that implementation with a fresh environment per case. |
Context and options
One evaluator owns one context. With the codeGeneration option off, the default, it is created as:
const runtimeContext = vm.createContext(Object.create(null), { codeGeneration: { strings: false, wasm: false } })
- Node globals are copied by the same rule as
createScriptContextinvm-script: every own property of the mainglobalThisthat the new context lacks, withglobalpointing at the new context. The evaluator file contains its own copy of these lines and leaves vm-script.ts unchanged. - The child still writes
tools, binding error classes,console, andprocesstoevaluator.globals, which isruntimeContext, before each evaluation. - The sandbox object has no prototype, as in the vm-script and AsyncFunction evaluators, so global lookups do not reach the main realm's
Functionthrough a prototype chain, asconstructor.constructor("…")()would. Main-realm values copied into the context, such assetTimeout,console,process, and tool namespaces, still have the main realm'sFunctionas theirconstructor, and the main realm has nocodeGenerationlimits, so the option restricts only the context's owneval,Function, and WebAssembly and is no isolation; the process sandbox provides isolation. - Only dynamic
import()loads modules, with no option: each Script getsimportModuleDynamically. There is norequire,module, or built-in module global; host bindings under those names stay available unchanged. Staticimportis unsupported. - The generated filename is
<directory>/.ptc-repl-<number>.js; no file exists at that path, and the name starts with a dot as a hidden file. The directory is the working directory when the evaluator is created, which is the directory of the environment's first call, and never changes. Node's default loader uses the filename as theimport()referrer, so relative imports always resolve from that directory, and the directory cannot be left out. Following later directory changes would need an explicit operation, such asjs_switch_cwd. - Values from bindings and imports belong to the main realm, so cross-realm checks such as
instanceof Arraycan be false, as with the REPL andvm-script.
The three options are evaluator creation parameters, not Config fields:
interface ScopedReplEvaluatorOptions extends ProgramEvaluatorOptions {
/** Force strict mode for every program; when off, a program's own leading "use strict" decides. Defaults to true. */
strict?: boolean
/** Allow eval, new Function and WebAssembly compilation inside the program context; defaults to false. */
codeGeneration?: boolean
/** Use the last top-level expression statement as the completion value; defaults to true. */
finalExpression?: boolean
}
| Option | On | Off | Notes |
|---|---|---|---|
strict (default on) | The whole Script starts with "use strict";, so every call runs strict | Not forced: when a program starts with a "use strict" directive, the inner function body starts with "use strict";, so the copy declarations cannot void it; other programs run sloppy | acorn scans with script rules in both states, and V8 reports strict-mode syntax violations at compilation. When off, in calls that run sloppy, assigning an undeclared name creates a global that every call shares without copies, top-level this is globalThis, and a name declared with a strict-mode reserved word such as package makes later strict calls fail to compile; see Risks |
codeGeneration (default off) | The context is created without the codeGeneration limits | The context is created as above | When on, a var declared inside eval is not in the scanned list and is not carried; when off, it still restricts only the context's own code generation and is no isolation |
finalExpression (default on) | The final expression becomes return { value: (…) } | No rewrite; the final value is discarded and the result is empty | When off, output comes only from printing; model guidance and consumers of the completion value, such as workflow-ptc, must change; top-level return fails in both states |
The integration uses the default of every option; the other values are intended uses kept by design, and each has tests.
Wrapper
Each call compiles to its own vm.Script. For example, after earlier calls left a (const), b (let), and v (var), the model writes:
let c = a + b
c * 2
The compiled source is:
"use strict";(function (__$binding_vals, __$binding_getters) { return async function () { let a = __$binding_vals[0]; let b = __$binding_vals[1]; var v = __$binding_vals[2]; __$binding_getters.push(() => a, () => b, () => v, () => c);
let c = a + b
return { value: (c * 2) }
} })
| Part | Contents | Reason |
|---|---|---|
"use strict"; | With the strict option on, the whole Script is strict; when off, it is omitted, and a program that starts with "use strict" gets one at the start of the inner function body instead | On forces strict mode; off keeps the copy declarations from voiding the model's directive |
Outer function (__$binding_vals, __$binding_getters) | Receives the copied values and an empty array and returns the inner function; the evaluator calls it synchronously | The internal values reach the program through a closure and stay out of its arguments; the outer function returns before the program runs, so its frame never appears in program stacks |
Inner async function () | The model's program is its body | Supports top-level await; top-level declarations are function locals, so calls never conflict; it is called without a receiver and without arguments, so this is undefined (globalThis when the program runs sloppy) and arguments is an empty arguments object |
| Copy declarations | Generated only for names that earlier calls left and this call does not declare or declares again only with var; values come from __$binding_vals by index | Index access is unaffected by names such as __proto__ |
__$binding_getters.push(...) | Before the model's code, pushes one read function per name, copied or declared, in the order the evaluator generated | The evaluator maps the i-th read function to the i-th name, so the prefix needs no name strings; a read function can be created early and read later, and it throws while its declaration is uninitialized |
| Final expression | Inserts return before the statement, { value: ( before the expression, and ) } after it | The same handling of the last statement as Node's processTopLevelAwait; the box keeps the async function from awaiting a returned Promise |
| Prefix on line 0 | Compiled with lineOffset: -1 | Line N of the model's code reports as line N |
| Closing on its own line | } }) | The model's last line can end with a // comment |
A copy declaration exists when the call does not declare the name or declares it again only with var; when the call declares it with let, const, class, or function, there is no copy and the call's declaration wins. Copy keywords:
| Case | Generated copy |
|---|---|
Not declared by this call; earlier let, const, function, or class | let |
Not declared by this call; earlier var | var |
Declared again by this call only with var | var |
A copy only carries the earlier value in, and writing it as const would only forbid later calls from assigning to it, so an earlier const is copied as let too: later calls can assign to it, changing only the copy, while it stays const in the call that declares it. The kernel does not check whether a call assigns the name, so it needs no syntax-tree walk and prints no warning.
A call that declares a name again only with var gets a copy too: repeated var declarations in one function are one variable, so that var starts from the earlier value and a declaration that does not run never replaces it with undefined, as a global var behaves in node:repl. A function of the same name in the same call initializes at the start of the function and takes precedence over the var, so it gets no copy.
Compilation and execution:
// Kernel: ScopedReplScopes.compile
const factory = new Script(source, { filename, lineOffset: -1, ...importOption }).runInContext(context) as ScopedReplFactory
const getters: (() => unknown)[] = []
const program = factory(values, getters)
this.compiled = { bindings, getters, replaced }
return program
// Evaluator: evaluate
const filename = resolve(baseDirectory, `.ptc-repl-${++submissions}.js`) // baseDirectory: process.cwd() at creation
const program = scopes.compile(code, { filename, context, finalExpression, ...importOption })
const start = Reflect.apply.bind(undefined, program, undefined, []) as ScopedReplProgram
void programDomain.run(() => Promise.resolve().then(start)).then(
(completion) => { finish({ ok: true, value: completion?.value }) },
(error: unknown) => { finish({ ok: false, error }) },
)
- The kernel always compiles with
filenameandlineOffset: -1, and the evaluator always passesimportModuleDynamically:VmModuleImportLoader.forFile(filename)with an import handler, otherwisevm.constants.USE_MAIN_CONTEXT_DEFAULT_LOADER. - A microtask calls the program directly, so synchronous throws carry no evaluator frames. The host receives the result with
.thenrather thanawait, which would append a host async frame. - Measured:
thisisundefined,arguments.lengthis 0, and a synchronous throw's stack holds only the program's frame; the domain catches timer errors from programs started this way; a boxed Promise is returned without being awaited. - Any occurrence of
__$binding_in the code, including strings and comments, reportsSyntaxError: Names starting with '__$binding_' are reservedat the first occurrence, and nothing runs.
Kernel: scope chain, acorn scan, and compilation
The kernel depends only on acorn and node:vm, not on the child process or the evaluator, so its tests compile and run consecutive programs in a plain vm context to check scope layering on their own. Its interface:
class ScopedReplScopes {
constructor(options: { strict: boolean; onRedeclaration?: (redeclarations: readonly ScopedReplRedeclaration[]) => void })
/** Scan and compile one program against the current bindings; throws SyntaxError and leaves the chain unchanged. */
compile(code: string, options: { filename: string; context: Context; finalExpression: boolean; importModuleDynamically?: ScriptOptions['importModuleDynamically'] }): ScopedReplProgram
}
type ScopedReplProgram = () => Promise<{ value: unknown } | undefined>
interface ScopedReplRedeclaration { name: string; previous: ScopedReplBindingKind; current: ScopedReplBindingKind }
type ScopedReplBindingKind = 'var' | 'let' | 'const' | 'function' | 'class'
interface ScopedReplBinding { name: string; kind: ScopedReplBindingKind }
interface ScopedReplScan {
/** Top-level bindings declared by the program, in source order. */
declarations: ReadonlyMap<string, ScopedReplBindingKind>
/** Whether the program starts with a "use strict" directive. */
useStrict: boolean
/** Last top-level statement when it is an expression statement. */
finalExpression?: { statementStart: number; expressionStart: number; expressionEnd: number }
}
/** Parse once; throws a SyntaxError laid out like V8's for every error acorn reports. */
function scanProgram(code: string, options: { filename: string }): ScopedReplScan
/** Build the Script source and every binding in the order of the read functions it pushes. */
function buildProgramSource(
code: string, scan: ScopedReplScan, carried: readonly ScopedReplBinding[], options: { strict: boolean; finalExpression: boolean },
): { source: string; bindings: ScopedReplBinding[] }
Parsing: regardless of the strict option, the kernel calls parse(code, { ecmaVersion: 'latest', sourceType: 'script', allowAwaitOutsideFunction: true }). These rules mostly match an async function body: top-level await is valid, top-level return is a syntax error, and two function declarations of one name, or a var and a function of one name, are valid. They differ in rejecting top-level using, await using, and static import and export. The scan does not decide strictness: with the option on, the Script's leading "use strict"; does, and V8 reports strict-mode syntax violations at compilation.
Before parsing, the kernel checks two things: a program that starts with #!, or any occurrence of __$binding_, reports an error, and the code is not parsed.
One parse yields four things:
-
The call's own top-level declarations, from top-level statements only:
Top-level statement Collected names Recorded keyword const,let, andvardeclarations, including destructuring (objects, arrays, defaults, rest elements)Every bound name The original keyword functiondeclarations, including async and generator functionsThe function name function; avarof the same name in the same call is recorded asfunctiontooclassdeclarationsThe class name classvarin the head offor (var …; …; …)andfor (var … in/of …)Every bound name varvarinside blocks,try/catch/finally,if/else, loop bodies,switch, labeled statements, andwith, includingforheads there; nested functions and classes are skippedEvery bound name var -
Whether a top-level
returnexists: acorn reports'return' outside of functionwith its position. -
The last statement: skipping empty statements from the end, the first statement found, if it is an expression statement, records the statement's and expression's offsets.
-
Whether the leading directives include
"use strict": acorn marks directive statements withdirective, and the kernel recordsuseStrict.
Out of scope: no search inside nested functions and classes; inside blocks only var is collected, while let, const, class, and function stay in their blocks; no reference analysis and no assignment search; no rewriting besides the three insertions around the final expression.
Syntax errors: when acorn fails, the kernel throws a SyntaxError directly without a V8 compilation. Its stack follows V8's layout:
/work/.ptc-repl-3.js:1
const x = ;
^
SyntaxError: Unexpected token
- The message is acorn's text without its trailing
(line:column). - Two messages use V8's wording:
'return' outside of functionreportsIllegal return statement, and an error positioned at the end of the code reportsUnexpected end of input. - An occurrence of
__$binding_reports an error with the same layout at its first position.
Scope chain state and the steps of a call
ScopedReplScopes holds:
bindings: Map<string, { kind: ScopedReplBindingKind; read(): unknown }>: the names available for copying and their read functions.kindis the keyword of the name's most recent declaration; a name that is only copied keeps its recorded keyword, so afunctioncopied asletstaysfunction.replaced: ReadonlyMap<string, { kind: ScopedReplBindingKind; value: unknown }>: the values that names redeclared by the most recently adopted program had when it was compiled.compiled: the binding order, read-function array, andreplacedvalues of the most recently compiled program.
Each compile(code, options):
- Adopts
compiled: when the read-function array holds at least one function per name, that program's prefix has run, sobindingsmaps the i-th name to the i-th read function andreplacedtakes its values. The child rejects overlapping evaluations (overlapping PTC evaluation), so the previous prefix always runs in a microtask before the next compilation; only two compilations in one synchronous run leave the first program unadopted. - Scans the program for its declarations and final expression; a syntax error is thrown and the state stays unchanged.
- Calls every read function in
bindingsfor its current value. When a read throws because its declaration never finished, the value fromreplacedis used; without either, the name is dropped. - Copies names the call does not declare or declares again only with
var, and collects their values invalues; for existing names the call declares, records the value in the newreplacedand a redeclaration{ name, previous, current }. - Generates and compiles the source. With the
strictoption off anduseStricttrue, the inner function body starts with"use strict";. A V8 compile failure throws the nativeSyntaxErrorwithout the compiler frames after the error heading (the same handling asvm-script, in a separate copy) and leaves the state unchanged. - Calls
onRedeclaration(redeclarations)when there are redeclarations. - Calls the outer function for the program, records
compiled, and returns the program.
Each evaluator evaluate(code, finish):
- Increments the submission number, including for submissions with syntax errors; the filename is
<directory at creation>/.ptc-repl-<number>.js. - Calls
scopes.compile; a thrown error ends the evaluation. - Starts the program from a microtask inside the domain and calls
finishwhen its Promise settles, takingcompletion?.valuewithout further awaiting.
Reads happen in kernel step 3, when a new program is compiled. Later assignments by earlier code change only that code's own variables.
Failed calls
| Call | Code | Result |
|---|---|---|
| 1 | const data = 1 | data is 1 |
| 2 | const data = await tools.fetch(), where fetch throws | Fails; call 2 declared data but never initialized it |
| 3 | data | 1: reading call 2's data in kernel step 3 throws, so the value recorded when call 2 was compiled is used |
| 4 | let fresh = await tools.fetch(), which throws | Fails; fresh is a new name |
| 5 | typeof fresh | "undefined": there is no earlier value, so the name is dropped |
- Declarations completed before a throw are kept. For example, after
let y = 5runs and the call then throws,yis still 5 in later calls. - A
varredeclaration starts from the copied earlier value, so reading it never throws and needs no fallback: an unreachedvarkeeps the earlier value. For example, when call 2 throws beforevar v = 6, or the tool call invar config = await tools.loadConfig({})fails, later calls still read the earlier value. Afunctionname holds the new function from the start of the call.
Asynchronous failures: aligned with the REPL
The REPL catches errors from the asynchronous work a program starts with Node's domain. The evaluator does the same:
- On creation, the evaluator calls
domain.create()forprogramDomainand starts every program insideprogramDomain.run(...). Timers, callbacks, Promise continuations, and EventEmitters that the program creates belong to that domain. programDomain.on('error', ...)passesUncaughtfollowed by the error text tooptions.reportError. The text is the error'sstack, orname: messagewhen the stack is not a string; errors are not formatted withutil.inspectbecause the domain addsdomainanddomainThrown, which inspect would print. A value that is not an error is formatted withutil.inspectafter deleting those two properties, as node:repl does.- Programs can make this formatting throw through a
stackaccessor,toString, or custom inspection. A throw from the listener would end the process, so the formatting runs intry/catchand a failure reportsprogram threw an unrenderable value, the same fallback text asprepareExceptionfor synchronous failures. ThereportErrorcall stays outside thetrybecause it is the child's own code, whose failures must surface. - When a callback throws a frozen or non-extensible object, Node's domain fails to add its properties before calling the listener, and the process ends, as with node:repl.
- The child handles
reportErrorunchanged: an active evaluation fails with the error; otherwise the error is discarded. Errors belong to the evaluation that created their callback (AsyncLocalStorage). - No process-wide
uncaughtExceptionorunhandledRejectionhandler is registered, so errors in the child's own code still surface. - When a program calls a tool, the asynchronous callbacks that the child creates for the call, such as RPC callbacks, also belong to the program's domain: an asynchronous error in the child's own code inside those callbacks is reported as the program's
Uncaught …failure, and the child keeps running. The existing REPL evaluator behaves the same.
Verified cases:
- REPL: timer throws and unhandled Promise rejections are caught both after and during an evaluation; an EventEmitter
errorevent is caught after an evaluation. - A standalone domain: in the main realm it catches timer throws, rejections created in timers, failures of unawaited async functions, and EventEmitter
errorevents; in a context with the defaultcodeGenerationlimits it catches timer throws and rejections created in timers; timer throws from programs started by a microtask are caught too.
Dynamic import
- A program's
import()goes through its Script'simportModuleDynamically:vm.constants.USE_MAIN_CONTEXT_DEFAULT_LOADERwithout an import handler, and the existingVmModuleImportLoader.forFile(filename)with one, as invm-script. - Relative specifiers resolve from the directory where the evaluator was created, which is the working directory of the environment's first call; later directory changes do not move it. node:repl and
vm-scriptfollow each call's directory; after the Agent changes directory withworking_directory, import with an absolute path to use the new directory. - There is no
require: node:repl'srequireresolves inconsistently (relative paths follow the current directory but are cached after their first resolution; packages resolve from the startup directory), so dynamicimport()is the only way to load modules.
Redeclaration notice (reserved)
The kernel calls onRedeclaration in step 6; the evaluator injects its reportRedeclarations(redeclarations: readonly ScopedReplRedeclaration[]), whose body stays empty with one TODO comment and prints nothing. A future notice writes to the program's console here, which makes it model-visible output.
Out of scope
- Memory reclamation: not handled; every call copies every name. Earlier calls without surviving closures are collected; when a call leaves a surviving function, that function keeps every value copied into that call.
- Resolving
import()from the working directory as it changes: an explicit operation such asjs_switch_cwdcan be added when needed. - Reading the earlier value of a name in its own
let,const, orclassredeclaration, such aslet count = count + 1: a program error. - Problems from mixing strict and sloppy calls with the
strictoption off, listed under Risks: left to a future reset tool. - The column shift on the final expression's line.
Integration
The scoped-repl format connects the evaluator with only the changes the connection requires, without refactoring or reusing existing code, so rebases and merges with other ongoing PTC work stay simple. The program format is scoped-repl and the Config field is replScoped.
| Location | Change |
|---|---|
PtcProgramFormat in ptc-runtime | Includes 'scoped-repl' |
config.ts in ptc-runtime-node | The boolean field replScoped, accepted only with persistent-in-memory and rejected at load otherwise; when omitted, resolveReplScoped takes the value that resolvePersistentExecution writes for function bodies with persistent-in-memory, currently true, and false in other modes |
execution.ts in ptc-runtime-node | In the persistent-in-memory branch with replScoped, format scoped-repl, retainsState true, REPL-statement TypeScript erasure, and new model guidance |
evaluators/index.ts in ptc-runtime-node | The evaluator table maps scoped-repl to this evaluator with the default of all three options |
protocol.ts in ptc-runtime-node | The protocol version is 3, so older bootstraps fail at readiness; the upgrade guide docs/upgrade-guide/v0.2.2-rc.1/ptc-bootstrap-protocol/ also covers the new PtcProgramFormat member |
host.ts in workflow-ptc | scoped-repl uses the same wrapper as repl, await (async () => {…})() |
configuration.ts in ptc-runtime-node (used by CUA to merge settings) | resolvePersistentExecution returns executionMode, scriptPersistence, and replScoped to merge together: script keeps retained VM Scripts with replScoped: false, scoped-repl yields replScoped: true, and function bodies and REPL statements yield persistent-in-memory with the constant SCOPED_REPL_BY_DEFAULT, currently true, replacing a stale profile value. That constant also sets the omitted default in config.ts, so changing it changes one line; config.ts imports this function from configuration.ts, which is itself a published package entry, and configuration.ts imports only the Config type, so the build emits no shared chunk |
| Documentation, snapshots, and tests | Package README configuration, behavior, and known limitations; the config catalog; the snapshot scenario ptc-scoped-repl; a built-artifact e2e case with replScoped: true |
tool-registry-ptc is unchanged: for every format other than function-body it uses its printing guidance. The model guidance describes the behavior above as rules for writing programs rather than as a list of edge cases: output uses inspect(...), in a sentence shared with the other modes; top-level return is unsupported; each call runs one program in its own top-level scope, and its top-level declarations and the names it received are copied, not referenced, into later programs, so objects stay shared; in one program, either use a copied name or declare it again, never both; assigning to a copied name, including an earlier const, changes only this program's copy; do not declare var inside blocks, because a block-level var changes which value later programs copy; earlier functions and still-running code keep the values they captured, so shared mutable state belongs in objects; when a program fails before a declaration of an existing name finishes, later programs keep the earlier value; modules load only with import(), without require or module, and relative import specifiers resolve from the directory of the environment's first program, while process.cwd() and relative file paths follow each program's directory. The shared retained-state sentences say to await tools and child processes before the program ends, because the formats that retain state have no top-level return. Tests pin the model guidance verbatim.
Alternatives considered
Nesting each call in the previous call's scope with direct eval. Calls can share live bindings, but earlier scopes are never reclaimed, positions require a source map or stack rewriting, and direct eval is unavailable in a context that forbids code generation from strings.
Rewriting declarations as var or global assignments. Every call shares one binding, which violates requirement 2, and model code must be rewritten.
Compiling each call as an ES module. Each module receives values through a synthetic module for the previous call and an appended export. Modules allow neither return nor a final value, and the prelude shifts every reported line; function parameters already pass values without modules.
Scope analysis with rewritten references. Allows finer memory reclamation, but needs scope analysis and code rewriting, which violates requirement 5.
Printing only, without a final expression. workflow-ptc and the repl format contract depend on completion values, and it departs from the existing REPL syntax.
Results from top-level return. Conflicts with requirement 3, requires the function-body format, and changes the run_code guidance.
Awaiting a final Promise. Matches vm-script but not the existing REPL; boxing as Node does keeps the REPL behavior.
Results for programs ending with if, for, or a block. Only Script top-level code has V8 completion values, which a function body lacks; supporting them requires rewriting more statements. node:repl has no result there either when the code contains await.
Writing the wrapper as async function (__$binding_vals, __$binding_getters). this is also undefined, but the program's arguments exposes both internal values and has length 3.
An async arrow wrapper. this is globalThis and arguments does not exist, as in the REPL; ours uses undefined, as a strict function call without a receiver does.
Returning read functions through a callback. The prefix would need name strings and protection against repeated calls; pushing into an array that the evaluator maps by index is simpler.
Recompiling with V8 for syntax errors. Yields V8's wording at the cost of another compilation; acorn already provides the position and message.
Module rules for strict programs. Module rules are stricter than an async function body: two function declarations of one name, a var and a function of one name, and await as a variable name in a nested ordinary function are rejected, and HTML-like comments are handled differently from V8's Script compilation. They are unusable with strict mode off, because module code is always strict.
Scanning inside the same function wrapper as the runtime. Matches the runtime grammar exactly and accepts top-level using, as Node's processTopLevelAwait does. It requires a statement walk to find top-level return, position mapping for the wrapper, and a guard against an extra } in the code closing the wrapper early; top-level using is not needed, so script rules are simpler.
Copying only assigned const names as let, with a warning. This walks the new call's syntax tree and tracks shadowing, which is scope analysis and violates requirement 5. Ours copies every const as let without a notice.
Starting a var redeclaration from undefined without a copy. var count = count + 1 then yields NaN, but a var not reached before a throw or in a branch that is not taken replaces the earlier value with undefined; for example, a failed tool call in var config = await tools.loadConfig({}) leaves later calls without the earlier config. With the copy, var count = count + 1 adds to the earlier value, as in node:repl, which is accepted for now.
Falling back only when a call failed and the value reads undefined. It needs no copy, but cannot tell an unreached declaration from one that ran and assigned undefined: a call that assigns undefined and then fails falls back by mistake, and an untaken branch or var x; without an initializer in a successful call is not covered.
Marking in the code whether each var ran. It distinguishes the cases exactly but rewrites model code at declarations, earlier assignments, and loop heads, which violates requirement 5.
Skipping copies of strict-mode reserved words in strict calls. It would keep names such as package, declared in a sloppy call, from breaking later strict calls; the problem comes from the model mixing both modes, so it is left to a future reset tool.
A process-wide handler for uncaught errors. It would also swallow errors from the child's own code; the REPL's domain covers only the asynchronous work programs start.
Not collecting var declarations inside blocks. A block-level var then collides with the copy declarations, so the whole program reports already declared, and the name is not carried to later calls; collecting it takes about 15 lines of statement traversal without reference analysis, so the scanner collects it.
Showing only .ptc-repl-<number>.js in stacks, without the directory. Without the directory, Node's default loader resolves relative import() specifiers from the current directory at import time, unlike calls with an import handler; a //# sourceURL comment that shows only the name makes V8 ignore lineOffset, so every line number grows by 1; handing imports to a callback would work, because both child launches pass --experimental-vm-modules and the callback can start import() from the first call's directory, but it requires scoped REPL imports to always go through a callback, which the default path does not do yet. The filename therefore keeps the directory of the first call for now.
Providing require, module, and built-in module globals, as node:repl does. node:repl's require resolves inconsistently, and following directory changes needs a rule for when the base moves; only import() remains.
Consequences
Gains:
- Later calls may declare an earlier name again with any keyword, while earlier functions, closures, and still-running code keep their values.
- Syntax, results, errors, and asynchronous failure handling match the existing REPL, and asynchronous program failures do not end the process.
- There is no scope analysis, and model code is rewritten only at the three insertion points of the final expression.
Costs:
- Copying at call start differs from the REPL: assignments that earlier code, such as timers and callbacks, makes to top-level variables afterwards are invisible to later calls; the model guidance states this.
- An assignment without declaration affects only later calls and is invisible to earlier functions; in the REPL both sides change.
- Strict mode differs from the REPL: assigning an undeclared name creates a global in the REPL and throws here.
- For callers that keep completions, a final Promise without
awaitmakes the host report a non-JSON completion, as in the existing REPL;run_codediscards completions and is unaffected. - Programs ending with
if,for, or a block have no result, while node:repl has one when the code contains noawait. - On the final expression's line, columns after the insertion point shift right by 17.
- Syntax error messages use acorn's text, which differs from V8's, for example
Unexpected tokeninstead ofUnexpected token ';'. - Scanning always uses script rules, so top-level
usingandawait usingare rejected; node:repl does not support them either. - Top-level
argumentsis an empty arguments object, while it does not exist in node:repl. - The context forbids code generation from strings by default: code relying on
evalornew Functionfails, including libraries loaded into this context through an import handler. The limit is no isolation, because copied main-realm functions such assetTimeout.constructorstill generate code. - There is no
require: code that relies on it must useawait import()instead. - Relative
import()keeps resolving from the directory of the environment's first call, also after the Agent changes directory. - Node documents
domainas deprecated; node:repl still builds on it, so a removal would change the REPL as well. - An error in the child's own code inside a tool call's asynchronous callbacks is reported as the program's
Uncaught …failure, and the child keeps running, as with the existing REPL. - Programs that start with
#!are rejected. - Any occurrence of
__$binding_, including in strings and comments, is rejected; internal names written with escapes such as\u005fare not checked. - acorn must keep pace with V8's syntax, or newer syntax is rejected before it runs.
- A
varredeclaration starts from the earlier value:var count = count + 1adds to it, as in node:repl, also when the earlier declaration wasletorconst, where node:repl reportsalready declared. - A
let,const, orclassredeclaration cannot read the earlier value of its name and throws, as inlet count = count + 1. This is a program error and is not handled. - Every earlier
constis copied asletinto later calls, which can assign to it without any notice. - With the
strictoption off, strict and sloppy calls can mix: a sloppy call that assigns an undeclared name creates a global shared by every call and earlier function, and a name declared in a sloppy call with a strict-mode reserved word (package,public,private,protected,interface,implements,static,let,yield,eval,arguments) makes later strict calls fail to compile. This is not handled and is left to a future reset tool. - When a callback throws a frozen or non-extensible object, Node's domain handling fails and the process ends, losing retained variables, as with node:repl.
- Every call generates a copy declaration and a read function for every earlier name: the prototype measured about 10 ms per call with 1000 names.
- When a call leaves a surviving function, every value copied into that call stays in memory.
Testing
- The kernel tests pass on their own and cover script-rule parsing and detection of a leading
"use strict", every statement in the declaration table, top-levelreturnincluding inside blocks, locating and boxing the final expression, programs that start with#!, occurrences of__$binding_, the syntax-error layout with both message substitutions, and the generated source; with thestrictoption off, a leading"use strict"takes effect both with and without copies; running consecutive programs in a plain vm context, they cover every item under Across calls, including keyword combinations, failed calls, timers, function redefinition, and redeclaration notices. - The evaluator tests use the evaluator and a real vm context and cover every item under Syntax and results, Environment, Errors, and Asynchronous failures and lifecycle, plus cross-call behavior through the evaluator, including that the global object's prototype chain does not reach the main realm's
Functionwhile copied main-realm functions such assetTimeout.constructorstill generate code, and that host bindings namedmoduleorrequirestay untouched. - Every option is tested on and off.
- Top-level
thisisundefined,arguments.lengthis 0, and a synchronous throw's stack holds only the program's frames. - Asynchronous failures: each listed case is tested after and during an evaluation; the text passed to
reportErrorstarts withUncaught; the process keeps running and variables are retained. A missingstack, a throwingstackaccessor, and throwing custom inspection reportname: messageor the fixed text without ending the process; a thrown plain object is reported withoutdomainThrown. - Imports: both the default loader and an import handler; relative specifiers resolve from the directory at creation, also after the working directory changes.
- The built-artifact e2e covers
replScoped: true. - Tests that mount
persistent-in-memorysetreplScopedexplicitly, and retained-execution tests run both values; one test checks the default when it is omitted. - New files have 100% coverage; existing evaluators and their tests are unchanged.
中文
问题
persistent-in-memory 用 node:repl 在同一个全局作用域里执行同一个 Agent 的每次 run_code(尽力保留 PTC 状态)。模型在后面的调用里再次声明前面用过的 const、let、class 名字时,V8 在执行前就报 SyntaxError: Identifier 'x' has already been declared,整段代码一行都不执行。模型说明只能要求"不要重复声明",但模型在多轮调用里很难记住每个名字。
把声明改成 var 能消除报错,但所有调用共用同一个绑定,后面的声明会改掉前面的函数、闭包和后台任务读到的值:
// call 1
var value = 1
var read = () => value
// call 2
var value = 2 // no error
// call 3
read() // 2: call 2 changed the read written by call 1
需要同时满足五条要求:
- 后面的调用可以用任意关键字再次声明前面的名字,不报错。
- 再次声明只对这次和之后的调用生效;前面的函数、闭包和仍在运行的代码继续使用它们原来的值。
- 写法与现有 REPL 一致:支持顶层
await,最后一个表达式的值是结果,不支持顶层return。 - 异步回调里的错误与现有 REPL 一样处理:进程不退出,变量保留。
- 不做作用域分析,不改写模型代码里的声明和引用;acorn 只用来列出顶层声明、找到最后一条语句。
决策
scoped REPL 求值器分成内核和求值器两个文件:
| 文件 | 内容 |
|---|---|
packages/ptc-runtime/ptc-runtime-node/src/evaluators/scoped-repl.ts | 内核:作用域链 ScopedReplScopes,负责状态表、拷贝与退回旧值、重复声明、编译成 Script;以及 acorn 扫描、顶层声明与关键字、顶层 return 检查、最后一条表达式、保留名检查、语法错误格式化、拼源码 |
packages/ptc-runtime/ptc-runtime-node/src/evaluators/scoped-repl-evaluator.ts | 求值器:导出 createScopedReplEvaluator(options),实现现有的 ProgramEvaluator 接口,把内核接入现有模式:执行环境和开关、动态 import 与 import 处理器、由微任务启动程序并取完成值、domain、注入 reportRedeclarations |
packages/ptc-runtime/ptc-runtime-node/tests/scoped-repl.spec.ts | 只测内核,可以单独运行:扫描、拼源码,以及在普通 vm context 里连续执行多段程序的作用域分层 |
packages/ptc-runtime/ptc-runtime-node/tests/scoped-repl-evaluator.spec.ts | 测接入层:完成值、执行环境、模块与 import、报错、异步错误;三个开关打开、关闭两种状态都测 |
求值器以 scoped-repl 格式接入现有执行模式,见"接入"。
精确行为
写法与结果
- 程序是一组语句,支持顶层
await。 - 最后一条非空的顶层语句是表达式语句时,它的值就是结果。值是 Promise 时不等待,Promise 本身就是结果,宿主会报
program completion must be lossless JSON,和现有 REPL 一样。这一项由finalExpression开关控制,默认开;关闭后末尾表达式的值被丢弃,结果只能靠打印,模型说明要随之调整。run_code对 TypeScript 运行时传discardCompletion: true,宿主丢弃完成值,模型用inspect(...)输出;不丢弃完成值的调用方(例如 workflow-ptc)仍以末尾表达式的值为结果。 - 最后一条是声明、
if、for、块等其他语句时,没有结果。 - 顶层
return,包括写在顶层if、循环和块里的,报SyntaxError: Illegal return statement,这次一行都不执行。经run_code执行时,宿主的 TypeScript 擦除会先拒绝它,报Return statement is not allowed here,和现有 REPL 相同。 - 静态
import、export报 acorn 的SyntaxError: 'import' and 'export' may appear only with 'sourceType: module';顶层using、await using同样被 acorn 拒绝,块里和函数里可以用。 - 以
#!开头的程序报SyntaxError: Hashbang lines are not supported,这次不执行:#!只能出现在 Script 的最开头,而生成的前缀排在程序前面。 - 输出是
inspect(...)等打印的内容;调用方不丢弃完成值时,再加上末尾表达式的值。
跨次调用
- 每次调用开始时,把前面留下的每个顶层名字拷贝成本次的同名声明;对象按引用共享,修改对象内部两边都看得到。
- 可以用任意关键字再次声明前面的名字,包括
var→let→const→class→function依次换。本次用let、const、class、function声明的名字不再拷贝,以本次为准;只用var重新声明的名字照样拷贝,这次的var从旧值开始。 - 不声明、直接给前面的名字赋值,改的是本次的拷贝:之后的调用看到新值,前面定义的函数仍看到旧值。前面用
const声明的名字也一样,它只在声明它的那次调用里不能赋值。 - 重定义函数后,前面定义的调用方仍然调用旧函数。
- 本次开始以后,前面的代码(定时器、回调)再给顶层变量赋值,之后的调用看不到;需要共享的可变状态放进对象。
- 前面定义的函数看不到后面才声明的名字,引用时报
ReferenceError。 - 一次调用用
let、const、class重新声明了某个名字,这次调用里的所有引用都指向新声明,包括写在声明之前的,拿不到旧值:在声明执行前读取报原生的ReferenceError: Cannot access 'x' before initialization。这类写法是代码本身的问题,不做处理。用var重新声明时,这次的var从旧值开始,直到被赋上新值,例如var count = count + 1在旧值上加 1;function从调用开始就是新函数。 - 一次调用中途出错时:抛错前已经执行完的声明保留;重新声明了已有名字、但没来得及赋值的,之后的调用退回旧值;没来得及赋值的新名字直接丢掉。
- 块里(包括
try/catch/finally、if/else、循环体、switch、标签语句)声明的var会提升到这次调用的顶层,和顶层var一样算这次的声明:带到之后的调用,也可以和前面任何关键字声明的名字同名。嵌套的函数和类里的var不算。 - 没执行到的
var再次声明,不论在抛错之后还是在没走到的分支里,都保留旧值;不带初始值的var x;也保留旧值。
执行环境
下面是默认行为,前两项各由一个开关控制,见"执行环境与开关"。
- strict 模式:给未声明的名字赋值报
ReferenceError;有意的全局变量写globalThis.x。开关关闭时不强制:程序以"use strict"开头,这次就按严格执行,否则按宽松执行。 - 禁止从字符串生成代码:执行环境自己的
eval、new Function报EvalError,从字节编译 WebAssembly 报CompileError。这不构成隔离:从主环境复制进来的函数(例如setTimeout.constructor)仍能在主环境里生成代码。 - 只能用动态
import()加载模块,相对路径按这个环境第一次调用时的 working directory 解析,之后的调用换了目录也不变;不提供require、module和fs、path这类内置模块全局。读写文件的相对路径仍跟随当前目录。 - 顶层
this是undefined,按宽松执行时是globalThis;顶层arguments是一个空的 arguments 对象,长度为 0。 - 顶层
var和function不会成为globalThis的属性。 setTimeout、Buffer、fetch、URL、structuredClone等 Node 全局对象可用。
报错
- 运行时报错的行号、列号是模型代码里的原生位置,跨次调用定义的函数显示它自己那次的文件名,例如
<目录>/.ptc-repl-1.js:2:9。最后一条表达式所在的那一行,插入点之后的列号右移 17,这次不处理。 - acorn 发现的语法错误直接报 acorn 的消息和位置,按 V8 的样式排版:文件名加行号、源码行、箭头。有两处消息改用 V8 的说法:顶层
return报Illegal return statement;报错位置在代码末尾时报Unexpected end of input。 - acorn 放行、V8 编译时才发现的错误,报 V8 原生的消息;strict 开关打开时,严格模式的语法违规(例如
with)也在这一步报出。 - 以上是求值器自己的行为。经
run_code执行时,宿主的 TypeScript 擦除先解析整段代码,多数语法错误由它报告,和现有 REPL 相同;只有擦除接受、扫描拒绝的代码(例如保留名、静态import、顶层using)才会看到上面的版式。 - 运行时错误报错误名加堆栈,没有
Uncaught前缀,堆栈里没有求值器自己的帧。
异步错误与生命周期
- 程序启动的定时器、回调、Promise 后续步骤和 EventEmitter 抛出的错误,以及未处理的 rejection,不让进程退出,变量保留。例外:回调里
throw的是冻结或不可扩展的对象时,Node 的 domain 在通知监听器之前给它加属性失败,进程结束,和 node:repl 相同;以这样的对象 reject 不受影响。 - 当时有执行在运行,就让那次执行以
Uncaught加错误文本失败;否则丢弃。错误文本取错误的stack,stack不是字符串时用"名字: 消息";不是 Error 的值,先去掉 domain 加上的domain、domainThrown再用util.inspect;转换出错时报program threw an unrenderable value。错误按创建回调时所属的执行归属,前一次留下的定时器在后一次运行时抛错,不会算到后一次头上。 - 执行结束后才打印的输出被丢弃;超时、取消和重置沿用现有的宿主逻辑(保留状态的 PTC 中断)。
与 node:repl、vm-script 的对比
✓ 表示支持,✗ 表示不支持,"—" 表示不适用:不开持久化的 vm-script 每次调用都是新进程,没有跨次状态。"持久化 vm-script"指设置 scriptPersistence: true 的 vm-script(#7992),"两种 vm-script"指它和不开持久化的 vm-script。"我们"一列是默认行为,括号里写和哪几列一致;执行环境一组把"我们"分成 strict 开启(默认)和关闭两列。跨次调用一组多列一项"预期支持跨次调用能力的一种实现",括号和备注里简称"预期实现"。
写法与结果
| 能力 | node:repl(现有 REPL 求值器) | 我们 | vm-script(不开持久化) | vm-script(开持久化,#7992) | 备注 |
|---|---|---|---|---|---|
顶层 return,包括写在 if 块里的 | ✗ | ✗(与 node:repl、vm-script、持久化 vm-script 一致) | ✗ | ✗ | 都报 Illegal return statement |
| 末尾表达式作为结果 | ✓ | ✓(默认;与 node:repl、vm-script、持久化 vm-script 一致) | ✓ | ✓ | 可以用 finalExpression 开关关闭 |
| 末尾是 Promise 时等它完成 | ✗ | ✗(与 node:repl 一致) | ✓ | ✓ | 不等时宿主报"不是 JSON" |
以 if、for、块结尾时有结果 | ✓ | ✗(与 node:repl、vm-script、持久化 vm-script 含 await 时一致) | ✓ | ✓ | node:repl 和两种 vm-script 只在代码不含 await 时有结果 |
顶层 await | ✓ | ✓(与 node:repl、vm-script、持久化 vm-script 一致) | ✓ | ✓ | node:repl 和两种 vm-script 靠改写整段代码;我们只改写最后一条表达式 |
静态 import | ✗ | ✗(与 node:repl、vm-script、持久化 vm-script 一致) | ✗ | ✗ | 我们报 acorn 的消息 |
顶层 using | ✗ | ✗(与 node:repl、vm-script、持久化 vm-script 一致) | ✗ | ✗ | 块里和函数里可以用 |
以 #! 开头的程序 | ✓ | ✗(与 node:repl、vm-script、持久化 vm-script 都不一致) | ✓ | ✓ | 我们报 Hashbang lines are not supported |
同一次里两个同名 function,或同名的 var 和 function | ✓ | ✓(与 node:repl、vm-script、持久化 vm-script 一致) | ✓ | ✓ | |
动态 import() | ✓ | ✓(与 node:repl、vm-script、持久化 vm-script 一致) | ✓ | ✓ |
跨次调用
| 能力 | node:repl | 预期支持跨次调用能力的一种实现 | 我们 | vm-script | 持久化 vm-script | 备注 |
|---|---|---|---|---|---|---|
| 跨次保留变量 | ✓ | ✓ | ✓(与 node:repl、预期实现、持久化 vm-script 一致) | ✗ | ✓ | node:repl 和持久化 vm-script 共用一个作用域;我们靠拷贝;预期实现也靠拷贝 |
| 用任意关键字再次声明 | ✗ | ✓ | ✓(与预期实现一致) | — | ✗ | node:repl 和持久化 vm-script 的 25 种关键字组合里 21 种报 already declared,只有 var、function 之间可以;我们预留重复声明提示,暂不输出;预期实现对 const 再次声明为 const 多输出一行警告 |
| 后面不声明直接赋值,前面定义的函数不受影响 | ✗ | ✓ | ✓(与预期实现一致) | — | ✗ | |
不重新声明,直接给前面的 const 赋值 | ✗ | ✓ | ✓(与预期实现一致) | — | ✗ | node:repl 和持久化 vm-script 报 TypeError;我们不提示;预期实现先打印警告 |
用 var 重新声明时读到同名旧值(var n = n + 1) | ✓ | ✗ | ✓(与 node:repl、持久化 vm-script 一致) | — | ✓ | node:repl 和持久化 vm-script 只在前面也是 var 时成立,前面是 let 时报 already declared;我们前面是 let、const 时也成立;预期实现得到 NaN |
| 重定义函数后,旧的调用方仍调旧函数 | ✗ | ✓ | ✓(与预期实现一致) | — | ✗ | |
| 前面的代码之后改的值,后面的调用能看到 | ✓ | ✗ | ✗(与预期实现一致) | — | ✓ | 定时器自增的变量,间隔 30 ms 读两次:node:repl 23、50;持久化 vm-script 22、44;我们 0、0;预期实现 15、15 |
| 前面定义的函数能看到后面才声明的名字 | ✓ | ✗ | ✗(与预期实现一致) | — | ✓ | 我们报 ReferenceError |
| 修改对象内部,两边都看得到 | ✓ | ✓ | ✓(与 node:repl、预期实现、持久化 vm-script 一致) | — | ✓ | |
| 初始化抛错后,这个名字还能继续用 | ✗ | ✓ | ✓(与预期实现一致) | — | ✗ | node:repl 和持久化 vm-script 里新名字永久不可用,再次声明报 already declared;我们已有名字退回旧值,新名字丢掉 |
| 抛错前已经执行完的声明会保留 | ✓ | ✓ | ✓(与 node:repl、预期实现、持久化 vm-script 一致) | — | ✓ | |
抛错前没执行到的 var 再次声明,保留旧值 | ✓ | ✓ | ✓(与 node:repl、预期实现、持久化 vm-script 一致) | — | ✓ | 我们把旧值拷进这次的 var,没走到的分支里的 var 也保留旧值;预期实现靠插入标记代码做到 |
块里新声明的 var 带到下一次 | ✓ | ✗ | ✓(与 node:repl、持久化 vm-script 一致) | — | ✓ | 我们收集块里的 var |
块里的 var 和已有的 let 同名时不报错 | ✗ | ✗ | ✓(与 node:repl、预期实现、持久化 vm-script 都不一致) | — | ✗ | node:repl 和持久化 vm-script 报 already declared;我们把它算作这次的新声明;预期实现也报 already declared |
执行环境
| 能力 | node:repl | 我们(strict,默认) | 我们(非 strict) | vm-script | 持久化 vm-script | 备注 |
|---|---|---|---|---|---|---|
| strict 模式 | ✗ | ✓(与 node:repl、vm-script、持久化 vm-script 都不一致) | ✗(与 node:repl、vm-script、持久化 vm-script 一致) | ✗ | ✗ | strict 关闭时,程序自己开头的 "use strict" 仍然生效 |
| 给未声明的名字赋值会创建全局变量 | ✓ | ✗(与 node:repl、vm-script、持久化 vm-script 都不一致) | ✓(与 node:repl、vm-script、持久化 vm-script 一致) | ✓ | ✓ | 非 strict 时创建的全局变量由所有调用和前面的函数共用,不走拷贝 |
顶层 var、function 挂到 globalThis | ✓ | ✗(与 node:repl、vm-script、持久化 vm-script 都不一致) | ✗(与 node:repl、vm-script、持久化 vm-script 都不一致) | ✓ | ✓ | |
顶层 this 是 globalThis | ✓ | ✗(与 node:repl、vm-script、持久化 vm-script 都不一致) | ✓(与 node:repl、vm-script、持久化 vm-script 一致) | ✓ | ✓ | 我们 strict 时是 undefined |
顶层 arguments | ✗ | ✓(与 node:repl、vm-script、持久化 vm-script 都不一致) | ✓(与 node:repl、vm-script、持久化 vm-script 都不一致) | ✗ | ✗ | 我们的是空的 arguments 对象 |
eval、new Function | ✓ | ✗(默认;与 node:repl、vm-script、持久化 vm-script 都不一致) | ✗(默认;与 node:repl、vm-script、持久化 vm-script 都不一致) | ✓ | ✓ | 可打开;只限执行环境自己的,复制进来的主环境函数仍能生成代码 |
| 编译 WebAssembly | ✓ | ✗(默认;与 node:repl、vm-script、持久化 vm-script 都不一致) | ✗(默认;与 node:repl、vm-script、持久化 vm-script 都不一致) | ✓ | ✓ | 与 eval 同一个开关 |
Node 全局对象(setTimeout、Buffer、fetch、URL、structuredClone) | ✓ | ✓(与 node:repl、vm-script、持久化 vm-script 一致) | ✓(与 node:repl、vm-script、持久化 vm-script 一致) | ✓ | ✓ | |
require、module、fs 等内置模块全局 | ✓ | ✗(与 vm-script、持久化 vm-script 一致) | ✗(与 vm-script、持久化 vm-script 一致) | ✗ | ✗ | 只能用 import() |
相对路径的 import() 跟随之后的目录变化 | ✓ | ✗(与 node:repl、vm-script、持久化 vm-script 都不一致) | ✗(与 node:repl、vm-script、持久化 vm-script 都不一致) | ✓ | ✓ | 我们固定用这个环境第一次调用时的目录 |
_、_error | ✓ | ✗(与 vm-script、持久化 vm-script 一致) | ✗(与 vm-script、持久化 vm-script 一致) | ✗ | ✗ | |
以 { a: 1 } 开头的代码当对象字面量 | ✗ | ✗(与 node:repl、vm-script、持久化 vm-script 一致) | ✗(与 node:repl、vm-script、持久化 vm-script 一致) | ✗ | ✗ | node:repl 原本当对象,现有 REPL 求值器强制改成语句块 |
报错
| 能力 | node:repl | 我们 | vm-script | 持久化 vm-script | 备注 |
|---|---|---|---|---|---|
报错行列号准确(代码不含 await) | ✓ | ✓(与 node:repl、vm-script、持久化 vm-script 一致) | ✓ | ✓ | 我们的例外是最后一条表达式所在行 |
报错行列号准确(代码含 await 时的第 1 行) | ✗ | ✓(与 node:repl、vm-script、持久化 vm-script 都不一致) | ✗ | ✗ | node:repl 和两种 vm-script 把第 22 列报成第 54 列 |
| 语法错误带源码行和箭头 | ✓ | ✓(与 node:repl、vm-script、持久化 vm-script 一致) | ✓ | ✓ | 我们按 acorn 给出的位置自己排版;这是求值器层的行为,经 run_code 时多数语法错误先由宿主的 TypeScript 擦除报告,与 node:repl 列相同 |
| 语法错误消息是 V8 的原文 | ✓ | ✗(与 node:repl、vm-script、持久化 vm-script 都不一致) | ✓ | ✓ | 我们用 acorn 的消息,acorn 放行、V8 拒绝的错误用 V8 原文 |
| 代码不完整时有明确提示 | ✗ | ✓(与 vm-script、持久化 vm-script 一致) | ✓ | ✓ | node:repl 的报错消息为空;经 run_code 时同样先由宿主擦除报告 |
| 报错带错误名和堆栈 | ✓ | ✓(与 node:repl、vm-script、持久化 vm-script 一致) | ✓ | ✓ | |
同步错误和 await 失败带 Uncaught 前缀 | ✓ | ✗(与 vm-script、持久化 vm-script 一致) | ✗ | ✗ | |
异步回调错误带 Uncaught 前缀 | ✓ | ✓(与 node:repl 一致) | — | — | 两种 vm-script 的进程直接退出,没有这类报告 |
异步错误与生命周期
| 能力 | node:repl | 我们 | vm-script | 持久化 vm-script | 备注 |
|---|---|---|---|---|---|
| 执行结束后回调里出错,进程不崩、变量保留 | ✓ | ✓(与 node:repl 一致) | — | ✗ | 持久化 vm-script 的进程退出,下一次调用报 worker-exit,变量全部丢失;不开持久化时执行结束后进程已关闭;回调里 throw 冻结的对象时,node:repl 和我们的进程都会结束 |
| 执行中回调里出错,只让这次失败、变量保留 | ✓ | ✓(与 node:repl 一致) | ✗ | ✗ | 两种 vm-script 的进程都退出,报 worker-exit,持久化 vm-script 的变量丢失 |
| 执行结束后才打印的输出被丢弃 | ✓ | ✓(与 node:repl、持久化 vm-script 一致) | — | ✓ | |
| 超时或取消后变量可以保留 | ✓ | ✓(与 node:repl、持久化 vm-script 一致) | — | ✓ | 前提是程序确认已经停住 |
数据来源:node:repl、vm-script 和持久化 vm-script 三列直接调用 master 的求值器实测(Node v24.18.0)。持久化 vm-script 用同一个求值器连续执行;一次调用内的各行与 vm-script 相同,跨次调用各行和两行异步错误单独实测,异步错误经真实子进程。"我们"一列的跨次调用各项由 scoped-repl.spec.ts 测试,其余各项由 scoped-repl-evaluator.spec.ts 测试。不开持久化的 vm-script 两行异步错误按现有实现推断;node:repl、持久化 vm-script 和我们的超时、取消、重置沿用同一套宿主逻辑。预期实现一列在该实现上实测,每个用例新开一个环境。 |
执行环境与开关
一个求值器只有一个执行环境。codeGeneration 开关关闭(默认)时这样创建:
const runtimeContext = vm.createContext(Object.create(null), { codeGeneration: { strings: false, wasm: false } })
- Node 全局对象按
vm-script的createScriptContext同样的规则复制进来:主环境globalThis上、新环境没有的自有属性逐个复制,global指向新环境自身。这几行在求值器文件里单独写一份,不改 vm-script.ts。 tools、绑定错误类、console、process仍由子进程在每次执行前写到evaluator.globals,也就是runtimeContext。- 沙箱对象没有原型,和 vm-script、AsyncFunction 两个求值器一样,全局查找因此不会经原型链走到主环境的
Function,例如constructor.constructor("…")()。但复制进来的主环境值(setTimeout、console、process、工具命名空间等)的constructor仍是主环境的Function,主环境没有codeGeneration限制,所以这个开关只限制执行环境自己的eval、Function和 WebAssembly,不构成隔离;隔离靠进程级沙箱。 - 只提供动态
import(),没有开关:每个 Script 都配importModuleDynamically。不提供require、module和内置模块全局;宿主以这些名字注入的绑定原样可用。静态import不支持。 - 生成的文件名是
<目录>/.ptc-repl-<序号>.js,这个路径上没有真实文件,文件名用点开头的隐藏名。目录是求值器创建时的 working directory,也就是这个环境第一次调用时的目录,之后不再改变;默认加载器把文件名当作import()的引用方,所以相对路径的 import 固定按这个目录解析,目录部分不能省。以后需要跟随目录变化时,另加显式操作(例如js_switch_cwd)。 - 绑定和 import 返回的值属于主环境,
instanceof Array这类跨环境判断可能为 false,和 REPL、vm-script相同。
三个开关是求值器的创建参数,不是 Config 字段:
interface ScopedReplEvaluatorOptions extends ProgramEvaluatorOptions {
/** Force strict mode for every program; when off, a program's own leading "use strict" decides. Defaults to true. */
strict?: boolean
/** Allow eval, new Function and WebAssembly compilation inside the program context; defaults to false. */
codeGeneration?: boolean
/** Use the last top-level expression statement as the completion value; defaults to true. */
finalExpression?: boolean
}
| 开关 | 打开时 | 关闭时 | 需要注意 |
|---|---|---|---|
strict(默认开) | 整个 Script 以 "use strict"; 开头,每次调用都按严格执行 | 不强制:程序以 "use strict" 指令开头时,在内层函数体最前面补一个 "use strict";,拷贝语句不会让它失效;否则按宽松执行 | 两种状态下 acorn 都按 script 规则扫描,严格模式的语法违规由 V8 编译时报出。关闭后,按宽松执行的调用里:给未声明的名字赋值会创建所有调用共用的全局变量,不走拷贝;顶层 this 是 globalThis;用严格模式保留字(如 package)声明的名字会让之后严格的调用编译失败,见"风险" |
codeGeneration(默认关) | 创建执行环境时不加 codeGeneration 限制 | 按上面的代码创建 | 打开后,eval 里新声明的 var 不在扫描出的名单里,不会带到下一次;关闭时也只限制执行环境自己的代码生成,不构成隔离 |
finalExpression(默认开) | 最后一条表达式改写成 return { value: (…) } | 不改写,末尾表达式的值被丢弃,结果为空 | 关闭后输出只能靠打印,模型说明和使用完成值的地方(例如 workflow-ptc)都要相应调整;顶层 return 在两种状态下都报错 |
接入时三个开关都取默认值;非默认值是设计上预留的用法,各自都有测试。
包装
每次调用编译成一个独立的 vm.Script。例如前面留下了 a(const)、b(let)、v(var),这次模型写了:
let c = a + b
c * 2
实际编译的源码是:
"use strict";(function (__$binding_vals, __$binding_getters) { return async function () { let a = __$binding_vals[0]; let b = __$binding_vals[1]; var v = __$binding_vals[2]; __$binding_getters.push(() => a, () => b, () => v, () => c);
let c = a + b
return { value: (c * 2) }
} })
| 部分 | 内容 | 原因 |
|---|---|---|
"use strict"; | strict 开关打开时,整个 Script 是 strict 模式;关闭时省略,程序以 "use strict" 开头时改为在内层函数体最前面补一个 | 打开时强制严格;关闭时保证前缀里的拷贝语句不会让模型写的指令失效 |
外层 function (__$binding_vals, __$binding_getters) | 接收拷贝进来的值和一个空数组,返回内层函数;由求值器同步调用 | 内部值通过闭包交给程序,不出现在程序的 arguments 里;外层在程序运行前就已返回,它的帧不会出现在程序的堆栈里 |
内层 async function () | 模型程序就是它的函数体 | 支持顶层 await;顶层声明是函数的局部变量,每次调用互不冲突;不带接收者、零参数调用,所以 this 是 undefined(按宽松执行时是 globalThis),arguments 是一个空的 arguments 对象 |
| 拷贝声明 | 只给"前面有、本次没声明或只用 var 重新声明"的名字生成,值按下标从 __$binding_vals 取 | 下标访问不受 __proto__ 这类名字影响 |
__$binding_getters.push(...) | 在模型代码之前,按求值器生成前缀时的名字顺序,给每个名字(拷贝来的和本次声明的)推入一个读取函数 | 求值器知道第 i 个读取函数对应第 i 个名字,前缀里不用写名字字符串;读取函数可以先建好、以后再读,声明还没初始化完时读取会抛错 |
| 最后一条表达式 | 语句前插入 return ,表达式前插入 { value: (,表达式后插入 ) } | 和 Node 的 processTopLevelAwait 处理最后一条语句的方式相同;外包一层对象,返回的 Promise 不会被 async 函数自动等待 |
| 前缀单独占第 0 行 | 编译时 lineOffset: -1 | 模型代码第 N 行就报告为第 N 行 |
| 收尾另起一行 | } }) | 模型代码最后一行可能以 // 注释结尾 |
拷贝语句在本次没有声明这个名字、或者只用 var 重新声明它时生成;本次用 let、const、class、function(包括 const)声明了它,就没有这行,以本次的声明为准。生成时的关键字:
| 情况 | 前缀里生成 |
|---|---|
本次没有声明,前面是 let、const、function、class | let |
本次没有声明,前面是 var | var |
本次只用 var 重新声明 | var |
拷贝语句只负责把旧值带进来,写成 const 唯一的效果是禁止之后的调用赋值,所以前面的 const 也拷成 let:之后的调用可以直接给它赋值,改的只是拷贝;声明它的那次调用里它仍是 const。不判断这次调用是否真的赋值,所以不需要遍历语法树,也不打印警告。
只用 var 重新声明时也生成拷贝:同一个函数里重复的 var 声明是同一个变量,所以这次的 var 从旧值开始,没执行到的声明不会把旧值换成 undefined,和 node:repl 里全局 var 的行为一致。同一次里同名的 function 在函数开头就初始化,优先于 var,这时不生成拷贝。
编译与运行:
// Kernel: ScopedReplScopes.compile
const factory = new Script(source, { filename, lineOffset: -1, ...importOption }).runInContext(context) as ScopedReplFactory
const getters: (() => unknown)[] = []
const program = factory(values, getters)
this.compiled = { bindings, getters, replaced }
return program
// Evaluator: evaluate
const filename = resolve(baseDirectory, `.ptc-repl-${++submissions}.js`) // baseDirectory: process.cwd() at creation
const program = scopes.compile(code, { filename, context, finalExpression, ...importOption })
const start = Reflect.apply.bind(undefined, program, undefined, []) as ScopedReplProgram
void programDomain.run(() => Promise.resolve().then(start)).then(
(completion) => { finish({ ok: true, value: completion?.value }) },
(error: unknown) => { finish({ ok: false, error }) },
)
- 内核编译时固定给出
filename和lineOffset: -1;求值器总是传importModuleDynamically:有 import 处理器时用VmModuleImportLoader.forFile(filename),没有时用vm.constants.USE_MAIN_CONTEXT_DEFAULT_LOADER。 - 程序由微任务直接调用,同步抛错时堆栈里没有求值器的帧。宿主用
.then接结果、不用await,否则 V8 会在堆栈末尾补一帧宿主的 async 帧。 - 已实测:
this是undefined,arguments.length是 0,同步抛错的堆栈只有程序自己的帧;domain 能接住这样启动的程序里定时器抛出的错误;外包对象里的 Promise 原样返回,不被等待。 - 代码里任何位置出现
__$binding_(包括字符串和注释),都报SyntaxError: Names starting with '__$binding_' are reserved,位置指向第一次出现的地方,这次不执行。
内核:作用域链、acorn 扫描与编译
内核文件只依赖 acorn 和 node:vm,不依赖子进程和求值器,可以在普通 vm context 里连续编译、执行多段程序,单独测试作用域分层。接口:
class ScopedReplScopes {
constructor(options: { strict: boolean; onRedeclaration?: (redeclarations: readonly ScopedReplRedeclaration[]) => void })
/** Scan and compile one program against the current bindings; throws SyntaxError and leaves the chain unchanged. */
compile(code: string, options: { filename: string; context: Context; finalExpression: boolean; importModuleDynamically?: ScriptOptions['importModuleDynamically'] }): ScopedReplProgram
}
type ScopedReplProgram = () => Promise<{ value: unknown } | undefined>
interface ScopedReplRedeclaration { name: string; previous: ScopedReplBindingKind; current: ScopedReplBindingKind }
type ScopedReplBindingKind = 'var' | 'let' | 'const' | 'function' | 'class'
interface ScopedReplBinding { name: string; kind: ScopedReplBindingKind }
interface ScopedReplScan {
/** Top-level bindings declared by the program, in source order. */
declarations: ReadonlyMap<string, ScopedReplBindingKind>
/** Whether the program starts with a "use strict" directive. */
useStrict: boolean
/** Last top-level statement when it is an expression statement. */
finalExpression?: { statementStart: number; expressionStart: number; expressionEnd: number }
}
/** Parse once; throws a SyntaxError laid out like V8's for every error acorn reports. */
function scanProgram(code: string, options: { filename: string }): ScopedReplScan
/** Build the Script source and every binding in the order of the read functions it pushes. */
function buildProgramSource(
code: string, scan: ScopedReplScan, carried: readonly ScopedReplBinding[], options: { strict: boolean; finalExpression: boolean },
): { source: string; bindings: ScopedReplBinding[] }
解析方式:不论 strict 开关,都调用 parse(code, { ecmaVersion: 'latest', sourceType: 'script', allowAwaitOutsideFunction: true })。这和 async 函数体的规则基本一致:顶层 await 合法,顶层 return 是语法错误,同一次里两个同名 function、同名的 var 和 function 都合法。不同的是顶层 using、await using 和静态 import、export 会被拒绝。扫描时不判断严格与否:开关打开时由 Script 开头的 "use strict"; 决定,严格模式的语法违规由 V8 编译时报出。
解析之前先查两件事:程序以 #! 开头,或者代码里任何位置出现 __$binding_,就报错,不再解析。
一次解析得到四样东西:
-
本次的顶层声明,只看顶层语句:
顶层语句 收集的名字 记录的关键字 const、let、var声明,含解构(对象、数组、默认值、剩余元素)每个绑定名 原关键字 function声明,含 async 和 generator函数名 function;同一次里同名的var也记为functionclass声明类名 classfor (var …; …; …)、for (var … in/of …)括号里的var每个绑定名 var块、 try/catch/finally、if/else、循环体、switch、标签语句、with里的var,包括其中for括号里的var;不进入函数和类每个绑定名 var -
有没有顶层
return:acorn 直接报'return' outside of function,并给出位置。 -
最后一条语句:从后往前跳过空语句
;,找到的第一条如果是表达式语句,记下语句和表达式的起止位置。 -
开头的指令里有没有
"use strict":acorn 在指令语句上标出directive,有就记下useStrict。
不做的事:不进入嵌套的函数和类;块里只找 var,let、const、class、function 留在块里;不分析引用,不找赋值;除了最后一条表达式的三处插入,不改写代码。
语法错误:acorn 报错时直接抛 SyntaxError,不再交给 V8 编译。stack 按 V8 的样式排版:
/work/.ptc-repl-3.js:1
const x = ;
^
SyntaxError: Unexpected token
- 消息取 acorn 的原文,去掉末尾的
(行:列)。 - 两处改用 V8 的说法:
'return' outside of function报Illegal return statement;报错位置在代码末尾时报Unexpected end of input。 - 代码里出现
__$binding_时,按同样的样式报错,位置指向第一次出现的地方。
作用域链状态与每次调用的步骤
ScopedReplScopes 持有:
bindings: Map<string, { kind: ScopedReplBindingKind; read(): unknown }>:当前可拷贝的名字和它们的读取函数。kind是名字最近一次声明用的关键字;只是被拷贝的名字沿用之前记下的关键字,例如function拷贝成let后仍记为function。replaced: ReadonlyMap<string, { kind: ScopedReplBindingKind; value: unknown }>:最近采用的那次程序重新声明的名字,在它编译时的旧值。compiled:最近一次编译的程序的绑定顺序、读取函数数组和它的replaced。
每次 compile(code, options):
- 采用
compiled:读取函数数组的长度不少于名字个数,说明那次的前缀已经执行过,bindings换成"第 i 个名字对应第 i 个读取函数",replaced换成它的值。子进程不允许两次执行重叠(overlapping PTC evaluation),上一次的前缀总在下一次编译之前的微任务里执行完;只有在一段同步代码里连续编译两次时,前一次才会因前缀还没执行而不被采用。 - 扫描,得到本次声明和最后一条表达式的位置;语法错误直接抛出,状态不再变化。
- 逐个调用
bindings里的读取函数取当前值。读取抛错(声明没初始化完)就用replaced里的值;两边都没有,就丢掉这个名字。 - 本次没声明、或只用
var重新声明的名字作为拷贝,值放进values;本次声明了的已有名字,把值记进新的replaced,同时记一条重复声明{ name, previous, current }。 - 拼源码,然后编译。
strict关闭且useStrict为 true 时,内层函数体最前面加"use strict";。V8 编译失败时抛出原生的SyntaxError,裁掉错误标题之后的编译器内部帧(和vm-script做法相同,代码单独写一份),状态不再变化。 - 有重复声明时调用
onRedeclaration(redeclarations)。 - 调用外层函数拿到程序,记下
compiled,返回程序。
求值器每次 evaluate(code, finish):
- 提交序号加 1(包括有语法错误的提交),文件名是
<创建时的目录>/.ptc-repl-<序号>.js。 - 调用
scopes.compile;抛错就用这个错误结束本次执行。 - 在 domain 里由微任务启动程序;Promise 完成后调用
finish,结果取completion?.value,不再等待。
读取发生在内核第 3 步,也就是新一次编译的时刻。之后前面的代码再给顶层变量赋值,改的只是它自己那次的变量。
失败的调用
| 第几次 | 代码 | 结果 |
|---|---|---|
| 1 | const data = 1 | data 是 1 |
| 2 | const data = await tools.fetch(),fetch 抛错 | 失败;第 2 次的 data 声明了,但没赋上值 |
| 3 | data | 1:内核第 3 步读第 2 次的 data 时抛错,改用第 2 次编译时记下的旧值 |
| 4 | let fresh = await tools.fetch(),抛错 | 失败;fresh 是新名字 |
| 5 | typeof fresh | "undefined":没有旧值,名字被丢掉 |
- 抛错之前已经执行完的声明保留。例如先执行了
let y = 5再抛错,之后的调用里y仍是 5。 var重新声明从拷贝进来的旧值开始,读取不会抛错,也不需要退回:没执行到的var保留旧值。例如第 2 次在var v = 6之前就抛错,或者var config = await tools.loadConfig({})的工具调用失败,之后的调用读到的仍是之前的值。function在函数开头就是新函数。
异步错误:对齐 REPL
REPL 用 Node 的 domain 接住程序启动的异步工作里抛出的错误。求值器照同样的做法:
- 创建求值器时调用
domain.create()得到programDomain,每个程序都在programDomain.run(...)里启动。程序创建的定时器、回调、Promise 后续步骤和 EventEmitter 都归属这个 domain。 programDomain.on('error', ...)把错误写成Uncaught加错误文本,交给options.reportError。错误文本取错误的stack,stack不是字符串时用"名字: 消息";不用util.inspect格式化 Error,是因为 domain 会往错误对象上加domain、domainThrown,inspect 会把它们一起打印出来。不是 Error 的值,先删掉这两个属性再用util.inspect,和 node:repl 一样。- 程序能让转换文字这一步抛错(
stack访问器、toString、自定义 inspect)。监听器里抛错会让 Node 结束进程,所以这一步包在try/catch里,出错时报program threw an unrenderable value,和同步失败时prepareException的兜底文字相同。reportError放在try外面,它是子进程自己的代码,出错应该照常暴露。 - 回调里
throw冻结或不可扩展的对象时,Node 的 domain 在调用监听器之前给它加属性就失败了,进程结束,和 node:repl 相同。 - 子进程处理
reportError的方式不变:当时有执行在运行,就让那次执行以这个错误失败;否则丢弃。错误按创建回调时所属的执行归属(AsyncLocalStorage)。 - 不在整个进程上注册
uncaughtException或unhandledRejection,子进程自身代码的错误仍然暴露。 - 程序调用工具时,子进程为这次调用创建的异步回调(例如 RPC 回调)也在程序的 domain 里:子进程自身代码在这些回调里的异步错误会被当成程序的
Uncaught …报告,子进程继续运行、不退出。现有 REPL 求值器也是这样。
已验证的情况:
- REPL:定时器里抛错、未处理的 Promise rejection,在执行结束后和执行中都被接住;EventEmitter 的
error事件在执行结束后被接住。 - 单独的 domain:在主环境里接住了定时器抛错、定时器里产生的 rejection、未被等待的 async 函数失败和 EventEmitter 的
error事件;在带默认codeGeneration限制的执行环境里,接住了定时器抛错和定时器里产生的 rejection;由微任务启动的程序里,定时器抛错同样被接住。
动态 import
- 程序里的
import()经过每个 Script 的importModuleDynamically:没有 import 处理器时用vm.constants.USE_MAIN_CONTEXT_DEFAULT_LOADER,有处理器时用现有的VmModuleImportLoader.forFile(filename),和vm-script相同。 - 相对路径按求值器创建时的目录解析,也就是这个环境第一次调用时的 working directory;之后的调用换了目录也不变。node:repl 和
vm-script跟随每次调用的目录;Agent 用working_directory换目录后,需要按新目录 import 时用绝对路径。 - 不提供
require:node:repl 的require解析规则不一致(相对路径跟随当前目录,但第一次解析后被缓存;包名固定按启动目录),只保留动态import()这一种加载方式。
重复声明提示(预留)
内核在第 6 步调用 onRedeclaration;求值器把自己的 reportRedeclarations(redeclarations: readonly ScopedReplRedeclaration[]) 注入进去,函数体先留空,只加一行 TODO 注释,不输出任何内容。以后要提示时,在这里写入程序的 console,内容会进入模型可见的输出。
范围外
- 内存回收:不处理,每次拷贝全部名字。没有闭包存活的旧调用会被回收;某次调用留下了存活的函数时,这个函数会留住这次开始时拷贝进来的所有值。
- 跟随 working directory 变化解析
import():以后需要时另加显式操作,例如js_switch_cwd。 let、const、class重新声明的初始化表达式里读同名旧值(例如let count = count + 1):属于代码问题。strict关闭时严格和宽松调用混用造成的问题,见"风险":交给以后的重置工具。- 最后一条表达式所在行的列号偏移。
接入
以 scoped-repl 格式接入时只做必需的改动,不顺手重构或复用现有代码,方便和其他进行中的 PTC 改动 rebase、合并。程序格式名是 scoped-repl,配置字段名是 replScoped。
| 位置 | 改动 |
|---|---|
ptc-runtime 的 PtcProgramFormat | 包含 'scoped-repl' |
ptc-runtime-node 的 config.ts | 布尔字段 replScoped,只能和 persistent-in-memory 一起用,否则加载时报错;省略时由 resolveReplScoped 取 resolvePersistentExecution 对函数体写入的 persistent-in-memory 取值(目前为 true),其他模式下为 false |
ptc-runtime-node 的 execution.ts | persistent-in-memory 分支里 replScoped 为 true 时:格式 scoped-repl,retainsState 为 true,TypeScript 按 REPL 语句擦除,配一段新的模型说明 |
ptc-runtime-node 的 evaluators/index.ts | 求值器表含 scoped-repl,三个开关取默认值 |
ptc-runtime-node 的 protocol.ts | 协议版本是 3,旧 bootstrap 在就绪阶段报错;升级指南 docs/upgrade-guide/v0.2.2-rc.1/ptc-bootstrap-protocol/ 同时说明 PtcProgramFormat 新增的成员 |
workflow-ptc 的 host.ts | scoped-repl 用和 repl 一样的包装 await (async () => {…})() |
ptc-runtime-node 的 configuration.ts(CUA 用它合并配置) | resolvePersistentExecution 返回 executionMode、scriptPersistence、replScoped 三个字段,一起合并:script 保持持久化的 VM Script,replScoped: false;scoped-repl 为 replScoped: true;函数体和 REPL 语句为 persistent-in-memory 加常量 SCOPED_REPL_BY_DEFAULT(目前为 true),覆盖 profile 里残留的值。这个常量同时决定 config.ts 里省略时的默认值,改它只改一行;config.ts 从 configuration.ts 导入这个函数,而 configuration.ts 本身也是包的发布入口;configuration.ts 只导入 Config 类型,所以打包不会生成共用的 chunk |
| 文档、快照和测试 | 包 README 的配置项、行为说明和"已知限制"、config catalog、快照场景 ptc-scoped-repl;构建产物的 e2e 含 replScoped: true 用例 |
tool-registry-ptc 没有改动:格式不是 function-body 时,它自动使用"打印输出"那套说明。模型说明把上面的行为写成编写程序的规则,而不是逐条列出边界情况:输出用 inspect(...),这句和其他模式共用;不支持顶层 return;每次调用在自己的顶层作用域里执行一段程序,它的顶层声明和继承来的名字以拷贝而不是引用的方式传给之后的程序,因此对象仍然共享;同一段程序里,对一个名字要么使用拷贝来的值,要么重新声明,不要两者都做;给拷贝来的名字赋值(包括前面的 const)只改这段程序的拷贝;不要在块里声明 var,因为块里的 var 会改变之后的程序拷贝到的值;之前的函数和仍在运行的代码保留它们捕获的值,需要共享的可变状态放进对象;程序在对已有名字的声明完成前失败时,之后的程序保留旧值;只能用 import(),没有 require 和 module,import 的相对路径按这个环境第一段程序的目录解析,process.cwd() 和读写文件的相对路径跟随每段程序的目录。保留状态的共用句子要求在程序结束前等待工具调用和子进程,因为保留状态的几种格式都不支持顶层 return。测试整段逐字比对模型说明。
考虑过的替代方案
用直接 eval 把每次调用嵌进上一次的作用域。 前后调用能共享活的绑定,但旧作用域永远无法回收,行列号要靠 source map 或改写堆栈才能对上;在禁止从字符串生成代码的执行环境里,直接 eval 也不可用。
把声明改写成 var 或全局赋值。 所有调用共用同一个绑定,违反要求 2,而且要改写模型代码。
每次调用编译成一个 ES 模块。 靠合成一个代表上一次调用的模块和追加的 export 传值。模块里既不能 return,也没有末尾表达式的值,前置代码还会让报错行号整体偏移;函数参数已经能传值,不需要模块。
作用域分析加改写引用。 能做更精细的内存回收,但需要作用域分析并改写代码,违反要求 5。
只用打印输出、不支持末尾表达式。 workflow-ptc 和 repl 格式的约定都依赖完成值,也偏离现有 REPL 的写法。
用顶层 return 给出结果。 与要求 3 冲突,需要换成函数体格式,run_code 的说明也会随之变化。
末尾的 Promise 等它完成。 和 vm-script 一致,但和现有 REPL 不一致;按 Node 的写法外包一层对象,保持 REPL 的行为。
以 if、for、块结尾时也给出结果。 只有在 Script 顶层执行时才有 V8 的完成值,函数体里拿不到;要支持就得改写更多语句。node:repl 在代码含 await 时同样没有结果。
包装直接写成 async function (__$binding_vals, __$binding_getters)。 this 同样是 undefined,但程序的 arguments 里能看到这两个内部值,长度是 3。
包装用 async 箭头函数。 this 是 globalThis、没有 arguments,和 REPL 一致;我们选择 strict 模式下不带接收者调用函数时的 undefined。
用回调交出读取函数。 前缀里要写名字字符串,还要防止程序重复调用;推入数组、由求值器按下标对应名字更简单。
语法错误交给 V8 再编译一次。 能拿到 V8 原文的消息,但要多编译一次;acorn 已经给出位置和消息,直接报。
strict 开启时按 module 规则扫描。 module 规则比 async 函数体严:同一次里两个同名 function、同名的 var 和 function、嵌套的普通函数里把 await 当变量名都会被拒绝,HTML 注释的处理也和 V8 编译 Script 时不一致。strict 关闭时也用不了,因为 module 规则一律是 strict 的。
套上和运行时一样的函数外壳再扫描。 和运行时的语法完全一致,顶层 using 也能用,Node 的 processTopLevelAwait 就是这样做的。但要自己遍历语句查找顶层 return,换算外壳带来的位置偏移,还要防止代码里多出的 } 提前关闭外壳;我们不需要顶层 using,script 规则更简单。
只把被赋值的 const 拷成 let,并打印警告。 要遍历新调用的语法树并判断遮蔽,属于作用域分析,违反要求 5。我们一律拷成 let,不提示。
var 重新声明不拷贝旧值,从 undefined 开始。 var count = count + 1 得到 NaN;但抛错前没执行到、或在没走到的分支里的 var 会把旧值换成 undefined,例如工具调用失败的 var config = await tools.loadConfig({}) 让之后的调用拿不到原来的 config。拷贝旧值后,var count = count + 1 在旧值上加 1,和 node:repl 一致,暂时接受。
只在调用失败、且读到 undefined 时退回旧值。 不用拷贝,但分不出"没执行到"和"执行了、值就是 undefined":赋上 undefined 之后同一次又失败时,会误退回旧值;成功调用里没走到的分支和不带初始值的 var x; 也覆盖不到。
在代码里插标记,记录 var 是否执行到。 能准确区分,但要在声明处、声明前的赋值和循环头改写模型代码,违反要求 5。
严格的调用里跳过严格模式保留字的拷贝。 能避免宽松调用里声明的 package 这类名字让之后严格的调用编译失败;但这是模型混用两种模式造成的,不处理,交给以后的重置工具。
在整个进程上监听未捕获的错误。 会把子进程自身代码的错误一起吞掉;REPL 用 domain,只接程序启动的异步工作。
不收集块里的 var。 块里的 var 会和拷贝语句撞名,整段程序报 already declared,这个名字也带不到之后的调用;收集它只需约 15 行语句遍历,不分析引用,所以采用收集。
堆栈里只显示 .ptc-repl-<序号>.js,不带目录。 文件名去掉目录后,默认加载器按导入那一刻的 cwd 解析相对 import(),和有 import 处理器时不一致;用 //# sourceURL 只显示短名时,V8 不再应用 lineOffset,每个行号多 1;把 import 交给回调函数可以解决:子进程两种启动方式都带 --experimental-vm-modules,回调可以在第一次调用的目录下发起 import();但这要求 scoped-repl 总是走回调形式的 import,默认路径现在没有走,所以文件名暂时保留第一次调用的目录。
提供 require、module 和内置模块全局(node:repl 做法)。 node:repl 的 require 解析规则不一致,跟随目录又要定义何时换基准;只保留 import()。
影响
得到的:
- 后面的调用可以用任意关键字再次声明前面的名字,前面的函数、闭包和仍在运行的代码继续使用原来的值。
- 写法、结果、报错和异步错误处理与现有 REPL 一致,程序的异步错误不让进程退出。
- 不做作用域分析;除了最后一条表达式的三处插入,不改写模型代码。
付出的:
- "开始时拷贝"和 REPL 不同:前面的代码(定时器、回调)之后再给顶层变量赋值,后面的调用看不到;模型说明写明了这一点。
- 不声明直接赋值只影响之后的调用,前面定义的函数看不到;REPL 里两边会一起变。
- strict 模式和 REPL 不同:REPL 里给未声明的名字赋值会创建全局变量,这里会报错。
- 调用方不丢弃完成值时,末尾是没写
await的 Promise,宿主报"不是 JSON",和现有 REPL 一样;run_code丢弃完成值,不受影响。 - 以
if、for、块结尾的程序没有结果,node:repl 在代码不含await时有。 - 最后一条表达式所在行,插入点之后的列号右移 17。
- 语法错误消息是 acorn 的文字,和 V8 原文不同,例如
Unexpected token而不是Unexpected token ';'。 - 扫描统一按 script 规则:顶层
using、await using会被拒绝,node:repl 也不支持。 - 顶层
arguments是一个空的 arguments 对象,node:repl 里它不存在。 - 执行环境默认禁止从字符串生成代码:依赖
eval或new Function的代码会失败,包括经 import 处理器加载到这个执行环境的库。这个限制不构成隔离,复制进来的主环境函数(例如setTimeout.constructor)仍能生成代码。 - 不提供
require:依赖require的代码要改用await import()。 - 相对路径的
import()固定按这个环境第一次调用时的目录解析,Agent 换目录后仍按旧目录。 - Node 文档把
domain标为已弃用;node:repl 仍建立在它上面,如果 Node 移除它,REPL 也会一起变。 - 子进程自身代码在工具调用的异步回调里出错时,会被当成程序的
Uncaught …报告,子进程继续运行,和现有 REPL 相同。 - 以
#!开头的程序被拒绝。 - 代码里任何位置出现
__$binding_(包括字符串、注释)都会被拒绝;用\u005f这类转义写出的内部名不在检查范围内。 - acorn 必须跟上 V8 支持的语法,否则较新的语法会被拒绝执行。
var重新声明从旧值开始:var count = count + 1在旧值上加 1,和 node:repl 一致;即使前面是let、const也是这样,node:repl 在这种情况下报already declared。let、const、class重新声明的初始化表达式读不到同名旧值,会报错,例如let count = count + 1。属于代码问题,不处理。- 前面的
const在之后的调用里一律拷成let,可以被赋值,而且没有提示。 strict关闭时,严格和宽松的调用可以混用:宽松调用里给未声明的名字赋值,会创建所有调用和旧函数共用的全局变量;宽松调用里用严格模式保留字(package、public、private、protected、interface、implements、static、let、yield、eval、arguments)声明的名字,会让之后严格的调用编译失败。不处理,交给以后的重置工具。- 回调里
throw冻结或不可扩展的对象时,Node 的 domain 处理本身出错,进程结束,保留的变量丢失,和 node:repl 相同。 - 每次调用都要为前面所有名字生成拷贝声明和读取函数:原型实测 1000 个名字时约 10 ms/次。
- 某次调用留下存活的函数时,这次拷贝进来的所有值都会一直留在内存里。
测试
- 内核测试单独运行即可通过,覆盖:script 规则解析和开头
"use strict"的检测、声明表里的每种语句、顶层return(包括块里的)、最后一条表达式的定位与插入、以#!开头的程序、代码里出现__$binding_、语法错误的排版和两处消息替换、拼出的源码;strict关闭时,程序开头的"use strict"在有拷贝和没有拷贝时都生效;并在普通 vm context 里连续执行多段程序,覆盖"跨次调用"里的每一条,包括各种关键字组合、失败的调用、定时器、函数重定义和重复声明通知。 - 求值器测试直接使用求值器和真实的 vm 执行环境,覆盖"写法与结果""执行环境""报错""异步错误与生命周期"里的每一条,以及经求值器的跨次调用;包括经全局对象的原型链拿不到主环境的
Function、复制进来的主环境函数(例如setTimeout.constructor)仍能生成代码,以及宿主以module、require为名的绑定原样保留。 - 三个开关各自在打开和关闭时都有测试。
- 顶层
this是undefined、arguments.length是 0,同步抛错的堆栈只有程序自己的帧。 - 异步错误:上面列出的每种情况在执行结束后和执行中各测一次,交给
reportError的文本以Uncaught开头;进程不退出,变量保留。stack为空、读取stack抛错、自定义 inspect 抛错时,分别报"名字: 消息"和固定文本,进程不退出;抛出普通对象时报错里没有domainThrown。 - import:默认加载器和 import 处理器两条路径,相对路径按创建时的目录解析,之后换了目录也不变。
- 构建产物的 e2e 覆盖
replScoped: true。 - 挂载
persistent-in-memory的测试都显式写replScoped,保留执行的测试两种取值都跑;只有一个测试检查省略时的默认值。 - 新文件覆盖率 100%;现有求值器及其测试不变。