https://developers.google.com/v8/get_started

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.

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

  // Create a string containing the JavaScript source code.
  String source =String::New("'Hello' + ', World'");   // Compile the source code.
  Script script =Script::Compile(source);
 
  // Run the script to get the result.
  Value result = script->Run();   // Convert the result to an ASCII string and print it.
  String::AsciiValue ascii(result);
  printf("%s\n",*ascii);
  return0;
}

To actually run this example in V8, you also need to add handles, a handle scope, and a context:

  • 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.
  • 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.
  • 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.

The following example is the same as the one above, except now it includes handles, a context, and a scope - it also includes a namespace and the v8 header file:

#include<v8.h>

usingnamespace 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);   // Here's how you could create a Persistent handle to the context, if needed.
  Persistent<Context> persistent_context(isolate, context);
 
  // Enter the created 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::New("'Hello' + ', World!'");   // Compile the source code.
  Handle<Script> script =Script::Compile(source);
 
  // Run the script to get the result.
  Handle<Value> result = script->Run();
 
  // The persistent handle needs to be eventually disposed.
  persistent_context.Dispose();   // Convert the result to an ASCII string and print it.
  String::AsciiValue ascii(result);
  printf("%s\n",*ascii);
  return0;
}

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 out/x64.release/obj.target/tools/gyp/libv8_{base,snapshot}.a -lpthread
  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

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code samples are licensed under the Apache 2.0 License.

Last updated June 19, 2013.

随机推荐

  1. SOA 面向服务的体系结构

    SOA:面向服务的体系结构(service-oriented architecture) 是一个组件模型,它将应用程序的不同功能单元(称为服务)通过这些服务之间定义良好的接口和契约联系起来. 接口是采 ...

  2. linux下关于svn提交的时候强制写注释

    在svn版本库的hooks文件夹下面,复制模版pre-commit.tmpl cp pre-commit.tmpl pre-commit chmod 777 pre-commit 1 2 1 2 na ...

  3. 飘逸的python - 增强的格式化字符串format函数

    自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱. 语法 它通过{}和 ...

  4. LSOF 安装与使用

    linux上安装: tar zxvf lsof_4.76.tar.gz cd lsof_4.76 ls 00.README.FIRST_4.76 lsof_4.76_src.tar.gz README ...

  5. [转]-CSS 元素垂直居中的6种方法

    原文地址:http://blog.zhourunsheng.com/2012/03/css-%E5%85%83%E7%B4%A0%E5%9E%82%E7%9B%B4%E5%B1%85%E4%B8%AD ...

  6. Windows系统

    1. 更改XP登录界面 怎样启用XP的经典登录界面 第一步:用管理员账号登录系统. 第二步:运行gpedit.msc启动组策略编辑器,找到"计算机配置"--"管理模板&q ...

  7. 跨平台编程:关于VS和QT那些事

    1.Win平台 Qt5.7 for Win32 (VS2013) 编辑器:Qt Creator 4.0 编译器:MSVC12 for X86 (cl.exe&link.exe) 调试器:CDB ...

  8. [手机取证] CelleBrite UFED Touch系列使用视频

    CelleBrite UFED Touch系列产品使用指导视频 (持续更新中) 1. CelleBrite UFED Touch产品介绍

  9. java 项目创建中的问题汇总

    eclipse 创建maven项目时 报错 Unable to create project from archetype [org.apache.maven.archetypes:ma 你看看你ma ...

  10. 一段比较有意思的代码——介绍system verilog中的新增幅值语句

    system verilog中新加了很多幅值语句,虽然都只适用于阻塞幅值,但是在某些场合中非常实用. 下面是一段有意思的代码,覆盖了一些用法. package definitions; typedef ...