Repository navigation
REPL: show the console output of evaluated code - #3268
officialasishkumar wants to merge 1 commit into
Conversation
When "Evaluate" is enabled, the compiled code runs in a hidden iframe and everything it logs only ends up in the browser's developer tools, which is inconvenient (and not available at all on most mobile browsers). Add a "console" tab next to "output" and "input AST" that shows what the evaluated code logs: - scopedEval now wraps the iframe's console.log/info/warn/error/debug and console.clear. Calls are still forwarded to the original methods, so the messages keep showing up in the developer tools too. - Errors thrown synchronously by the code, as well as uncaught errors and unhandled promise rejections from callbacks, are reported as "Uncaught ..." messages. - The console is cleared every time the code is evaluated again, and only the latest 1000 messages are kept so that logging in a loop can't freeze the page. Updates are batched in a microtask. - Logged values are formatted by a small cross-realm `inspect` (formatConsoleArgs) that supports printf-style substitutions, nested objects/arrays with a depth limit, Map/Set, typed arrays, errors, dates, functions, classes and circular references. Getters are not invoked. - When "Evaluate" is disabled, the tab explains how to enable it. Add Playwright tests covering the logged messages (including printf-style substitutions, sync and async errors) and the placeholder shown when evaluation is disabled.
✅ Deploy Preview for babel-v9 ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for babel-v8 ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
JLHwung
left a comment
There was a problem hiding this comment.
Thank you for opening a PR.
Per our AI policy, please disclose AI usage involved in this PR. And if you have used LLM to assist, please let the LLM delegate a new agent to review this PR before you are opening.
| const contentWindow = getIframe().contentWindow; | ||
|
|
||
| // Every evaluation starts with an empty console. | ||
| clearConsole(); |
There was a problem hiding this comment.
Clearing console here doesn't stop old code from logging. The hidden sandbox iframe is reused in different runs, so any setInterval or pending promises will keep writing to the cleared console. For example, setInterval(() => console.log('tick'), 1500) keeps printing after user edits the code. Consider create a new iframe for each run.
| ) : ( | ||
| entries.map((entry, index) => ( | ||
| <pre | ||
| key={index} |
There was a problem hiding this comment.
When console entries reach the MAX_CONSOLE_ENTRIES limit, every key will points to different content on every batch. Because we have role="log" here, an accessibility screen reader may announce them again. Consider use a stable id for each message.
|
|
||
| useEffect(() => { | ||
| // Keep the latest messages visible. | ||
| container.current.scrollTop = container.current.scrollHeight; |
There was a problem hiding this comment.
We should only scroll down when the view is already at or near the bottom: Every time new messages arrive, this effect scrolls the panel to the very bottom. It doesn't check where user were looking, so it pulls down even if user had scrolled up.
| }; | ||
|
|
||
| _onConsoleChange = (consoleEntries: ReadonlyArray<ConsoleEntry>) => { | ||
| this.setState({ consoleEntries }); |
There was a problem hiding this comment.
This comment is from claude:
Consider use useSyncExternalStore to let ConsolePanel subscribes to itself rather than trigger the re-render of the REPL component whenever there is new console entries:
// scopedEval.ts
const listeners = new Set<() => void>();
let snapshot: ReadonlyArray<ConsoleEntry> = [];
function notifyConsoleListeners() {
isNotifyScheduled = false;
snapshot = consoleEntries.slice();
listeners.forEach(l => l());
}
export function subscribeConsole(l: () => void) {
listeners.add(l);
return () => listeners.delete(l);
}
export const getConsoleSnapshot = () => snapshot;// ConsolePanel.tsx
const entries = useSyncExternalStore(subscribeConsole, getConsoleSnapshot);
When "Evaluate" is enabled, the REPL runs the compiled code in a hidden iframe, but everything the code logs only ends up in the browser's developer tools. That makes quick experiments awkward and doesn't work at all on most mobile browsers. This adds a console tab next to "output" and "input AST" that shows what the evaluated code logs.
Changes:
scopedEvalwraps the iframe'sconsole.log/info/warn/error/debugandconsole.clear. Calls are still forwarded to the original methods, so the messages keep showing up in the developer tools too.Uncaught ...messages. Warnings and errors use the same colors as the existing error/warning bars.inspectinlib/formatConsoleArgs.tsinstead ofJSON.stringify. It supports printf-style substitutions (%s %d %i %f %o %O %c %%), nested objects/arrays with a depth limit,Map/Set, typed arrays, errors, dates, functions/classes and circular references. Getters are shown as[Getter]and are not invoked, so formatting has no side effects. If a value can't be inspected (e.g. a revoked proxy), the user'sconsolecall still doesn't throw.Example:
console.log("answer:", { value: 42, list: [1, "two"] }); console.warn("%s is deprecated", "foo");showsanswer: { value: 42, list: [ 1, "two" ] }followed by a highlightedfoo is deprecated.Testing:
tests/repl.spec.tswith Playwright tests for the logged messages (formatting, printf-style substitutions, sync and async errors) and for the placeholder shown when evaluation is disabled. Both fail onmainand pass with this change. The existing REPL visual test still passes. The Argosreplscreenshot will show a diff because of the new tab label.console.clear()and switching tabs.Fixes #1392