JavaScriptCore:

        Reviewed by Oliver.

        - fix http://bugs.webkit.org/show_bug.cgi?id=16498
          ''.constructor.toString() gives [function]

        Test: fast/js/function-names.html

        * kjs/array_object.cpp:
        (KJS::ArrayObjectImp::ArrayObjectImp): Use the class name as the constructor's function name.
        * kjs/bool_object.cpp:
        (KJS::BooleanObjectImp::BooleanObjectImp): Ditto.
        * kjs/date_object.cpp:
        (KJS::DateObjectImp::DateObjectImp): Ditto.
        * kjs/error_object.cpp:
        (KJS::ErrorPrototype::ErrorPrototype): Make the error object be an Error.
        (KJS::ErrorObjectImp::ErrorObjectImp): Use the class name as the constructor's function name.
        (KJS::NativeErrorPrototype::NativeErrorPrototype): Take const UString&.
        (KJS::NativeErrorImp::NativeErrorImp): Use the prototype's name as the constructor's function
        name.
        * kjs/error_object.h: Change ErrorPrototype to inherit from ErrorInstance. Change the
        NativeErrorImp constructor to take a NativeErrorPrototype pointer for its prototype.
        * kjs/function.h: Removed unneeded constructor for internal functions without names.
        We want to avoid those!
        * kjs/function_object.cpp:
        (KJS::functionProtoFuncToString): Removed code that writes out just [function] for functions
        that have no names. There's no reason to do that.
        (KJS::FunctionObjectImp::FunctionObjectImp): Use the class name as the constructor's
        function name.
        * kjs/internal.cpp: Removed the unused constructor.
        * kjs/number_object.cpp:
        (KJS::fractionalPartToString): Marked static for internal linkage.
        (KJS::exponentialPartToString): Ditto.
        (KJS::numberProtoFuncToPrecision): Removed an unneeded else.
        (KJS::NumberObjectImp::NumberObjectImp): Use the class name as the constructor's
        function name.
        (KJS::NumberObjectImp::getValueProperty): Tweaked formatting.
        * kjs/object_object.cpp:
        (KJS::ObjectObjectImp::ObjectObjectImp): Use "Object" for the function name.
        * kjs/regexp_object.cpp:
        (KJS::RegExpObjectImp::RegExpObjectImp): Use "RegExp" for the function name.
        * kjs/string_object.cpp:
        (KJS::StringObjectImp::StringObjectImp): Use the class name as the constructor's
        function name.

LayoutTests:

        Reviewed by Oliver.

        - test for http://bugs.webkit.org/show_bug.cgi?id=16498
          ''.constructor.toString() gives [function]

        * fast/js/function-names-expected.txt: Updated for new tests.
        * fast/js/kde/resources/function.js: Updated test to expect the format that Gecko uses
        for native code, which we now match character for character.
        * fast/js/resources/function-names.js: Added tests for the names of all the constructors.



git-svn-id: http://svn.webkit.org/repository/webkit/trunk@29817 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/JavaScriptCore/ChangeLog b/JavaScriptCore/ChangeLog
index a37be24..a7da613 100644
--- a/JavaScriptCore/ChangeLog
+++ b/JavaScriptCore/ChangeLog
@@ -1,3 +1,49 @@
+2008-01-27  Darin Adler  <darin@apple.com>
+
+        Reviewed by Oliver.
+
+        - fix http://bugs.webkit.org/show_bug.cgi?id=16498
+          ''.constructor.toString() gives [function]
+
+        Test: fast/js/function-names.html
+
+        * kjs/array_object.cpp:
+        (KJS::ArrayObjectImp::ArrayObjectImp): Use the class name as the constructor's function name.
+        * kjs/bool_object.cpp:
+        (KJS::BooleanObjectImp::BooleanObjectImp): Ditto.
+        * kjs/date_object.cpp:
+        (KJS::DateObjectImp::DateObjectImp): Ditto.
+        * kjs/error_object.cpp:
+        (KJS::ErrorPrototype::ErrorPrototype): Make the error object be an Error.
+        (KJS::ErrorObjectImp::ErrorObjectImp): Use the class name as the constructor's function name.
+        (KJS::NativeErrorPrototype::NativeErrorPrototype): Take const UString&.
+        (KJS::NativeErrorImp::NativeErrorImp): Use the prototype's name as the constructor's function
+        name.
+        * kjs/error_object.h: Change ErrorPrototype to inherit from ErrorInstance. Change the
+        NativeErrorImp constructor to take a NativeErrorPrototype pointer for its prototype.
+        * kjs/function.h: Removed unneeded constructor for internal functions without names.
+        We want to avoid those!
+        * kjs/function_object.cpp:
+        (KJS::functionProtoFuncToString): Removed code that writes out just [function] for functions
+        that have no names. There's no reason to do that.
+        (KJS::FunctionObjectImp::FunctionObjectImp): Use the class name as the constructor's
+        function name.
+        * kjs/internal.cpp: Removed the unused constructor.
+        * kjs/number_object.cpp:
+        (KJS::fractionalPartToString): Marked static for internal linkage.
+        (KJS::exponentialPartToString): Ditto.
+        (KJS::numberProtoFuncToPrecision): Removed an unneeded else.
+        (KJS::NumberObjectImp::NumberObjectImp): Use the class name as the constructor's
+        function name.
+        (KJS::NumberObjectImp::getValueProperty): Tweaked formatting.
+        * kjs/object_object.cpp:
+        (KJS::ObjectObjectImp::ObjectObjectImp): Use "Object" for the function name.
+        * kjs/regexp_object.cpp:
+        (KJS::RegExpObjectImp::RegExpObjectImp): Use "RegExp" for the function name.
+        * kjs/string_object.cpp:
+        (KJS::StringObjectImp::StringObjectImp): Use the class name as the constructor's
+        function name.
+
 2008-01-26  Darin Adler  <darin@apple.com>
 
         Reviewed by Oliver.
diff --git a/JavaScriptCore/kjs/array_object.cpp b/JavaScriptCore/kjs/array_object.cpp
index e1d17c9..40da7d0 100644
--- a/JavaScriptCore/kjs/array_object.cpp
+++ b/JavaScriptCore/kjs/array_object.cpp
@@ -1,6 +1,6 @@
 /*
  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
- *  Copyright (C) 2003, 2007 Apple Inc. All rights reserved.
+ *  Copyright (C) 2003, 2007, 2008 Apple Inc. All rights reserved.
  *  Copyright (C) 2003 Peter Kelly (pmk@post.com)
  *  Copyright (C) 2006 Alexey Proskuryakov (ap@nypop.com)
  *
@@ -720,10 +720,8 @@
 
 // ------------------------------ ArrayObjectImp -------------------------------
 
-ArrayObjectImp::ArrayObjectImp(ExecState* exec,
-                               FunctionPrototype* funcProto,
-                               ArrayPrototype* arrayProto)
-        : InternalFunctionImp(funcProto)
+ArrayObjectImp::ArrayObjectImp(ExecState* exec, FunctionPrototype* funcProto, ArrayPrototype* arrayProto)
+    : InternalFunctionImp(funcProto, arrayProto->classInfo()->className)
 {
     // ECMA 15.4.3.1 Array.prototype
     put(exec, exec->propertyNames().prototype, arrayProto, DontEnum|DontDelete|ReadOnly);
diff --git a/JavaScriptCore/kjs/bool_object.cpp b/JavaScriptCore/kjs/bool_object.cpp
index 4a6eb51..10bb738 100644
--- a/JavaScriptCore/kjs/bool_object.cpp
+++ b/JavaScriptCore/kjs/bool_object.cpp
@@ -85,7 +85,7 @@
 
 
 BooleanObjectImp::BooleanObjectImp(ExecState* exec, FunctionPrototype* functionPrototype, BooleanPrototype* booleanPrototype)
-    : InternalFunctionImp(functionPrototype)
+    : InternalFunctionImp(functionPrototype, booleanPrototype->classInfo()->className)
 {
     putDirect(exec->propertyNames().prototype, booleanPrototype, DontEnum | DontDelete | ReadOnly);
 
diff --git a/JavaScriptCore/kjs/date_object.cpp b/JavaScriptCore/kjs/date_object.cpp
index 8160657..6b757c2 100644
--- a/JavaScriptCore/kjs/date_object.cpp
+++ b/JavaScriptCore/kjs/date_object.cpp
@@ -1,6 +1,6 @@
 /*
  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
- *  Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
+ *  Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
  *
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Lesser General Public
@@ -439,10 +439,8 @@
 
 // TODO: MakeTime (15.9.11.1) etc. ?
 
-DateObjectImp::DateObjectImp(ExecState *exec,
-                             FunctionPrototype *funcProto,
-                             DatePrototype *dateProto)
-  : InternalFunctionImp(funcProto)
+DateObjectImp::DateObjectImp(ExecState* exec, FunctionPrototype* funcProto, DatePrototype* dateProto)
+  : InternalFunctionImp(funcProto, dateProto->classInfo()->className)
 {
   static const Identifier* parsePropertyName = new Identifier("parse");
   static const Identifier* UTCPropertyName = new Identifier("UTC");
diff --git a/JavaScriptCore/kjs/error_object.cpp b/JavaScriptCore/kjs/error_object.cpp
index 9b329e6..7c0eee1 100644
--- a/JavaScriptCore/kjs/error_object.cpp
+++ b/JavaScriptCore/kjs/error_object.cpp
@@ -42,7 +42,7 @@
 
 // ECMA 15.9.4
 ErrorPrototype::ErrorPrototype(ExecState* exec, ObjectPrototype* objectPrototype, FunctionPrototype* functionPrototype)
-    : JSObject(objectPrototype)
+    : ErrorInstance(objectPrototype)
 {
     // The constructor will be added later in ErrorObjectImp's constructor
 
@@ -71,12 +71,11 @@
 // ------------------------------ ErrorObjectImp -------------------------------
 
 ErrorObjectImp::ErrorObjectImp(ExecState* exec, FunctionPrototype* funcProto, ErrorPrototype* errorProto)
-    : InternalFunctionImp(funcProto)
+    : InternalFunctionImp(funcProto, errorProto->classInfo()->className)
 {
     // ECMA 15.11.3.1 Error.prototype
     putDirect(exec->propertyNames().prototype, errorProto, DontEnum|DontDelete|ReadOnly);
     putDirect(exec->propertyNames().length, jsNumber(1), DontDelete|ReadOnly|DontEnum);
-    //putDirect(namePropertyName, jsString(n));
 }
 
 bool ErrorObjectImp::implementsConstruct() const
@@ -106,7 +105,7 @@
 
 // ------------------------------ NativeErrorPrototype ----------------------
 
-NativeErrorPrototype::NativeErrorPrototype(ExecState* exec, ErrorPrototype* errorProto, UString name, UString message)
+NativeErrorPrototype::NativeErrorPrototype(ExecState* exec, ErrorPrototype* errorProto, const UString& name, const UString& message)
     : JSObject(errorProto)
 {
     putDirect(exec->propertyNames().name, jsString(name), 0);
@@ -117,8 +116,8 @@
 
 const ClassInfo NativeErrorImp::info = { "Function", &InternalFunctionImp::info, 0 };
 
-NativeErrorImp::NativeErrorImp(ExecState* exec, FunctionPrototype* funcProto, JSObject* prot)
-    : InternalFunctionImp(funcProto)
+NativeErrorImp::NativeErrorImp(ExecState* exec, FunctionPrototype* funcProto, NativeErrorPrototype* prot)
+    : InternalFunctionImp(funcProto, Identifier(prot->getDirect(exec->propertyNames().name)->getString()))
     , proto(prot)
 {
     putDirect(exec->propertyNames().length, jsNumber(1), DontDelete|ReadOnly|DontEnum); // ECMA 15.11.7.5
diff --git a/JavaScriptCore/kjs/error_object.h b/JavaScriptCore/kjs/error_object.h
index 8283c5d..9734085 100644
--- a/JavaScriptCore/kjs/error_object.h
+++ b/JavaScriptCore/kjs/error_object.h
@@ -33,7 +33,7 @@
         static const ClassInfo info;
     };
 
-    class ErrorPrototype : public JSObject {
+    class ErrorPrototype : public ErrorInstance {
     public:
         ErrorPrototype(ExecState*, ObjectPrototype*, FunctionPrototype*);
     };
@@ -52,12 +52,12 @@
 
     class NativeErrorPrototype : public JSObject {
     public:
-        NativeErrorPrototype(ExecState*, ErrorPrototype*, UString name, UString message);
+        NativeErrorPrototype(ExecState*, ErrorPrototype*, const UString& name, const UString& message);
     };
 
     class NativeErrorImp : public InternalFunctionImp {
     public:
-        NativeErrorImp(ExecState*, FunctionPrototype*, JSObject*);
+        NativeErrorImp(ExecState*, FunctionPrototype*, NativeErrorPrototype*);
 
         virtual bool implementsConstruct() const;
         virtual JSObject* construct(ExecState*, const List&);
diff --git a/JavaScriptCore/kjs/function.h b/JavaScriptCore/kjs/function.h
index 00f939f..a45221e 100644
--- a/JavaScriptCore/kjs/function.h
+++ b/JavaScriptCore/kjs/function.h
@@ -1,7 +1,7 @@
 // -*- c-basic-offset: 2 -*-
 /*
  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
- *  Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved.
+ *  Copyright (C) 2003, 2006, 2007, 2008 Apple Inc. All rights reserved.
  *  Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca)
  *  Copyright (C) 2007 Maks Orlovich
  *
@@ -41,7 +41,6 @@
   class InternalFunctionImp : public JSObject {
   public:
     InternalFunctionImp();
-    InternalFunctionImp(FunctionPrototype*);
     InternalFunctionImp(FunctionPrototype*, const Identifier&);
 
     virtual bool implementsCall() const;
diff --git a/JavaScriptCore/kjs/function_object.cpp b/JavaScriptCore/kjs/function_object.cpp
index a61db85..dc20e70 100644
--- a/JavaScriptCore/kjs/function_object.cpp
+++ b/JavaScriptCore/kjs/function_object.cpp
@@ -1,8 +1,6 @@
-// -*- c-basic-offset: 2 -*-
 /*
- *  This file is part of the KDE libraries
  *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
- *  Copyright (C) 2003, 2004, 2005, 2006 Apple Computer, Inc.
+ *  Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
  *
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Lesser General Public
@@ -78,10 +76,7 @@
         return jsString("function " + fi->functionName().ustring() + "(" + fi->body->paramString() + ") " + fi->body->toString());
     }
 
-    if (thisObj->inherits(&InternalFunctionImp::info) && !static_cast<InternalFunctionImp*>(thisObj)->functionName().isNull())
-        return jsString("\nfunction " + static_cast<InternalFunctionImp*>(thisObj)->functionName().ustring() + "() {\n    [native code]\n}\n");
-
-    return jsString("[function]");
+    return jsString("function " + static_cast<InternalFunctionImp*>(thisObj)->functionName().ustring() + "() {\n    [native code]\n}");
 }
 
 JSValue* functionProtoFuncApply(ExecState* exec, JSObject* thisObj, const List& args)
@@ -136,7 +131,7 @@
 // ------------------------------ FunctionObjectImp ----------------------------
 
 FunctionObjectImp::FunctionObjectImp(ExecState* exec, FunctionPrototype* functionPrototype)
-    : InternalFunctionImp(functionPrototype)
+    : InternalFunctionImp(functionPrototype, functionPrototype->classInfo()->className)
 {
     putDirect(exec->propertyNames().prototype, functionPrototype, DontEnum | DontDelete | ReadOnly);
 
diff --git a/JavaScriptCore/kjs/internal.cpp b/JavaScriptCore/kjs/internal.cpp
index 74e4e99..5482b48 100644
--- a/JavaScriptCore/kjs/internal.cpp
+++ b/JavaScriptCore/kjs/internal.cpp
@@ -1,7 +1,7 @@
 /*
  *  Copyright (C) 1999-2002 Harri Porten (porten@kde.org)
  *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
- *  Copyright (C) 2004, 2007 Apple Inc. All rights reserved.
+ *  Copyright (C) 2004, 2007, 2008 Apple Inc. All rights reserved.
  *
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
@@ -226,11 +226,6 @@
 {
 }
 
-InternalFunctionImp::InternalFunctionImp(FunctionPrototype* funcProto)
-  : JSObject(funcProto)
-{
-}
-
 InternalFunctionImp::InternalFunctionImp(FunctionPrototype* funcProto, const Identifier& name)
   : JSObject(funcProto)
   , m_name(name)
diff --git a/JavaScriptCore/kjs/number_object.cpp b/JavaScriptCore/kjs/number_object.cpp
index 6b03f8c..94fc7e7 100644
--- a/JavaScriptCore/kjs/number_object.cpp
+++ b/JavaScriptCore/kjs/number_object.cpp
@@ -1,7 +1,6 @@
-// -*- c-basic-offset: 2 -*-
 /*
  *  Copyright (C) 1999-2000,2003 Harri Porten (porten@kde.org)
- *  Copyright (C) 2007 Apple Inc. All rights reserved.
+ *  Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
  *
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Lesser General Public
@@ -274,7 +273,7 @@
     return jsString(s + m.substr(0, kMinusf));
 }
 
-void fractionalPartToString(char* buf, int& i, const char* result, int resultLength, int fractionalDigits)
+static void fractionalPartToString(char* buf, int& i, const char* result, int resultLength, int fractionalDigits)
 {
     if (fractionalDigits <= 0)
         return;
@@ -295,7 +294,7 @@
         buf[i++] = '0';
 }
 
-void exponentialPartToString(char* buf, int& i, int decimalPoint)
+static void exponentialPartToString(char* buf, int& i, int decimalPoint)
 {
     buf[i++] = 'e';
     buf[i++] = (decimalPoint >= 0) ? '+' : '-';
@@ -441,7 +440,7 @@
 
     if (e == precision - 1)
         return jsString(s + m);
-    else if (e >= 0) {
+    if (e >= 0) {
         if (e + 1 < m.size())
             return jsString(s + m.substr(0, e + 1) + "." + m.substr(e + 1));
         return jsString(s + m);
@@ -463,7 +462,7 @@
 @end
 */
 NumberObjectImp::NumberObjectImp(ExecState* exec, FunctionPrototype* funcProto, NumberPrototype* numberProto)
-    : InternalFunctionImp(funcProto)
+    : InternalFunctionImp(funcProto, numberProto->classInfo()->className)
 {
     // Number.Prototype
     putDirect(exec->propertyNames().prototype, numberProto, DontEnum|DontDelete|ReadOnly);
@@ -481,17 +480,18 @@
 {
     // ECMA 15.7.3
     switch (token) {
-    case NaNValue:
-        return jsNaN();
-    case NegInfinity:
-        return jsNumberCell(-Inf);
-    case PosInfinity:
-        return jsNumberCell(Inf);
-    case MaxValue:
-        return jsNumberCell(1.7976931348623157E+308);
-    case MinValue:
-        return jsNumberCell(5E-324);
+        case NaNValue:
+            return jsNaN();
+        case NegInfinity:
+            return jsNumberCell(-Inf);
+        case PosInfinity:
+            return jsNumberCell(Inf);
+        case MaxValue:
+            return jsNumberCell(1.7976931348623157E+308);
+        case MinValue:
+            return jsNumberCell(5E-324);
     }
+    ASSERT_NOT_REACHED();
     return jsNull();
 }
 
@@ -506,6 +506,7 @@
     JSObject* proto = exec->lexicalGlobalObject()->numberPrototype();
     NumberInstance* obj = new NumberInstance(proto);
 
+    // FIXME: Check args[0]->isUndefined() instead of args.isEmpty()?
     double n = args.isEmpty() ? 0 : args[0]->toNumber(exec);
     obj->setInternalValue(jsNumber(n));
     return obj;
@@ -514,8 +515,8 @@
 // ECMA 15.7.2
 JSValue* NumberObjectImp::callAsFunction(ExecState* exec, JSObject*, const List& args)
 {
-    double n = args.isEmpty() ? 0 : args[0]->toNumber(exec);
-    return jsNumber(n);
+    // FIXME: Check args[0]->isUndefined() instead of args.isEmpty()?
+    return jsNumber(args.isEmpty() ? 0 : args[0]->toNumber(exec));
 }
 
 } // namespace KJS
diff --git a/JavaScriptCore/kjs/object_object.cpp b/JavaScriptCore/kjs/object_object.cpp
index 05a9b26..b28fd65 100644
--- a/JavaScriptCore/kjs/object_object.cpp
+++ b/JavaScriptCore/kjs/object_object.cpp
@@ -175,7 +175,7 @@
 // ------------------------------ ObjectObjectImp --------------------------------
 
 ObjectObjectImp::ObjectObjectImp(ExecState* exec, ObjectPrototype* objProto, FunctionPrototype* funcProto)
-  : InternalFunctionImp(funcProto)
+  : InternalFunctionImp(funcProto, "Object")
 {
   // ECMA 15.2.3.1
   putDirect(exec->propertyNames().prototype, objProto, DontEnum|DontDelete|ReadOnly);
diff --git a/JavaScriptCore/kjs/regexp_object.cpp b/JavaScriptCore/kjs/regexp_object.cpp
index 68c164c..58b1a82 100644
--- a/JavaScriptCore/kjs/regexp_object.cpp
+++ b/JavaScriptCore/kjs/regexp_object.cpp
@@ -285,7 +285,7 @@
 };
 
 RegExpObjectImp::RegExpObjectImp(ExecState* exec, FunctionPrototype* funcProto, RegExpPrototype* regProto)
-  : InternalFunctionImp(funcProto)
+  : InternalFunctionImp(funcProto, "RegExp")
   , d(new RegExpObjectImpPrivate)
 {
   // ECMA 15.10.5.1 RegExp.prototype
diff --git a/JavaScriptCore/kjs/string_object.cpp b/JavaScriptCore/kjs/string_object.cpp
index 43c245e..3478f5e 100644
--- a/JavaScriptCore/kjs/string_object.cpp
+++ b/JavaScriptCore/kjs/string_object.cpp
@@ -1,7 +1,7 @@
 // -*- c-basic-offset: 2 -*-
 /*
  *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
- *  Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
+ *  Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
  *
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Lesser General Public
@@ -987,10 +987,8 @@
 
 // ------------------------------ StringObjectImp ------------------------------
 
-StringObjectImp::StringObjectImp(ExecState* exec,
-                                 FunctionPrototype* funcProto,
-                                 StringPrototype* stringProto)
-  : InternalFunctionImp(funcProto)
+StringObjectImp::StringObjectImp(ExecState* exec, FunctionPrototype* funcProto, StringPrototype* stringProto)
+  : InternalFunctionImp(funcProto, stringProto->classInfo()->className)
 {
   // ECMA 15.5.3.1 String.prototype
   putDirect(exec->propertyNames().prototype, stringProto, DontEnum|DontDelete|ReadOnly);