V8 Binding 笔记

2 篇文章 0 订阅
1 篇文章 0 订阅

读于此:https://docs.google.com/presentation/d/1OFG81taxgjOGU43sv9WHvPZkt5--KnM6gSijWN8NMcU/edit?disco=AAAAAECHbXY&pli=1#slide=id.p


Web IDL定义了DOM(Webkit) 与 ECMAScript(V8, JavaScriptCore, ObjectC,GObject,CPP)的绑定规范。
如何添加一个 新的绑定,有三个步骤:
假如实现了HTMLElement
Step1:需要在WebKit中实现HTMLElement
-WebCore/html/HTMLElement.{h,cpp}, etc
Step2:需要编写一个相应的IDL 文件
-WebCore/html/HTMLElement.idl
Step3:Build 绑定代码会自动生成。

e.g

Step1: Write an IDL file

//Node.idl
interface Node{
  attribute Node firstChild;
  ...;
};

Step2: generate the binding code
-Perl scripts in WebCore/binding/scripts parse the IDL file and auto-generate the binding code

IDL files CodeGeneratorV8.pm *.h,*.cpp
    | CodeGeneratorJS.pm *.h,*.cpp IDLParser.pm-> CodeGeneratorObjC.pm *.h,*.cpp
CodeGeneratorGObject.pm *.h,*.cpp
CodeGeneratorCPP.pm *.h,*.cpp

Step3:

-An example of the generate code
//V8 calls back this method when div.firstChild is evaluated
static v8:Handle<v8::Value>
firstChildAttrGetter(..., const v8::AccessorInfo& info)
{
	//Retrieve a Dom object from a V8 object.
	Node* imp = V8Node::toNative(info.Holder());
	//Call a WebKit method.
	Node* firstChild = imp->firstChild();
	//Convert a DOM object to a V8 object, and return.
	return toV8(firstChild, ...);
}


********************************
Where you should check.
-IDL parser:
-WebCore/bindings/scripts/IDLParser.pm
-Code generators:
-WebCore/bindings/scripts/CodeGenerator{V8,JS,ObjeC,GObject,CPP}.pm
-Helper Perl scripts:
WebCore/bindings/scripts/*.pm


-Generated code for V8:
-src/out/{Release,Debug}/obj/gen/webkit/bindings/*.h
-src/out/{Release,Debug}/obj,gen/webcore/bindings/*.cpp
*************************
V8 Data 
v8::Handle<v8::Value> value;
v8:Value==> Data Type: Value, Integer, Boolean, String, Object, Function or ...etc
v8:Handle==>Data allocated: Handle, Local, Persistent.


当使用V8 传递数据时,数据类型尽量使用Value,容器尽量使用Handle,也就是说尽量使用
V8::Handle<v8::Value>
容器:
1) Local 
-Will be auto-deallocated when the current scope exits
-Don't hold the Local data on heap
2)Persistent
-Never deallocated unless you explicitly call Persistent::Dispose()
-Persistent::New() must be balanced with Persistent::Dispose()
3)Handle 是Local 与 Persistent 的父类


数据类型: Value 是根类
Object 和 Primitive 继承于Value
Object 包含:Array, Function, Date, RegExp, BooleanObject, StringObject and NumberObject
Primitive 包含:Boolean, Number, String, Integer, Int32 and Uint32

Summary
V8 API Document in v8.h 

V8 utility methods are documented in V8Binding.h

DOM Object:

V8XXX::toNative(): Wrapper => DOM
toV8(): DOM => Wrapper

Garbage Collection
V8/JSC knows te JavaScript object graph only
WebKit knows the DOM trees only
Bindings are responsible for managing the relationship between the two graphs.

How to tell the reachability to V8
(1) Override visitDOMWrapper() in your custom binding

(2) Add the reachability by v8::V8::AddImplicitReferences()

void V8NodeList::visitDOMWrapper(DOMDataStore* store, void* object, v8::Persistent<v8::Object> wrapper) {
  NodeList* impl = static_cast<NodeList*>(object);
  Node* owner = ... // Get the owner Node of impl
  v8::Persistent<v8::Object> ownerWrapper = ... // Get the wrapper object of owner
  if (/* the wrapper object exists */)
   v8::V8::AddImplicitReferences(ownerWrapper, &wrapper, 1);
}

You need to add the references by visitDOMWrapper() and AddImplicitReferences()


V8 GC Design

Redesigning V8 Garbage Collection for DOM Objects
https://docs.google.com/document/d/1OMG0fXB3DDvBaQ2YgxWLzzKjn9nWp8y_oTdQqFkBWhw/edit?pli=1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值