JavaScriptCore:
[WebKit-https.git] / JavaScriptGlue / UserObjectImp.cpp
index 247dde6..499f708 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005 Apple Computer, Inc.  All rights reserved.
+ * Copyright (C) 2005, 2008 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
 
 #include "config.h"
 #include "UserObjectImp.h"
-#include <JavaScriptCore/PropertyNameArray.h>
-
-const ClassInfo UserObjectImp::info = {"UserObject", 0, 0, 0};
-
-class UserObjectPrototypeImp : public UserObjectImp {
-  public:
-    UserObjectPrototypeImp();
-    static UserObjectPrototypeImp* GlobalUserObjectPrototypeImp();
-  private:
-    static UserObjectPrototypeImp* sUserObjectPrototypeImp;
-};
-
-UserObjectPrototypeImp* UserObjectPrototypeImp::sUserObjectPrototypeImp = 0;
-
-UserObjectPrototypeImp::UserObjectPrototypeImp()
-  : UserObjectImp()
-{
-}
-
-UserObjectPrototypeImp* UserObjectPrototypeImp::GlobalUserObjectPrototypeImp()
-{
-    if (!sUserObjectPrototypeImp)
-    {
-            sUserObjectPrototypeImp  = new UserObjectPrototypeImp();
-            static ProtectedPtr<UserObjectPrototypeImp> protectPrototype;
-    }
-    return sUserObjectPrototypeImp;
-}
 
+#include <JavaScriptCore/JSString.h>
+#include <JavaScriptCore/PropertyNameArray.h>
 
-UserObjectImp::UserObjectImp(): JSObject(), fJSUserObject(0)
-{
-}
+const ClassInfo UserObjectImp::info = { "UserObject", 0, 0, 0 };
 
-UserObjectImp::UserObjectImp(JSUserObject* userObject) :
-    JSObject(UserObjectPrototypeImp::GlobalUserObjectPrototypeImp()),
-    fJSUserObject((JSUserObject*)userObject->Retain())
+UserObjectImp::UserObjectImp(PassRefPtr<StructureID> structure, JSUserObject* userObject)
+    : JSObject(structure)
+    fJSUserObject((JSUserObject*)userObject->Retain())
 {
 }
 
@@ -79,14 +51,14 @@ const ClassInfo * UserObjectImp::classInfo() const
     return &info;
 }
 
-bool UserObjectImp::implementsCall() const
+CallType UserObjectImp::getCallData(CallData& callData)
 {
-    return fJSUserObject ? fJSUserObject->ImplementsCall() : false;
+    return fJSUserObject ? fJSUserObject->getCallData(callData) : CallTypeNone;
 }
 
-JSValue *UserObjectImp::callAsFunction(ExecState *exec, JSObject *thisObj, const List &args)
+JSValuePtr UserObjectImp::callAsFunction(ExecState *exec, JSObject *thisObj, const ArgList &args)
 {
-    JSValue *result = jsUndefined();
+    JSValuePtr result = jsUndefined();
     JSUserObject* jsThisObj = KJSValueToJSObject(thisObj, exec);
     if (jsThisObj) {
         CFIndex argCount = args.size();
@@ -95,7 +67,7 @@ JSValue *UserObjectImp::callAsFunction(ExecState *exec, JSObject *thisObj, const
         CFMutableArrayRef jsArgs = CFArrayCreateMutable(0, 0, &arrayCallBacks);
         if (jsArgs) {
             for (CFIndex i = 0; i < argCount; i++) {
-                JSUserObject* jsArg = KJSValueToJSObject(args[i], exec);
+                JSUserObject* jsArg = KJSValueToJSObject(args.at(exec, i), exec);
                 CFArrayAppendValue(jsArgs, (void*)jsArg);
                 jsArg->Release();
             }
@@ -103,9 +75,9 @@ JSValue *UserObjectImp::callAsFunction(ExecState *exec, JSObject *thisObj, const
 
         JSUserObject* jsResult;
         { // scope
-            JSLock::DropAllLocks dropLocks;
+            JSLock::DropAllLocks dropLocks(exec);
 
-            // implementsCall should have guarded against a NULL fJSUserObject.
+            // getCallData should have guarded against a NULL fJSUserObject.
             assert(fJSUserObject);
             jsResult = fJSUserObject->CallFunction(jsThisObj, jsArgs);
         }
@@ -132,7 +104,7 @@ void UserObjectImp::getPropertyNames(ExecState *exec, PropertyNameArray& propert
             CFIndex i;
             for (i = 0; i < count; i++) {
                 CFStringRef propertyName = (CFStringRef)CFArrayGetValueAtIndex(cfPropertyNames, i);
-                propertyNames.add(CFStringToIdentifier(propertyName));
+                propertyNames.add(CFStringToIdentifier(propertyName, exec));
             }
             CFRelease(cfPropertyNames);
         }
@@ -140,16 +112,16 @@ void UserObjectImp::getPropertyNames(ExecState *exec, PropertyNameArray& propert
     JSObject::getPropertyNames(exec, propertyNames);
 }
 
-JSValue *UserObjectImp::userObjectGetter(ExecState *, JSObject *, const Identifier& propertyName, const PropertySlot& slot)
+JSValuePtr  UserObjectImp::userObjectGetter(ExecState*, const Identifier& propertyName, const PropertySlot& slot)
 {
-    UserObjectImp *thisObj = static_cast<UserObjectImp *>(slot.slotBase());
+    UserObjectImp *thisObj = static_cast<UserObjectImp *>(asObject(slot.slotBase()));
     // getOwnPropertySlot should have guarded against a null fJSUserObject.
     assert(thisObj->fJSUserObject);
     
     CFStringRef cfPropName = IdentifierToCFString(propertyName);
     JSUserObject *jsResult = thisObj->fJSUserObject->CopyProperty(cfPropName);
     ReleaseCFType(cfPropName);
-    JSValue *result = JSObjectKJSValue(jsResult);
+    JSValuePtr result = JSObjectKJSValue(jsResult);
     jsResult->Release();
 
     return result;
@@ -168,8 +140,8 @@ bool UserObjectImp::getOwnPropertySlot(ExecState *exec, const Identifier& proper
         jsResult->Release();
         return true;
     } else {
-        JSValue *kjsValue = toPrimitive(exec);
-        if (kjsValue->type() != NullType && kjsValue->type() != UndefinedType) {
+        JSValuePtr kjsValue = toPrimitive(exec);
+        if (!kjsValue->isUndefinedOrNull()) {
             JSObject *kjsObject = kjsValue->toObject(exec);
             if (kjsObject->getPropertySlot(exec, propertyName, slot))
                 return true;
@@ -178,7 +150,7 @@ bool UserObjectImp::getOwnPropertySlot(ExecState *exec, const Identifier& proper
     return JSObject::getOwnPropertySlot(exec, propertyName, slot);
 }
 
-void UserObjectImp::put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr)
+void UserObjectImp::put(ExecState *exec, const Identifier &propertyName, JSValuePtr value, int attr)
 {
     if (!fJSUserObject)
         return;
@@ -197,9 +169,9 @@ JSUserObject* UserObjectImp::GetJSUserObject() const
     return fJSUserObject;
 }
 
-JSValue *UserObjectImp::toPrimitive(ExecState *exec, JSType preferredType) const
+JSValuePtr UserObjectImp::toPrimitive(ExecState *exec, JSType) const
 {
-    JSValue *result = jsUndefined();
+    JSValuePtr result = jsUndefined();
     JSUserObject* jsObjPtr = KJSValueToJSObject(toObject(exec), exec);
     CFTypeRef cfValue = jsObjPtr ? jsObjPtr->CopyCFValue() : 0;
     if (cfValue) {
@@ -214,15 +186,15 @@ JSValue *UserObjectImp::toPrimitive(ExecState *exec, JSType preferredType) const
                 result = jsBoolean(false);
             }
         } else if (cfType == CFStringGetTypeID()) {
-            result = jsString(CFStringToUString((CFStringRef)cfValue));
+            result = jsString(exec, CFStringToUString((CFStringRef)cfValue));
         } else if (cfType == CFNumberGetTypeID()) {
             double d = 0.0;
             CFNumberGetValue((CFNumberRef)cfValue, kCFNumberDoubleType, &d);
-            result = jsNumber(d);
+            result = jsNumber(exec, d);
         } else if (cfType == CFURLGetTypeID()) {
             CFURLRef absURL = CFURLCopyAbsoluteURL((CFURLRef)cfValue);
             if (absURL) {
-                result = jsString(CFStringToUString(CFURLGetString(absURL)));
+                result = jsString(exec, CFStringToUString(CFURLGetString(absURL)));
                 ReleaseCFType(absURL);
             }
         }
@@ -391,25 +363,19 @@ UString UserObjectImp::toString(ExecState *exec) const
             }
             else
             {
-                CFStringRef cfNumStr = 0;
+                CFStringRef cfNumStr;
+                double d = 0;
+                CFNumberGetValue((CFNumberRef)cfValue, kCFNumberDoubleType, &d);
                 if (CFNumberIsFloatType((CFNumberRef)cfValue))
                 {
-                    double d = 0;
-                    CFNumberGetValue((CFNumberRef)cfValue, kCFNumberDoubleType, &d);
-                    cfNumStr = CFStringCreateWithFormat(0, 0, CFSTR("%f"), (float)d);
+                    cfNumStr = CFStringCreateWithFormat(0, 0, CFSTR("%f"), d);
                 }
                 else
                 {
-                    int i = 0;
-                    CFNumberGetValue((CFNumberRef)cfValue, kCFNumberIntType, &i);
-                    cfNumStr = CFStringCreateWithFormat(0, 0, CFSTR("%d"), (int)i);
-                }
-
-                if (cfNumStr)
-                {
-                    result = CFStringToUString(cfNumStr);
-                    ReleaseCFType(cfNumStr);
+                    cfNumStr = CFStringCreateWithFormat(0, 0, CFSTR("%.0f"), d);
                 }
+                result = CFStringToUString(cfNumStr);
+                ReleaseCFType(cfNumStr);
             }
         }
         else if (cfType == CFArrayGetTypeID())