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面向对象的魔术方法.png(不间断更新)
- Linux Epoll相关知识
其实在Linux下设计并发网络程序,向来不缺少方法,比如典型的Apache模型(Process Per Connection,简称PPC),TPC(Thread PerConnection)模型,以及 ...
- matlab 车牌分割的算法
function Touying(g) f=rgb2gray(g); % [m n]=size(f); [row col]=size(f); % T=graythresh(f) % T=T*255 % ...
- hammerJs-v2.0.4详解
一.前言 移动端框架当前还处在初级阶段,但相对于移动端的应用来说已经有很长时间了.虽然暂时还没有PC端开发的需求量大,但移动端的Web必然是一种趋势,在接触移动端脚本的过程中,最开始想到的是juqer ...
- go 版本 gRPC 环境搭建(3.0正式版)
之前装过 gRPC 的各个测试版本,有些残余的文件,正式版的安装和之前残留的清除整理如下: 安装 go 版本的 gRPC go 的安装略过.需要 go 1.5 以上版本. $ go version ...
- [C#对Oracle操作]C#操作调用Orcale存储过程有参数
/// <summary> /// 获取ERP固定资产计提数据 /// </summary> /// <param name="strCompanyCode&q ...
- insertAdjacentHTML方法示例
添加HTML内容与文本内容以前用的是innerHTML与innerText方法,最近发现还有insertAdjacentHTML和insertAdjacentText方法,这两个方法更灵活,可以在指定 ...
- Norflash控制器的Verilog建模之一
摘要:今天驱动一款SPANSION公司生产的norflash——S29AL032D70,没有别的参考资料,大致了解一下norflash的内部cmos电路架构以及其用途之后,直接看手册吧. 如何看手册: ...
- C++设计模式-Memento备忘录模式
Memento模式作用:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样以后就可将该对象恢复到原先保存的状态. UML图: Originator:负责创建一个备忘录Me ...
- dsquery、netdom工具示例
C:\>netdom query fsmo架构主机 DC1.lypower.com.cn域命名主机 DC1.lypower.com.cnPDC ...