Repository navigation
Conversation
The ghost text and Tab resolve the chain in front of the cursor on each key. The walk called every getter and Proxy trap on the way, so code that the user only typed ran: a getter with a side effect ran once for each key, a getter that loops froze the REPL, and a getter that calls process.exit() ended it. One walk now serves both completion bindings. It reads a data property, a native property, and a getter that isBunDefinedGetter accepts. It stops in front of a getter of the program and in front of a Proxy, and calls neither. Tab leaves the line as typed where the walk stops, and inserts no name from a list that a Proxy cut short.
|
Status
|
There was a problem hiding this comment.
Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.
Beyond the inline findings, I also checked the mechanics of the new walk in src/jsc/bindings/bindings.cpp: both out-params (incomplete, refused) are written before every early return, getPrototype is only ever called on an object the Proxy check already passed so no getPrototypeOf trap runs, every throwing call is checked under the top exception scope and cleared before returning to Rust, and in src/runtime/cli/repl.rs a one-name list flagged incomplete falls through to the listing branch rather than being inserted. Those paths look correct; what remains for a maintainer is the policy question the inline comments raise (which Bun-made Proxies and Bun getters that read program objects should be exempt), which is a design call rather than a defect in the code.
Extended reasoning...
The change replaces ordinary [[Get]] and getOwnPropertyNames in the two REPL completion bindings with a shared prototype walk that stops at program-defined Proxies and non-Bun getters, threads two bool out-params through the Rust caller, and adds pty and piped-stdin tests. It touches no auth, crypto or injection surface; the risk is behavior regression in REPL completion and exception safety in native code. The three confirmed inline findings are about the policy boundary (Bun-made Proxies, Bun getters that read user objects, vm-context global proxies) rather than memory or exception bugs, and the ruled-out checks above cover the mechanical correctness of the walk and the Rust integration.
…n/1a5108d4/repl-completion-no-user-code
The global of a node:vm context asks its sandbox for each name. When the sandbox is a Proxy, the completion walk called its getOwnPropertyDescriptor trap through that global. The walk now stops in front of such a global, as it does in front of a Proxy. The comments said that every Proxy other than process.env on Windows is the program's. Bun makes more of them (require.cache, the http2 socket), and completion stops at those too, because some of them forward to an object of the program. The comments, the enum value and the docs now say that the walk stops at a Proxy.
There was a problem hiding this comment.
Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.
Beyond the inline nits, I checked the plumbing of the new walk: refused/incomplete are set false before every early return, the exception scope is checked after each visit, getPrototype and slot.getValue, so nothing escapes to the Rust caller, and the headers.h/repl.rs signatures match. The vm-context-on-a-Proxy route raised on the previous push now stops in isProxyForREPLCompletion (GlobalProxyType branch) and has a row in programCode.
Extended reasoning...
The change rewrites the two REPL completion bindings in src/jsc/bindings/bindings.cpp to walk the prototype chain with getOwnPropertySlot, refusing program-defined getters and Proxies, and threads refused/incomplete out-params through src/runtime/cli/repl.rs; it touches no auth, crypto or injection surface. The remaining question is a product one (stricter than Node's completer, deliberately stopping at Bun-made Proxies), which the inline findings and the PR's own notes already surface for a maintainer.
The completion walk refused a chain through such a property, so Tab left the line as typed. The property reads as undefined and there is nothing to call, so the chain leads nowhere and Tab indents, as it does on main.
There was a problem hiding this comment.
Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.
Beyond the inline note, I also checked the exception handling of the new walk in src/jsc/bindings/bindings.cpp: every call that can enter JS (getPrototype, getOwnPropertySlot, slot.getValue, the Bun-accepted getter) is followed by a scope check, and both entry points clear the exception before returning to the Rust caller, so nothing escapes into repl.rs. The latest push also handles the vm-context-on-a-Proxy and setter-only-accessor paths raised on earlier versions (isProxyForREPLCompletion unwraps the global proxy to the NodeVMGlobalObject sandbox, and isGetterNull() maps a setter-only property to a miss), and both have tests in test/js/bun/repl/repl.test.ts.
Extended reasoning...
The change rewrites the REPL completion lookup in bindings.cpp to walk the prototype chain with own-property slots instead of [[Get]], refusing program-defined getters and Proxies, threads two new out-params through headers.h and repl.rs, and adds 16 tests. It touches no auth, crypto or injection surface; the one posted finding is a code-placement nit and a further verified finding was not posted, so approval is withheld and the note only records what else was examined.
| // Completion reads one Proxy: process.env on Windows, which Bun makes (windowsEnv in ProcessObjectInternals.ts). It stops at every other one, also at those of Bun's own modules (require.cache, the http2 socket), because some of them forward to an object of the program (mock.property in node:test). | ||
| static bool isBunProxyForREPLCompletion(JSC::JSGlobalObject* globalObject, JSC::JSObject* object) | ||
| { | ||
| #if OS(WINDOWS) | ||
| return object == defaultGlobalObject(globalObject)->m_processEnvObject.get(); | ||
| #else | ||
| UNUSED_PARAM(globalObject); | ||
| UNUSED_PARAM(object); | ||
| return false; | ||
| #endif | ||
| } | ||
|
|
||
| // A Proxy, or the global of a node:vm context whose sandbox is one: that global asks its sandbox for each name. | ||
| static bool isProxyForREPLCompletion(JSC::JSObject* object) | ||
| { | ||
| switch (object->type()) { | ||
| case JSC::ProxyObjectType: | ||
| return true; | ||
| case JSC::GlobalProxyType: | ||
| object = uncheckedDowncast<JSC::JSGlobalProxy>(object)->target(); | ||
| if (!object) | ||
| return false; | ||
| [[fallthrough]]; | ||
| case JSC::GlobalObjectType: { | ||
| if (object->classInfo() != Bun::NodeVMGlobalObject::info()) | ||
| return false; | ||
| JSC::JSObject* sandbox = uncheckedDowncast<Bun::NodeVMGlobalObject>(object)->contextifiedObject(); | ||
| return sandbox && sandbox->type() == JSC::ProxyObjectType; | ||
| } | ||
| default: | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| enum class REPLCompletionWalk : uint8_t { | ||
| Found, | ||
| End, | ||
| Proxy, | ||
| Threw, | ||
| }; | ||
|
|
||
| // Calls `visit` on `object` and then on each prototype until one returns true. Completion runs on every key the user types, so the walk stops in front of a Proxy and calls none of its traps. | ||
| template<typename Visit> | ||
| static REPLCompletionWalk walkForREPLCompletion(JSC::JSGlobalObject* globalObject, JSC::TopExceptionScope& scope, JSC::JSObject* object, const Visit& visit) | ||
| { | ||
| unsigned prototypeCount = 0; | ||
| for (JSC::JSObject* current = object;;) { | ||
| if (isProxyForREPLCompletion(current) && !isBunProxyForREPLCompletion(globalObject, current)) [[unlikely]] | ||
| return REPLCompletionWalk::Proxy; | ||
|
|
||
| bool found = visit(current); | ||
| if (scope.exception()) [[unlikely]] | ||
| return REPLCompletionWalk::Threw; | ||
| if (found) | ||
| return REPLCompletionWalk::Found; | ||
|
|
||
| JSC::JSValue prototype = current->getPrototype(globalObject); | ||
| if (scope.exception()) [[unlikely]] | ||
| return REPLCompletionWalk::Threw; | ||
| if (!prototype.isObject() || ++prototypeCount > JSC::JSObject::maximumPrototypeChainDepth) | ||
| return REPLCompletionWalk::End; | ||
| current = JSC::asObject(prototype); | ||
| } | ||
| } |
There was a problem hiding this comment.
🟡 nit (optional): maintainers get another 65 lines of REPL-only logic in the monolithic bindings.cpp, which the repository's review rules name as a file not to grow. The new helpers isBunProxyForREPLCompletion, isProxyForREPLCompletion, REPLCompletionWalk and walkForREPLCompletion at src/jsc/bindings/bindings.cpp:7061-7124 are used only by the two Bun__REPL__ bindings below them. Fix: move the REPL completion walk, together with the four existing Bun__REPL__ bindings, into a REPL-owned file (for example src/jsc/bindings/BunREPL.cpp) and update the otherMentions entry added in src/codegen/builtins/host/runtime-dependencies.builtins.ts:578 to that file.
Why this was flagged
The diff adds static helpers and an enum at src/jsc/bindings/bindings.cpp:7061-7124 and extends Bun__REPL__getCompletions and Bun__REPL__getProperty in the same file. The repository review instructions say new code goes in the module that owns the feature and explicitly list 'no bindings in monolithic bindings.cpp'. On the base branch the four Bun__REPL__ functions were already there, so this is a pre-existing placement that the change grows rather than fixes. No runtime behavior differs; the cost is to maintainers reading and building the file. The dependency catalog at src/codegen/builtins/host/runtime-dependencies.builtins.ts:578 now also names bindings.cpp for the m_processEnvObject.get mention at bindings.cpp:7065, so a move must update that entry.
Verification: nit. Trigger: unconditional on merge; this is a placement/scope issue, nothing runs differently. The diff adds, inside the monolithic file, four new REPL-only static helpers and an enum (src/jsc/bindings/bindings.cpp:7062), whose only callers are Bun__REPL__getCompletions (:7128) and Bun__REPL__getProperty (:7197) in the same file. The repository review instructions state "no bindings in monolithic bindings.cpp".
There was a problem hiding this comment.
The four Bun__REPL__ bindings are in bindings.cpp on main, and this PR adds no binding. It adds four static helpers that only the two completion bindings use, so they are beside those.
A file that the REPL owns is the better home for all of it. I did not do the move in this PR, for these reasons:
- A move of
Bun__REPL__getCompletionsandBun__REPL__getPropertyturns the change to each of them into a delete and an add. The line-level change is what this PR is about. - repl: Tab lists, counts and inserts identifier names only #46316 rewrites
Bun__REPL__getCompletionsin place, and repl: evaluate input the REPL transform does not take through each caller's result path (-e/-pexit code,.copy,_) #45267 editsBun__REPL__evaluateandBun__REPL__formatValuein place. A move of the four bindings conflicts with both. - A new
.cppinsrc/jsc/bindings/shifts the unified bundles, or it needs an entry innoUnifyinscripts/build/unified.ts, which is one more translation unit that compiles alone.
A follow-up that only moves the four bindings and these helpers, with no change in behaviour, is cleaner after the open REPL PRs land. If a maintainer wants the move in this PR, I will do it here.
Stacked on #47152.
Problem
bun replcompletion runs code the user only typed:o.x.thcalls the getterx3 times. A getter that loops freezes the REPL, and one that exits ends it.Bun__REPL__getProperty(src/jsc/bindings/bindings.cpp:7158) does an ordinary[[Get]]for each name before the cursor, on each key.Bun__REPL__getCompletionscalls Proxy traps.Fix
isBunDefinedGetteraccepts. It stops in front of a program getter and in front of a Proxy.test/js/bun/repl/repl.test.ts. 16 new tests fail on main. All 159 pass.Background
getOwnPropertySlotfills it and calls nothing.rl.columns.).process.exit()or a side effect. To refuse every getter losesfs.promises..Downsides
require("typescript"), jsdom'sdom.window.. Evaluate the getter, then complete from_.crypto.fips.). Per chain step: +13 instructions (own data) to +580 (9 prototype hops). Text: +512 bytes.Notes
This reverses a choice of #30413. That PR added the suggestion drawn while you type, and
Bun__REPL__getProperty. Its description says: "This deliberately runs getters, as node's completion preview does". Its review flagged that getters and Proxy traps "run on every keystroke while the terminal is in raw mode", and proposed a lookup withgetOwnPropertySlotthat does not call the getter. The author's reply kept the calls as "the same behaviour node's REPL has", with a watchdog as a follow-up "if it turns out to matter in practice". No maintainer commented on that thread. This PR builds that lookup. A maintainer can narrow it, see "Narrower rules".Calls of program code while each chain is typed, one key at a time. Release builds, on a pty. "main" is 11c3671.
bun --interactivebun replmainbun replhere__own.x.al__inst.x.al__glob.alutil.deprecate(fn)as getter__proxy.a.th__holder.p.ze__child.a.th__heir.ze__vmGlobal.a.th__child.a.this not the same in every run (66, then 60).bun --interactive: the class getter, the getter on globalThis, both Proxy prototype rows and the vm context row. Node tests own properties only. A class getter withprocess.exit(7)ends Node,bun --interactiveandbun replon main with code 7 whilek.x.ais typed. This branch stays alive.bun --interactiveis a port of Node's completer (src/js/internal/repl/completion.js). This PR does not change it, so the two REPLs now differ in those five rows.bun replhas no such check.The rule.
process.envon Windows, which is a Proxy that Bun makes (windowsEnv). The walk reads it as main does.node:vmcontext whose sandbox is a Proxy counts as a Proxy. That global asks its sandbox for each name, so a read through it calls thegetOwnPropertyDescriptortrap.getOwnPropertySlot. The slot is an own-property slot, not aVMInquiryslot: that one forbids the JS that Bun's lazy properties run on their first read.isBunDefinedGetteraccepts its getter, see Read accessors whose getter wraps user code at load time, not while an importer links #47152 for that test. Else the walk stops. An accessor with a setter and no getter reads as undefined, so the chain leads to nothing.Bun__REPL__getPropertyreports the stop throughbool* refused.Bun__REPL__getCompletionsreports a list that a Proxy cut short throughbool* incomplete.refused, or withincompleteand no name, Tab redraws the line. Withincompleteand names, Tab lists them and inserts none, and no suggestion is drawn. A chain that leads to nothing still gets the two spaces.Not fixed. Program code still runs on these routes.
rl.columns.on a readline interface whoseoutputhas acolumnsgetter (8 calls, as on main), aRegExpsubclass withget global()behindr.flags.(3, as on main).Error.prepareStackTraceruns once for each Error that completion lists or whosestackit reads (as on main).process.stdoutcalls anewListenerlistener (1, as on main).getand that the program installs as a getter (get: http.get) is called.ReadableStreamDefaultReader.prototype.closed.thstill prints 3 lateTypeErrorlines. The native getter returns a promise that it rejected. A blindmarkAsHandled()on the result would also hide a rejection that the program made and that a native getter only returns. Handle the rejection of a native getter that the formatter or a debugger preview read #41630 adds the test that tells the two apart. With it this is one call at the read.vm.Script.prototypedo not return for a wrong receiver (node:vm: Script results are own properties of the instance, as in Node #41776), sovm.Script.prototype.sourceMapURL.still freezes the REPL.process.exit()nor a side effect that finishes.Completions that stop.
ts = require("typescript"), thents.SyntaxKind.Unk(main suggestsnown). All 2,248 exports oftypescript6.0.2 are own getters. esbuild writes the exports of a CommonJS bundle this way. Node v26.3.0 calls no own getter, so it stops there too.dom = new JSDOM(...), thendom.window.docuanddoc.body.inner(main suggestsmentandHTML). These are class getters, which Node v26.3.0 calls.ts.versi).NapiClass, the FFI function class). The FFI case has a test. The N-API case follows fromNapi::definePropertyand was not run with an addon._does not help there. Copy the object first (copy = { ...proxy }).process.envon Windows:require("module").wrapper.(main completeslentolength), the socket of an http2 session or stream (session.socket.), an object frommock.property()ofnode:test, a function fromCallTracker.calls(). The http2 andCallTrackercases are read from the code and were not run.require.cache.has no completions on main either, because its keys are paths.mock.property()andCallTracker.calls()forward each read to an object of the program. On main, Tab ont.x.alruns the getterxof the object behindmock.property(). A mark on each Proxy that reads only Bun's own state is possible (a private name on the handler). This PR does not add one.module.wrapper,require.cacheget: getName(7):crypto.fips,http.ServermaxHeadersCountandhttpAllowHalfOpen,Bun.sql.options,IncomingMessagerawHeaders,crypto.Hash_readableStateand_writableStatedeprecate()wrapper (7):OutgoingMessage_headersand_headerNames,dgram.Socket_handle,_receiving,_bindState,_queue,_reuseAddrdocs/runtime/repl.mdx, "Completion") says where completion stops and what to do.Narrower rules a maintainer can choose.
slot.slotBase() == receiver). Class getters then complete again and run again, with the freeze and the exit.NO_COLOR, on a continuation line and in.editor, and one Tab through a looping class getter still freezes the REPL.rl.columns.,r.flags.andget: http.getclose. It losesfs.promises.,map.size.,buffer.buffer.andx.__proto__..Measurements. Release builds of main 11c3671 and of this branch, gdb single-step of the main thread, piped stdin, the third and fourth Tab cycle. The counts are the same in both cycles unless two values are given.
Bun__REPL__getProperty, one callo.a)o.constructor)process.argv)process.title)map.size)fs.promises)o.a.b.c.zzq, no getter and no Proxy: 12,275 to 12,296 instructions, 1,969 to 1,985 conditional branches, 38 to 41 indirect calls, 12 to 12 syscalls.Bun__REPL__getCompletions, one call: 4,670 to 4,692 for 2 candidates. Forprocess.(over 100 names): 73,731 to 73,734 with the same environment. That count moves with the environment variables alone (71,609 to 74,229 on main), so the builds show no difference there.mi_mallocin the two bindings 6 to 6,mi_free6 to 6. The result array and its strings are built as before.nm -S,size):Bun__REPL__getProperty349 to 912 bytes,Bun__REPL__getCompletions1,536 to 1,624, whole binary 138,038,120 to 138,038,632 with Read accessors whose getter wraps user code at load time, not while an importer links #47152. The stripped file has the same size.Bun__REPL__symbols: 4 to 4, none exported. Two signatures change, each in three places (bindings.cpp,headers.h,repl.rs).process.envexception: 0 to 2. The piped Tab tests ran on Windows 2019 x64 and on Windows 11 aarch64 in CI build 134345 (9559627): 78 pass, 0 fail, the pty tests are todo there.nm,size.perf,strace,valgrindandbloatyare not on the machine.Tests.
programCodeis one table of what a program can put behind a read (12 rows). A piped test presses Tab on each row and runs on every platform. One pty test for each row types the chain, then presses Right, End and Tab twice. Both read one counter per row. On main every row counts.get: process.exitand a looping getter and types through both. On main the REPL exits with code 0.test.eachrows pin what still completes, one row for each kind of read (lazy property, native accessor, engine getter, three spellings of a getter insrc/js, module namespace,process.env). They pass on main.process.envnames, a chain through one, and throughprocess.env.TZ. Nothing ran on Windows locally. CI build 134345 ran this test on both Windows lanes. It is the Windows coverage of theprocess.envexception.process.exittest (host name), theutil.deprecaterow (any builtin function),fs.promises.readF(getter syntax),__events.defaultMaxListeners.toF(the nameget).BUN_JSC_validateExceptionChecks=1, andtest/internal/source-lints.Other open PRs.
Bun__REPL__getCompletionswithBun__REPL__forEachCompletionand pins a Tab test that lists theownKeysnames of a Proxy. Whichever lands second keeps the stop inside the new function and changes that row to "the line stays as typed".quits.now.,crashes.now.), and need another trigger after this PR.DIMconstant of the test file, which two new tests use.Placement. The helpers are in
bindings.cpp, beside the two completion bindings that use them. The fourBun__REPL__bindings are in that file on main.REVIEW.mdasks for no bindings inbindings.cpp. This PR adds none and does not move the existing ones: a move hides the change to the two functions, it conflicts with #46316 and #45267, and a new.cppshifts the unified bundles or compiles alone. A follow-up can move the four bindings with no change in behaviour. A maintainer can ask for the move here.Self-review. 45 concerns raised, 42 addressed in the code, the tests or this text. Rejected:
bun:internal-for-testingthat calls the walk in-process: it adds a host function for tests only. The piped tests run on every platform today.no test proof · iteration 0 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/js/bun/repl/repl.test.ts