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 "JSFunction.h"
25#include "JSGlobalObject.h"
26#include "PrototypeFunction.h"
27#include "StringPrototype.h"
28
29namespace JSC {
30
31static NEVER_INLINE JSValue stringFromCharCodeSlowCase(ExecState* exec, const ArgList& args)
32{
33    unsigned length = args.size();
34    UChar* buf;
35    PassRefPtr<UStringImpl> impl = UStringImpl::createUninitialized(length, buf);
36    for (unsigned i = 0; i < length; ++i)
37        buf[i] = static_cast<UChar>(args.at(i).toUInt32(exec));
38    return jsString(exec, impl);
39}
40
41static JSValue JSC_HOST_CALL stringFromCharCode(ExecState* exec, JSObject*, JSValue, const ArgList& args)
42{
43    if (LIKELY(args.size() == 1))
44        return jsSingleCharacterString(exec, args.at(0).toUInt32(exec));
45    return stringFromCharCodeSlowCase(exec, args);
46}
47
48ASSERT_CLASS_FITS_IN_CELL(StringConstructor);
49
50StringConstructor::StringConstructor(ExecState* exec, NonNullPassRefPtr<Structure> structure, Structure* prototypeFunctionStructure, StringPrototype* stringPrototype)
51    : InternalFunction(&exec->globalData(), structure, Identifier(exec, stringPrototype->classInfo()->className))
52{
53    // ECMA 15.5.3.1 String.prototype
54    putDirectWithoutTransition(exec->propertyNames().prototype, stringPrototype, ReadOnly | DontEnum | DontDelete);
55
56    // ECMA 15.5.3.2 fromCharCode()
57    putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 1, exec->propertyNames().fromCharCode, stringFromCharCode), DontEnum);
58
59    // no. of arguments for constructor
60    putDirectWithoutTransition(exec->propertyNames().length, jsNumber(exec, 1), ReadOnly | DontEnum | DontDelete);
61}
62
63// ECMA 15.5.2
64static JSObject* constructWithStringConstructor(ExecState* exec, JSObject*, const ArgList& args)
65{
66    if (args.isEmpty())
67        return new (exec) StringObject(exec, exec->lexicalGlobalObject()->stringObjectStructure());
68    return new (exec) StringObject(exec, exec->lexicalGlobalObject()->stringObjectStructure(), args.at(0).toString(exec));
69}
70
71ConstructType StringConstructor::getConstructData(ConstructData& constructData)
72{
73    constructData.native.function = constructWithStringConstructor;
74    return ConstructTypeHost;
75}
76
77// ECMA 15.5.1
78static JSValue JSC_HOST_CALL callStringConstructor(ExecState* exec, JSObject*, JSValue, const ArgList& args)
79{
80    if (args.isEmpty())
81        return jsEmptyString(exec);
82    return jsString(exec, args.at(0).toString(exec));
83}
84
85CallType StringConstructor::getCallData(CallData& callData)
86{
87    callData.native.function = callStringConstructor;
88    return CallTypeHost;
89}
90
91} // namespace JSC
92