webengine/osswebengine/WebCore/bindings/js/symbian/kjs_console.cpp
changeset 0 dd21522fd290
child 68 92a765b5b3e7
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 
       
    20 #include "kjs_console.h"
       
    21 #include "kjs/Lookup.h"
       
    22 #include "Chrome.h"
       
    23 #include "PlatformString.h"
       
    24 #include "kjs/context.h"
       
    25 #include "kjs/nodes.h"
       
    26 
       
    27 //#include "flogger.h"
       
    28 
       
    29 // EXTERNAL DATA STRUCTURES
       
    30 
       
    31 // EXTERNAL FUNCTION PROTOTYPES
       
    32 
       
    33 // CONSTANTS
       
    34 const TInt KFormattedMaxLength=0x200;
       
    35 
       
    36 // MACROS
       
    37 
       
    38 // LOCAL CONSTANTS AND MACROS
       
    39 
       
    40 // MODULE DATA STRUCTURES
       
    41 
       
    42 // LOCAL FUNCTION PROTOTYPES
       
    43 
       
    44 // FORWARD DECLARATIONS
       
    45 using namespace KJS;
       
    46 using namespace WebCore;
       
    47 const ClassInfo ConsoleObject::info = { "ConsoleObject", 0, &ConsoleTable, 0 };
       
    48 
       
    49 // ============================= LOCAL FUNCTIONS ===============================
       
    50 /*
       
    51 @begin ConsoleTable 7
       
    52 error ConsoleObject::error DontDelete|Function 1
       
    53 info ConsoleObject::information DontDelete|Function 1
       
    54 warn ConsoleObject::warn DontDelete|Function 1
       
    55 log ConsoleObject::log DontDelete|Function 1
       
    56 debug ConsoleObject::debug DontDelete|Function 1
       
    57 assert ConsoleObject::assert DontDelete|Function 1
       
    58 lineno ConsoleObject::lineno DontDelete|ReadOnly
       
    59 sourceURL ConsoleObject::sourceURL DontDelete|ReadOnly
       
    60 @end
       
    61 */
       
    62 
       
    63 // ============================ MEMBER FUNCTIONS ===============================
       
    64 
       
    65 // ----------------------------------------------------------------------------
       
    66 // ConsoleObject::toString
       
    67 // Returns string representation of the object
       
    68 //
       
    69 //
       
    70 // ----------------------------------------------------------------------------
       
    71 UString ConsoleObject::toString(ExecState *exec) const
       
    72 {
       
    73     return "[object ConsoleObject]";
       
    74 }
       
    75 
       
    76 
       
    77 // ----------------------------------------------------------------------------
       
    78 // ConsoleObject::getValueProperty
       
    79 //
       
    80 //
       
    81 // ----------------------------------------------------------------------------
       
    82 JSValue* ConsoleObject::getValueProperty(ExecState *exec, int token) const
       
    83     {
       
    84     switch( token )
       
    85         {
       
    86         case error:
       
    87 		case information:
       
    88 		case warn:
       
    89 		case log:
       
    90 		case debug:
       
    91 		case assert:
       
    92             {
       
    93             ConsoleFunc *cf = new ConsoleFunc( exec,token );
       
    94             return cf;
       
    95             }
       
    96         default:
       
    97             return throwError(exec, GeneralError);
       
    98         }
       
    99     return jsUndefined();
       
   100     }
       
   101 
       
   102 // ----------------------------------------------------------------------------
       
   103 // ConsoleObject::getOwnPropertySlot
       
   104 //
       
   105 //
       
   106 //
       
   107 // ----------------------------------------------------------------------------
       
   108 bool ConsoleObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
       
   109 {
       
   110     const HashEntry* entry = Lookup::findEntry(&ConsoleTable, propertyName);
       
   111     if (entry)
       
   112         {
       
   113         slot.setStaticEntry(this, entry, staticValueGetter<ConsoleObject>);
       
   114         return true;
       
   115         }
       
   116 
       
   117     return JSObject::getOwnPropertySlot(exec, propertyName, slot);
       
   118 }
       
   119 
       
   120 // ----------------------------------------------------------------------------
       
   121 // ConsoleFunc::formatMsg
       
   122 // formats the arguments into a message according to the format string(first arg).
       
   123 //
       
   124 // ----------------------------------------------------------------------------
       
   125 void ConsoleFunc::formatMsg(ExecState *exec, const List &args)
       
   126 {
       
   127     d->m_formattedMsg = String();
       
   128     if (!args.size())
       
   129         return;
       
   130 
       
   131     bool validPattern = true;  // is this valid pattern, i.e. %s, %x, %f, %i, %d, etc?
       
   132     bool reachedMaxLen = false;
       
   133     String KTruncateSign("...");
       
   134     TInt lenReserved = 50;
       
   135     TInt64 maxInteger = 0x100000000;
       
   136 
       
   137     // retrieve the format string
       
   138     ListIterator it = args.begin();
       
   139     String formatStr((*it)->toString(exec));
       
   140     it++;
       
   141     bool endOfArgs = (it != args.end())? false: true;
       
   142 
       
   143     // format the value according to the converters:
       
   144     for (TInt index = formatStr.find('%', 0);
       
   145     (index != KErrNotFound) && !reachedMaxLen;
       
   146     index = formatStr.find('%',0))
       
   147     {
       
   148         // copy the buffer before next %
       
   149         if (index + d->m_formattedMsg.length() > KFormattedMaxLength - lenReserved)
       
   150         {
       
   151             d->m_formattedMsg.append(formatStr.left(KFormattedMaxLength - d->m_formattedMsg.length() - KTruncateSign.length()));
       
   152             d->m_formattedMsg.append(KTruncateSign);
       
   153             reachedMaxLen = true;
       
   154             break;
       
   155         }
       
   156         d->m_formattedMsg.append(formatStr.left(index));
       
   157         UChar patternChar = formatStr[index +1];
       
   158 
       
   159         JSValue* argValue = (*it);
       
   160         switch (patternChar.unicode())
       
   161         {
       
   162         case 's':
       
   163         case 'S':
       
   164             // retrieve and copy string
       
   165             if (!endOfArgs)
       
   166             {
       
   167                 if (argValue->toString(exec).size() + d->m_formattedMsg.length() <= KFormattedMaxLength - lenReserved)
       
   168                     d->m_formattedMsg.append(argValue->toString(exec));
       
   169                 else
       
   170                 {
       
   171                     d->m_formattedMsg.append(String(argValue->toString(exec)).left(d->m_formattedMsg.length() - KTruncateSign.length()));
       
   172                     d->m_formattedMsg.append(KTruncateSign);
       
   173                     reachedMaxLen = true;
       
   174                 }
       
   175             }
       
   176             break;
       
   177         case 'f':
       
   178         case 'F':
       
   179             // convert to float and
       
   180             if (!endOfArgs)
       
   181             {
       
   182                if (argValue == jsNaN() || !argValue->isNumber())
       
   183                     d->m_formattedMsg.append(String("[not valid float]"));
       
   184                 else
       
   185                     d->m_formattedMsg.append(String::number(argValue->getNumber()));
       
   186             }
       
   187             break;
       
   188         case 'i':
       
   189         case 'd':
       
   190             if (!endOfArgs)
       
   191             {
       
   192 				if (argValue == jsNaN() || !argValue->isNumber())
       
   193 				   d->m_formattedMsg.append(String("[not valid integer]"));
       
   194 				else
       
   195                 	d->m_formattedMsg.append(String::number(static_cast<int32_t>(argValue->getNumber())));
       
   196             }
       
   197             break;
       
   198         case 'u':
       
   199         case 'x':
       
   200             if (!endOfArgs)
       
   201             {
       
   202                 uint32_t uintValue;
       
   203                 if(!argValue->getUInt32(uintValue))
       
   204                     d->m_formattedMsg.append(String("[not valid integer]"));
       
   205                 else
       
   206                     if (patternChar == 'x')
       
   207                     {
       
   208 						char hex[32];
       
   209 						snprintf(hex, 32, "%x", uintValue);
       
   210                          d->m_formattedMsg.append(hex);
       
   211                     }
       
   212                     else
       
   213                         d->m_formattedMsg.append(String::number(uintValue));
       
   214             }
       
   215             break;
       
   216         case '%':
       
   217             d->m_formattedMsg.append('%');
       
   218             break;
       
   219         default:
       
   220             validPattern = false;
       
   221         }   // switch
       
   222         if (!endOfArgs) ++it;
       
   223         if (it == args.end())
       
   224             endOfArgs = true;
       
   225 
       
   226         int remainedLen;
       
   227         if (validPattern)
       
   228             remainedLen = formatStr.length() - index - 2;
       
   229         else
       
   230             remainedLen = formatStr.length() - index - 1;
       
   231         formatStr = formatStr.right(remainedLen);
       
   232     } // for
       
   233 
       
   234     if (!reachedMaxLen && formatStr.length() + d->m_formattedMsg.length() <= KFormattedMaxLength)
       
   235     {
       
   236         d->m_formattedMsg.append(formatStr);
       
   237     }
       
   238     else if (!reachedMaxLen)
       
   239     {
       
   240         d->m_formattedMsg.append(formatStr.left(KFormattedMaxLength - d->m_formattedMsg.length() - KTruncateSign.length()));
       
   241         d->m_formattedMsg.append(KTruncateSign);
       
   242     }
       
   243 }
       
   244 
       
   245 // ----------------------------------------------------------------------------
       
   246 // ConsoleFunc::ConsoleFunc
       
   247 // Default constructor
       
   248 //
       
   249 //
       
   250 // ----------------------------------------------------------------------------
       
   251 ConsoleFunc::ConsoleFunc(ExecState* exec, int fType)
       
   252     : JSObject(exec->lexicalInterpreter()->builtinObjectPrototype()),
       
   253     funcType(fType)
       
   254     {
       
   255     d = new ConsolePrivate;
       
   256     d->m_url = String(exec->context()->currentBody()->sourceURL());
       
   257     d->m_lineno = exec->context()->lineno(); // get the execution line no.
       
   258     }
       
   259 
       
   260 
       
   261     // ----------------------------------------------------------------------------
       
   262     // ConsoleFunc::callAsFunction
       
   263     // Calls this object as if it is a function
       
   264     //
       
   265     //
       
   266     // ----------------------------------------------------------------------------
       
   267 
       
   268 	JSValue* ConsoleFunc::callAsFunction(ExecState *exec, JSObject *thisObj, const List &args)
       
   269     {
       
   270         //RFileLogger::Write(_L("Browser"), _L("console_debug.txt"), EFileLoggingModeAppend,
       
   271         //    _L("callAsFunction called"));
       
   272 
       
   273         if (!thisObj->inherits(&ConsoleObject::info)) {
       
   274             return throwError(exec, GeneralError);
       
   275         }
       
   276 
       
   277         if (d->m_lineno == -1)
       
   278                 d->m_lineno = exec->context()->currentBody()->lineNo();
       
   279         ConsoleObject *console = static_cast<ConsoleObject *>(thisObj);
       
   280 
       
   281         formatMsg(exec, args);
       
   282         switch (funcType)
       
   283         {
       
   284         case ConsoleObject::error:
       
   285         case ConsoleObject::information:
       
   286         case ConsoleObject::warn:
       
   287         case ConsoleObject::log:
       
   288             console->m_chrome->addMessageToConsole(JSMessageSource,
       
   289                 (WebCore::MessageLevel)funcType, String(d->m_formattedMsg), d->m_lineno, String(d->m_url));
       
   290             break;
       
   291         case ConsoleObject::debug:
       
   292             {
       
   293                 break;
       
   294             }
       
   295         case ConsoleObject::assert:
       
   296             {
       
   297                 break;
       
   298             }
       
   299         default:
       
   300             break;
       
   301         }
       
   302 
       
   303         return jsUndefined();
       
   304     }
       
   305     //END OF FILE
       
   306 
       
   307 
       
   308 
       
   309