JavaScriptCore/runtime/StringConstructor.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
       
     3  *  Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
       
     4  *
       
     5  *  This library is free software; you can redistribute it and/or
       
     6  *  modify it under the terms of the GNU Lesser General Public
       
     7  *  License as published by the Free Software Foundation; either
       
     8  *  version 2 of the License, or (at your option) any later version.
       
     9  *
       
    10  *  This library is distributed in the hope that it will be useful,
       
    11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13  *  Lesser General Public License for more details.
       
    14  *
       
    15  *  You should have received a copy of the GNU Lesser General Public
       
    16  *  License along with this library; if not, write to the Free Software
       
    17  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
       
    18  *
       
    19  */
       
    20 
       
    21 #include "config.h"
       
    22 #include "StringConstructor.h"
       
    23 
       
    24 #include "Executable.h"
       
    25 #include "JITCode.h"
       
    26 #include "JSFunction.h"
       
    27 #include "JSGlobalObject.h"
       
    28 #include "PrototypeFunction.h"
       
    29 #include "StringPrototype.h"
       
    30 
       
    31 namespace JSC {
       
    32 
       
    33 static NEVER_INLINE JSValue stringFromCharCodeSlowCase(ExecState* exec)
       
    34 {
       
    35     unsigned length = exec->argumentCount();
       
    36     UChar* buf;
       
    37     PassRefPtr<UStringImpl> impl = UStringImpl::createUninitialized(length, buf);
       
    38     for (unsigned i = 0; i < length; ++i)
       
    39         buf[i] = static_cast<UChar>(exec->argument(i).toUInt32(exec));
       
    40     return jsString(exec, impl);
       
    41 }
       
    42 
       
    43 static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState* exec)
       
    44 {
       
    45     if (LIKELY(exec->argumentCount() == 1))
       
    46         return JSValue::encode(jsSingleCharacterString(exec, exec->argument(0).toUInt32(exec)));
       
    47     return JSValue::encode(stringFromCharCodeSlowCase(exec));
       
    48 }
       
    49 
       
    50 ASSERT_CLASS_FITS_IN_CELL(StringConstructor);
       
    51 
       
    52 StringConstructor::StringConstructor(ExecState* exec, JSGlobalObject* globalObject, NonNullPassRefPtr<Structure> structure, Structure* prototypeFunctionStructure, StringPrototype* stringPrototype)
       
    53     : InternalFunction(&exec->globalData(), globalObject, structure, Identifier(exec, stringPrototype->classInfo()->className))
       
    54 {
       
    55     // ECMA 15.5.3.1 String.prototype
       
    56     putDirectWithoutTransition(exec->propertyNames().prototype, stringPrototype, ReadOnly | DontEnum | DontDelete);
       
    57 
       
    58     // ECMA 15.5.3.2 fromCharCode()
       
    59 #if ENABLE(JIT) && ENABLE(JIT_OPTIMIZE_NATIVE_CALL)
       
    60     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, globalObject, prototypeFunctionStructure, 1, exec->propertyNames().fromCharCode, exec->globalData().getHostFunction(stringFromCharCode, fromCharCodeThunkGenerator)), DontEnum);
       
    61 #else
       
    62     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, globalObject, prototypeFunctionStructure, 1, exec->propertyNames().fromCharCode, stringFromCharCode), DontEnum);
       
    63 #endif
       
    64     // no. of arguments for constructor
       
    65     putDirectWithoutTransition(exec->propertyNames().length, jsNumber(exec, 1), ReadOnly | DontEnum | DontDelete);
       
    66 }
       
    67 
       
    68 // ECMA 15.5.2
       
    69 static EncodedJSValue JSC_HOST_CALL constructWithStringConstructor(ExecState* exec)
       
    70 {
       
    71     if (!exec->argumentCount())
       
    72         return JSValue::encode(new (exec) StringObject(exec, exec->lexicalGlobalObject()->stringObjectStructure()));
       
    73     return JSValue::encode(new (exec) StringObject(exec, exec->lexicalGlobalObject()->stringObjectStructure(), exec->argument(0).toString(exec)));
       
    74 }
       
    75 
       
    76 ConstructType StringConstructor::getConstructData(ConstructData& constructData)
       
    77 {
       
    78     constructData.native.function = constructWithStringConstructor;
       
    79     return ConstructTypeHost;
       
    80 }
       
    81 
       
    82 // ECMA 15.5.1
       
    83 static EncodedJSValue JSC_HOST_CALL callStringConstructor(ExecState* exec)
       
    84 {
       
    85     if (!exec->argumentCount())
       
    86         return JSValue::encode(jsEmptyString(exec));
       
    87     return JSValue::encode(jsString(exec, exec->argument(0).toString(exec)));
       
    88 }
       
    89 
       
    90 CallType StringConstructor::getCallData(CallData& callData)
       
    91 {
       
    92     callData.native.function = callStringConstructor;
       
    93     return CallTypeHost;
       
    94 }
       
    95 
       
    96 } // namespace JSC