Webkit的自定义属性获取函数以及属性删除函数实现

概述: [CustomEnumerateProperty] 当给定的接口被枚举时,允许你为指定接口的属性获取函数编写自己的实现. 同样,当接口的属性被删除时,[CustomDeleteProperty]允许你编写自己的实现.

customEnumerateProperty](i), [CustomDeleteProperty](i)


用法: 这两个修饰可作用在interface,用法如下:

    [
        CustomEnumerateProperty,
        CustomDeleteProperty
    ] interface DataTransferItemList {
    };
  • [CustomEnumerateProperty] in JavaScriptCore: 你能编写DataTransferItemList的属性获取函数,具体来说,你可以编写这个函数JSXXX::getOwnPropertyNames(...), 它来自于WebCore/bindings/js/JSDataTransferItemListCustom.cpp:
 void JSDataTransferItemList::getOwnPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
{
    JSDataTransferItemList* thisObject = jsCast<JSDataTransferItemList*>(object);
    ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
    for (unsigned i = 0; i < static_cast<DataTransferItemList*>(thisObject->impl())->length(); ++i)
        propertyNames.add(Identifier::from(exec, i));
     Base::getOwnPropertyNames(thisObject, exec, propertyNames, mode);
}
    [

        CustomDeleteProperty
    ] interface Storage {
    };
  • [CustomDeleteProperty] : 当Storage的属性被删除时,我们可以编写自己的属性删除函数,具体点说,就是像这样子编写JSStorage::deleteProperty(...) 函数,它位于WebCore/bindings/js/JSStorageCustom.cpp:
bool JSStorage::deleteProperty(JSCell* cell, ExecState* exec, PropertyName propertyName)
{
    JSStorage* thisObject = jsCast<JSStorage*>(cell);
    // Only perform the custom delete if the object doesn't have a native property by this name.
    // Since hasProperty() would end up calling canGetItemsForName() and be fooled, we need to check
    // the native property slots manually.
    PropertySlot slot;
    if (getStaticValueSlot<JSStorage, Base>(exec, s_info.propHashTable(exec), thisObject, propertyName, slot))
        return false;
        
    JSValue prototype = thisObject->prototype();
    if (prototype.isObject() && asObject(prototype)->hasProperty(exec, propertyName))
        return false;


    thisObject->m_impl->removeItem(propertyNameToString(propertyName));
    return true;
}
参考:
1 http://trac.webkit.org/wiki/WebKitIDL#CustomEnumerateProperty
2 souce code of webkit

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值