thatcher | 43a8c517 | 2006-01-19 02:40:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 Apple Computer, Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of |
| 14 | * its contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
mjs | 3bfb61b | 2006-03-02 09:12:06 +0000 | [diff] [blame] | 29 | #include "config.h" |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 30 | #include "UserObjectImp.h" |
mjs | b3598b8 | 2006-07-16 21:06:28 +0000 | [diff] [blame] | 31 | #include <JavaScriptCore/PropertyNameArray.h> |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 32 | |
| 33 | const ClassInfo UserObjectImp::info = {"UserObject", 0, 0, 0}; |
| 34 | |
ggaren | 42d6fcd | 2007-02-22 22:10:38 +0000 | [diff] [blame] | 35 | UserObjectImp::UserObjectImp(JSUserObject* userObject) |
| 36 | : fJSUserObject((JSUserObject*)userObject->Retain()) |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 37 | { |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 38 | } |
| 39 | |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 40 | UserObjectImp::~UserObjectImp() |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 41 | { |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 42 | if (fJSUserObject) |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 43 | fJSUserObject->Release(); |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 44 | } |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 45 | |
| 46 | const ClassInfo * UserObjectImp::classInfo() const |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 47 | { |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 48 | return &info; |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 49 | } |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 50 | |
| 51 | bool UserObjectImp::implementsCall() const |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 52 | { |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 53 | return fJSUserObject ? fJSUserObject->ImplementsCall() : false; |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 54 | } |
| 55 | |
darin | b139ea1 | 2005-12-11 02:03:35 +0000 | [diff] [blame] | 56 | JSValue *UserObjectImp::callAsFunction(ExecState *exec, JSObject *thisObj, const List &args) |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 57 | { |
darin | b139ea1 | 2005-12-11 02:03:35 +0000 | [diff] [blame] | 58 | JSValue *result = jsUndefined(); |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 59 | JSUserObject* jsThisObj = KJSValueToJSObject(thisObj, exec); |
| 60 | if (jsThisObj) { |
| 61 | CFIndex argCount = args.size(); |
| 62 | CFArrayCallBacks arrayCallBacks; |
| 63 | JSTypeGetCFArrayCallBacks(&arrayCallBacks); |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 64 | CFMutableArrayRef jsArgs = CFArrayCreateMutable(0, 0, &arrayCallBacks); |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 65 | if (jsArgs) { |
| 66 | for (CFIndex i = 0; i < argCount; i++) { |
| 67 | JSUserObject* jsArg = KJSValueToJSObject(args[i], exec); |
| 68 | CFArrayAppendValue(jsArgs, (void*)jsArg); |
| 69 | jsArg->Release(); |
| 70 | } |
| 71 | } |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 72 | |
mjs | fc41525 | 2005-11-23 05:40:07 +0000 | [diff] [blame] | 73 | JSUserObject* jsResult; |
| 74 | { // scope |
mjs | a564fe1 | 2005-11-27 03:10:03 +0000 | [diff] [blame] | 75 | JSLock::DropAllLocks dropLocks; |
mjs | fc41525 | 2005-11-23 05:40:07 +0000 | [diff] [blame] | 76 | |
| 77 | // implementsCall should have guarded against a NULL fJSUserObject. |
| 78 | assert(fJSUserObject); |
| 79 | jsResult = fJSUserObject->CallFunction(jsThisObj, jsArgs); |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 80 | } |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 81 | |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 82 | if (jsResult) { |
| 83 | result = JSObjectKJSValue(jsResult); |
| 84 | jsResult->Release(); |
| 85 | } |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 86 | |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 87 | ReleaseCFType(jsArgs); |
| 88 | jsThisObj->Release(); |
| 89 | } |
| 90 | return result; |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | |
mjs | b3598b8 | 2006-07-16 21:06:28 +0000 | [diff] [blame] | 94 | void UserObjectImp::getPropertyNames(ExecState *exec, PropertyNameArray& propertyNames) |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 95 | { |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 96 | JSUserObject* ptr = GetJSUserObject(); |
| 97 | if (ptr) { |
| 98 | CFArrayRef cfPropertyNames = ptr->CopyPropertyNames(); |
| 99 | if (cfPropertyNames) { |
| 100 | CFIndex count = CFArrayGetCount(cfPropertyNames); |
| 101 | CFIndex i; |
| 102 | for (i = 0; i < count; i++) { |
| 103 | CFStringRef propertyName = (CFStringRef)CFArrayGetValueAtIndex(cfPropertyNames, i); |
mjs | b3598b8 | 2006-07-16 21:06:28 +0000 | [diff] [blame] | 104 | propertyNames.add(CFStringToIdentifier(propertyName)); |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 105 | } |
| 106 | CFRelease(cfPropertyNames); |
| 107 | } |
| 108 | } |
mjs | b3598b8 | 2006-07-16 21:06:28 +0000 | [diff] [blame] | 109 | JSObject::getPropertyNames(exec, propertyNames); |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 110 | } |
| 111 | |
ggaren | f336dd7 | 2005-12-14 01:00:12 +0000 | [diff] [blame] | 112 | JSValue *UserObjectImp::userObjectGetter(ExecState *, JSObject *, const Identifier& propertyName, const PropertySlot& slot) |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 113 | { |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 114 | UserObjectImp *thisObj = static_cast<UserObjectImp *>(slot.slotBase()); |
ggaren | ab4c722 | 2005-11-11 05:15:02 +0000 | [diff] [blame] | 115 | // getOwnPropertySlot should have guarded against a null fJSUserObject. |
| 116 | assert(thisObj->fJSUserObject); |
| 117 | |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 118 | CFStringRef cfPropName = IdentifierToCFString(propertyName); |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 119 | JSUserObject *jsResult = thisObj->fJSUserObject->CopyProperty(cfPropName); |
| 120 | ReleaseCFType(cfPropName); |
darin | b139ea1 | 2005-12-11 02:03:35 +0000 | [diff] [blame] | 121 | JSValue *result = JSObjectKJSValue(jsResult); |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 122 | jsResult->Release(); |
| 123 | |
| 124 | return result; |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 125 | } |
| 126 | |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 127 | bool UserObjectImp::getOwnPropertySlot(ExecState *exec, const Identifier& propertyName, PropertySlot& slot) |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 128 | { |
ggaren | ab4c722 | 2005-11-11 05:15:02 +0000 | [diff] [blame] | 129 | if (!fJSUserObject) |
| 130 | return false; |
| 131 | |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 132 | CFStringRef cfPropName = IdentifierToCFString(propertyName); |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 133 | JSUserObject *jsResult = fJSUserObject->CopyProperty(cfPropName); |
| 134 | ReleaseCFType(cfPropName); |
| 135 | if (jsResult) { |
| 136 | slot.setCustom(this, userObjectGetter); |
| 137 | jsResult->Release(); |
| 138 | return true; |
| 139 | } else { |
darin | b139ea1 | 2005-12-11 02:03:35 +0000 | [diff] [blame] | 140 | JSValue *kjsValue = toPrimitive(exec); |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 141 | if (kjsValue->type() != NullType && kjsValue->type() != UndefinedType) { |
darin | b139ea1 | 2005-12-11 02:03:35 +0000 | [diff] [blame] | 142 | JSObject *kjsObject = kjsValue->toObject(exec); |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 143 | if (kjsObject->getPropertySlot(exec, propertyName, slot)) |
| 144 | return true; |
| 145 | } |
| 146 | } |
darin | b139ea1 | 2005-12-11 02:03:35 +0000 | [diff] [blame] | 147 | return JSObject::getOwnPropertySlot(exec, propertyName, slot); |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 148 | } |
| 149 | |
darin | b139ea1 | 2005-12-11 02:03:35 +0000 | [diff] [blame] | 150 | void UserObjectImp::put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr) |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 151 | { |
ggaren | ab4c722 | 2005-11-11 05:15:02 +0000 | [diff] [blame] | 152 | if (!fJSUserObject) |
| 153 | return; |
| 154 | |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 155 | CFStringRef cfPropName = IdentifierToCFString(propertyName); |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 156 | JSUserObject *jsValueObj = KJSValueToJSObject(value, exec); |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 157 | |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 158 | fJSUserObject->SetProperty(cfPropName, jsValueObj); |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 159 | |
| 160 | if (jsValueObj) jsValueObj->Release(); |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 161 | ReleaseCFType(cfPropName); |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 162 | } |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 163 | |
| 164 | JSUserObject* UserObjectImp::GetJSUserObject() const |
| 165 | { |
| 166 | return fJSUserObject; |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 167 | } |
| 168 | |
ggaren | 9044b93 | 2006-02-10 08:42:21 +0000 | [diff] [blame] | 169 | JSValue *UserObjectImp::toPrimitive(ExecState *exec, JSType preferredType) const |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 170 | { |
darin | b139ea1 | 2005-12-11 02:03:35 +0000 | [diff] [blame] | 171 | JSValue *result = jsUndefined(); |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 172 | JSUserObject* jsObjPtr = KJSValueToJSObject(toObject(exec), exec); |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 173 | CFTypeRef cfValue = jsObjPtr ? jsObjPtr->CopyCFValue() : 0; |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 174 | if (cfValue) { |
| 175 | CFTypeID cfType = CFGetTypeID(cfValue); // toPrimitive |
| 176 | if (cfValue == GetCFNull()) { |
darin | 9bd6064 | 2005-12-11 00:35:59 +0000 | [diff] [blame] | 177 | result = jsNull(); |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 178 | } |
| 179 | else if (cfType == CFBooleanGetTypeID()) { |
| 180 | if (cfValue == kCFBooleanTrue) { |
darin | 9bd6064 | 2005-12-11 00:35:59 +0000 | [diff] [blame] | 181 | result = jsBoolean(true); |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 182 | } else { |
darin | 9bd6064 | 2005-12-11 00:35:59 +0000 | [diff] [blame] | 183 | result = jsBoolean(false); |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 184 | } |
| 185 | } else if (cfType == CFStringGetTypeID()) { |
darin | 9bd6064 | 2005-12-11 00:35:59 +0000 | [diff] [blame] | 186 | result = jsString(CFStringToUString((CFStringRef)cfValue)); |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 187 | } else if (cfType == CFNumberGetTypeID()) { |
| 188 | double d = 0.0; |
| 189 | CFNumberGetValue((CFNumberRef)cfValue, kCFNumberDoubleType, &d); |
darin | 9bd6064 | 2005-12-11 00:35:59 +0000 | [diff] [blame] | 190 | result = jsNumber(d); |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 191 | } else if (cfType == CFURLGetTypeID()) { |
| 192 | CFURLRef absURL = CFURLCopyAbsoluteURL((CFURLRef)cfValue); |
| 193 | if (absURL) { |
darin | 9bd6064 | 2005-12-11 00:35:59 +0000 | [diff] [blame] | 194 | result = jsString(CFStringToUString(CFURLGetString(absURL))); |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 195 | ReleaseCFType(absURL); |
| 196 | } |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 197 | } |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 198 | ReleaseCFType(cfValue); |
| 199 | } |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 200 | if (jsObjPtr) |
mjs | 1edef72 | 2005-10-07 04:22:50 +0000 | [diff] [blame] | 201 | jsObjPtr->Release(); |
| 202 | return result; |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | |
| 206 | bool UserObjectImp::toBoolean(ExecState *exec) const |
| 207 | { |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 208 | bool result = false; |
| 209 | JSUserObject* jsObjPtr = KJSValueToJSObject(toObject(exec), exec); |
| 210 | CFTypeRef cfValue = jsObjPtr ? jsObjPtr->CopyCFValue() : 0; |
| 211 | if (cfValue) |
| 212 | { |
| 213 | CFTypeID cfType = CFGetTypeID(cfValue); // toPrimitive |
| 214 | if (cfValue == GetCFNull()) |
| 215 | { |
| 216 | // |
| 217 | } |
| 218 | else if (cfType == CFBooleanGetTypeID()) |
| 219 | { |
| 220 | if (cfValue == kCFBooleanTrue) |
| 221 | { |
| 222 | result = true; |
| 223 | } |
| 224 | } |
| 225 | else if (cfType == CFStringGetTypeID()) |
| 226 | { |
| 227 | if (CFStringGetLength((CFStringRef)cfValue)) |
| 228 | { |
| 229 | result = true; |
| 230 | } |
| 231 | } |
| 232 | else if (cfType == CFNumberGetTypeID()) |
| 233 | { |
| 234 | if (cfValue != kCFNumberNaN) |
| 235 | { |
| 236 | double d; |
| 237 | if (CFNumberGetValue((CFNumberRef)cfValue, kCFNumberDoubleType, &d)) |
| 238 | { |
| 239 | if (d != 0) |
| 240 | { |
| 241 | result = true; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | else if (cfType == CFArrayGetTypeID()) |
| 247 | { |
| 248 | if (CFArrayGetCount((CFArrayRef)cfValue)) |
| 249 | { |
| 250 | result = true; |
| 251 | } |
| 252 | } |
| 253 | else if (cfType == CFDictionaryGetTypeID()) |
| 254 | { |
| 255 | if (CFDictionaryGetCount((CFDictionaryRef)cfValue)) |
| 256 | { |
| 257 | result = true; |
| 258 | } |
| 259 | } |
| 260 | else if (cfType == CFSetGetTypeID()) |
| 261 | { |
| 262 | if (CFSetGetCount((CFSetRef)cfValue)) |
| 263 | { |
| 264 | result = true; |
| 265 | } |
| 266 | } |
| 267 | else if (cfType == CFURLGetTypeID()) |
| 268 | { |
| 269 | CFURLRef absURL = CFURLCopyAbsoluteURL((CFURLRef)cfValue); |
| 270 | if (absURL) |
| 271 | { |
| 272 | CFStringRef cfStr = CFURLGetString(absURL); |
| 273 | if (cfStr && CFStringGetLength(cfStr)) |
| 274 | { |
| 275 | result = true; |
| 276 | } |
| 277 | ReleaseCFType(absURL); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | if (jsObjPtr) jsObjPtr->Release(); |
| 282 | ReleaseCFType(cfValue); |
| 283 | return result; |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | double UserObjectImp::toNumber(ExecState *exec) const |
| 287 | { |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 288 | double result = 0; |
| 289 | JSUserObject* jsObjPtr = KJSValueToJSObject(toObject(exec), exec); |
| 290 | CFTypeRef cfValue = jsObjPtr ? jsObjPtr->CopyCFValue() : 0; |
| 291 | if (cfValue) |
| 292 | { |
| 293 | CFTypeID cfType = CFGetTypeID(cfValue); |
| 294 | |
| 295 | if (cfValue == GetCFNull()) |
| 296 | { |
| 297 | // |
| 298 | } |
| 299 | else if (cfType == CFBooleanGetTypeID()) |
| 300 | { |
| 301 | if (cfValue == kCFBooleanTrue) |
| 302 | { |
| 303 | result = 1; |
| 304 | } |
| 305 | } |
| 306 | else if (cfType == CFStringGetTypeID()) |
| 307 | { |
| 308 | result = CFStringGetDoubleValue((CFStringRef)cfValue); |
| 309 | } |
| 310 | else if (cfType == CFNumberGetTypeID()) |
| 311 | { |
| 312 | CFNumberGetValue((CFNumberRef)cfValue, kCFNumberDoubleType, &result); |
| 313 | } |
| 314 | } |
| 315 | ReleaseCFType(cfValue); |
| 316 | if (jsObjPtr) jsObjPtr->Release(); |
| 317 | return result; |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | UString UserObjectImp::toString(ExecState *exec) const |
| 321 | { |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 322 | UString result; |
| 323 | JSUserObject* jsObjPtr = KJSValueToJSObject(toObject(exec), exec); |
| 324 | CFTypeRef cfValue = jsObjPtr ? jsObjPtr->CopyCFValue() : 0; |
| 325 | if (cfValue) |
| 326 | { |
| 327 | CFTypeID cfType = CFGetTypeID(cfValue); |
| 328 | if (cfValue == GetCFNull()) |
| 329 | { |
| 330 | // |
| 331 | } |
| 332 | else if (cfType == CFBooleanGetTypeID()) |
| 333 | { |
| 334 | if (cfValue == kCFBooleanTrue) |
| 335 | { |
| 336 | result = "true"; |
| 337 | } |
| 338 | else |
| 339 | { |
| 340 | result = "false"; |
| 341 | } |
| 342 | } |
| 343 | else if (cfType == CFStringGetTypeID()) |
| 344 | { |
| 345 | result = CFStringToUString((CFStringRef)cfValue); |
| 346 | } |
| 347 | else if (cfType == CFNumberGetTypeID()) |
| 348 | { |
| 349 | if (cfValue == kCFNumberNaN) |
| 350 | { |
| 351 | result = "Nan"; |
| 352 | } |
| 353 | else if (CFNumberCompare(kCFNumberPositiveInfinity, (CFNumberRef)cfValue, 0) == 0) |
| 354 | { |
| 355 | result = "Infinity"; |
| 356 | } |
| 357 | else if (CFNumberCompare(kCFNumberNegativeInfinity, (CFNumberRef)cfValue, 0) == 0) |
| 358 | { |
| 359 | result = "-Infinity"; |
| 360 | } |
| 361 | else |
| 362 | { |
darin | fa0c87f | 2007-02-16 21:56:43 +0000 | [diff] [blame] | 363 | CFStringRef cfNumStr; |
| 364 | double d = 0; |
| 365 | CFNumberGetValue((CFNumberRef)cfValue, kCFNumberDoubleType, &d); |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 366 | if (CFNumberIsFloatType((CFNumberRef)cfValue)) |
| 367 | { |
darin | fa0c87f | 2007-02-16 21:56:43 +0000 | [diff] [blame] | 368 | cfNumStr = CFStringCreateWithFormat(0, 0, CFSTR("%f"), d); |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 369 | } |
| 370 | else |
| 371 | { |
darin | fa0c87f | 2007-02-16 21:56:43 +0000 | [diff] [blame] | 372 | cfNumStr = CFStringCreateWithFormat(0, 0, CFSTR("%.0f"), d); |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 373 | } |
darin | fa0c87f | 2007-02-16 21:56:43 +0000 | [diff] [blame] | 374 | result = CFStringToUString(cfNumStr); |
| 375 | ReleaseCFType(cfNumStr); |
darin | 0965469 | 2005-10-10 23:32:34 +0000 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | else if (cfType == CFArrayGetTypeID()) |
| 379 | { |
| 380 | // |
| 381 | } |
| 382 | else if (cfType == CFDictionaryGetTypeID()) |
| 383 | { |
| 384 | // |
| 385 | } |
| 386 | else if (cfType == CFSetGetTypeID()) |
| 387 | { |
| 388 | // |
| 389 | } |
| 390 | else if (cfType == CFURLGetTypeID()) |
| 391 | { |
| 392 | CFURLRef absURL = CFURLCopyAbsoluteURL((CFURLRef)cfValue); |
| 393 | if (absURL) |
| 394 | { |
| 395 | CFStringRef cfStr = CFURLGetString(absURL); |
| 396 | if (cfStr) |
| 397 | { |
| 398 | result = CFStringToUString(cfStr); |
| 399 | } |
| 400 | ReleaseCFType(absURL); |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | ReleaseCFType(cfValue); |
| 405 | if (jsObjPtr) jsObjPtr->Release(); |
| 406 | return result; |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | void UserObjectImp::mark() |
| 410 | { |
darin | b139ea1 | 2005-12-11 02:03:35 +0000 | [diff] [blame] | 411 | JSObject::mark(); |
ggaren | ab4c722 | 2005-11-11 05:15:02 +0000 | [diff] [blame] | 412 | if (fJSUserObject) |
| 413 | fJSUserObject->Mark(); |
mjs | 7f5285a | 2005-09-06 08:10:03 +0000 | [diff] [blame] | 414 | } |