Sunday 16 October 2011

Using the V8 javascript shell (D8)

V8 is a fast and nimble JavaScript engine used by the Google chrome browser. People interested in experimenting with this JavaScript engine will find the D8 shell environment supplied with the V8 source code to be highly useful. But unfortunately I was not able to find any ready to use documentation for D8 online. So here is my attempt at creating a small user guide for the uninitiated. You can download and build V8 by following the instructions given here. Alternatively you can fork the (official?) V8 source code on github.com.


Build d8 by using the following command in a console...
~/Source/v8$ scons mode=debug arch=x64 console=readline d8 


Then run d8


~/Source/v8$ ./d8_g

V8 version 3.7.1 (candidate) [console: readline]
d8> 



So without further ado here is the list of commands you can use on D8...


1. print(arg1, arg2, ..., argN) - As the name suggests the print method can be used to print to the console. Note print automatically inserts a new line character at the end.



d8> print("Hello", "world", 5, 3.14, [1,2], {"a":5})
Hello world 5 3.14 1,2 [object Object]
d8> 

2. write(arg1, arg2, ..., argN) - Same as print but does not insert a newline character at the end.

3. read(filename) - Read a file from the disk (or anywhere you want) and store contents in a JavaScript string. The first argument is the file name (can be a full path, relative path etc the operating system path conventions apply)

d8> var s = read("LICENSE")
d8> s
This license applies to all parts of V8 that are not externally
maintained libraries.  The externally maintained libraries used by V8
...omitted...
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

4. readline() - Read a line from console. Actually you can read multiple lines from the console if you end each line with the '\' character.

d8> l = readline()
This is a line of text.
This is a line of text.
d8> readline()
line1\
line2
line1
line2
d8>

Note: The the text entered by the user is echoed again by the readline() method.

5. load(filename) - load and execute a JavaScript file from disk (or from anywhere you like).

d8> load("hello.js")
Hello
d8> 

6. quit([exitCode]) - Exit D8 shell. The exit code is optional.

d8>quit()

~/Source/v8$ echo $?

0


d8> quit(12)
~/Source/v8$ echo $?
12
7. version() - Print the current version of the D8 shell.

d8> version()
3.7.1 (candidate)
d8> 

8. enableProfiler() / disableProfiler() - Consult official profiler documentation for more details.

POSIX Commands

Now if you are on a posix machine then you have access to the following additional commands. 
Note: The following descriptions have been adapted from the v8 source code comments.

1. os.system("program_name", ["arg1", "arg2", ...], timeout1, timeout2) - Run an external command, passing the arguments to the program.  The standard output  of the program will be picked up and returned as a multiline string.  If  timeout1 is present then it should be a number.  -1 indicates no timeout  and a positive number is used as a timeout in milliseconds that limits the  time spent waiting between receiving output characters from the program.  timeout2, if present, should be a number indicating the limit in  milliseconds on the total running time of the program.  Exceptions are  thrown on timeouts or other errors or if the exit status of the program  indicates an error.

d8> os.system("date")
Sun Oct 16 19:52:46 IST 2011


2. os.chdir(dir)- changes directory to the given directory.

d8> os.chdir("obj")
d8> os.system("pwd")
/home/xyz/Source/Cpp/v8/obj

d8> os.chdir("..")
d8> os.system("pwd")
/home/xyz/Source/Cpp/v8

3. os.setenv(variable, value) - sets an environment variable.  Repeated calls to this method leak memory due to the API of setenv in the standard C library. 

d8> os.setenv("myvar", "myvalue")
d8> os.system("printenv", ["myvar"])
myvalue

d8> 

4. os.unsetenv(variable) - unsets an environment variable.

d8> os.unsetenv("myvar")
d8> os.system("printenv", ["myvar"])
(d8):1: Child exited with status 1
os.system("printenv", ["myvar"])
   ^

d8> 

5. os.umask(value)-calls the umask system call and returns the old umask.



d8> oldmask = os.umask(0777)
18
d8> oldmask.toString(8)
22
d8>


6. os.mkdirp(name, mask) - creates a directory.  The mask (if present) is anded  with the current umask.  Intermediate directories are created if necessary.  An exception is not thrown if the directory already exists.  Analogous to  the "mkdir -p" command.



d8> os.mkdirp("abc")
d8> os.system("ls")
abc


7. rmdir(dir) - remove specified directory.



d8> os.rmdir("abc")
d8> 

No comments:

Post a Comment