Changeset 41905 in webkit for trunk/JavaScriptCore/API
- Timestamp:
- Mar 22, 2009, 11:01:46 PM (16 years ago)
- Location:
- trunk/JavaScriptCore/API
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/API/JSCallbackObjectFunctions.h
r40046 r41905 127 127 propertyNameRef = OpaqueJSString::create(propertyName.ustring()); 128 128 JSLock::DropAllLocks dropAllLocks(exec); 129 if (JSValueRef value = getProperty(ctx, thisRef, propertyNameRef.get(), toRef(exec->exceptionSlot()))) { 129 JSValueRef exception = 0; 130 JSValueRef value = getProperty(ctx, thisRef, propertyNameRef.get(), &exception); 131 exec->setException(toJS(exception)); 132 if (value) { 130 133 slot.setValue(toJS(value)); 134 return true; 135 } 136 if (exception) { 137 slot.setValue(jsUndefined()); 131 138 return true; 132 139 } … … 170 177 propertyNameRef = OpaqueJSString::create(propertyName.ustring()); 171 178 JSLock::DropAllLocks dropAllLocks(exec); 172 if (setProperty(ctx, thisRef, propertyNameRef.get(), valueRef, toRef(exec->exceptionSlot()))) 179 JSValueRef exception = 0; 180 bool result = setProperty(ctx, thisRef, propertyNameRef.get(), valueRef, &exception); 181 exec->setException(toJS(exception)); 182 if (result || exception) 173 183 return; 174 184 } … … 182 192 propertyNameRef = OpaqueJSString::create(propertyName.ustring()); 183 193 JSLock::DropAllLocks dropAllLocks(exec); 184 if (setProperty(ctx, thisRef, propertyNameRef.get(), valueRef, toRef(exec->exceptionSlot()))) 194 JSValueRef exception = 0; 195 bool result = setProperty(ctx, thisRef, propertyNameRef.get(), valueRef, &exception); 196 exec->setException(toJS(exception)); 197 if (result || exception) 185 198 return; 186 199 } else … … 214 227 propertyNameRef = OpaqueJSString::create(propertyName.ustring()); 215 228 JSLock::DropAllLocks dropAllLocks(exec); 216 if (deleteProperty(ctx, thisRef, propertyNameRef.get(), toRef(exec->exceptionSlot()))) 229 JSValueRef exception = 0; 230 bool result = deleteProperty(ctx, thisRef, propertyNameRef.get(), &exception); 231 exec->setException(toJS(exception)); 232 if (result || exception) 217 233 return true; 218 234 } … … 269 285 arguments[i] = toRef(args.at(exec, i)); 270 286 JSLock::DropAllLocks dropAllLocks(exec); 271 return toJS(callAsConstructor(execRef, constructorRef, argumentCount, arguments.data(), toRef(exec->exceptionSlot()))); 287 JSValueRef exception = 0; 288 JSObject* result = toJS(callAsConstructor(execRef, constructorRef, argumentCount, arguments.data(), &exception)); 289 exec->setException(toJS(exception)); 290 return result; 272 291 } 273 292 } … … 286 305 if (JSObjectHasInstanceCallback hasInstance = jsClass->hasInstance) { 287 306 JSLock::DropAllLocks dropAllLocks(exec); 288 return hasInstance(execRef, thisRef, toRef(value), toRef(exec->exceptionSlot())); 307 JSValueRef exception = 0; 308 bool result = hasInstance(execRef, thisRef, toRef(value), &exception); 309 exec->setException(toJS(exception)); 310 return result; 289 311 } 290 312 } … … 318 340 arguments[i] = toRef(args.at(exec, i)); 319 341 JSLock::DropAllLocks dropAllLocks(exec); 320 return toJS(callAsFunction(execRef, functionRef, thisObjRef, argumentCount, arguments.data(), toRef(exec->exceptionSlot()))); 342 JSValueRef exception = 0; 343 JSValuePtr result = toJS(callAsFunction(execRef, functionRef, thisObjRef, argumentCount, arguments.data(), &exception)); 344 exec->setException(toJS(exception)); 345 return result; 321 346 } 322 347 } … … 378 403 if (JSObjectConvertToTypeCallback convertToType = jsClass->convertToType) { 379 404 JSLock::DropAllLocks dropAllLocks(exec); 380 if (JSValueRef value = convertToType(ctx, thisRef, kJSTypeNumber, toRef(exec->exceptionSlot()))) { 405 406 JSValueRef exception = 0; 407 JSValueRef value = convertToType(ctx, thisRef, kJSTypeNumber, &exception); 408 exec->setException(toJS(exception)); 409 if (value) { 381 410 double dValue; 382 411 return toJS(value).getNumber(dValue) ? dValue : NaN; … … 396 425 if (JSObjectConvertToTypeCallback convertToType = jsClass->convertToType) { 397 426 JSValueRef value; 427 JSValueRef exception = 0; 398 428 { 399 429 JSLock::DropAllLocks dropAllLocks(exec); 400 value = convertToType(ctx, thisRef, kJSTypeString, toRef(exec->exceptionSlot())); 430 value = convertToType(ctx, thisRef, kJSTypeString, &exception); 431 exec->setException(toJS(exception)); 401 432 } 402 433 if (value) 403 434 return toJS(value).getString(); 435 if (exception) 436 return ""; 404 437 } 405 438 … … 444 477 propertyNameRef = OpaqueJSString::create(propertyName.ustring()); 445 478 JSLock::DropAllLocks dropAllLocks(exec); 446 if (JSValueRef value = getProperty(toRef(exec), thisRef, propertyNameRef.get(), toRef(exec->exceptionSlot()))) 479 JSValueRef exception = 0; 480 JSValueRef value = getProperty(toRef(exec), thisRef, propertyNameRef.get(), &exception); 481 exec->setException(toJS(exception)); 482 if (value) 447 483 return toJS(value); 484 if (exception) 485 return jsUndefined(); 448 486 } 449 487 … … 489 527 propertyNameRef = OpaqueJSString::create(propertyName.ustring()); 490 528 JSLock::DropAllLocks dropAllLocks(exec); 491 if (JSValueRef value = getProperty(toRef(exec), thisRef, propertyNameRef.get(), toRef(exec->exceptionSlot()))) 529 530 JSValueRef exception = 0; 531 JSValueRef value = getProperty(toRef(exec), thisRef, propertyNameRef.get(), &exception); 532 exec->setException(toJS(exception)); 533 if (value) 492 534 return toJS(value); 535 if (exception) 536 return jsUndefined(); 493 537 } 494 538 -
trunk/JavaScriptCore/API/tests/testapi.c
r41895 r41905 129 129 if (JSStringIsEqualToUTF8CString(propertyName, "alwaysOne") 130 130 || JSStringIsEqualToUTF8CString(propertyName, "cantFind") 131 || JSStringIsEqualToUTF8CString(propertyName, "throwOnGet") 131 132 || JSStringIsEqualToUTF8CString(propertyName, "myPropertyName") 132 133 || JSStringIsEqualToUTF8CString(propertyName, "hasPropertyLie") … … 154 155 return JSValueMakeUndefined(context); 155 156 } 156 157 158 if (JSStringIsEqualToUTF8CString(propertyName, "throwOnGet")) { 159 return JSEvaluateScript(context, JSStringCreateWithUTF8CString("throw 'an exception'"), object, JSStringCreateWithUTF8CString("test script"), 1, exception); 160 } 161 157 162 if (JSStringIsEqualToUTF8CString(propertyName, "0")) { 158 163 *exception = JSValueMakeNumber(context, 1); … … 173 178 return true; // pretend we set the property in order to swallow it 174 179 180 if (JSStringIsEqualToUTF8CString(propertyName, "throwOnSet")) { 181 JSEvaluateScript(context, JSStringCreateWithUTF8CString("throw 'an exception'"), object, JSStringCreateWithUTF8CString("test script"), 1, exception); 182 } 183 175 184 return false; 176 185 } … … 185 194 186 195 if (JSStringIsEqualToUTF8CString(propertyName, "throwOnDelete")) { 187 *exception = JSValueMakeNumber(context, 2);196 JSEvaluateScript(context, JSStringCreateWithUTF8CString("throw 'an exception'"), object, JSStringCreateWithUTF8CString("test script"), 1, exception); 188 197 return false; 189 198 } … … 215 224 UNUSED_PARAM(exception); 216 225 226 if (argumentCount > 0 && JSValueIsString(context, arguments[0]) && JSStringIsEqualToUTF8CString(JSValueToStringCopy(context, arguments[0], 0), "throwOnCall")) { 227 JSEvaluateScript(context, JSStringCreateWithUTF8CString("throw 'an exception'"), object, JSStringCreateWithUTF8CString("test script"), 1, exception); 228 return JSValueMakeUndefined(context); 229 } 230 217 231 if (argumentCount > 0 && JSValueIsStrictEqual(context, arguments[0], JSValueMakeNumber(context, 0))) 218 232 return JSValueMakeNumber(context, 1); … … 226 240 UNUSED_PARAM(object); 227 241 242 if (argumentCount > 0 && JSValueIsString(context, arguments[0]) && JSStringIsEqualToUTF8CString(JSValueToStringCopy(context, arguments[0], 0), "throwOnConstruct")) { 243 JSEvaluateScript(context, JSStringCreateWithUTF8CString("throw 'an exception'"), object, JSStringCreateWithUTF8CString("test script"), 1, exception); 244 return object; 245 } 246 228 247 if (argumentCount > 0 && JSValueIsStrictEqual(context, arguments[0], JSValueMakeNumber(context, 0))) 229 248 return JSValueToObject(context, JSValueMakeNumber(context, 1), exception); … … 236 255 UNUSED_PARAM(context); 237 256 UNUSED_PARAM(constructor); 257 258 if (JSValueIsString(context, possibleValue) && JSStringIsEqualToUTF8CString(JSValueToStringCopy(context, possibleValue, 0), "throwOnHasInstance")) { 259 JSEvaluateScript(context, JSStringCreateWithUTF8CString("throw 'an exception'"), constructor, JSStringCreateWithUTF8CString("test script"), 1, exception); 260 return false; 261 } 238 262 239 263 JSStringRef numberString = JSStringCreateWithUTF8CString("Number"); … … 310 334 return jsClass; 311 335 } 336 337 static bool EvilExceptionObject_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleValue, JSValueRef* exception) 338 { 339 UNUSED_PARAM(context); 340 UNUSED_PARAM(constructor); 341 342 JSStringRef hasInstanceName = JSStringCreateWithUTF8CString("hasInstance"); 343 JSValueRef hasInstance = JSObjectGetProperty(context, constructor, hasInstanceName, exception); 344 JSStringRelease(hasInstanceName); 345 346 JSObjectRef function = JSValueToObject(context, hasInstance, exception); 347 JSValueRef result = JSObjectCallAsFunction(context, function, constructor, 1, &possibleValue, exception); 348 return result && JSValueToBoolean(context, result); 349 } 350 351 static JSValueRef EvilExceptionObject_convertToType(JSContextRef context, JSObjectRef object, JSType type, JSValueRef* exception) 352 { 353 UNUSED_PARAM(object); 354 UNUSED_PARAM(exception); 355 JSStringRef funcName; 356 switch (type) { 357 case kJSTypeNumber: 358 funcName = JSStringCreateWithUTF8CString("toNumber"); 359 break; 360 case kJSTypeString: 361 funcName = JSStringCreateWithUTF8CString("toStringExplicit"); 362 break; 363 default: 364 return NULL; 365 break; 366 } 367 368 JSValueRef func = JSObjectGetProperty(context, object, funcName, exception); 369 JSStringRelease(funcName); 370 JSObjectRef function = JSValueToObject(context, func, exception); 371 if (!function) 372 return NULL; 373 JSValueRef value = JSObjectCallAsFunction(context, function, object, 0, NULL, exception); 374 if (!value) 375 return (JSValueRef)JSStringCreateWithUTF8CString("convertToType failed"); 376 return value; 377 } 378 379 JSClassDefinition EvilExceptionObject_definition = { 380 0, 381 kJSClassAttributeNone, 382 383 "EvilExceptionObject", 384 NULL, 385 386 NULL, 387 NULL, 388 389 NULL, 390 NULL, 391 NULL, 392 NULL, 393 NULL, 394 NULL, 395 NULL, 396 NULL, 397 NULL, 398 EvilExceptionObject_hasInstance, 399 EvilExceptionObject_convertToType, 400 }; 401 402 static JSClassRef EvilExceptionObject_class(JSContextRef context) 403 { 404 UNUSED_PARAM(context); 405 406 static JSClassRef jsClass; 407 if (!jsClass) 408 jsClass = JSClassCreate(&EvilExceptionObject_definition); 409 410 return jsClass; 411 } 412 312 413 313 414 static JSValueRef Base_get(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) … … 669 770 JSObjectSetProperty(context, globalObject, myObjectIString, myObject, kJSPropertyAttributeNone, NULL); 670 771 JSStringRelease(myObjectIString); 772 773 JSObjectRef EvilExceptionObject = JSObjectMake(context, EvilExceptionObject_class(context), NULL); 774 JSStringRef EvilExceptionObjectIString = JSStringCreateWithUTF8CString("EvilExceptionObject"); 775 JSObjectSetProperty(context, globalObject, EvilExceptionObjectIString, EvilExceptionObject, kJSPropertyAttributeNone, NULL); 776 JSStringRelease(EvilExceptionObjectIString); 671 777 672 778 JSValueRef exception; -
trunk/JavaScriptCore/API/tests/testapi.js
r41896 r41905 81 81 delete MyObject.cantDelete; 82 82 shouldBe("MyObject.cantDelete", 1); 83 shouldBe("delete MyObject.throwOnDelete", 2); // deleteProperty -- should throw 283 shouldBe("delete MyObject.throwOnDelete", "an exception"); 84 84 MyObject.cantSet = 1; 85 85 shouldBe("MyObject.cantSet", undefined); 86 shouldBe("MyObject.throwOnGet", "an exception"); 87 shouldBe("MyObject.throwOnSet = 5", "an exception"); 88 shouldBe("MyObject('throwOnCall')", "an exception"); 89 shouldBe("new MyObject('throwOnConstruct')", "an exception"); 90 shouldBe("'throwOnHasInstance' instanceof MyObject", "an exception"); 86 91 87 92 var foundMyPropertyName = false; … … 146 151 shouldBe("derived.protoDup = 0", 2); 147 152 153 shouldBe("undefined instanceof MyObject", false); 154 EvilExceptionObject.hasInstance = function f() { return f(); }; 155 EvilExceptionObject.__proto__ = undefined; 156 shouldThrow("undefined instanceof EvilExceptionObject"); 157 EvilExceptionObject.hasInstance = function () { return true; }; 158 shouldBe("undefined instanceof EvilExceptionObject", true); 159 160 EvilExceptionObject.toNumber = function f() { return f(); } 161 shouldThrow("EvilExceptionObject*5"); 162 EvilExceptionObject.toStringExplicit = function f() { return f(); } 163 shouldThrow("String(EvilExceptionObject)"); 164 165 148 166 if (failed) 149 167 throw "Some tests failed";
Note:
See TracChangeset
for help on using the changeset viewer.