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 "StringPrototype.h"
29
30namespace JSC {
31
32static NEVER_INLINE JSValue stringFromCharCodeSlowCase(ExecState* exec)
33{
34    unsigned length = exec->argumentCount();
35    UChar* buf;
36    PassRefPtr<StringImpl> impl = StringImpl::createUninitialized(length, buf);
37    for (unsigned i = 0; i < length; ++i)
38        buf[i] = static_cast<UChar>(exec->argument(i).toUInt32(exec));
39    return jsString(exec, impl);
40}
41
42static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState* exec)
43{
44    if (LIKELY(exec->argumentCount() == 1))
45        return JSValue::encode(jsSingleCharacterString(exec, exec->argument(0).toUInt32(exec)));
46    return JSValue::encode(stringFromCharCodeSlowCase(exec));
47}
48
49ASSERT_CLASS_FITS_IN_CELL(StringConstructor);
50
51StringConstructor::StringConstructor(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, Structure* functionStructure, StringPrototype* stringPrototype)
52    : InternalFunction(&exec->globalData(), globalObject, structure, Identifier(exec, stringPrototype->classInfo()->className))
53{
54    // ECMA 15.5.3.1 String.prototype
55    putDirectWithoutTransition(exec->globalData(), exec->propertyNames().prototype, stringPrototype, ReadOnly | DontEnum | DontDelete);
56
57    // ECMA 15.5.3.2 fromCharCode()
58#if ENABLE(JIT) && ENABLE(JIT_OPTIMIZE_NATIVE_CALL)
59    putDirectFunctionWithoutTransition(exec, new (exec) JSFunction(exec, globalObject, functionStructure, 1, exec->propertyNames().fromCharCode, exec->globalData().getHostFunction(stringFromCharCode, fromCharCodeThunkGenerator)), DontEnum);
60#else
61    putDirectFunctionWithoutTransition(exec, new (exec) JSFunction(exec, globalObject, functionStructure, 1, exec->propertyNames().fromCharCode, stringFromCharCode), DontEnum);
62#endif
63    // no. of arguments for constructor
64    putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(1), ReadOnly | DontEnum | DontDelete);
65}
66
67// ECMA 15.5.2
68static EncodedJSValue JSC_HOST_CALL constructWithStringConstructor(ExecState* exec)
69{
70    JSGlobalObject* globalObject = asInternalFunction(exec->callee())->globalObject();
71    if (!exec->argumentCount())
72        return JSValue::encode(new (exec) StringObject(exec, globalObject->stringObjectStructure()));
73    return JSValue::encode(new (exec) StringObject(exec, globalObject->stringObjectStructure(), exec->argument(0).toString(exec)));
74}
75
76ConstructType StringConstructor::getConstructData(ConstructData& constructData)
77{
78    constructData.native.function = constructWithStringConstructor;
79    return ConstructTypeHost;
80}
81
82// ECMA 15.5.1
83static 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
90CallType StringConstructor::getCallData(CallData& callData)
91{
92    callData.native.function = callStringConstructor;
93    return CallTypeHost;
94}
95
96} // namespace JSC
97