Google V8编程详解(三)Handle & HandleScope
上一章简单的演示了一个Helloworld Demo。里面涉及到了V8的一些基本类型和概念,本章将围绕这个Demo对V8的基本类型和相关概念进行讲解。
这里还是先把Demo贴出来便于后面分析:
- #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;
- }
Handle
在V8中,内存分配都是在V8的Heap中进行分配的,JavaScript的值和对象也都存放在V8的Heap中。这个Heap由V8独立的去维护,失去引用的对象将会被V8的GC掉并可以重新分配给其他对象。而Handle即是对Heap中对象的引用。V8为了对内存分配进行管理,GC需要对V8中的所有对象进行跟踪,而对象都是用Handle方式引用的,所以GC需要对Handle进行管理,这样GC就能知道Heap中一个对象的引用情况,当一个对象的Handle引用为发生改变的时候,GC即可对该对象进行回收(gc)或者移动。因此,V8编程中必须使用Handle去引用一个对象,而不是直接通过C++的方式去获取对象的引用,直接通过C++的方式去直接去引用一个对象,会使得该对象无法被V8管理。
Handle分为Local和Persistent两种。从字面上就能知道,Local是局部的,它同时被HandleScope进行管理。persistent,类似与全局的,不受HandleScope的管理,其作用域可以延伸到不同的函数,而Local是局部的,作用域比较小。Persistent Handle对象需要Persistent::New, Persistent::Dispose配对使用,类似于C++中new和delete.Persistent::MakeWeak可以用来弱化一个Persistent Handle,如果一个对象的唯一引用Handle是一个Persistent,则可以使用MakeWeak方法来如果该引用,该方法可以出发GC对被引用对象的回收。
HandleScope
一个函数中,可以有很多Handle,而HandleScope则相当于用来装Handle(Local)的容器,当HandleScope生命周期结束的时候,Handle也将会被释放,会引起Heap中对象引用的更新。HandleScope是分配在栈上,不能通过New的方式进行创建。对于同一个作用域内可以有多个HandleScope,新的HandleScope将会覆盖上一个HandleScope,并对Local Handle进行管理。下面通过代码来讲解HandleScope的生命周期:
- #include <v8.h>
- using namespace v8;
- int main(int argc, char* argv[]) {
- // Create a stack-allocated handle scope.
- HandleScope handle_scope;
- // >>>>>>>>>>>>>>>>>>>>>>>>从这里开始,是HandleScope的生命周期的开始
- // 从此之后的所有Local Handle都这个handle_scope对象进行管理
- // Create a new context.
- Persistent<Context> context = Context::New(); //Persistent Handle
- // 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!'"); //Local Handle
- // Compile the source code.
- Handle<Script> script = Script::Compile(source); //Local Handle
- // Run the script to get the result.
- Handle<Value> result = script->Run(); //Local Handle
- // 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;
- // <<<<<<<<<<<<<<<<<<<<<<<到这里,handle_scope的生命周期结束,其析构函数将被调用,其内部的所有Local Handle将被释放
- }
Context
Context值得是JavaScript的执行环境。每个JavaScript都必须执行在一个Context中。Context有多个,而且可以在不同的Context中进行切换。
- Persistent<Context> context = Context::New();
- Context::Scope context_scope(context);
这段代码就是申请一个Persistent contetxt,并通过Context::Scope切换到该context中。在这个Demo中,
- Context::Scope context_scope(context);
之后的所有操作都执行在context中。
我们还可以使用
- Persistent<Context> context_Ex = Context::New();
- Context::Scope context_scope_Ex(context_Ex);
来切换到context_Ex中去。
这里只是简单的了解下Context的概念,后面将单独开辟一个章节来详细讲述V8的Context。

从这张图可以比较清楚的看到Handle,HandleScope,以及被Handle引用的对象之间的关系。从图中可以看到,V8的对象都是存在V8的Heap中,而Handle则是对该对象的引用。
版权申明:
转载文章请注明原文出处,任何用于商业目的,请联系本人:hyman_tan@126.com
Google V8编程详解(三)Handle & HandleScope的更多相关文章
- 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编程详解(四)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编程详解(二)HelloWorld
转自http://blog.csdn.net/feiyinzilgd/article/details/8248448 上一章讲到了V8的编译和安装,这一章开始从一个demo着手. 这里选用了官方文档的 ...
- Google V8编程详解(一)V8的编译安装(Ubuntu)
V8的编译比较简单,需要同时安装git和svn. 下载V8源码: git clone git://github.com/v8/v8.git v8 && cd v8 切换到最新版本: g ...
- Linux 网络编程详解三(p2p点对点聊天)
//p2p点对点聊天多进程版--服务器(信号的使用) #include <stdio.h> #include <stdlib.h> #include <string.h& ...
- ORACLE PL/SQL编程详解
ORACLE PL/SQL编程详解 编程详解 SQL语言只是访问.操作数据库的语言,并不是一种具有流程控制的程序设计语言,而只有程序设计语言才能用于应用软件的开发.PL /SQL是一种高级数据库程序设 ...
- ORACLE PL/SQL编程详解(转)
原帖地址:http://blog.csdn.net/chenjinping123/article/details/8737604 ORACLE PL/SQL编程详解 SQL语言只是访问.操作数据库的语 ...
随机推荐
- VS2010 下 将 EntityFramework 的版本从 4.0 升级到 5.0
1. 下载安装 EF 5.x DbContext Generator for C# : 下载地址:https://visualstudiogallery.msdn.microsoft.com/da74 ...
- OAF_文件系列9_实现OAF解析Excel并读取至数据库JXL
ddd puroder. webui. poLineExcelImport.java
- java线程池
http://cuisuqiang.iteye.com/blog/2019372 Java四种线程池的使用 java线程线程池监控 Java通过Executors提供四种线程池,分别为:newCach ...
- JVM实用参数(六) 吞吐量收集器
在实践中我们发现对于大多数的应用领域,评估一个垃圾收集(GC)算法如何根据如下两个标准: 吞吐量越高算法越好 暂停时间越短算法越好 首先让我们来明确垃圾收集(GC)中的两个术语:吞吐量(through ...
- ArcGIS Engine中数据的加载 (转)
1.加载Shapefile数据 1 IWorkspaceFactory pWorkspaceFactory; 2 IFeatureWorkspace pFeatureWorkspace; 3 IFea ...
- Druid安装-单机
单机版安装 下载安装包http://static.druid.io/artifacts/releases/druid-0.9.1.1-bin.tar.gz 安装 解压缩 安装zookeeper cu ...
- 【HEVC】4、HM-16.7编码一个CU(帧内部分) 3.帧内预测各种模式实现
HEVC中一共定义了35中帧内编码预测模式,编号分别以0-34定义.其中模式0定义为平面模式(INTRA_PLANAR),模式1定义为均值模式(INTRA_DC),模式2~34定义为角度预测模式(IN ...
- RDD常用方法之subtract&intersection&cartesian
subtract Return an RDD with the elements from `this` that are not in `other` . def subtract(othe ...
- EF文章连接
http://www.cnblogs.com/shanyou/archive/2011/07/17/2108953.html http://www.cnblogs.com/haogj/archive/ ...
- opencv基于混合高斯模型的图像分割
#include "stdafx.h" #include <opencv\cv.h> #include <opencv\highgui.h> #includ ...