V8 Engine

V8 Javascript engine (Github), is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++. It is used in Chrome and in Node.js, among others. It implements ECMAScript and WebAssembly, and runs on Windows 7 or later, macOS 10.12+, and Linux systems that use x64, IA-32, ARM, or MIPS processors. V8 can run standalone, or can be embedded into any C++ application.

Advanced guide

The V8 API provides functions for compiling and executing scripts, accessing C++ methods and data structures, handling errors, and enabling security checks. Your application can use V8 just like any other C++ library. Your C++ code accesses V8 through the V8 API by including the header include/v8.h.

Handles and garbage collection

Contexts

Templates

A template is a blueprint for JavaScript functions and objects in a context. You can use a template to wrap C++ functions and data structures within JavaScript objects so that they can be manipulated by JavaScript scripts.

Accessors

Accessing dynamic variables

Interceptors

Security model

Exceptions

Inheritance

Guide to embedding V8

This document is intended for C++ programmers who want to embed the V8 JavaScript engine within a C++ application. It helps you to make your own application’s C++ objects and methods available to JavaScript, and to make JavaScript objects and functions available to your C++ application.

Follow the steps below to run the example yourself:

  1. Download the V8 source code by following the Git instructions.
  2. The instructions for this hello world example have last been tested with V8 v7.1.11. You can check out this branch with git checkout refs/tags/7.1.11 -b sample -t
  3. Create a build configuration using the helper script:
    tools/dev/v8gen.py x64.release.sample
    

    You can inspect and manually edit the build configuration by running:

    gn args out.gn/x64.release.sample
    
  4. Build the static library on a Linux 64 system:
    ninja -C out.gn/x64.release.sample v8_monolith
    
  5. Compile hello-world.cc, linking to the static library created in the build process. For example, on 64bit Linux using the GNU compiler:
    g++ -I. -Iinclude samples/hello-world.cc -o hello_world -lv8_monolith -Lout.gn/x64.release.sample/obj/ -pthread -std=c++0x -DV8_COMPRESS_POINTERS
    
  6. For more complex code, V8 fails without an ICU data file. Copy this file to where your binary is stored:
    cp out.gn/x64.release.sample/icudtl.dat .
    
  7. Run the hello_world executable file at the command line. e.g. On Linux, in the V8 directory, run:
    ./hello_world
    
  8. It prints Hello, World!. Yay!

If you are looking for an example which is in sync with master, check out the file hello-world.cc. This is a very simple example and you’ll likely want to do more than just execute scripts as strings.