Getting Started(v8 hello world)
来源:互联网 发布:淘宝xbox360手柄一百多 编辑:程序博客网 时间:2024/05/21 22:40
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
- Hello World
- Run the Hello World example
Audience
This document is intended for C++ programmers who want to embed the V8 JavaScript engine within a C++ application.
Back to top
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); return 0;}
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>using namespace 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); return 0;}
Back to top
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.cpp
in 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_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
- 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.
Back to top
- Getting Started(v8 hello world)
- go, Getting Started, Hello world
- v8学习---Hello world
- Getting Started with 64-bit ARM Development: Hello World and Linux on ARMv8 Fast Models
- Dojo -- Getting Started篇之Hello Dojo!
- Getting started
- Getting Started
- Getting Started
- Getting Started
- Getting Started
- getting started
- Getting Started
- Getting Started
- Getting Started
- Getting Started
- V8 JavaScript Engine 入门指南 2 -- Hello World
- Resin Getting Started
- Getting Started with Smartphone
- TestNG·三 测试方法
- ArcEngine c# 创建shp图层,并将要素写入图层保存
- TestNG·四 测试方法之工厂
- TestNG·五 运行TestNG
- This application has not been initialized with an ESRI license and will not function correctly 解决办法
- Getting Started(v8 hello world)
- TestNG·六 测试结果
- TestNG·七 annotation
- TestNG·八 并发测试
- Google V8编程详解(一)V8的编译安装(Ubuntu)
- Google V8编程详解(二)HelloWorld
- Java读写Excel
- Hibernate复习笔记
- 黄聪:wordpress调用函数大全