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.
随机推荐
- mac java 安装路径
google了一下,发现了这篇文章Important Java Directories on Mac OS X,可以使用工具命令"/usr/libexec/java_home"来定 ...
- Js 根据身份证号获取年龄-性别
参考:http://www.tuicool.com/articles/J7r2ien 方式一: $scope.GetAgeAndSexByIDNum = function (IdCardNO) { / ...
- Hadoop总结篇之四---底层通信是怎么做到的
上一篇介绍了一个job的提交过程.期间多次提到通信协议.那么协议是什么? 协议其实就是通信的双方所遵守的一套规范,这套规范规定了通信时传输的数据的固定的格式. 4.1 RPC协议:在hadoop中,我 ...
- zookeeper在集群负载均衡中的应用
zookeeper本身是不提供负载均衡的策略,需要自己来实现,所以这里确切的说,是在负载均衡中应用到了zookeeper做集群的协调. 对于HTTP请求的负载均衡,成熟的解决方案是Nginx(或Hap ...
- 无索引状态下比较DataTable的几种过滤方法效率
先构造一个DataTable: public DataTable GetDataTable() { DataTable dtTmp = new DataTable(); dtTmp.Columns.A ...
- 从click事件理解DOM事件流
事件流是用来解释页面上的不同元素接受一个事件的顺序,首先要明确两点: 1.一个事件的影响元素可能不止一个(同心圆理论),但目标元素只有一个. 2.如果这些元素都绑定了相同名称的事件函数,我们怎么知道这 ...
- Unity关于脚本前面的勾选框
今天做项目时需要在某个事件条件下禁用某个脚本,但是突然发现这个脚本前面没有勾选框,,,就像这样 网上搜了下,原来是需要在脚本中加上void Start()方法,即使这个方法里什么都没有 void St ...
- Round() 四舍五入 js银行家算法(转)
首先问一下round(0.825,2) 返回的结果,大家猜一猜, 首先SQL server 返回的是 0.83 js的返回结果 是0.83,code 如下: var b = 0.825; ...
- git 远程仓库 轻松创建
很多时候,为了方面管理我们写的代码,我们采用git 远程仓库来进行管理和备份.防止代码被他人篡改或删除.那如何来进行创建远程仓库呢? 1.我们必须有一个远程服务器端,在这里可以把任意一台电脑作为服务器 ...
- IIS启用兼容模式设置(win2k3—Win7)
点击添加按钮(上图),弹出下面的对话框(下图).在自定义HTTP头名处输入: X-UA-compatible 在自定义HTTP头值处输入: IE=EmulateIE7 (输入时注意不要留下空格)输入完 ...