NicolasPetton / Indium

A JavaScript development environment for Emacs
https://indium.readthedocs.io
GNU General Public License v3.0
1.13k stars 61 forks source link

Can I emulate Chrome DevTools console with the JS REPL buffer? #260

Open tmurph opened 3 years ago

tmurph commented 3 years ago

For context, I'm new to Node and I'm learning by making a simple repl-style app, using Node's repl library. It's just a repl with one predefined variable. Here's the entire app.js:

// app.js
const repl = require('repl');

const r = repl.start('repl> ');
r.context.hello = 'Hello World!';

When I start the app (with --inspect) and connect through Chrome DevTools, the devtools console knows about the repl context variable. I can evaluate hello and see "Hello World!" come back.

When I indium-launch the app, the JS REPL buffer doesn't know about the repl context variable. When I evaluate hello I get a reference error.

I can roughly follow indium's execution path, so I can see why this is happening (evaluation happens in the node server's global context, not in the app's repl context). I just can't figure out if there's a way to tweak the JS REPL setup so that it better emulates Chrome's console behavior.

Has anybody got this working?

NicolasPetton commented 3 years ago

I never tried to do anything like that. I'm wondering how this actually works, have you tried inspecting the Chrome devtools to see how the console handles the context?

CDP's Runtime.evaluate can take an optional contextId parameter, but I'm wondering how would Indium know about the context id.

NicolasPetton commented 3 years ago

I guess a Runtime.executionContextCreated event must be fired.