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. Nginx限速遇到的问题

    公司使用的是Nginx做文件服务器,最近服务器流量增大,老板提出要给每个客户端进行限速. 在Nginx中进行限速配置: http { limit_zone one $binary_remote_add ...

  2. vi 颜色配置

    一.vi 默认黑色背景上,蓝色注释很难看不清,必须重新设置. (1)查看vi支持的配色方案: $ ls /usr/share/vim/vim72/colors (2)支持如下方案: blue.vim ...

  3. IOS WebView修改contentInset 导致webview长按弹出菜单跳动的解决方法

    最近在项目中需要用到webview 加载H5 并且在webview 底部使用原生UI添加其他空间比如广告.或者评论(Scrollview) 最初使用修改webview中scrollview 的cont ...

  4. ubuntu 彻底卸载软件

    找到此软件名称,然后sudo apt-get purge ......(点点为为程序名称),purge参数为彻底删除文件,然后sudo apt-get autoremove,sudo apt-get ...

  5. Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(分组背包+dsu)

    D. Arpa's weak amphitheater and Mehrdad's valuable Hoses Problem Description: Mehrdad wants to invit ...

  6. css3 flex盒子布局

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. jsLoader、cssLoader、imageLoader

    //js文件加载 function jsLoader(url,callback){ var script = document.createElement("script"); s ...

  8. jquery分页插件

    css代码: /*分页*/ .pageList { clear: both; overflow: hidden; } .pageList a, .pageList span { border: 1px ...

  9. Xilinx FPGA全局时钟和全局时钟资源的使用方法

    对FPGA的全局时钟了解不多,遂转载一篇文档: http://xilinx.eetop.cn/?action-viewnews-itemid-42 目前,大型设计一般推荐使用同步时序电路.同步时序电路 ...

  10. git将已经同步的某类文件加入忽略列表并同步

    1> 添加.gitignore文件到根目录,并在文件中写入忽略文件的类型或具体路径,比如: *.zip 和 /RSGIS/SnowEffect/SnowEffect.pro.user 2> ...