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. spring @value 注入static

    import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Com ...

  2. Js 根据身份证号获取年龄-性别

    参考:http://www.tuicool.com/articles/J7r2ien 方式一: $scope.GetAgeAndSexByIDNum = function (IdCardNO) { / ...

  3. Oracle表空间(tablespaces)

    http://www.cnblogs.com/fnng/archive/2012/08/12/2634485.html 我们知道oarcle数据库真正存放数据的是数据文件(data files),Oa ...

  4. [系统开发] 基于Ansible的产品上线系统

    前言: 应部门急需,开发了一套基于Ansible Playbook的产品上线系统.由于时间很紧,UI直接套用了之前开发的一套perl cgi模板,后续计划用 django 重新编写. 个人感觉该系统的 ...

  5. SqlBulkCopy

    private static void DataTableToSQLServer( DataTable dt) { string connectionString = GetConnectionStr ...

  6. [Linux] 关于Centos6中ulimit nproc用户进程数的限制

    一.缘由: 在启动mongodb的时候,有Warning提示soft rlimits too low,就是用户使用进程数过小,遂调高系统资源关于用户最大进程数的限制ulimit -u. 先暂时使设置生 ...

  7. [C#对Oracle操作]C#操作调用Orcale存储过程有参数

    /// <summary> /// 获取ERP固定资产计提数据 /// </summary> /// <param name="strCompanyCode&q ...

  8. CentOS7:安装Zabbix

    参考:CentOS 7 yum安装Zabbix 1. 安装Zabbix Server EPEL源里面有Zabbix的安装包,所以需要先安装EPEL. Zabbix源也可以从这里获得:http://re ...

  9. html基础 2

    HTML 文本格式化实例 (我不知道为什么“正常显示文本”这几个字不用加标签,虽然它有在<body>标签内) <html> <body> <b>文本为黑 ...

  10. eclipse 引用项目(转)

    1. 项目右键 -->Properties 2. Java Build Path  a) Projects --> Add  b) Required Project Selection - ...