Getting Started with Embedding
Pages 42
- Home
- API stability
- ARM Debugging
- Becoming a committer
- Blink layout tests
- Building from Source
- Building with GN
- Building with Gyp
- Checking out source
- Code of conduct
- Committer's responsibility
- Contributing
- Cpp Style Guide
- Cross compiling for ARM
- D8 on Android
- Debugging Protocol
- Design Elements
- Embedder's Guide
- Example code
- Experiments with Strengthening JavaScript
- Feature Launch Process
- GDB JIT Interface
- Getting Started with Embedding
- Handling of Ports
- i18n support
- Interpreter
- Introduction
- Merging & Patching
- Profiling Chromium with v8
- Release Process
- Reporting security bugs
- Runtime functions
- Stack Trace API
- Testing
- Tracing V8
- Triaging issues
- TurboFan
- Using D8
- Using Git
- Using V8’s internal profiler
- V8 Linux perf Integration
- V8 Profiler
- Show 27 more pages…
- What is V8?
- Introduction
- Building from Source (Start here)
- Contributing
- Automated test infrastructure
- Documentation
- Using D8
- Debugging
- Writing Optimizable JavaScript
- Embedding V8
- Experiments
- Under the Hood
Clone this wiki locally
This document introduces some key V8 concepts and provides a hello world example to get you started with V8 code.
Audience
This document is intended for C++ programmers who want to embed the V8 JavaScript engine within a C++ application.
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.
- 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.
- 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.
Run the Example
Follow the steps below to run the example yourself:
- Download the V8 source code and build V8 by following the download and build instructions.
- This hello world example is compatible with the version 4.8. You can check out this branch with
git checkout -b 4.8 -t branch-heads/4.8. - Build via
make x64.releaseon a Linux x64 system to generate the correct binaries.
- This hello world example is compatible with the version 4.8. You can check out this branch with
- Copy the complete code from the previous section (the second code snippet), paste it into your favorite text editor, and save as
hello_world.cppin the V8 directory that was created during your V8 build. -
Compile
hello_world.cpp, linking to the static libraries created in the build process. For example, on 64bit Linux using the GNU compiler:g++ -I. hello_world.cpp -o hello_world -Wl,--start-group out/x64.release/obj.target/{tools/gyp/libv8_{base,libbase,external_snapshot,libplatform},third_party/icu/libicu{uc,i18n,data}}.a -Wl,--end-group -lrt -ldl -pthread -std=c++0x - V8 requires its 'startup snapshot' to run. Copy the snapshot files to where your binary is stored:
cp out/x64.release/*.bin . - Run the
hello_worldexecutable file at the command line. For example, on Linux, still in the V8 directory, type the following at the command line:./hello_world - 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. If you are looking for an example which is in sync with master simply check out the file hello_world.cc.