Running V8 Javascript Engine Standalone
Asked Answered
A

10

132

I want to run a Javascript console on top of V8. How do I do this?

Ar answered 26/11, 2009 at 9:10 Comment(0)
N
121

V8 is easy to build and does not come with the Java VM overhead from Mozilla's standalone Javascript interpreter. Luckily, V8 ships with code for building a console. Here is how to build this:

$> svn co http://v8.googlecode.com/svn/trunk v8-trunk
...
$> cd v8-trunk
$> scons
$> g++ ./samples/shell.cc -o v8-shell -I include libv8.a 

Now, we have a standalone binary called v8-shell.

Running the console:

$> ./v8-shell 
V8 version 2.0.2
> var x = 10;
> x
10
> function foo(x) { return x * x; }
> foo
function foo(x) { return x * x; }
> quit()

Executing Javascript from the command line:

$> ./v8-shell -e 'print("10*10 = " + 10*10)'
10*10 = 100

Many more features are documented in the help:

$> ./v8-shell --help
Usage:
...
Neurologist answered 26/11, 2009 at 9:10 Comment(8)
Thanks for the tip and explicit instructions. I had to add the option -lpthread to the g++ command under ubuntu 10.04.Givens
scons failed for me on Ubuntu, but running sudo apt-get install libc6-dev-i386 solved that.Kobold
and if your on x86_64 do a: 'scons arch=x64' until its fixed in trunk code.google.com/p/v8/issues/detail?id=429#c1Karinakarine
Or pass the -arch i386 to g++.Mouser
Please note that this v8-shell is intended to be a toy example. The "real" v8 shell is called d8. See my answer for more info.Bundy
$> g++ ./samples/shell.cc -o -lpthread v8-shell -I include libv8.a use above if you are using ubuntu 10.04..enjoy!!!Losing
I got $ scons scons: *** No SConstruct file found. File "/usr/local/Cellar/scons/2.3.4/libexec/scons-local/SCons/Script/Main.py", line 920, in _main when doing this. Any idea what I'm doing wrong?Hothouse
The method for fetching the source code seems changed. See here.Pimentel
B
61

To build the developer console, rather than the example 'shell' toy application, copy-paste the below commands to your terminal.

sudo apt-get install subversion scons libreadline-dev
svn co http://v8.googlecode.com/svn/trunk v8
cd v8/
scons console=readline d8

These instruction will work for Ubuntu/Debian with a "generic" kernel. For other distributions, you will need to replace the apt-get command with whatever package tool you have available. On 64-bit systems you may need to add arch=x64. The console=readline option enables the readline system, to make it feel a bit more like a standard shell.

More complete documentation here: http://code.google.com/apis/v8/build.html


Note:

enter image description here

See also: Building v8 with GYP

Bundy answered 26/11, 2009 at 9:10 Comment(4)
I had to add arch=x64 to build on 64bit ubuntu.Lamppost
@Hugh: It enables the readline system; this is what allows you to use the up arrow to get the previous command, among other niceties.Bundy
Once building has finished, run the shell by entering ./d8.Dobb
on ubunt 12.04: fatal error: readline/readline.h: No such file or directory - any ideas?Trajectory
S
44

How about running V8 Javascript via command line using node.js?

node.js uses v8 as it's engine and adds a lot of functionality on top of it.


For example on Mac OSX if you have Homebrew installed, simply issue:

    $ brew install node
    $ node
    > 
Somerville answered 26/11, 2009 at 9:10 Comment(1)
IMHO node.js is the new defacto standard when it comes to JavaScript consoles. It uses v8, and is probably the best option to use for this.Myrilla
M
24

On Mac OS X be sure to have brew installed. Then just run the command (sudo) brew install v8, depending on your machine this may take some time. To start the V8 console, just run v8 - Voilà!

Tip: To quit the console, just run quit() and don't forget the parentheses!

Mllly answered 26/11, 2009 at 9:10 Comment(4)
Or just press Ctrl-D to quit :)Statist
Or Ctrl-C to kill itGodevil
You can build v8 by itself and it's simple enough, using brew is just way too cumbersome.Barvick
How is 'brew install v8' cumbersome?Tourneur
C
6

I think this might have changed. I read the manual and build v8 like this:

moose@pc08$ svn co http://v8.googlecode.com/svn/trunk v8-trunk
moose@pc08$ cd v8-trunk
moose@pc08$ make dependencies
moose@pc08$ make ia32.release

added export PATH=${PATH}:/home/moose/Downloads/v8-trunk/out/ia32.release to my .bashrc

moose@pc08 ~ $ source ~/.bashrc
moose@pc08 ~ $ d8 A_tic_tac_toe_Tomek.js < A-small-practice.in

(With javascript from aditsu and A-small-practice.in from Google Code Jam)

Compost answered 26/11, 2009 at 9:10 Comment(1)
A page full of responses and yours, the least regarded, contains the final advice I needed.Generality
K
5

After following the build instructions (Google's V8 Build Docs) for your system;

[v8 directory]$ cd out/native
[v8 directory]$ ./shell (sample shell)
[v8 directory]$ ./d8 (console: dumb)

I created an alias in my .bash_profile to facilitate invocation of the shell.

alias v8='/Volumes/Dev/GitHub/v8/out/native/shell'

Typing v8 at the CLI (in a new Terminal or shell -- to reload your bash profile) yields the v8 shell. JavaScript at the command prompt! :)

Katherinakatherine answered 26/11, 2009 at 9:10 Comment(0)
N
4

In case you would like to run your javascript source code using the v8 engine or any version of it, you may utilize the jsvu command-line tool. It is developed and maintained by Google engineers and, besides, it offers the feature of installing other javascript engines apart from v8, such as spidermonkey, chakracore, javascriptcore, and xs.

Newbold answered 26/11, 2009 at 9:10 Comment(0)
P
4

If you use ArchLinux, you can use pacman -S v8 to install it. Then use d8 to start it in your shell. Enjoy it.

Piceous answered 26/11, 2009 at 9:10 Comment(1)
That appears to be on the AUR so cannot be installed with pacman.Pimentel
J
2

If you're planning to embed V8, then by all means build it and play with "d8".

If on the other hand, you do not plan to extend V8 or treat it as optional, then just use Node.JS. Don't bother with pure V8.

Node.js has truly rich I/O, extensions, libraries (like Perl CPAN, Python Eggs, Ruby Gems), and community.

Joeannjoed answered 26/11, 2009 at 9:10 Comment(1)
You don't need to use node.js, it's just a wrapper and extra dependency.Barvick
M
0

If you are on Windows:

  1. Install MSYS2
  2. Open MSYS2 terminal from start menu.
  3. Install your compiler: pacman -Syu mingw-w64-i686-toolchain
  4. Install v8: mingw-w64-i686-v8
  5. Verify that you have d8 as a new interpreter in your path.
  6. If you want to run d8 outside MSYS2, you have to add msys2/mingw/bin to the windows path
Test it
  1. Navigate to c:\msys2\home\user\
  2. Create a test.js file
console.log('Hello You!');
console.log('Would you tell me your name?');
const name = readline();
console.log('Hello '+name+' !!');
  1. Run: d8 test.js

You can also download binaries from here and unzip with peazip.

Good luck !!

Minstrel answered 26/11, 2009 at 9:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.