Using d8

d8 (which stands for Debug8 or “Debug Aid”) is V8’s own minimalist debug shell.

d8 is useful for running some JavaScript locally or debugging changes you have made to V8. Building V8 using GN for x64 outputs a d8 binary in out.gn/x64.optdebug/d8. You can call d8 with the --help argument for more information about usage and flags.

Printing output is probably going to be very important if you plan to use d8 to run JavaScript files rather than interactively. This can be achieved using console.log:

$ cat test.js
console.log('Hello world!');

$ out.gn/x64.optdebug/d8 test.js
Hello world!

d8 also comes with a global print function that does the same thing. However, console.log is preferred over print since it works in web browsers as well.

Read input

Using read() you can store the contents of a file into a variable.

d8> const license = read('LICENSE');
d8> license
"This license applies to all parts of V8 that are not externally
maintained libraries. The externally maintained libraries used by V8
are:
(etc.)"

Use readline() to interactively enter text:

d8> const greeting = readline();
Welcome
d8> greeting
"Welcome"

Load external scripts

load() runs another JavaScript file in the current context, meaning that you can then access anything declared in that file.

$ cat util.js
function greet(name) {
return 'Hello, ' + name;
}

$ d8
d8> load('util.js');
d8> greet('World!');
"Hello, World!"

Pass flags into JavaScript

It’s possible to make command-line arguments available to your JavaScript code at runtime with d8. Just pass them after -- on the command line. You can then access them at the top-level of your script using the arguments object.

out.gn/x64.optdebug/d8 -- hi

You can now access an array of the arguments using the arguments object:

d8> arguments[0]
"hi"

More resources

Kevin Ennis’s D8 Guide has really good information about exploring V8 using d8.