V8 - 2 - Getting Started 新手导读

Getting Started 新手导读html

 

This document introduces some key V8 concepts and provides a hello world example to get you started with V8 code.程序员

这篇文章介绍一些关于V8的概念的关键,以及一个小例子 “Hello World” 来帮助你使用V8的代码。api

Contents 目录app

阅读对象编辑器

Hello Worldide

运行例子ui

Audience 阅读对象this

This document is intended for C++ programmers who want to embed the V8 JavaScript engine within a C++ application.google

这篇文档是面向那些想要嵌入V8 JS引擎到其C++程序中的C++程序员。spa

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.

让咱们来看看在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 (上下文/环境)

· 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的垃圾回收工做方式。

· 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

· 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:

下一个例子与上面相同,不过要利用HandleContext以及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.

  //销毁包含Contextpersistent
  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,垃圾回收,ContextScope的详细介绍在 嵌入指南 中。

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.

下载V8的源码以及构建V8能够参考下载与构建手册.

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


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


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.

固然这只是个很是简单的例子,当你之后须要作更多事情的时候就好象执行这个脚本字符串。更多的信息参考 嵌入指南 。


千里之行,始于足下。

但愿多多指点,咱就会有质量的翻译更多。

相关文章
相关标签/搜索