Getting Started 新手导读
This document introduces some key V8 concepts and provides a hello world example to get you started with V8 code.
这篇文章介绍一些关于V8的概念的关键,以及一个小例子 “Hello World” 来帮助你使用V8的代码。
Contents 目录
阅读对象
运行例子
Audience 阅读对象
This document is intended for C++ programmers who want to embed the V8 JavaScript engine within a C++ application.
这篇文档是面向那些想要嵌入V8 JS引擎到其C++程序中的C++程序员。
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.
让我们来看看在Hello World例子中V8是如何将JS语句变成一个String参数,执行JS代码,并且将结果打印到标准输出中。
/*---------------------------------------------------------------*/
int main(int argc, char* argv[]) {
// Create a string containing the JavaScript source code.
//创建一个包含JS代码的String
String source= String::New("'Hello' + ', World'");
// Compile the source code.
//编译JS源码
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.
//转换结果到ASCII字符类型并打印它
String::AsciiValue ascii(result);
printf("%s\n", *ascii);
return 0;
}
/*---------------------------------------------------------------*/
To actually run this example in V8, you also need to add handles, a handle scope, and a context:
当实际在V8中运行这个例子,你也需要去添加一个 Handle (控制/句柄/或是锅把?:-) ),一个Handle Scope (控制范围) 以及一个 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.
Handle是一个对象的指针,所有V8对象都要链接到Handle,这是必需的,因为这是V8的垃圾回收工作方式。
· 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.
Scope 可以看作是一个包含了众多Handle的容器,当你用完你的Handle,你不需要费功夫去删除(译注:肯能指C++中的内存释放工作)每一个Handle,取而代之的是你仅仅只需要删除它们的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.
Context是一个执行环境,它允许JS代码运行在一个单独的,相互间不相关的V8单一实例中。你必须明确指定JS代码运行在哪一个Context里。
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:
下一个例子与上面相同,不过要利用Handle,Context以及Scope,并且还需要引用命名空间和调用V8头文件。
/*---------------------------------------------------------------*/
#include <v8.h>
using namespace v8;
int main(int argc, char* argv[]) {
// Create a stack-allocated handle scope.
//创建Handle的堆栈分配空间
HandleScope handle_scope;
// Create a new context.
//创建一个新的Context
Persistent<Context> context= Context::New();
// Enter the created context for compiling and
// running the hello world script.
//进入创建勇于编译和运行Hello World脚本的Context。
Context::Scope context_scope(context);
// Create a string containing the JavaScript source code.
//创建包含JS代码的字符串
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();
// Dispose the persistent context.
//销毁包含Context的persistent
context.Dispose();
// Convert the result to an ASCII string and print it.
//转换结果到ASCII字符串且打印
String::AsciiValue ascii(result);
printf("%s\n", *ascii);
return 0;
}
/*---------------------------------------------------------------*/
Handles, garbage collection, contexts, and scopes are discussed in greater detail in the Embedder's Guide.
讨论Handle,垃圾回收,Context和Scope的详细介绍在 嵌入指南 中。
Follow the steps below to run the example yourself:
跟随以下的步骤来运行你的例子。
1 Download the V8 source code and build V8 by following the download and build instructions.
下载V8的源码以及构建V8可以参考下载与构建手册.
2 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.
从前面的文章复制完整的代码到你喜欢的编辑器,并且保存为 hello_world.cpp 到V8 文件夹下,程序会利用你构建的V8来创建。
3 Compile hello_world.cpp, linking to the libv8.a library created in the build process.
For example, on Linux using the GNU compiler:
g++ -Iinclude hello_world.cpp -o hello_world libv8.a -lpthread
编译hello_world.cpp,链接到libv8.a库到构建流程。比如:在Linux下使用GNU编译器:
g++ -Iinclude hello_world.cpp -o hello_world libv8.a -lpthread
4 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
在命令行下运行hello_world的可执行文件。
比如在Linux 下,保持在V8目录中,输入以下命令:
./hello_world
5 You will see Hello, World!.
之后你会看到,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.
当然这只是个非常简单的例子,当你以后需要做更多事情的时候就好象执行这个脚本字符串。更多的信息参考 嵌入指南 。
千里之行,始于足下。
希望多多指点,咱就会有质量的翻译更多。
来源:oschina
链接:https://my.oschina.net/u/130138/blog/14812