<!DOCTYPE html>
<html>
<head>
  
  <title>Exploring the V8 JS engine (Part 1 of 2)</title>
  <link rel="alternate" type="application/rss+xml" title="RSS feed" href="/rss.xml">
  <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Yanone+Kaffeesatz:400,700">
  <link rel="stylesheet" href="/public/css/style.css">
  
</head>
<body>
  <article itemscope itemtype="http://schema.org/BlogPosting">
    <header>
      <aside itemprop="datePublished">
        January 07, 2012
      </aside><a href='/' rel='author'>Joseph Adams</a>
    </header>
    <h1 itemprop="name">Exploring the V8 JS engine (Part 1 of 2)</h1>
    
    
    
    <div itemprop="articleBody">
      <p>This is the first part of a 2 part series giving a simple technical overview of the V8 Javascript engine.</p>
      <p>First of all some basic facts:</p>
      <ul>
        <li>Developed and maintained by Google</li>
        <li>Javascript engine behind Google Chrome</li>
        <li>Also powers Node.js?</li>
        <li>Really Fast!</li>
      </ul>
      <p>V8 is written in C++ so you should have basic understanding of OOP and some C/C++ knowledge wouldn't do bad too.</p>
      <p>The engine (V8) executes Javascript in so called <code>contexts</code>, these are sandboxed and you can create multiple contexts in one V8 virtual machine (engine).</p>
      <p>This has the advantage that if you run 2 (or more) Javascript programs you don't have to worry about namespacing. The creation of these contexts is not as memory hungry as you may think, so don't worry about that.</p>
      <p>One of the great features of V8 is sharing C++ functions, objects and variables with Javascript, which we will cover in the second part of this series. To use V8 you write a C++ program that uses the V8 libs (to set up the contexts, scopes, templates) and then executes a string which is your Javascript program. You will understand how this works later in this post.</p>
      <p>I'll take you through setting up the V8 lib and a simple <code>Hello World!</code> program.</p>
      <h2>Downloading and Building</h2>
      <p>I'm using a Mac to build the library, but .</p>
      <p>To build V8 you will need:</p>
      <ul>
        <li>Subversion 1.4 or higher</li>
        <li>Python 2.4 or higher</li>
        <li>SCons 1.0.0 or higher</li>
        <li>GNU Compiler (GCC) 4.x.x</li>
      </ul>
      <p>To download the source type this into you terminal:</p>
      <pre><code>
$ mkdir ~/dev/
$ cd ~/dev/
$ svn checkout http://v8.googlecode.com/svn/trunk/ v8-read-only
$ mv v8-read-only v8
</code></pre>
      <p>Now you have the source you can build the V8 library and header file:</p>
      <pre><code>
$ scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
...
</code></pre>
      <p>Simple isn't it? Now once the build has finished you should be able to see the <code>libv8.a</code> file. This file is important for compiling the C++ program that uses the V8 library.</p>
      <h2>Your first V8 program</h2>
      <p>Now that you have all the parts needed for compiling a C++ program that uses the V8 classes, let's get to the interesting part.</p>
      <p>Here is a simple C++ program that runs the Javascript: <code>"Hello World!"</code>. Obviously, this is not a very spectacular Javascript program, but it should do to our needs. The comments explain what a each part does:</p>
      <pre><code>
#include 

using namespace v8;

int main(int argc, char* argv[]) {

  // Create a new context.
  Persistent&lt;Context&gt; context = Context::New();

  // Enter the created context for compiling and
  // running the hello world program.
  context-&gt;Enter();

  // Create a stack-allocated handle scope.
  HandleScope handle_scope;

  // Create a string containing the JavaScript code
  // to execute (notice the quotation).
  Handle&lt;String&gt; source = String::New(" 'Hello World!'; ");

  // Compile the Javascript code.
  Handle&lt;Script&gt; script = Script::Compile(source);

  // Run the script to get the result.
  Handle&lt;Value&gt; result = script-&gt;Run();

  // Get rid of the persistent context.
  context.Dispose();

  // Convert the result to an ASCII string and print it.
  String::AsciiValue ascii(result);
  printf("%s\n", *ascii);

  return 0;
}
</code></pre>
      <p>Save that in a file called hello_world.cc in the <code>~/dev/v8/</code> directory.</p>
      <p>Next you need to compile the <code>hello_world.cc</code> file by doing:</p>
      <pre><code>
$ g++ -m32 -Iinclude libv8.a -lpthread hello_world.cc -o hello_world
</code></pre>
      <p>This compiles the hello world program to a 32-bit executable that includes the <code>libv8.a</code> library.</p>
      <p>If the compilation was successful you should have a new file called <code>hello_world</code> in your V8 directory. Then you execute the file and it should print out "Hello World!" (without the quotes):</p>
      <pre><code>
$ ./hello_world
Hello World!
</code></pre>
      <p>Congratulations you have just run your first program that uses V8.</p>
      <p>In the next part of this series we'll look into sharing variables and objects and dig into the source of Node.js.</p>
    </div>
    <footer>
      <aside itemprop="datePublished">
        January 07, 2012
      </aside><a href='/' rel='author'>Joseph Adams</a>
    </footer>
  </article>
  
</body>
</html>
