Getting Started
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
This document is intended for C++ programmers who want to embed the V8 JavaScript engine within a C++ application.
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);
return0;
}
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>
- usingnamespace 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);
return0;
}
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.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code samples are licensed under the Apache 2.0 License.
随机推荐
- Oracle补习班第七天
Keep conscience clear, then never fear. 问心无愧,永无畏惧 服务端监听 lsnrctl status #查看监听服务 netca #配置动态监听 lsnrct ...
- Hololens开发笔记之连接PC实现资源共享
官网原文介绍:https://developer.microsoft.com/en-us/windows/holographic/using_the_windows_device_portal Hol ...
- JavaScript的学习2
1. a.字符串运算符 符号 功能 + 字符串连接 += 将左边的值加上右边的值然后再赋值给左边的变量 b.比较运算符 运算符 含义 说明 > 大于 M>N,当M大于N时,返回TRUE ...
- Android sqlite数据库自定义存放路径办法参考(未验证)
public class TestDB extends SQLiteOpenHelper { private static final String DATABASE_NAME = "use ...
- 【解决】AWS服务控制台中上传文件失败
使用IE 11,在 AWS Services Console 中不管是 S3 还是 Elastic Beanstalk 的页面中上传页面都会失败,提示信息如下: A problem occurred ...
- How to pronounce symbols on keyboard
Refefrence: http://answers.yahoo.com/question/index?qid=20100607151104AAtQxhc ~ “tilde” or “tweedle” ...
- oracle ebs应用产品安全性-交叉验证规则
转自: http://blog.itpub.net/298600/viewspace-625138/ 定义: Oracle键弹性域可以根据自定义键弹性域时所定义的规则,执行段值组合的自动交叉验证.使用 ...
- mysql日志与备份恢复
一.mysql日志: mysql的日志种类有很多,常见的有二进制日志,一般查询日志,满查询日志,中继日志,事务日志等,具体信息可以通过 mysql> SHOW GLOBAL VARIABLES ...
- 关于android.view.WindowManager$BadTokenException问题出现以及解决的一些记录
1.出现 在app showdialog()时偶尔会出现,根据stackoverflow.com的描述,貌似是show的时候用作context的activity以及destroy了,,,一些异步操作会 ...
- 字符集和字符编码(Charset & Encoding)
字符集和字符编码(Charset & Encoding)[转] 1.基础知识 计算机中储存的信息都是用二进制数表示的:而我们在屏幕上看到的英文.汉字等字符是二进制数转换之后的结果.通俗的说,按 ...