https://developers.google.com/v8/intro
本文內容
- 介紹
- 關於 V8 引擎
- V8 引擎入門
- 參考資料
介紹
V8 是 Google 開源的、高性能的 JavaScript 引擎。V8 是由 C++ 編寫,並用在 Google 開源瀏覽器 Chrome 中。
Google 的 V8 項目旨在幫助那些 C ++ 開發者在他們的應用程序中使用 V8,以及對 V8 的設計和性能感興趣的人。本文及其后的文章將介紹 V8,和如何在你的代碼中使用 V8,並提供一套 JavaScript benchmarks 來測量 V8 的性能。
關於 V8 引擎
V8 實現了 ECMA-262 第 5 版描述的 ECMAScript,可運行在 Windows(XP 或更高)、Mac OS X(10.5 或更高)和使用 IA-32、x64 或 ARM 處理器的 Linux 系統。
V8 編譯和執行 JavaScript 源代碼,處理對象內存分配,對不再使用的對象進行回收。V8 的垃圾回收(Google 自己說是“能停止世界、新一代、准確的垃圾回收”)是其性能的關鍵。其他的性能方面,如 V8 設計元素(V8 Design Elements)。
JavaScript 是瀏覽器中最常用的客戶端腳本,如用來操作 DOM(Document Object Model,文檔對象模型)。但是,DOM 通常由 JavaScript 提供,而不是瀏覽器。V8—Google Chrome 就是這樣。但是,V8 提供所有的數據類型、操作符、對象和 ECMA 標准規定的函數。
V8 可以使任何 C++ 應用程序向 JavaScript 代碼公開自己的對象和函數。由你決定向 JavaScript 公開你希望的對象和函數。在這方面,有很多應用程序都這么做,如,Adobe Flash 和蘋果 Mac OS X 中 Dashboard 部件和和雅虎部件。
V8 引擎入門
首先,需要下載 V8 源代碼並生成 V8。之后,演示 V8 代碼的 "Hello World" 示例。在演示 "Hello World" 示例時,介紹一些關鍵的 V8 概念。
- Hello World 示例
- 運行 Hello World 示例
Hello World 示例
把一個 JavaScript 語句作為一個字符串參數,作為 JavaScript 代碼執行,並輸出結果。但是下面代碼不能執行,因為它還缺少 V8 必要的部分。
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);
return 0;
}
若真正在 V8 中運行該示例,還需要添加句柄(handles)、作用域(handle scope)和上下文環境(context):
- 一個句柄是一個指向對象的指針。所有 V8 對象都是通過句柄訪問,這樣 V8 垃圾回收才能起作用。
- 一個作用域可以看做任意數量句柄的容器。這樣,當使用完句柄后,不用單獨刪除每個,而是簡單刪除作用域。
- 一個上下文環境一個執行環境,允許單獨的、不相關的 JavaScript 代碼運行在 V8 單個實例。必須在你要執行的 JavaScript 代碼中顯示指定上下文環境。
這些概念會在 嵌入式指南 中更多描述。
下面代碼同上邊一樣,但包含了句柄、作用域和上下文環境。另外,也包含了命名空間和 V8 頭文件:
#include <v8.h>
using namespace v8;
int main(int argc, char* argv[]) {
// Create a stack-allocated handle scope.
HandleScope handle_scope;
// Create a new context.
Persistent<Context> context = Context::New();
// 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();
// Dispose the persistent context.
context.Dispose();
// Convert the result to an ASCII string and print it.
String::AsciiValue ascii(result);
printf("%s\n", *ascii);
return 0;
}
運行 Hello World 示例
你可以按如下步驟運行示例:
- 下載 V8 源代碼,並按指示生成 V8。
- 復制上面的代碼,並另存為 hello_world.cpp 到 V8 生成的目錄。
- 編譯 hello_world.cpp,鏈接到生成期間創建的靜態庫。例如,在 64 位 Linux,使用 GNU 編譯器:
g++ -Iinclude hello_world.cc -o hello_world out/x64.release/obj.target/tools/gyp/libv8_{base,snapshot}.a -lpthread
- 命令行執行 hello_world 可執行文件。例如,在 Linux,當前目錄為 V8 目錄,輸入下面命令:
./hello_world
- 你會看到終端輸出 Hello, World!
這僅僅是一個簡單的例子,你可能想執行更多腳本。
參考資料
- JavaScript 引擎——Visual Studio 環境生成 V8 引擎
- V8 項目 https://developers.google.com/v8/build
- Chrom 項目 http://dev.chromium.org/
- 嵌入式指南 https://developers.google.com/v8/embed
- KasperLund-V8 PPT http://files.cnblogs.com/liuning8023/KasperLund-V8.rar
下載 Hello World Demo - Hello World 程序
下載 Demo - 簡單執行 JavaScript 語句。包括獲得 JSON 中的數據,字符串長度和字符串所有字符大寫。
(該 Demo 要想運行起來,說容易,也不容易。在 VS 里將 V8 的包含文件和庫追加到項目的包含目錄和庫目錄設置里。)