Changeset 15384 in webkit for trunk/JavaScriptCore
- Timestamp:
- Jul 12, 2006, 2:55:55 AM (19 years ago)
- Location:
- trunk/JavaScriptCore
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/API/JSCallbackObject.cpp
r15376 r15384 236 236 } 237 237 238 bool 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 247 bool 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 238 261 bool JSCallbackObject::implementsCall() const 239 262 { -
trunk/JavaScriptCore/API/JSCallbackObject.h
r15310 r15384 55 55 virtual JSObject* construct(ExecState*, const List& args); 56 56 57 virtual bool implementsHasInstance() const; 58 virtual bool hasInstance(ExecState *exec, JSValue *value); 59 57 60 virtual bool implementsCall() const; 58 61 virtual JSValue* callAsFunction(ExecState*, JSObject* thisObj, const List &args); -
trunk/JavaScriptCore/API/JSClassRef.cpp
r15224 r15384 31 31 using namespace KJS; 32 32 33 const JSObjectCallbacks kJSObjectCallbacksNone = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };33 const JSObjectCallbacks kJSObjectCallbacksNone = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 34 34 35 35 JSClassRef JSClassCreate(JSStaticValue* staticValues, JSStaticFunction* staticFunctions, const JSObjectCallbacks* callbacks, JSClassRef parentClass) -
trunk/JavaScriptCore/API/JSObjectRef.h
r15376 r15384 194 194 /*! 195 195 @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. 197 197 @param context The current execution context. 198 198 @param constructor A JSObject that is the constructor being called. … … 207 207 If your callback were invoked by the JavaScript expression 'new myConstructorFunction()', constructor would be set to myConstructorFunction. 208 208 209 If this callback is NULL, using your object as a constructor in a 'new' statementwill throw an exception.209 If this callback is NULL, using your object as a constructor in a 'new' expression will throw an exception. 210 210 */ 211 211 typedef JSObjectRef 212 212 (*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 225 bool HasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception); 226 227 If 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 229 If this callback is NULL, using your object in an 'instanceof' will always return false. 230 231 Standard JavaScript practice calls for objects that implement the callAsConstructor callback to implement the hasInstance callback as well. 232 */ 233 typedef bool 234 (*JSObjectHasInstanceCallback) (JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception); 213 235 214 236 /*! … … 241 263 @field getPropertyList The callback invoked when adding an object's properties to a property list. 242 264 @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. 244 267 @field convertToType The callback invoked when converting an object to a particular JavaScript type. 245 268 */ … … 255 278 JSObjectCallAsFunctionCallback callAsFunction; 256 279 JSObjectCallAsConstructorCallback callAsConstructor; 280 JSObjectHasInstanceCallback hasInstance; 257 281 JSObjectConvertToTypeCallback convertToType; 258 282 } JSObjectCallbacks; … … 344 368 @abstract Convenience method for creating a JavaScript constructor with a given callback as its implementation. 345 369 @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. 347 371 @result A JSObject that is a constructor. The object's prototype will be the default object prototype. 348 372 */ -
trunk/JavaScriptCore/API/testapi.c
r15376 r15384 201 201 } 202 202 203 static 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 203 214 static JSValueRef MyObject_convertToType(JSContextRef context, JSObjectRef object, JSType type, JSValueRef* exception) 204 215 { … … 229 240 JSObjectCallbacks MyObject_callbacks = { 230 241 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, 241 253 }; 242 254 -
trunk/JavaScriptCore/API/testapi.js
r15133 r15384 82 82 shouldBe("typeof constructedObject", "object"); 83 83 shouldBe("constructedObject.value", 1); 84 shouldBe("(new MyObject()) instanceof MyObject", true); 85 shouldBe("(new Object()) instanceof MyObject", false); -
trunk/JavaScriptCore/ChangeLog
r15376 r15384 1 2006-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 1 17 2006-07-11 Geoffrey Garen <[email protected]> 2 18
Note:
See TracChangeset
for help on using the changeset viewer.