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.

固然這只是個很是簡單的例子,當你之後須要作更多事情的時候就好象執行這個腳本字符串。更多的信息參考 嵌入指南 。


千里之行,始於足下。

但願多多指點,咱就會有質量的翻譯更多。

相關文章
相關標籤/搜索