一、自定义命名属性的获取(Getter)以及设置(Setter)函数:[CustomNamedGetter](i),[CustomNamedSetter](i)命名属性的W3C链接如下:The spec of named properties(注意,下面描述的webkit行为和W3C的规范是不同的)
总结:[CustomNamedGetter]或者[CustomNamedSetter]允许你为命名属性的getter或者setter编写自己的绑定函数.
用法如下:
[CustomNamedGetter,CustomNamedSetter] interface MichaelNamedGetter {};
当Michael.foooooo被执行时,命名的getters定义了它的行为, 这里的foooooooo不是Michael的属性. 当语句"XXX.foooooooo = …" 执行时,,命名的setter定义了它的行为。[CustomNamedGetter]和[CustomNamedSetter]允许你按照如下方式编写自己的绑定:
[CustomNamedGetter]in: You can write customJSXXX::canGetItemsForName(…)andJSXXX::nameGetter(…)in WebCore/bindings/js/JSXXXCustom.cpp: bool JSXXX::canGetItemsForName(ExecState* exec, XXX* impl, const Identifier& propertyName){…;}JSValue JSXXX::nameGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName){…;}[CustomNamedSetter]in: You can write customJSXXX::putDelegate(…)in WebCore/bindings/js/JSXXXCustom.cpp: bool JSXXX::putDelegate(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot){…;}
二、自定义属性的获取和赋值函数
[Custom](m,a),[CustomGetter](a),[CustomSetter](a)
总结: 他们允许你这样编写绑定代码。
用法:[Custom]可用于函数和属性.[CustomGetter],[CustomSetter]只能用于属性:
[Custom] void func();[CustomGetter, JSCustomSetter] attribute DOMString str;
我们应该尽可能少的使用自定义的绑定代码,因为他们可能有问题。在考虑使用他们之前,你应该慎重思考:我真的需要自定义绑定代码吗?推荐你修改代码生成器而不是自定义绑定代码。
在解释细节之前,让我澄清这些IDL属性之间的关系。
[Custom]修饰于方法,便是你可以编写方法的绑定代码。[CustomGetter]or[CustomSetter]修饰属性,表示你可以为属性的getter、setter编写自己的代码。
举例:
Method: interface XXX {[Custom] void func(int a, int b);};
你可以编写绑定代码:WebCore/bindings/js/JSXXXCustom.cpp:
JSValue JSXXX::func(ExecState* exec){…;}
参考更多例子:WebCore/bindings/js/JSXXXCustom.cpp
Attribute getter: interface XXX {[CustomGetter] attribute DOMString str;};
编写绑定代码: WebCore/bindings/js/JSXXXCustom.cpp:
JSValue JSXXX::str(ExecState* exec) const{…;}
参考更多例子:WebCore/bindings/js/JSXXXCustom.cpp
Attribute setter: interface XXX {[CustomSetter] attribute DOMString str;};
编写绑定代码: WebCore/bindings/js/JSXXXCustom.cpp:
void JSXXX::setStr(ExecState*, JSValue value){…;}
注意: ObjC, GObject,CPP 绑定bindings 不支持自己定义的绑定代码.
真正的强者,不是流泪的人,而是含泪奔跑的人。