Changeset 15483 in webkit for trunk/JavaScriptCore/API/testapi.c


Ignore:
Timestamp:
Jul 17, 2006, 2:06:57 AM (19 years ago)
Author:
ggaren
Message:

Reviewed by Maciej.


  • Changed the initialize callback to run from least derived class (parent class) to most derived class. This enables C++ style initialization, and derived class overriding of member data.


  • Added excpetion propopgation to JSObjectMake, to support initialize exceptions, and generally round out our policy of making function signatures as long as possible.
  • API/JSCallbackObject.h: Use ExecState instead of ContextRef, cuz we're in C++ land now.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/API/testapi.c

    r15482 r15483  
    242242}
    243243
    244 static bool didFinalize = false;
     244static bool MyObject_didFinalize = false;
    245245static void MyObject_finalize(JSObjectRef object)
    246246{
    247247    UNUSED_PARAM(context);
    248248    UNUSED_PARAM(object);
    249     didFinalize = true;
     249    MyObject_didFinalize = true;
    250250}
    251251
     
    291291}
    292292
     293static void Base_initialize(JSContextRef context, JSObjectRef object, JSValueRef* exception)
     294{
     295    assert(!JSObjectGetPrivate(object));
     296    JSObjectSetPrivate(object, (void*)1);
     297}
     298
     299static bool Base_didFinalize;
     300static void Base_finalize(JSObjectRef object)
     301{
     302    assert((void*)3 == JSObjectGetPrivate(object));
     303    Base_didFinalize = true;
     304}
     305
     306static JSClassRef Base_class(JSContextRef context)
     307{
     308    static JSClassRef jsClass;
     309    if (!jsClass) {
     310        JSClassDefinition definition = kJSClassDefinitionNull;
     311        definition.initialize = Base_initialize;
     312        definition.finalize = Base_finalize;
     313        jsClass = JSClassCreate(&definition);
     314    }
     315    return jsClass;
     316}
     317
     318static void Derived_initialize(JSContextRef context, JSObjectRef object, JSValueRef* exception)
     319{
     320    assert((void*)1 == JSObjectGetPrivate(object));
     321    JSObjectSetPrivate(object, (void*)2);
     322}
     323
     324static void Derived_finalize(JSObjectRef object)
     325{
     326    assert((void*)2 == JSObjectGetPrivate(object));
     327    JSObjectSetPrivate(object, (void*)3);
     328}
     329
     330static JSClassRef Derived_class(JSContextRef context)
     331{
     332    static JSClassRef jsClass;
     333    if (!jsClass) {
     334        JSClassDefinition definition = kJSClassDefinitionNull;
     335        definition.parentClass = Base_class(context);
     336        definition.initialize = Derived_initialize;
     337        definition.finalize = Derived_finalize;
     338        jsClass = JSClassCreate(&definition);
     339    }
     340    return jsClass;
     341}
     342
    293343static JSValueRef print_callAsFunction(JSContextRef context, JSObjectRef functionObject, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
    294344{
     
    312362    UNUSED_PARAM(constructorObject);
    313363   
    314     JSObjectRef result = JSObjectMake(context, NULL, 0);
     364    JSObjectRef result = JSObjectMake(context, NULL, 0, NULL);
    315365    if (argumentCount > 0) {
    316366        JSStringRef value = JSStringCreateWithUTF8CString("value");
     
    341391    JSValueRef jsOne = JSValueMakeNumber(context, 1);
    342392    JSValueRef jsOneThird = JSValueMakeNumber(context, 1.0 / 3.0);
    343     JSObjectRef jsObjectNoProto = JSObjectMake(context, NULL, JSValueMakeNull(context));
     393    JSObjectRef jsObjectNoProto = JSObjectMake(context, NULL, JSValueMakeNull(context), NULL);
    344394
    345395    // FIXME: test funny utf8 characters
     
    395445#endif // __APPLE__
    396446
    397     JSObjectRef myObject = JSObjectMake(context, MyObject_class(context), NULL);
     447    JSObjectRef myObject = JSObjectMake(context, MyObject_class(context), NULL, NULL);
    398448    assert(didInitialize);
    399449    JSStringRef myObjectIString = JSStringCreateWithUTF8CString("MyObject");
     
    509559#endif // __APPLE__
    510560   
    511     jsGlobalValue = JSObjectMake(context, NULL, NULL);
     561    jsGlobalValue = JSObjectMake(context, NULL, NULL, NULL);
    512562    JSValueProtect(context, jsGlobalValue);
    513563    JSGarbageCollect(context);
     
    617667    assert(!JSObjectGetPrivate(myConstructor));
    618668   
    619     o = JSObjectMake(context, NULL, NULL);
     669    o = JSObjectMake(context, NULL, NULL, NULL);
    620670    JSObjectSetProperty(context, o, jsOneIString, JSValueMakeNumber(context, 1), kJSPropertyAttributeNone, NULL);
    621671    JSObjectSetProperty(context, o, jsCFIString,  JSValueMakeNumber(context, 1), kJSPropertyAttributeDontEnum, NULL);
     
    640690    assert(JSValueIsEqual(context, v, o, NULL));
    641691   
    642    
     692    exception = NULL;
     693    o = JSObjectMake(context, Derived_class(context), NULL, &exception);
     694    assert(!exception);
     695    assert(JSObjectGetPrivate(o) == (void*)2);
     696    o = NULL;
    643697   
    644698    char* scriptUTF8 = createStringWithContentsOfFile("testapi.js");
     
    659713
    660714    // Allocate a few dummies so that at least one will be collected
    661     JSObjectMake(context, MyObject_class(context), 0);
    662     JSObjectMake(context, MyObject_class(context), 0);
     715    JSObjectMake(context, MyObject_class(context), NULL, NULL);
     716    JSObjectMake(context, MyObject_class(context), NULL, NULL);
    663717    JSGarbageCollect(context);
    664     assert(didFinalize);
     718    assert(MyObject_didFinalize);
     719    assert(Base_didFinalize);
    665720
    666721    JSStringRelease(jsEmptyIString);
Note: See TracChangeset for help on using the changeset viewer.