Getting Started
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.
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:
- A 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 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.
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;
}
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.
- 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++ -Iinclude hello_world.cc -o hello_world out/x64.release/obj.target/tools/gyp/libv8_{base,snapshot}.a -lpthread - 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.
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.
随机推荐
- PHP入门part4
字符串函数 strlen(string); 获取字符串的长度,这里的长度是指该字符串的字节长度:!!utf-8里英文字母和符号占1个字节,中文是占3个字节. substr(string,number1 ...
- storm基础系列之五---------接入数据收集系统flume
1.基本结构介绍 flume是三层架构,agent,collector,storage.每一层都可水平扩展. 其中,agent就是数据采集方:collector是数据整合方:storage是各种数据落 ...
- OAF_开发系列03_实现OAF如何在保存前判断数据是否存在变更(案例)
2014-06-26 Created By BaoXinjian
- [AIR] 打开我的电脑
import flash.filesystem.File; import flash.desktop.NativeProcess; import flash.desktop.NativeProcess ...
- Git——1
集中式版本控制系统,版本库是集中存放在中央服务器的,而干活的时候,用的都是自己的电脑,所以要先从中央服务器取得最新的版本,然后开始干活,干完活了,再把自己的活推送给中央服务器.中央服务器就好比是一个图 ...
- 使用Javah 生成C/C++头文件
注意:编写java的接口文件. 注意native代码端一定不要有大括号,且要有“:”结尾. public native int add(int x ,int y); 1. 需要让eclipse自动编译 ...
- C# 6.0可能的新特性及C#发展历程
据扯,C# 6.0在不远的将来就发布了,对应的IDE可能是VS 2014(.Net Framework 5.0),因为VS 2013已于2013年10月份发布了,对应的是.Net Franework ...
- TCP\IP三次握手连接,四次握手断开分析
TCP(Transmission Control Protocol) 传输控制协议 TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: 位码即tcp标志位,有6种标 ...
- 安装mysql步骤
1.yum install lrzsz 安装上传功能2.查看系统是否装有MySql :rpm -qa | grep mysql3.查看系统Mysql位置 :whereis mysql4.卸载Mysq ...
- 软件调试——IA-32 保护模式下寄存器一览
最近在看张银奎先生的<调试软件>一书,想将关键的技术记录下来,以便日后查阅,也分享给想看之人吧. 1 通用寄存器 EAX,EBX,ECX,EDX:用于运算的通用寄存器,可以使用AX,BX等 ...