Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Getting Started with Embedding
This document introduces some key V8 concepts and provides a
hello world example to get you started with V8 code.
First, some key concepts you will need:
- An isolate is a VM instance with its own heap.
- A local 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 handle 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.
These concepts are discussed in greater detail in the Embedder's Guide.
Run the Example
Follow the steps below to run the example yourself:
- Download the V8 source code by following the git instructions.
- The instructions for this hello world example have last been tested with V8 7.1.11. You can check out this branch with
git checkout refs/tags/7.1.11 -b sample -t
- Create a build configuration using the helper script:
You can inspect and manually edit the build configuration by running
gn args out.gn/x64.release.sample
- Build the static library on a Linux 64 system:
ninja -C out.gn/x64.release.sample v8_monolith
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
- For more complex code, V8 will fail without an ICU data file. Copy this file to where your binary is stored:
cp out.gn/x64.release.sample/icudtl.dat .
- Run the
hello_worldexecutable file at the command line. e.g. On Linux, in the V8 directory, run:
- You will see
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. If you are looking for an example which is in sync with master simply check out the file