Changeset 15473 in webkit for trunk/JavaScriptCore/API


Ignore:
Timestamp:
Jul 16, 2006, 6:48:27 PM (19 years ago)
Author:
ggaren
Message:

Reviewed by Maciej.


  • Properly document and handle NULL callbacks for static properties. We throw an exception in any case other than a ReadOnly property with a NULL setProperty callback, because a NULL callback almost certainly indicates a programming error. Also throw an exception if hasProperty returns true for a property that getProperty can't get.


  • If a static setProperty callback returns 'false', to indicate that the property was not set, we no longer forward the set request up the class chain, because that's almost certainly not what the programmer expected.
  • API/JSCallbackObject.cpp: (KJS::JSCallbackObject::getOwnPropertySlot): (KJS::JSCallbackObject::put): (KJS::JSCallbackObject::staticValueGetter): (KJS::JSCallbackObject::staticFunctionGetter): (KJS::JSCallbackObject::callbackGetter):
  • API/JSObjectRef.h:
  • API/minidom.js:
  • API/testapi.c: (MyObject_hasProperty):
  • API/testapi.js:
Location:
trunk/JavaScriptCore/API
Files:
5 edited

Legend:

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

    r15469 r15473  
    108108
    109109        if (__JSClass::StaticValuesTable* staticValues = jsClass->staticValues) {
    110             if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep())) {
    111                 if (entry->getProperty) {
    112                     slot.setCustom(this, staticValueGetter);
    113                     return true;
    114                 }
     110            if (staticValues->contains(propertyName.ustring().rep())) {
     111                slot.setCustom(this, staticValueGetter);
     112                return true;
    115113            }
    116114        }
     
    149147                if (entry->attributes & kJSPropertyAttributeReadOnly)
    150148                    return;
    151                 if (JSObjectSetPropertyCallback setProperty = entry->setProperty) {
    152                     if (setProperty(context, thisRef, propertyNameRef, valueRef, toRef(exec->exceptionSlot())))
    153                         return;
    154                 }
     149                if (JSObjectSetPropertyCallback setProperty = entry->setProperty)
     150                    setProperty(context, thisRef, propertyNameRef, valueRef, toRef(exec->exceptionSlot()));
     151                else
     152                    throwError(exec, ReferenceError, "Writable static value property defined with NULL setProperty callback.");
    155153            }
    156154        }
     
    393391                        return toJS(value);
    394392
    395     return jsUndefined();
     393    return throwError(exec, ReferenceError, "Static value property defined with NULL getProperty callback.");
    396394}
    397395
     
    407405        if (__JSClass::StaticFunctionsTable* staticFunctions = jsClass->staticFunctions) {
    408406            if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) {
    409                 JSObject* o = new JSCallbackFunction(exec, entry->callAsFunction, propertyName);
    410                 thisObj->putDirect(propertyName, o, entry->attributes);
    411                 return o;
    412             }
    413         }
    414     }
    415 
    416     return jsUndefined();
     407                if (JSObjectCallAsFunctionCallback callAsFunction = entry->callAsFunction) {
     408                    JSObject* o = new JSCallbackFunction(exec, callAsFunction, propertyName);
     409                    thisObj->putDirect(propertyName, o, entry->attributes);
     410                    return o;
     411                }
     412            }
     413        }
     414    }
     415
     416    return throwError(exec, ReferenceError, "Static function property defined with NULL callAsFunction callback.");
    417417}
    418418
     
    430430                return toJS(value);
    431431
    432     return jsUndefined();
     432    return throwError(exec, ReferenceError, "hasProperty callback returned true for a property that doesn't exist.");
    433433}
    434434
  • trunk/JavaScriptCore/API/JSObjectRef.h

    r15469 r15473  
    263263@field name A null-terminated UTF8 string containing the property's name.
    264264@field getProperty A JSObjectGetPropertyCallback to invoke when getting the property's value.
    265 @field setProperty A JSObjectSetPropertyCallback to invoke when setting the property's value.
     265@field setProperty A JSObjectSetPropertyCallback to invoke when setting the property's value. May be NULL if the ReadOnly attribute is set.
    266266@field attributes A logically ORed set of JSPropertyAttributes to give to the property.
    267267*/
  • trunk/JavaScriptCore/API/minidom.js

    r15133 r15473  
    166166}
    167167
     168function shouldBe(a, b)
     169{
     170    var evalA;
     171    try {
     172        evalA = eval(a);
     173    } catch(e) {
     174        evalA = e;
     175    }
     176   
     177    if (evalA == b || isNaN(evalA) && typeof evalA == 'number' && isNaN(b) && typeof b == 'number')
     178        print("PASS: " + a + " should be " + b + " and is.", "green");
     179    else
     180        print("__FAIL__: " + a + " should be " + b + " but instead is " + evalA + ".", "red");
     181}
     182
    168183function test()
    169184{
     
    222237        print("caught: " + e);
    223238    }
     239   
     240    oldNodeType = node.nodeType;
     241    node.nodeType = 1;
     242    shouldBe("node.nodeType", oldNodeType);
    224243
    225244    /*
  • trunk/JavaScriptCore/API/testapi.c

    r15469 r15473  
    115115    if (JSStringIsEqualToUTF8CString(propertyName, "alwaysOne")
    116116        || JSStringIsEqualToUTF8CString(propertyName, "cantFind")
    117         || JSStringIsEqualToUTF8CString(propertyName, "myPropertyName")) {
     117        || JSStringIsEqualToUTF8CString(propertyName, "myPropertyName")
     118        || JSStringIsEqualToUTF8CString(propertyName, "hasPropertyLie")) {
    118119        return true;
    119120    }
     
    243244}
    244245
     246static JSStaticValue evilStaticValues[] = {
     247    { "nullGetSet", 0, 0, kJSPropertyAttributeNone },
     248    { 0, 0, 0, 0 }
     249};
     250
     251static JSStaticFunction evilStaticFunctions[] = {
     252    { "nullCall", 0, kJSPropertyAttributeNone },
     253    { 0, 0, 0 }
     254};
     255
    245256JSClassDefinition MyObject_definition = {
    246257    0,
     
    249260    NULL,
    250261   
    251     NULL,
    252     NULL,
     262    evilStaticValues,
     263    evilStaticFunctions,
    253264   
    254265    MyObject_initialize,
  • trunk/JavaScriptCore/API/testapi.js

    r15462 r15473  
    4040}
    4141
     42function shouldThrow(a)
     43{
     44    var result = "__FAIL__: " + a + " did not throw an exception.";
     45   
     46    var evalA;
     47    try {
     48        eval(a);
     49    } catch(e) {
     50        result = "PASS: " + a + " threw: " + e;
     51    }
     52   
     53    print(result);
     54}
     55
    4256shouldBe("typeof MyObject", "function"); // our object implements 'call'
    4357MyObject.cantFind = 1;
     
    6983      : "__FAIL__: MyObject.regularType was not enumerated");
    7084
     85myObject = new MyObject();
     86
    7187shouldBe("delete MyObject.regularType", true);
    7288shouldBe("MyObject.regularType", undefined);
    7389shouldBe("MyObject(0)", 1);
    7490shouldBe("MyObject()", undefined);
    75 shouldBe("typeof new MyObject()", "object");
     91shouldBe("typeof myObject", "object");
    7692shouldBe("MyObject ? 1 : 0", true); // toBoolean
    7793shouldBe("+MyObject", 1); // toNumber
     
    8399shouldBe("typeof constructedObject", "object");
    84100shouldBe("constructedObject.value", 1);
    85 shouldBe("(new MyObject()) instanceof MyObject", true);
     101shouldBe("myObject instanceof MyObject", true);
    86102shouldBe("(new Object()) instanceof MyObject", false);
     103
     104shouldThrow("MyObject.nullGetSet = 1");
     105shouldThrow("MyObject.nullGetSet");
     106shouldThrow("MyObject.nullCall()");
     107shouldThrow("MyObject.hasPropertyLie");
Note: See TracChangeset for help on using the changeset viewer.