转自http://blog.csdn.net/feiyinzilgd/article/details/8248448

上一章讲到了V8的编译和安装,这一章开始从一个demo着手。

这里选用了官方文档的一个非常简洁的HelloWorld.cc,代码如下:

  1. #include <v8.h>
  2. using namespace v8;
  3. int main(int argc, char* argv[]) {
  4. // Create a stack-allocated handle scope.
  5. HandleScope handle_scope;
  6. // Create a new context.
  7. Persistent<Context> context = Context::New();
  8. // Enter the created context for compiling and
  9. // running the hello world script.
  10. Context::Scope context_scope(context);
  11. // Create a string containing the JavaScript source code.
  12. Handle<String> source = String::New("'Hello' + ', World!'");
  13. // Compile the source code.
  14. Handle<Script> script = Script::Compile(source);
  15. // Run the script to get the result.
  16. Handle<Value> result = script->Run();
  17. // Dispose the persistent context.
  18. context.Dispose();
  19. // Convert the result to an ASCII string and print it.
  20. String::AsciiValue ascii(result);
  21. printf("%s\n", *ascii);
  22. return 0;
  23. }

我的目录结构如下:

编译运行:

  1. g++ -I../include helloworld.cc  -o helloworld -lpthread -lv8
  1. ./helloworld

就可以在屏幕上看到输出结果了。

看到demo上有一些Context,Scope,Value等等,先不要慌张,其实就是V8的一些基本 数据类型,这些在后面会逐个一一讲到。

  1. Handle<String> source = String::New("'Hello' + ', World!'");

看到这句话,其实就是在加载一个js文件了。只不过这个js文件内容为:

  1. 'Hello' + ', World!'

那么这里,source就已经是加载过的js文件字符串内容了,接下来V8需要对js进行编译解释。

  1. Handle<Script> script = Script::Compile(source);
  2. Handle<Value> result = script->Run();

最后就是JS的执行了。这里虽然只有简单的几个语句,但是V8对于JS的编译和运行做了很多很复杂的操作。关于V8是如何编译和运行JS的,在后面章节将做详细的分析。

版权申明:
转载文章请注明原文出处,任何用于商业目的,请联系本人:hyman_tan@126.com

Google V8编程详解(二)HelloWorld的更多相关文章

  1. Google V8编程详解附录

    Google V8编程详工具函数 头文件:utils.h #ifndef UTILS_H_ #define UTILS_H_ #include "v8.h" #include &l ...

  2. Google V8编程详解(五)JS调用C++

    http://blog.csdn.net/feiyinzilgd/article/details/8453230 最近由于忙着解决个人单身的问题,时隔这么久才更新第五章. 上一章主要讲了Google ...

  3. Google V8编程详解(三)Handle & HandleScope

    上一章简单的演示了一个Helloworld Demo.里面涉及到了V8的一些基本类型和概念,本章将围绕这个Demo对V8的基本类型和相关概念进行讲解. 这里还是先把Demo贴出来便于后面分析: #in ...

  4. Google V8编程详解(四)Context

    http://blog.csdn.net/feiyinzilgd/article/details/8266780 上一章,比较略提了下V8的Context.本章将详细的讲解下Context的概念以及用 ...

  5. Google V8编程详解(序)Cloud App

    此系列文章转载于此http://blog.csdn.net/feiyinzilgd/article/details/8247723          应用程序发展到今天,应用程序的概念也在不断地发生着 ...

  6. Google V8编程详解(一)V8的编译安装(Ubuntu)

    V8的编译比较简单,需要同时安装git和svn. 下载V8源码: git clone git://github.com/v8/v8.git v8 && cd v8 切换到最新版本: g ...

  7. Linux 网络编程详解二(socket创建流程、多进程版)

    netstat -na | grep " --查看TCP/IP协议连接状态 //socket编程提高版--服务器 #include <stdio.h> #include < ...

  8. [顶]ORACLE PL/SQL编程详解之二:PL/SQL块结构和组成元素(为山九仞,岂一日之功)

    原文:[顶]ORACLE PL/SQL编程详解之二:PL/SQL块结构和组成元素(为山九仞,岂一日之功) [顶]ORACLE PL/SQL编程详解之二: PL/SQL块结构和组成元素(为山九仞,岂一日 ...

  9. ORACLE PL/SQL编程详解

    ORACLE PL/SQL编程详解 编程详解 SQL语言只是访问.操作数据库的语言,并不是一种具有流程控制的程序设计语言,而只有程序设计语言才能用于应用软件的开发.PL /SQL是一种高级数据库程序设 ...

随机推荐

  1. du 使用详解 linux查看目录大小 linux统计目录大小并排序 查看目录下所有一级子目录文件夹大小 du -h --max-depth=1 |grep [

    常用命令 du -h --max-depth=1 |grep [TG] |sort   #查找上G和T的目录并排序 du -sh    #统计当前目录的大小,以直观方式展现 du -h --max-d ...

  2. Map以及Set的遍历(EntrySet方法,补充enumeration和Iterator的区别)

    public void mearge(Map map) { Map returnMap = new HashMap<>(); // 转换为Entry Set<Map.Entry< ...

  3. sklearn学习笔记2

    Text classifcation with Naïve Bayes In this section we will try to classify newsgroup messages using ...

  4. [手机取证] CelleBrite UFED Touch系列使用视频

    CelleBrite UFED Touch系列产品使用指导视频 (持续更新中) 1. CelleBrite UFED Touch产品介绍

  5. (Tree)94.Binary Tree Inorder Traversal

    /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * Tre ...

  6. JavaScript 命名规则

    来源 :http://www.codelifter.com/main/tips/tip_020.shtml The following are the rules for naming JavaScr ...

  7. VisualStudio2010正则表达式查找和替换

    把 TRACE(_T("something etc."); 替换为 TRACE(_T("something etc."));查找内容:表达式:TRACE\(_T ...

  8. 一些常用的sql语句

    1.查询表里的null值:is null 和 is not null  select*from student where email is null       返回的该表里面邮箱为null的结果集 ...

  9. js输出到控制台

    console.log(object[, object, ...])在控制台输出一条消息.如果有多个参数,输出时会用空格隔开这些参数. 第一个参数可以是一个包含格式化占位符输出的字符串,例如: con ...

  10. [ json editor] 如何在网页中使用Json editor 插件

    [目的] 在自己的网页上交由用户进行json的可视化编辑 [难点]1.json中含有递归嵌套的数组和对象 2.json中的基本值类型有数字.字符串和布尔型 [方法]使用daviddurman的Flex ...