Changeset 15443 in webkit for trunk/JavaScriptCore
- Timestamp:
- Jul 14, 2006, 9:10:31 PM (19 years ago)
- Location:
- trunk/JavaScriptCore
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/API/JSCallbackObject.cpp
r15400 r15443 90 90 // optional optimization to bypass getProperty in cases when we only need to know if the property exists 91 91 if (JSObjectHasPropertyCallback hasProperty = jsClass->callbacks.hasProperty) { 92 if (hasProperty(context, thisRef, propertyNameRef , toRef(exec->exceptionSlot()))) {92 if (hasProperty(context, thisRef, propertyNameRef)) { 93 93 slot.setCustom(this, callbackGetter); 94 94 return true; … … 322 322 } 323 323 324 bool JSCallbackObject::toBoolean(ExecState* exec) const325 {326 JSContextRef context = toRef(exec);327 JSObjectRef thisRef = toRef(this);328 329 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parent)330 if (JSObjectConvertToTypeCallback convertToType = jsClass->callbacks.convertToType)331 if (JSValueRef value = convertToType(context, thisRef, kJSTypeBoolean, toRef(exec->exceptionSlot())))332 return toJS(value)->getBoolean();333 334 return JSObject::toBoolean(exec);335 }336 337 324 double JSCallbackObject::toNumber(ExecState* exec) const 338 325 { -
trunk/JavaScriptCore/API/JSCallbackObject.h
r15385 r15443 63 63 virtual void getPropertyList(ReferenceList& propertyList, bool recursive); 64 64 65 virtual bool toBoolean(ExecState*) const;66 65 virtual double toNumber(ExecState*) const; 67 66 virtual UString toString(ExecState*) const; -
trunk/JavaScriptCore/API/JSObjectRef.cpp
r15434 r15443 121 121 } 122 122 123 JSValueRef JSObjectGetProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName )123 JSValueRef JSObjectGetProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) 124 124 { 125 125 JSLock lock; … … 130 130 JSValue* jsValue = jsObject->get(exec, Identifier(nameRep)); 131 131 if (jsValue->isUndefined()) 132 return 0; 132 jsValue = 0; 133 if (exec->hadException()) { 134 if (exception) 135 *exception = toRef(exec->exception()); 136 exec->clearException(); 137 } 133 138 return toRef(jsValue); 134 139 } 135 140 136 void JSObjectSetProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSPropertyAttributes attributes )141 void JSObjectSetProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSPropertyAttributes attributes, JSValueRef* exception) 137 142 { 138 143 JSLock lock; … … 143 148 144 149 jsObject->put(exec, Identifier(nameRep), jsValue, attributes); 150 if (exec->hadException()) { 151 if (exception) 152 *exception = toRef(exec->exception()); 153 exec->clearException(); 154 } 145 155 } 146 156 … … 168 178 } 169 179 170 bool JSObjectDeleteProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName )180 bool JSObjectDeleteProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) 171 181 { 172 182 JSLock lock; … … 175 185 UString::Rep* nameRep = toJS(propertyName); 176 186 177 return jsObject->deleteProperty(exec, Identifier(nameRep)); 187 bool result = jsObject->deleteProperty(exec, Identifier(nameRep)); 188 if (exec->hadException()) { 189 if (exception) 190 *exception = toRef(exec->exception()); 191 exec->clearException(); 192 } 193 return result; 178 194 } 179 195 -
trunk/JavaScriptCore/API/JSObjectRef.h
r15434 r15443 88 88 @param object The JSObject to search for the property. 89 89 @param propertyName A JSString containing the name of the property look up. 90 @param exception A pointer to a JSValueRef in which to return an exception, if any.91 90 @result true if object has the property, otherwise false. 92 91 @discussion If you named your function HasProperty, you would declare it like this: 93 92 94 bool HasProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName , JSValueRef* exception);93 bool HasProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName); 95 94 96 95 If this function returns false, the hasProperty request forwards to object's static property table, then its parent class chain (which includes the default object class), then its prototype chain. 97 96 98 This callback enables optimization in cases where only a property's existence needs to be known, not its value, and computing its value would be expensive. If this callback is NULL, the getProperty callback will be used to service hasProperty requests. 97 This callback enables optimization in cases where only a property's existence needs to be known, not its value, and computing its value would be expensive. 98 99 If this callback is NULL, the getProperty callback will be used to service hasProperty requests. 99 100 */ 100 101 typedef bool 101 (*JSObjectHasPropertyCallback) (JSContextRef context, JSObjectRef object, JSStringRef propertyName , JSValueRef* exception);102 (*JSObjectHasPropertyCallback) (JSContextRef context, JSObjectRef object, JSStringRef propertyName); 102 103 103 104 /*! … … 212 213 /*! 213 214 @typedef JSObjectHasInstanceCallback 214 @abstract The callback invoked when an object is used inan 'instanceof' expression.215 @param context The current execution context. 216 @param constructor The JSObject receiving the hasInstance request215 @abstract hasInstance The callback invoked when an object is used as the target of an 'instanceof' expression. 216 @param context The current execution context. 217 @param constructor The JSObject that is the target of the 'instanceof' expression. 217 218 @param possibleInstance The JSValue being tested to determine if it is an instance of constructor. 218 219 @param exception A pointer to a JSValueRef in which to return an exception, if any. 219 @result true if possibleInstance is an instance of constructor, otherwise false 220 @result true if possibleInstance is an instance of constructor, otherwise false. 220 221 221 222 @discussion If you named your function HasInstance, you would declare it like this: … … 223 224 bool HasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception); 224 225 225 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. .226 227 If this callback is NULL, using your object in an 'instanceof' will alwaysreturn false.226 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. 227 228 If this callback is NULL, 'instanceof' expressions that target your object will return false. 228 229 229 230 Standard JavaScript practice calls for objects that implement the callAsConstructor callback to implement the hasInstance callback as well. … … 245 246 246 247 If this function returns false, the conversion request forwards to object's parent class chain (which includes the default object class). 248 249 This function is only invoked when converting an object to number or string. An object converted to boolean is 'true.' An object converted to object is itself. 247 250 */ 248 251 typedef JSValueRef … … 261 264 @field addPropertiesToList The callback invoked when adding an object's properties to a property list. 262 265 @field callAsFunction The callback invoked when an object is called as a function. 263 @field hasInstance The callback invoked when an object is used inan 'instanceof' expression.266 @field hasInstance The callback invoked when an object is used as the target of an 'instanceof' expression. 264 267 @field callAsConstructor The callback invoked when an object is used as a constructor in a 'new' expression. 265 268 @field convertToType The callback invoked when converting an object to a particular JavaScript type. … … 414 417 @param object The JSObject whose property you want to get. 415 418 @param propertyName A JSString containing the property's name. 419 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception. 416 420 @result The property's value if object has the property, otherwise NULL. 417 421 */ 418 JSValueRef JSObjectGetProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName );422 JSValueRef JSObjectGetProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception); 419 423 420 424 /*! … … 425 429 @param propertyName A JSString containing the property's name. 426 430 @param value A JSValue to use as the property's value. 431 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception. 427 432 @param attributes A logically ORed set of JSPropertyAttributes to give to the property. 428 433 */ 429 void JSObjectSetProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSPropertyAttributes attributes );434 void JSObjectSetProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSPropertyAttributes attributes, JSValueRef* exception); 430 435 431 436 /*! … … 435 440 @param object The JSObject whose property you want to delete. 436 441 @param propertyName A JSString containing the property's name. 442 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception. 437 443 @result true if the delete operation succeeds, otherwise false (for example, if the property has the kJSPropertyAttributeDontDelete attribute set). 438 444 */ 439 bool JSObjectDeleteProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName );445 bool JSObjectDeleteProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception); 440 446 441 447 /*! … … 496 502 @param argc An integer count of the number of arguments in argv. 497 503 @param argv A JSValue array of the arguments to pass to the function. 498 @param exception A pointer to a JSValueRef in which to store an uncaught exception, if any. Pass NULL if you do not care to store an uncaughtexception.499 @result The JSValue that results from calling object as a function, or NULL if an uncaughtexception is thrown or object is not a function.504 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception. 505 @result The JSValue that results from calling object as a function, or NULL if an exception is thrown or object is not a function. 500 506 */ 501 507 JSValueRef JSObjectCallAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argc, JSValueRef argv[], JSValueRef* exception); … … 514 520 @param argc An integer count of the number of arguments in argv. 515 521 @param argv A JSValue array of the arguments to pass to the function. 516 @param exception A pointer to a JSValueRef in which to store an uncaught exception, if any. Pass NULL if you do not care to store an uncaughtexception.517 @result The JSObject that results from calling object as a constructor, or NULL if an uncaughtexception is thrown or object is not a constructor.522 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception. 523 @result The JSObject that results from calling object as a constructor, or NULL if an exception is thrown or object is not a constructor. 518 524 */ 519 525 JSObjectRef JSObjectCallAsConstructor(JSContextRef context, JSObjectRef object, size_t argc, JSValueRef argv[], JSValueRef* exception); -
trunk/JavaScriptCore/API/JSValueRef.cpp
r15428 r15443 138 138 } 139 139 140 bool JSValueIsInstanceOfConstructor(JSContextRef context, JSValueRef value, JSObjectRef constructor )140 bool JSValueIsInstanceOfConstructor(JSContextRef context, JSValueRef value, JSObjectRef constructor, JSValueRef* exception) 141 141 { 142 142 ExecState* exec = toJS(context); … … 145 145 if (!jsConstructor->implementsHasInstance()) 146 146 return false; 147 bool result = jsConstructor->hasInstance(exec, jsValue); 148 if (exec->hadException()) 149 exec->clearException(); 147 bool result = jsConstructor->hasInstance(exec, jsValue); // false if an exception is thrown 148 if (exec->hadException()) { 149 if (exception) 150 *exception = toRef(exec->exception()); 151 exec->clearException(); 152 } 150 153 return result; 151 154 } … … 172 175 } 173 176 174 bool JSValueToBoolean(JSContextRef context, JSValueRef value, JSValueRef* exception) 175 { 176 JSLock lock; 177 ExecState* exec = toJS(context); 178 JSValue* jsValue = toJS(value); 179 180 bool boolean = jsValue->toBoolean(exec); 181 if (exec->hadException()) { 182 if (exception) 183 *exception = toRef(exec->exception()); 184 exec->clearException(); 185 boolean = false; 186 } 187 return boolean; 177 bool JSValueToBoolean(JSContextRef context, JSValueRef value) 178 { 179 ExecState* exec = toJS(context); 180 JSValue* jsValue = toJS(value); 181 return jsValue->toBoolean(exec); 188 182 } 189 183 -
trunk/JavaScriptCore/API/JSValueRef.h
r15428 r15443 146 146 /*! 147 147 @function 148 @abstract Tests whether a JavaScript value is an object constructed by 149 a given constructor, as compared by the JS instanceof operator. 150 @param context The execution context to use. 151 @param value The JSValue to test. 152 @param object The constructor to test against. 153 @result true if value is an object constructed by constructor, as compared 154 by the JS instanceof operator, otherwise false. 155 */ 156 bool JSValueIsInstanceOfConstructor(JSContextRef context, JSValueRef value, JSObjectRef constructor); 148 @abstract Tests whether a JavaScript value is an object constructed by a given constructor, as compared by the JS instanceof operator. 149 @param context The execution context to use. 150 @param value The JSValue to test. 151 @param object The constructor to test against. 152 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception. 153 @result true if value is an object constructed by constructor, as compared by the JS instanceof operator, otherwise false. 154 */ 155 bool JSValueIsInstanceOfConstructor(JSContextRef context, JSValueRef value, JSObjectRef constructor, JSValueRef* exception); 157 156 158 157 // Creating values … … 205 204 @param context The execution context to use. 206 205 @param value The JSValue to convert. 207 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception. 208 @result The boolean result of conversion, or false if an exception is thrown. 209 */ 210 bool JSValueToBoolean(JSContextRef context, JSValueRef value, JSValueRef* exception); 206 @result The boolean result of conversion. 207 */ 208 bool JSValueToBoolean(JSContextRef context, JSValueRef value); 211 209 212 210 /*! … … 236 234 @param value The JSValue to convert. 237 235 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception. 238 @result The JSObject result of conversion, or NULL if conversion fails.236 @result The JSObject result of conversion, or NULL if an exception is thrown. 239 237 */ 240 238 JSObjectRef JSValueToObject(JSContextRef context, JSValueRef value, JSValueRef* exception); -
trunk/JavaScriptCore/API/minidom.c
r15437 r15443 41 41 42 42 JSStringRef printIString = JSStringCreateWithUTF8CString("print"); 43 JSObjectSetProperty(context, globalObject, printIString, JSObjectMakeFunction(context, print), kJSPropertyAttributeNone );43 JSObjectSetProperty(context, globalObject, printIString, JSObjectMakeFunction(context, print), kJSPropertyAttributeNone, NULL); 44 44 JSStringRelease(printIString); 45 45 46 46 JSStringRef node = JSStringCreateWithUTF8CString("Node"); 47 JSObjectSetProperty(context, globalObject, node, JSObjectMakeConstructor(context, JSNode_construct), kJSPropertyAttributeNone );47 JSObjectSetProperty(context, globalObject, node, JSObjectMakeConstructor(context, JSNode_construct), kJSPropertyAttributeNone, NULL); 48 48 JSStringRelease(node); 49 49 -
trunk/JavaScriptCore/API/testapi.c
r15437 r15443 39 39 static void assertEqualsAsBoolean(JSValueRef value, bool expectedValue) 40 40 { 41 if (JSValueToBoolean(context, value , NULL) != expectedValue)41 if (JSValueToBoolean(context, value) != expectedValue) 42 42 fprintf(stderr, "assertEqualsAsBoolean failed: %p, %d\n", value, expectedValue); 43 43 } … … 106 106 } 107 107 108 static bool MyObject_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName , JSValueRef* exception)108 static bool MyObject_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName) 109 109 { 110 110 UNUSED_PARAM(context); … … 160 160 return true; 161 161 162 if (JSStringIsEqualToUTF8CString(propertyName, "throwOnDelete")) { 163 *exception = JSValueMakeNumber(2); 164 return false; 165 } 166 162 167 return false; 163 168 } … … 206 211 207 212 JSStringRef numberString = JSStringCreateWithUTF8CString("Number"); 208 JSObjectRef numberConstructor = JSValueToObject(context, JSObjectGetProperty(context, JSContextGetGlobalObject(context), numberString ), NULL);213 JSObjectRef numberConstructor = JSValueToObject(context, JSObjectGetProperty(context, JSContextGetGlobalObject(context), numberString, NULL), NULL); 209 214 JSStringRelease(numberString); 210 215 211 return JSValueIsInstanceOfConstructor(context, possibleValue, numberConstructor );216 return JSValueIsInstanceOfConstructor(context, possibleValue, numberConstructor, NULL); 212 217 } 213 218 … … 218 223 219 224 switch (type) { 220 case kJSTypeBoolean:221 *exception = JSValueMakeNumber(2);222 return NULL;223 225 case kJSTypeNumber: 224 226 return JSValueMakeNumber(1); … … 288 290 if (argc > 0) { 289 291 JSStringRef value = JSStringCreateWithUTF8CString("value"); 290 JSObjectSetProperty(context, result, value, argv[0], kJSPropertyAttributeNone );292 JSObjectSetProperty(context, result, value, argv[0], kJSPropertyAttributeNone, NULL); 291 293 JSStringRelease(value); 292 294 } … … 371 373 assert(didInitialize); 372 374 JSStringRef myObjectIString = JSStringCreateWithUTF8CString("MyObject"); 373 JSObjectSetProperty(context, globalObject, myObjectIString, myObject, kJSPropertyAttributeNone );375 JSObjectSetProperty(context, globalObject, myObjectIString, myObject, kJSPropertyAttributeNone, NULL); 374 376 JSStringRelease(myObjectIString); 375 377 … … 389 391 assert(exception); 390 392 391 exception = NULL; 392 assert(!JSValueToBoolean(context, myObject, &exception)); 393 assert(exception); 393 assert(JSValueToBoolean(context, myObject)); 394 394 395 395 exception = NULL; … … 505 505 506 506 JSStringRef array = JSStringCreateWithUTF8CString("Array"); 507 v = JSObjectGetProperty(context, globalObject, array );507 v = JSObjectGetProperty(context, globalObject, array, NULL); 508 508 assert(v); 509 509 JSObjectRef arrayConstructor = JSValueToObject(context, v, NULL); … … 511 511 result = JSObjectCallAsConstructor(context, arrayConstructor, 0, NULL, NULL); 512 512 assert(result); 513 assert(JSValueIsInstanceOfConstructor(context, result, arrayConstructor ));514 assert(!JSValueIsInstanceOfConstructor(context, JSValueMakeNull(), arrayConstructor ));513 assert(JSValueIsInstanceOfConstructor(context, result, arrayConstructor, NULL)); 514 assert(!JSValueIsInstanceOfConstructor(context, JSValueMakeNull(), arrayConstructor, NULL)); 515 515 516 516 JSStringRef functionBody; … … 521 521 assert(!JSObjectMakeFunctionWithBody(context, functionBody, NULL, 1, &exception)); 522 522 assert(JSValueIsObject(exception)); 523 v = JSObjectGetProperty(context, JSValueToObject(context, exception, NULL), line );523 v = JSObjectGetProperty(context, JSValueToObject(context, exception, NULL), line, NULL); 524 524 assert(v); 525 525 assertEqualsAsNumber(v, 2); // FIXME: Lexer::setCode bumps startingLineNumber by 1 -- we need to change internal callers so that it doesn't have to (saying '0' to mean '1' in the API would be really confusing -- it's really confusing internally, in fact) … … 537 537 JSStringRef print = JSStringCreateWithUTF8CString("print"); 538 538 JSObjectRef printFunction = JSObjectMakeFunction(context, print_callAsFunction); 539 JSObjectSetProperty(context, globalObject, print, printFunction, kJSPropertyAttributeNone );539 JSObjectSetProperty(context, globalObject, print, printFunction, kJSPropertyAttributeNone, NULL); 540 540 JSStringRelease(print); 541 541 … … 545 545 JSStringRef myConstructorIString = JSStringCreateWithUTF8CString("MyConstructor"); 546 546 JSObjectRef myConstructor = JSObjectMakeConstructor(context, myConstructor_callAsConstructor); 547 JSObjectSetProperty(context, globalObject, myConstructorIString, myConstructor, kJSPropertyAttributeNone );547 JSObjectSetProperty(context, globalObject, myConstructorIString, myConstructor, kJSPropertyAttributeNone, NULL); 548 548 JSStringRelease(myConstructorIString); 549 549 … … 552 552 553 553 o = JSObjectMake(context, NULL, NULL); 554 JSObjectSetProperty(context, o, jsOneIString, JSValueMakeNumber(1), kJSPropertyAttributeNone );555 JSObjectSetProperty(context, o, jsCFIString, JSValueMakeNumber(1), kJSPropertyAttributeDontEnum );554 JSObjectSetProperty(context, o, jsOneIString, JSValueMakeNumber(1), kJSPropertyAttributeNone, NULL); 555 JSObjectSetProperty(context, o, jsCFIString, JSValueMakeNumber(1), kJSPropertyAttributeDontEnum, NULL); 556 556 JSPropertyEnumeratorRef enumerator = JSObjectCreatePropertyEnumerator(o); 557 557 int count = 0; -
trunk/JavaScriptCore/API/testapi.js
r15404 r15443 50 50 delete MyObject.cantDelete; 51 51 shouldBe("MyObject.cantDelete", 1); 52 shouldBe("delete MyObject.throwOnDelete", 2); // deleteProperty -- should throw 2 52 53 MyObject.cantSet = 1; 53 54 shouldBe("MyObject.cantSet", undefined); … … 73 74 shouldBe("MyObject()", undefined); 74 75 shouldBe("typeof new MyObject()", "object"); 75 shouldBe("MyObject ? 1 : 0", 2); // toBoolean -- should throw 276 shouldBe("MyObject ? 1 : 0", true); // toBoolean 76 77 shouldBe("+MyObject", 1); // toNumber 77 78 shouldBe("(MyObject.toString())", "[object CallbackObject]"); // toString -
trunk/JavaScriptCore/ChangeLog
r15440 r15443 1 2006-07-14 Geoffrey Garen <[email protected]> 2 3 Reviewed by Maciej. 4 5 - Finalized exception handling in the API. 6 7 setProperty can throw because it throws for built-in arrays. getProperty 8 and deleteProperty can throw because setProperty can throw and we want 9 to be consistent, and also because they seem like "actions." callAsFunction, 10 callAsConstructor, and hasInstance can throw, because they caan throw for 11 all built-ins. 12 13 toBoolean can't throw because it's defined that way in the spec. 14 15 - Documented that toBoolean and toObject can't be overridden by custom 16 objects because they're defined that way in the spec. 17 1 18 === Safari-521.17 === 2 19 -
trunk/JavaScriptCore/bindings/objc/objc_runtime.mm
r15071 r15443 305 305 } 306 306 307 bool ObjcFallbackObjectImp::toBoolean(ExecState *) const307 bool ObjcFallbackObjectImp::toBoolean(ExecState *) const 308 308 { 309 309 id targetObject = _instance->getObject();
Note:
See TracChangeset
for help on using the changeset viewer.