JavaScriptCore:
[WebKit-https.git] / JavaScriptGlue / UserObjectImp.cpp
index b969050..499f708 100644 (file)
@@ -1,41 +1,42 @@
+/*
+ * 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
+ * are met:
+ *
+ * 1.  Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer. 
+ * 2.  Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution. 
+ * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
+ *     its contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission. 
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
 #include "UserObjectImp.h"
-#include "JavaScriptCore/reference_list.h"
 
-const ClassInfo UserObjectImp::info = {"UserObject", 0, 0, 0};
+#include <JavaScriptCore/JSString.h>
+#include <JavaScriptCore/PropertyNameArray.h>
 
-class UserObjectPrototypeImp : public UserObjectImp {
-  public:
-    UserObjectPrototypeImp();
-    static UserObjectPrototypeImp* GlobalUserObjectPrototypeImp();
-  private:
-    static UserObjectPrototypeImp* sUserObjectPrototypeImp;
-};
+const ClassInfo UserObjectImp::info = { "UserObject", 0, 0, 0 };
 
-UserObjectPrototypeImp* UserObjectPrototypeImp::sUserObjectPrototypeImp = 0;
-
-UserObjectPrototypeImp::UserObjectPrototypeImp()
-  : UserObjectImp()
-{
-}
-
-UserObjectPrototypeImp* UserObjectPrototypeImp::GlobalUserObjectPrototypeImp()
-{
-    if (!sUserObjectPrototypeImp)
-    {
-            sUserObjectPrototypeImp  = new UserObjectPrototypeImp();
-            static ProtectedPtr<UserObjectPrototypeImp> protectPrototype;
-    }
-    return sUserObjectPrototypeImp;
-}
-
-
-UserObjectImp::UserObjectImp(): ObjectImp(), fJSUserObject(0)
-{
-}
-
-UserObjectImp::UserObjectImp(JSUserObject* userObject) :
-    ObjectImp(UserObjectPrototypeImp::GlobalUserObjectPrototypeImp()),
-    fJSUserObject((JSUserObject*)userObject->Retain())
+UserObjectImp::UserObjectImp(PassRefPtr<StructureID> structure, JSUserObject* userObject)
+    : JSObject(structure)
+    , fJSUserObject((JSUserObject*)userObject->Retain())
 {
 }
 
@@ -50,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;
 }
 
-ValueImp *UserObjectImp::callAsFunction(ExecState *exec, ObjectImp *thisObj, const List &args)
+JSValuePtr UserObjectImp::callAsFunction(ExecState *exec, JSObject *thisObj, const ArgList &args)
 {
-    ValueImp *result = jsUndefined();
+    JSValuePtr result = jsUndefined();
     JSUserObject* jsThisObj = KJSValueToJSObject(thisObj, exec);
     if (jsThisObj) {
         CFIndex argCount = args.size();
@@ -66,7 +67,7 @@ ValueImp *UserObjectImp::callAsFunction(ExecState *exec, ObjectImp *thisObj, con
         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();
             }
@@ -74,9 +75,9 @@ ValueImp *UserObjectImp::callAsFunction(ExecState *exec, ObjectImp *thisObj, con
 
         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);
         }
@@ -93,9 +94,8 @@ ValueImp *UserObjectImp::callAsFunction(ExecState *exec, ObjectImp *thisObj, con
 }
 
 
-ReferenceList UserObjectImp::propList(ExecState *exec, bool recursive)
+void UserObjectImp::getPropertyNames(ExecState *exec, PropertyNameArray& propertyNames)
 {
-    ReferenceList list = ObjectImp::propList(exec, recursive);
     JSUserObject* ptr = GetJSUserObject();
     if (ptr) {
         CFArrayRef cfPropertyNames = ptr->CopyPropertyNames();
@@ -104,25 +104,24 @@ ReferenceList UserObjectImp::propList(ExecState *exec, bool recursive)
             CFIndex i;
             for (i = 0; i < count; i++) {
                 CFStringRef propertyName = (CFStringRef)CFArrayGetValueAtIndex(cfPropertyNames, i);
-                list.append(Reference(this, CFStringToIdentifier(propertyName)));
+                propertyNames.add(CFStringToIdentifier(propertyName, exec));
             }
             CFRelease(cfPropertyNames);
         }
     }
-
-    return list;
+    JSObject::getPropertyNames(exec, propertyNames);
 }
 
-ValueImp *UserObjectImp::userObjectGetter(ExecState *, 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);
-    ValueImp *result = JSObjectKJSValue(jsResult);
+    JSValuePtr result = JSObjectKJSValue(jsResult);
     jsResult->Release();
 
     return result;
@@ -141,17 +140,17 @@ bool UserObjectImp::getOwnPropertySlot(ExecState *exec, const Identifier& proper
         jsResult->Release();
         return true;
     } else {
-        ValueImp *kjsValue = toPrimitive(exec);
-        if (kjsValue->type() != NullType && kjsValue->type() != UndefinedType) {
-            ObjectImp *kjsObject = kjsValue->toObject(exec);
+        JSValuePtr kjsValue = toPrimitive(exec);
+        if (!kjsValue->isUndefinedOrNull()) {
+            JSObject *kjsObject = kjsValue->toObject(exec);
             if (kjsObject->getPropertySlot(exec, propertyName, slot))
                 return true;
         }
     }
-    return ObjectImp::getOwnPropertySlot(exec, propertyName, slot);
+    return JSObject::getOwnPropertySlot(exec, propertyName, slot);
 }
 
-void UserObjectImp::put(ExecState *exec, const Identifier &propertyName, ValueImp *value, int attr)
+void UserObjectImp::put(ExecState *exec, const Identifier &propertyName, JSValuePtr value, int attr)
 {
     if (!fJSUserObject)
         return;
@@ -170,9 +169,9 @@ JSUserObject* UserObjectImp::GetJSUserObject() const
     return fJSUserObject;
 }
 
-ValueImp *UserObjectImp::toPrimitive(ExecState *exec, Type preferredType) const
+JSValuePtr UserObjectImp::toPrimitive(ExecState *exec, JSType) const
 {
-    ValueImp *result = jsUndefined();
+    JSValuePtr result = jsUndefined();
     JSUserObject* jsObjPtr = KJSValueToJSObject(toObject(exec), exec);
     CFTypeRef cfValue = jsObjPtr ? jsObjPtr->CopyCFValue() : 0;
     if (cfValue) {
@@ -187,15 +186,15 @@ ValueImp *UserObjectImp::toPrimitive(ExecState *exec, Type 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);
             }
         }
@@ -364,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())
@@ -418,7 +411,7 @@ UString UserObjectImp::toString(ExecState *exec) const
 
 void UserObjectImp::mark()
 {
-    ObjectImp::mark();
+    JSObject::mark();
     if (fJSUserObject)
         fJSUserObject->Mark();
 }