Yes, D8 is a command line tool that allows you to run and debug Javascript scripts. It comes with V8.
Getting D8
Getting V8 on your machine is not hard. Follow these instructions:
https://developers.google.com/v8/build
(The part with GYP looks a little messy but I got through it easily on a Linux box.)
With V8 installed, you now have d8, which allows you to run javascript from the command line.
$ d8 myscript.js
Using D8
Look at d8 --help
for the profiling options. These will be very similar to the options you can use when you want to use profiling on Chrome.
If you want to debug start d8 with --debugger
. The script will break when it encounters the statement debugger;
in your code, or when you type debugger;
in the d8 shell. (You get the shell by starting d8 with --shell
.) You know you're debugging when the shell prompt goes from d8>
to dbg>
. To get help with the debugger type help
at the dbg>
prompt.
The d8 debugger is an old school command line debugger that will be painful to use on large complex scripts if you are used to GUI debuggers.
Using a GUI debugger with D8
If you want to use a GUI debugger you can use the Chrome DevTools for Java, which includes an Eclipse debugger:
https://code.google.com/p/chromedevtools/
To get it working:
- install Eclipse
- install ChromeDevTools plugin (I got help from this video: http://www.youtube.com/watch?v=_uzSw_fb7NQ)
- create a new debug configuration of type "Standalone V8 VM", setting host:localhost and port:5858 for the connection params.
To debug code running in D8:
- start D8 with your script and with --debugger_agent, and --debugger_port if necessary based on your config; D8's default port is 5858.
- in Eclipse launch the debug configuration you created above. The scripts will show up in the interface
- set breakpoints in the Eclipse UI and have at it. (You do not need
debugger;
statements in your code.)
Note! D8 only listens for the debugger on the localhost. So if you are on a separate machine, then this won't work. There is no flag that will make it listen on another IP. See:
https://code.google.com/p/v8/issues/detail?id=1855
Gotchas of running Javascript in D8
Note that the Javascript environment in D8 is not the same as the one in a browser window. There are a number of functions you can't use:
- setTimeout and its cousins
- XMLHttpRequest
- and quite a few more. Check the ECMA specs to see what you actually CAN use.
Remember there is no window.*
or document.*
or DOM!