Google V8编程详解(二)HelloWorld
转自http://blog.csdn.net/feiyinzilgd/article/details/8248448
上一章讲到了V8的编译和安装,这一章开始从一个demo着手。
这里选用了官方文档的一个非常简洁的HelloWorld.cc,代码如下:
- #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;
- }
我的目录结构如下:
编译运行:
- g++ -I../include helloworld.cc -o helloworld -lpthread -lv8
- ./helloworld
就可以在屏幕上看到输出结果了。
看到demo上有一些Context,Scope,Value等等,先不要慌张,其实就是V8的一些基本 数据类型,这些在后面会逐个一一讲到。
- Handle<String> source = String::New("'Hello' + ', World!'");
看到这句话,其实就是在加载一个js文件了。只不过这个js文件内容为:
- 'Hello' + ', World!'
那么这里,source就已经是加载过的js文件字符串内容了,接下来V8需要对js进行编译解释。
- Handle<Script> script = Script::Compile(source);
- Handle<Value> result = script->Run();
最后就是JS的执行了。这里虽然只有简单的几个语句,但是V8对于JS的编译和运行做了很多很复杂的操作。关于V8是如何编译和运行JS的,在后面章节将做详细的分析。
版权申明:
转载文章请注明原文出处,任何用于商业目的,请联系本人:hyman_tan@126.com
Google V8编程详解(二)HelloWorld的更多相关文章
- Google V8编程详解附录
Google V8编程详工具函数 头文件:utils.h #ifndef UTILS_H_ #define UTILS_H_ #include "v8.h" #include &l ...
- Google V8编程详解(五)JS调用C++
http://blog.csdn.net/feiyinzilgd/article/details/8453230 最近由于忙着解决个人单身的问题,时隔这么久才更新第五章. 上一章主要讲了Google ...
- Google V8编程详解(三)Handle & HandleScope
上一章简单的演示了一个Helloworld Demo.里面涉及到了V8的一些基本类型和概念,本章将围绕这个Demo对V8的基本类型和相关概念进行讲解. 这里还是先把Demo贴出来便于后面分析: #in ...
- Google V8编程详解(四)Context
http://blog.csdn.net/feiyinzilgd/article/details/8266780 上一章,比较略提了下V8的Context.本章将详细的讲解下Context的概念以及用 ...
- Google V8编程详解(序)Cloud App
此系列文章转载于此http://blog.csdn.net/feiyinzilgd/article/details/8247723 应用程序发展到今天,应用程序的概念也在不断地发生着 ...
- Google V8编程详解(一)V8的编译安装(Ubuntu)
V8的编译比较简单,需要同时安装git和svn. 下载V8源码: git clone git://github.com/v8/v8.git v8 && cd v8 切换到最新版本: g ...
- Linux 网络编程详解二(socket创建流程、多进程版)
netstat -na | grep " --查看TCP/IP协议连接状态 //socket编程提高版--服务器 #include <stdio.h> #include < ...
- [顶]ORACLE PL/SQL编程详解之二:PL/SQL块结构和组成元素(为山九仞,岂一日之功)
原文:[顶]ORACLE PL/SQL编程详解之二:PL/SQL块结构和组成元素(为山九仞,岂一日之功) [顶]ORACLE PL/SQL编程详解之二: PL/SQL块结构和组成元素(为山九仞,岂一日 ...
- ORACLE PL/SQL编程详解
ORACLE PL/SQL编程详解 编程详解 SQL语言只是访问.操作数据库的语言,并不是一种具有流程控制的程序设计语言,而只有程序设计语言才能用于应用软件的开发.PL /SQL是一种高级数据库程序设 ...
随机推荐
- putty远程连接ubuntu
步骤一.在ubuntu系统中安装ssh,可使用如下的命令进行安装: sudo apt-get install openssh-server步骤二.为了保险起见,安装完成后重启一下ssh服务,命令如 ...
- Events with Dojo(翻译)
In this tutorial, we will be exploring dojo/on and how Dojo makes it easy to connect to DOM events. ...
- js对话框5秒自动消失
使用了easyui的对话框控件 <html> <head> <title>5秒后关闭对话框</title> <meta http-equiv=&q ...
- Windows消息机制详解
消息是指什么? 消息系统对于一个win32程序来说十分重要,它是一个程序运行的动力源泉.一个消息,是系统定义的一个32位的值,他唯一的定义了一个事件,向 Windows发出一个通知,告诉应用 ...
- collectionview使用
创建UICollectionViewFlowLayout 对象来设置相关的布局,包括itemSize,headerReferenceSize,sectionInset.设置对应的布局大小,相关的和顶部 ...
- mysql数据备份
转载自:http://blog.chinaunix.net/uid-16480950-id-102800.html http://www.cnblogs.com/xuejie/archive/20 ...
- [转帖]The Lambda Calculus for Absolute Dummies (like myself)
Monday, May 7, 2012 The Lambda Calculus for Absolute Dummies (like myself) If there is one highly ...
- AfxMessageBox和MessageBox区别
如果用MFC的话,请尽量使用afxmessagebox,因为这个全局的对话框最安全,也最方便. 但是在WIN32 SDK的情况下只能使用MESSAGEBOX. MessageBox()是Win3 ...
- Ubuntu12.04解决gedit中文乱码问题
Ubuntu12.04,终端中分别输入下面两条指令: gsettings set org.gnome.gedit.preferences.encodings auto-detected “['GB18 ...
- echo同时输出到多个文件中
echo "test"| tee -a file1 file2 如果想去掉在屏幕上的显示 echo "test"| tee -a file1 file2 > ...