码迷,mamicode.com
首页 > Web开发 > 详细

Webkit IDL中自定义[命名]属性的获取(Getter)以及设置(Setter)函数

时间:2015-02-20 17:30:54      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:

一、自定义命名属性的获取(Getter)以及设置(Setter)函数:

[CustomNamedGetter](i), [CustomNamedSetter](i)

总结: [CustomNamedGetter] 或者 [CustomNamedSetter] 允许你为命名属性的getter或者setter编写自己的绑定函数.

用法如下:

    [
        CustomNamedGetter,
        CustomNamedSetter
    ] interface MichaelNamedGetter {
    };

当Michael.foooooo被执行时,命名的getters定义了它的行为, 这里的foooooooo不是Michael的属性. 当语句"XXX.foooooooo = ..." 执行时,命名的setter定义了它的行为。[CustomNamedGetter] 和 [CustomNamedSetter] 允许你按照如下方式编写自己的绑定:

  • [CustomNamedGetter] in JavaScriptCore: You can write custom JSXXX::canGetItemsForName(...) and JSXXX::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 JavaScriptCore: You can write custom JSXXX::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 不支持自己定义的绑定代码.


Webkit IDL中自定义[命名]属性的获取(Getter)以及设置(Setter)函数

标签:

原文地址:http://blog.csdn.net/lichwei1983/article/details/43888717

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!