Chrome V8

Getting Started

This document introduces some key V8 concepts and provides a hello world example to get you started with V8 code.

Contents

Audience

This document is intended for C++ programmers who want to embed the V8 JavaScript engine within a C++ application.

Back to top

Hello World

Let's look at a Hello World example that takes a JavaScript statement as a string argument, executes it as JavaScript code, and prints the result to standard out.

#include <v8.h>

using namespace v8;

int main(int argc, char* argv[]) {
  // Get the default Isolate created at startup.
  Isolate* isolate = Isolate::GetCurrent();

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

  // Create a new context.
  Handle<Context> context = Context::New(isolate);

  // Enter the context for compiling and running the hello world script.
  Context::Scope context_scope(context);

  // Create a string containing the JavaScript source code.
  Handle<String> source = String::NewFromUtf8(isolate, "'Hello' + ', World!'");
  
  // Compile the source code.
  Handle<Script> script = Script::Compile(source);
  
  // Run the script to get the result.
  Handle<Value> result = script->Run();
  
  // Convert the result to an UTF8 string and print it.
  String::Utf8Value utf8(result);
  printf("%s\n", *utf8);
  return 0;
}
  • An isolate is a VM instance with its own heap.
  • A handle is a pointer to an object. All V8 objects are accessed using handles, they are necessary because of the way the V8 garbage collector works.
  • A scope can be thought of as a container for any number of handles. When you've finished with your handles, instead of deleting each one individually you can simply delete their scope.
  • A context is an execution environment that allows separate, unrelated, JavaScript code to run in a single instance of V8. You must explicitly specify the context in which you want any JavaScript code to be run.

These concepts are discussed in greater detail in the Embedder's Guide.

Back to top

Run the Example

Follow the steps below to run the example yourself:

  1. Download the V8 source code and build V8 by following the download and build instructions.
  2. Copy the complete code from the previous section (the second code snippet), paste it into your favorite text editor, and save as hello_world.cpp in the V8 directory that was created during your V8 build.
  3. Compile hello_world.cpp, linking to the static libraries created in the build process. For example, on 64bit Linux using the GNU compiler:
    g++ -Iinclude hello_world.cc -o hello_world -Wl,--start-group out/x64.release/obj.target/{tools/gyp/libv8_{base.x64,snapshot},third_party/icu/libicu{uc,i18n,data}}.a -Wl,--end-group -lrt
  4. Run the hello_world executable file at the command line.
    For example, on Linux, still in the V8 directory, type the following at the command line:
    ./hello_world
  5. You will see Hello, World!.

Of course this is a very simple example and it's likely you'll want to do more than just execute scripts as strings! For more information see the Embedder's Guide.

Back to top

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.