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.cpp
in 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_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
- 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.
随机推荐
- HDU2896 病毒侵袭
题目大意:给出若干病毒的特征码,不超过500个.每个病毒的特征码长度在20~200之间.现在有若干网站的源代码,要检测网站的源代码中是否包含病毒.网站的个数不超过1000个,每个网站的源代码长度在70 ...
- Nginx -- Gzip 压缩功能作用
1.对应的压缩参数说明# 开启gzip压缩功能gzip on; # 设置允许压缩的页面最小字节数,页面字节数从header头的Content-Length中获取.默认值是0,不管页面多大都进行压缩,建 ...
- 使用 Linq 对多个对象进行join操作 C#
class A { public int id { get; set; } public string name { get; set; } } class B { public int id { g ...
- 动画--android图片点击放大动画,并遮挡旁边的控件
http://blog.csdn.net/s13488941815/article/details/40649823: 首先是点击放大可以使用android自带的缩放动画,因为要遮盖其他控件,就需要控 ...
- Wampserver2.5配置虚拟主机出现403 Forbidden的处理方案
WampServer是一款由法国人开发的Apache Web服务器.PHP解释器以 及MySQL数据库的整合软件包.免去了开发人员将时间花费在繁琐的配置环境过程,从而腾出更多精力去做开发.在windo ...
- 【原创】Linux常用管理命令总结
一.文件夹操作:1.查看文件夹ls [-al]/dir Diredtory_Name2.建立文件夹mkdir [-p] Diredtory_Name3.删除文件夹rm -r[f] Diredtory_ ...
- Partitioning & Archiving tables in SQL Server (Part 1: The basics)
Reference: http://blogs.msdn.com/b/felixmar/archive/2011/02/14/partitioning-amp-archiving-tables-in- ...
- Robotium怎样判断测试结果
Robotium判断测试结果的方法主要有三类:assert.is.search.assert方法除了Robotium API,还有Junit中的所有断言方法,Junit的断言方法下篇详解. void ...
- 世界国家 的数据库sql
, '中国', 'CHINA'); , '阿尔巴尼亚', 'ALB'); , '阿尔及利亚', 'DZA'); , '阿富汗', 'AFG'); , '阿根廷', 'ARG'); , '阿拉伯联合酋长 ...
- css3动画2(transform用法)
1.直接写在样式里,比如一个小箭头,transform:rotate(135deg)即可 2.写动画过程,@keyframes和transform和animation组合起来用 写在@keyframe ...