Changeset 15384 in webkit for trunk/JavaScriptCore


Ignore:
Timestamp:
Jul 12, 2006, 2:55:55 AM (19 years ago)
Author:
mjs
Message:

4eviewed by Geoff.


  • add handling of hasInstance callback for API objects
  • API/JSCallbackObject.cpp: (KJS::JSCallbackObject::implementsHasInstance): Check if callback is present. (KJS::JSCallbackObject::hasInstance): Invoke appropriate callback.
  • API/JSCallbackObject.h:
  • API/JSClassRef.cpp:
  • API/JSObjectRef.h:
  • API/testapi.c: (MyObject_hasInstance): Test case; should match what construct would do.
  • API/testapi.js:
Location:
trunk/JavaScriptCore
Files:
7 edited

Legend:

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

    r15376 r15384  
    236236}
    237237
     238bool JSCallbackObject::implementsHasInstance() const
     239{
     240    for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parent)
     241        if (jsClass->callbacks.hasInstance)
     242            return true;
     243
     244    return false;
     245}
     246
     247bool JSCallbackObject::hasInstance(ExecState *exec, JSValue *value)
     248{
     249    JSContextRef execRef = toRef(exec);
     250    JSObjectRef thisRef = toRef(this);
     251
     252    for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parent)
     253        if (JSObjectHasInstanceCallback hasInstance = jsClass->callbacks.hasInstance)
     254            return hasInstance(execRef, thisRef, toRef(value), toRef(exec->exceptionSlot()));
     255
     256    ASSERT(0); // implementsHasInstance should prevent us from reaching here
     257    return 0;
     258}
     259
     260
    238261bool JSCallbackObject::implementsCall() const
    239262{
  • trunk/JavaScriptCore/API/JSCallbackObject.h

    r15310 r15384  
    5555    virtual JSObject* construct(ExecState*, const List& args);
    5656
     57    virtual bool implementsHasInstance() const;
     58    virtual bool hasInstance(ExecState *exec, JSValue *value);
     59
    5760    virtual bool implementsCall() const;
    5861    virtual JSValue* callAsFunction(ExecState*, JSObject* thisObj, const List &args);
  • trunk/JavaScriptCore/API/JSClassRef.cpp

    r15224 r15384  
    3131using namespace KJS;
    3232
    33 const JSObjectCallbacks kJSObjectCallbacksNone = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
     33const JSObjectCallbacks kJSObjectCallbacksNone = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    3434
    3535JSClassRef JSClassCreate(JSStaticValue* staticValues, JSStaticFunction* staticFunctions, const JSObjectCallbacks* callbacks, JSClassRef parentClass)
  • trunk/JavaScriptCore/API/JSObjectRef.h

    r15376 r15384  
    194194/*!
    195195@typedef JSObjectCallAsConstructorCallback
    196 @abstract The callback invoked when an object is used as a constructor in a 'new' statement.
     196@abstract The callback invoked when an object is used as a constructor in a 'new' expression.
    197197@param context The current execution context.
    198198@param constructor A JSObject that is the constructor being called.
     
    207207If your callback were invoked by the JavaScript expression 'new myConstructorFunction()', constructor would be set to myConstructorFunction.
    208208
    209 If this callback is NULL, using your object as a constructor in a 'new' statement will throw an exception.
     209If this callback is NULL, using your object as a constructor in a 'new' expression will throw an exception.
    210210*/
    211211typedef JSObjectRef
    212212(*JSObjectCallAsConstructorCallback) (JSContextRef context, JSObjectRef constructor, size_t argc, JSValueRef argv[], JSValueRef* exception);
     213
     214/*!
     215@typedef JSObjectHasInstanceCallback
     216@abstract The callback invoked when an object is used in an 'instanceof' expression.
     217@param context The current execution context.
     218@param constructor The JSObject receiving the hasInstance request
     219@param possibleInstance The JSValue being tested to determine if it is an instance of constructor.
     220@param exception A pointer to a JSValueRef in which to return an exception, if any.
     221@result true if possibleInstance is an instance of constructor, otherwise false
     222
     223@discussion If you named your function HasInstance, you would declare it like this:
     224
     225bool HasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception);
     226
     227If your callback were invoked by the JavaScript expression 'someValue instanceof myObject', constructor would be set to myObject and possibleInstance would be set to someValue..
     228
     229If this callback is NULL, using your object in an 'instanceof' will always return false.
     230
     231Standard JavaScript practice calls for objects that implement the callAsConstructor callback to implement the hasInstance callback as well.
     232*/
     233typedef bool
     234(*JSObjectHasInstanceCallback)  (JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception);
    213235
    214236/*!
     
    241263@field getPropertyList The callback invoked when adding an object's properties to a property list.
    242264@field callAsFunction The callback invoked when an object is called as a function.
    243 @field callAsConstructor The callback invoked when an object is used as a constructor in a 'new' statement.
     265@field hasInstance The callback invoked when an object is used in an 'instanceof' expression.
     266@field callAsConstructor The callback invoked when an object is used as a constructor in a 'new' expression.
    244267@field convertToType The callback invoked when converting an object to a particular JavaScript type.
    245268*/
     
    255278    JSObjectCallAsFunctionCallback      callAsFunction;
    256279    JSObjectCallAsConstructorCallback   callAsConstructor;
     280    JSObjectHasInstanceCallback         hasInstance;
    257281    JSObjectConvertToTypeCallback       convertToType;
    258282} JSObjectCallbacks;
     
    344368@abstract Convenience method for creating a JavaScript constructor with a given callback as its implementation.
    345369@param context The execution context to use.
    346 @param callAsConstructor The JSObjectCallAsConstructorCallback to invoke when the constructor is used in a 'new' statement.
     370@param callAsConstructor The JSObjectCallAsConstructorCallback to invoke when the constructor is used in a 'new' expression.
    347371@result A JSObject that is a constructor. The object's prototype will be the default object prototype.
    348372*/
  • trunk/JavaScriptCore/API/testapi.c

    r15376 r15384  
    201201}
    202202
     203static bool MyObject_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleValue, JSValueRef* exception)
     204{
     205    UNUSED_PARAM(context);
     206
     207    JSStringRef numberString = JSStringCreateWithUTF8CString("Number");
     208    JSObjectRef numberConstructor = JSValueToObject(context, JSObjectGetProperty(context, JSContextGetGlobalObject(context), numberString));
     209    JSStringRelease(numberString);
     210
     211    return JSValueIsInstanceOfConstructor(context, possibleValue, numberConstructor);
     212}
     213
    203214static JSValueRef MyObject_convertToType(JSContextRef context, JSObjectRef object, JSType type, JSValueRef* exception)
    204215{
     
    229240JSObjectCallbacks MyObject_callbacks = {
    230241    0,
    231     &MyObject_initialize,
    232     &MyObject_finalize,
    233     &MyObject_hasProperty,
    234     &MyObject_getProperty,
    235     &MyObject_setProperty,
    236     &MyObject_deleteProperty,
    237     &MyObject_getPropertyList,
    238     &MyObject_callAsFunction,
    239     &MyObject_callAsConstructor,
    240     &MyObject_convertToType,
     242    MyObject_initialize,
     243    MyObject_finalize,
     244    MyObject_hasProperty,
     245    MyObject_getProperty,
     246    MyObject_setProperty,
     247    MyObject_deleteProperty,
     248    MyObject_getPropertyList,
     249    MyObject_callAsFunction,
     250    MyObject_callAsConstructor,
     251    MyObject_hasInstance,
     252    MyObject_convertToType,
    241253};
    242254
  • trunk/JavaScriptCore/API/testapi.js

    r15133 r15384  
    8282shouldBe("typeof constructedObject", "object");
    8383shouldBe("constructedObject.value", 1);
     84shouldBe("(new MyObject()) instanceof MyObject", true);
     85shouldBe("(new Object()) instanceof MyObject", false);
  • trunk/JavaScriptCore/ChangeLog

    r15376 r15384  
     12006-07-12  Maciej Stachowiak  <[email protected]>
     2
     3        4eviewed by Geoff.
     4       
     5        - add handling of hasInstance callback for API objects
     6
     7        * API/JSCallbackObject.cpp:
     8        (KJS::JSCallbackObject::implementsHasInstance): Check if callback is present.
     9        (KJS::JSCallbackObject::hasInstance): Invoke appropriate callback.
     10        * API/JSCallbackObject.h:
     11        * API/JSClassRef.cpp:
     12        * API/JSObjectRef.h:
     13        * API/testapi.c:
     14        (MyObject_hasInstance): Test case; should match what construct would do.
     15        * API/testapi.js:
     16
    1172006-07-11  Geoffrey Garen  <[email protected]>
    218
Note: See TracChangeset for help on using the changeset viewer.