Changeset 87588 in webkit


Ignore:
Timestamp:
May 27, 2011 5:28:56 PM (13 years ago)
Author:
ggaren@apple.com
Message:

2011-05-27 Geoffrey Garen <ggaren@apple.com>

Reviewed by Oliver Hunt.

JS API is too aggressive about throwing exceptions for NULL get or set operations
https://bugs.webkit.org/show_bug.cgi?id=61678

  • API/JSCallbackObject.h: Changed our staticValueGetter to a regular function that returns a JSValue, so it can fail and still forward to normal property lookup.
  • API/JSCallbackObjectFunctions.h: (JSC::::getOwnPropertySlot): Don't throw an exception when failing to access a static property -- just forward the access. This allows objects to observe get/set operations but still let the JS object manage lifetime.

(JSC::::put): Ditto.

(JSC::::getStaticValue): Same as JSCallbackObject.h.

  • API/tests/testapi.c: (MyObject_set_nullGetForwardSet):
  • API/tests/testapi.js: Updated tests to reflect slightly less strict behavior, which matches headerdoc claims.
Location:
trunk/Source/JavaScriptCore
Files:
5 edited

Legend:

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

    r87586 r87588  
    189189    static EncodedJSValue JSC_HOST_CALL construct(ExecState*);
    190190   
    191     static JSValue staticValueGetter(ExecState*, JSValue, const Identifier&);
     191    JSValue getStaticValue(ExecState*, const Identifier&);
    192192    static JSValue staticFunctionGetter(ExecState*, JSValue, const Identifier&);
    193193    static JSValue callbackGetter(ExecState*, JSValue, const Identifier&);
  • trunk/Source/JavaScriptCore/API/JSCallbackObjectFunctions.h

    r84660 r87588  
    150150        if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
    151151            if (staticValues->contains(propertyName.impl())) {
    152                 slot.setCustom(this, staticValueGetter);
    153                 return true;
     152                JSValue value = getStaticValue(exec, propertyName);
     153                if (value) {
     154                    slot.setValue(value);
     155                    return true;
     156                }
    154157            }
    155158        }
     
    226229                    if (result || exception)
    227230                        return;
    228                 } else
    229                     throwError(exec, createReferenceError(exec, "Attempt to set a property that is not settable."));
     231                }
    230232            }
    231233        }
     
    516518
    517519template <class Base>
    518 JSValue JSCallbackObject<Base>::staticValueGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
    519 {
    520     JSCallbackObject* thisObj = asCallbackObject(slotBase);
    521    
    522     JSObjectRef thisRef = toRef(thisObj);
     520JSValue JSCallbackObject<Base>::getStaticValue(ExecState* exec, const Identifier& propertyName)
     521{
     522    JSObjectRef thisRef = toRef(this);
    523523    RefPtr<OpaqueJSString> propertyNameRef;
    524524   
    525     for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass)
     525    for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
    526526        if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec))
    527527            if (StaticValueEntry* entry = staticValues->get(propertyName.impl()))
     
    543543                }
    544544
    545     return throwError(exec, createReferenceError(exec, "Static value property defined with NULL getProperty callback."));
     545    return JSValue();
    546546}
    547547
  • trunk/Source/JavaScriptCore/API/tests/testapi.c

    r87586 r87588  
    312312}
    313313
     314static bool MyObject_set_nullGetForwardSet(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
     315{
     316    UNUSED_PARAM(ctx);
     317    UNUSED_PARAM(object);
     318    UNUSED_PARAM(propertyName);
     319    UNUSED_PARAM(value);
     320    UNUSED_PARAM(exception);
     321    return false; // Forward to parent class.
     322}
     323
    314324static JSStaticValue evilStaticValues[] = {
    315325    { "nullGetSet", 0, 0, kJSPropertyAttributeNone },
     326    { "nullGetForwardSet", 0, MyObject_set_nullGetForwardSet, kJSPropertyAttributeNone },
    316327    { 0, 0, 0, 0 }
    317328};
  • trunk/Source/JavaScriptCore/API/tests/testapi.js

    r87586 r87588  
    9595shouldBe("'throwOnHasInstance' instanceof MyObject", "an exception");
    9696
     97MyObject.nullGetForwardSet = 1;
     98shouldBe("MyObject.nullGetForwardSet", 1);
     99
    97100var foundMyPropertyName = false;
    98101var foundRegularType = false;
     
    163166shouldBe("(new Object()) instanceof MyObject", false);
    164167
    165 shouldThrow("MyObject.nullGetSet = 1");
    166 shouldThrow("MyObject.nullGetSet");
     168MyObject.nullGetSet = 1;
     169shouldBe("MyObject.nullGetSet", 1);
    167170shouldThrow("MyObject.nullCall()");
    168171shouldThrow("MyObject.hasPropertyLie");
  • trunk/Source/JavaScriptCore/ChangeLog

    r87586 r87588  
     12011-05-27  Geoffrey Garen  <ggaren@apple.com>
     2
     3        Reviewed by Oliver Hunt.
     4
     5        JS API is too aggressive about throwing exceptions for NULL get or set operations
     6        https://bugs.webkit.org/show_bug.cgi?id=61678
     7
     8        * API/JSCallbackObject.h: Changed our staticValueGetter to a regular
     9        function that returns a JSValue, so it can fail and still forward to
     10        normal property lookup.
     11
     12        * API/JSCallbackObjectFunctions.h:
     13        (JSC::::getOwnPropertySlot): Don't throw an exception when failing to
     14        access a static property -- just forward the access. This allows objects
     15        to observe get/set operations but still let the JS object manage lifetime.
     16
     17        (JSC::::put): Ditto.
     18
     19        (JSC::::getStaticValue): Same as JSCallbackObject.h.
     20
     21        * API/tests/testapi.c:
     22        (MyObject_set_nullGetForwardSet):
     23        * API/tests/testapi.js: Updated tests to reflect slightly less strict
     24        behavior, which matches headerdoc claims.
     25
    1262011-05-27  Geoffrey Garen  <ggaren@apple.com>
    227
Note: See TracChangeset for help on using the changeset viewer.