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.
随机推荐
- Angular.JS学习笔记——1
内容来自:http://www.runoob.com/angularjs/angularjs-intro.html AngularJS 是一个 JavaScript 框架.它是一个以 JavaScri ...
- 山东省选 郁闷的小J
小J是国家图书馆的一位图书管理员,他的工作是管理一个巨大的书架.虽然他很能吃苦耐劳,但是由于这个书架十分巨大,所以他的工作效率总是很低,以致他面临着被解雇的危险,这也正是他所郁闷的. 具体说来,书架由 ...
- web页面如何播放amr的音频文件
这个需求由来已久,公司的语音订单很多,每次客服都是从服务器down下语音来听.很不方便..于是我就上网扒拉看有么有什么web播放器能播放amr格式的音频文件,amr百度百科 总之找了很久.,,然后发现 ...
- 基础14_转义字符和特殊字符ASCII
一.摘要 PSQL转义字符 二.PLSQL转义字符 PLSQL对应的字符和序号关系 二.PLSQL特殊字符 PLSQL对应的字符和序号关系 1. 转义字符为' '; )||'%'; --A&B ...
- SearchBar 修改 cancel button 文字 及颜色
#pragma mark - - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{ _searchController.searchB ...
- flddler使用方法
http://blog.csdn.net/geekgjie/article/details/8029936
- linux内核调试技巧之addr2line
addr2line工具是一个可以将指令的地址和可执行影像转换为文件名,函数名和源代码行数的工具.这在内核执行过程中出现崩溃时,可用于快速定位出出错的位置,进而找出代码的bug. 用法 addr2lin ...
- OS X升级El Capitan后,git difftool无法打开diffmerge的解决方法
在git项目下执行git difftool,出现如下报错 /Library/Developer/CommandLineTools/usr/libexec/git-core/mergetools/dif ...
- jQuery 中对 CommonJs 的支持处理
jQuery 中对 CommonJs提供了直接支持,可以在 CommonJs模块中直接引用 jQuery 对象,这是如何实现的呢? 从 factory 函数说起 说先看 jQuery 的主体函数定义, ...
- SQL Server performance tips
Refer to: http://harriyott.com/2006/01/sql-server-performance-tips A colleague of mine has been look ...