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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。