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. NVMe over Fabrics:概念、应用和实现

    对于大部分人来说,NVMe over Fabrics(简称NVMf)还是个新东西,因为其第一个正式版本的协议在今年6月份才发布.但是这并不影响人们对NVMf的关注,因为这项依托于NVMe的技术很可能继 ...

  2. 02shell编程环境的搭建

    02shell编程环境的搭建 [02]Shell编程 02shell编程环境的搭建 在不同的操作系统上搭建shell编程环境 Linux Windows Mac 编辑器的选择 系统环境的搭建 注: 选 ...

  3. Windows 查看端口占用和关闭进程

    支持原创地址 :http://www.cnblogs.com/moodlxs/p/4145384.html 开始--运行--cmd 进入命令提示符 输入netstat -ano 即可看到所有连接的PI ...

  4. C#动手实践:Kinect V2 开发(1):初步了解及环境搭建

    该分享使用的是Kinect 二代,C#做为编程语言,请知悉 Kinect是微软在2009年6月2日的E3游戏展上,正式公布的XBOX360体感周边外设.它是一种3D体感摄影机,同时它导入了即时动态捕捉 ...

  5. [Maven]Maven详解

    转自:http://www.cnblogs.com/hongwz/p/5456578.html 一.前言     以前做过的项目中,没有真正的使用过Maven,只知道其名声很大,其作用是用来管理jar ...

  6. Netty服务端与客户端(源码一)

    首先,整理NIO进行服务端开发的步骤: (1)创建ServerSocketChannel,配置它为非阻塞模式. (2)绑定监听,配置TCP参数,backlog的大小. (3)创建一个独立的I/O线程, ...

  7. Spring IoC容器总结(未完)

    在面向对象系统中,对象封装了数据和对数据的处理,对象的依赖关系常常体现在对数据和方法的依赖上.这些依赖关系可以通过把对象的依赖注入交给框架或IOC容器来完成,这种从具体对象手中交出控制的做法是非常有价 ...

  8. javascript slice

    定义和用法 slice() 方法可提取字符串的某个部分,并以新的字符串返回被提取的部分. 语法 stringObject.slice(start,end) 参数 描述 start 要抽取的片断的起始下 ...

  9. XUtils 3 使用

    源代码:https://github.com/wyouflf/xUtils 基本使用:http://blog.csdn.net/abc6368765/article/details/50699334 ...

  10. 2015-06-02 关于mvc表格点击按钮自动添加一行<tr></tr>

    前台代码: @using (Html.BeginForm("ContactPerson", "User", FormMethod.Post, new { @cl ...