Changeset 53638 in webkit for trunk/JavaScriptCore/API


Ignore:
Timestamp:
Jan 21, 2010, 12:46:03 PM (15 years ago)
Author:
[email protected]
Message:

2010-01-21 Kent Hansen <[email protected]>

Reviewed by Geoffrey Garen.

Object.getOwnPropertyDescriptor always returns undefined for JS API objects
https://bugs.webkit.org/show_bug.cgi?id=33946

Ideally the getOwnPropertyDescriptor() reimplementation should return an
access descriptor that wraps the property getter and setter callbacks, but
that approach is much more involved than returning a value descriptor.
Keep it simple for now.

  • API/JSCallbackObject.h:
  • API/JSCallbackObjectFunctions.h: (JSC::::getOwnPropertyDescriptor):
  • API/tests/testapi.js:
Location:
trunk/JavaScriptCore/API
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/API/JSCallbackObject.h

    r53170 r53638  
    6262    virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
    6363    virtual bool getOwnPropertySlot(ExecState*, unsigned, PropertySlot&);
     64    virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&);
    6465   
    6566    virtual void put(ExecState*, const Identifier&, JSValue, PutPropertySlot&);
  • trunk/JavaScriptCore/API/JSCallbackObjectFunctions.h

    r53170 r53638  
    169169
    170170template <class Base>
     171bool JSCallbackObject<Base>::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
     172{
     173    PropertySlot slot;
     174    if (getOwnPropertySlot(exec, propertyName, slot)) {
     175        // Ideally we should return an access descriptor, but returning a value descriptor is better than nothing.
     176        JSValue value = slot.getValue(exec, propertyName);
     177        if (!exec->hadException())
     178            descriptor.setValue(value);
     179        // We don't know whether the property is configurable, but assume it is.
     180        descriptor.setConfigurable(true);
     181        // We don't know whether the property is enumerable (we could call getOwnPropertyNames() to find out), but assume it isn't.
     182        descriptor.setEnumerable(false);
     183        return true;
     184    }
     185
     186    return Base::getOwnPropertyDescriptor(exec, propertyName, descriptor);
     187}
     188
     189template <class Base>
    171190void JSCallbackObject<Base>::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
    172191{
  • trunk/JavaScriptCore/API/tests/testapi.js

    r43603 r53638  
    114114    fail("MyObject.regularType was not enumerated");
    115115
     116var alwaysOneDescriptor = Object.getOwnPropertyDescriptor(MyObject, "alwaysOne");
     117shouldBe('typeof alwaysOneDescriptor', "object");
     118shouldBe('alwaysOneDescriptor.value', MyObject.alwaysOne);
     119shouldBe('alwaysOneDescriptor.configurable', true);
     120shouldBe('alwaysOneDescriptor.enumerable', false); // Actually it is.
     121var cantFindDescriptor = Object.getOwnPropertyDescriptor(MyObject, "cantFind");
     122shouldBe('typeof cantFindDescriptor', "object");
     123shouldBe('cantFindDescriptor.value', MyObject.cantFind);
     124shouldBe('cantFindDescriptor.configurable', true);
     125shouldBe('cantFindDescriptor.enumerable', false);
     126try {
     127    // If getOwnPropertyDescriptor() returned an access descriptor, this wouldn't throw.
     128    Object.getOwnPropertyDescriptor(MyObject, "throwOnGet");
     129} catch (e) {
     130    pass("getting property descriptor of throwOnGet threw exception");
     131}
     132var myPropertyNameDescriptor = Object.getOwnPropertyDescriptor(MyObject, "myPropertyName");
     133shouldBe('typeof myPropertyNameDescriptor', "object");
     134shouldBe('myPropertyNameDescriptor.value', MyObject.myPropertyName);
     135shouldBe('myPropertyNameDescriptor.configurable', true);
     136shouldBe('myPropertyNameDescriptor.enumerable', false); // Actually it is.
     137try {
     138    // if getOwnPropertyDescriptor() returned an access descriptor, this wouldn't throw.
     139    Object.getOwnPropertyDescriptor(MyObject, "hasPropertyLie");
     140} catch (e) {
     141    pass("getting property descriptor of hasPropertyLie threw exception");
     142}
     143shouldBe('Object.getOwnPropertyDescriptor(MyObject, "doesNotExist")', undefined);
     144
    116145myObject = new MyObject();
    117146
     
    156185shouldBe("derived.protoDup = 0", 2);
    157186
     187shouldBe('Object.getOwnPropertyDescriptor(derived, "baseProto")', undefined);
     188shouldBe('Object.getOwnPropertyDescriptor(derived, "baseProtoDup")', undefined);
     189var baseDupDescriptor = Object.getOwnPropertyDescriptor(derived, "baseDup");
     190shouldBe('typeof baseDupDescriptor', "object");
     191shouldBe('baseDupDescriptor.value', derived.baseDup);
     192shouldBe('baseDupDescriptor.configurable', true);
     193shouldBe('baseDupDescriptor.enumerable', false);
     194var baseOnlyDescriptor = Object.getOwnPropertyDescriptor(derived, "baseOnly");
     195shouldBe('typeof baseOnlyDescriptor', "object");
     196shouldBe('baseOnlyDescriptor.value', derived.baseOnly);
     197shouldBe('baseOnlyDescriptor.configurable', true);
     198shouldBe('baseOnlyDescriptor.enumerable', false);
     199shouldBe('Object.getOwnPropertyDescriptor(derived, "protoOnly")', undefined);
     200var protoDupDescriptor = Object.getOwnPropertyDescriptor(derived, "protoDup");
     201shouldBe('typeof protoDupDescriptor', "object");
     202shouldBe('protoDupDescriptor.value', derived.protoDup);
     203shouldBe('protoDupDescriptor.configurable', true);
     204shouldBe('protoDupDescriptor.enumerable', false);
     205var derivedOnlyDescriptor = Object.getOwnPropertyDescriptor(derived, "derivedOnly");
     206shouldBe('typeof derivedOnlyDescriptor', "object");
     207shouldBe('derivedOnlyDescriptor.value', derived.derivedOnly);
     208shouldBe('derivedOnlyDescriptor.configurable', true);
     209shouldBe('derivedOnlyDescriptor.enumerable', false);
     210
    158211shouldBe("undefined instanceof MyObject", false);
    159212EvilExceptionObject.hasInstance = function f() { return f(); };
Note: See TracChangeset for help on using the changeset viewer.