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 "JSCInlines.h"
29#include "StringPrototype.h"
30
31namespace JSC {
32
33static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState*);
34
35}
36
37#include "StringConstructor.lut.h"
38
39namespace JSC {
40
41const ClassInfo StringConstructor::s_info = { "Function", &InternalFunction::s_info, 0, ExecState::stringConstructorTable, CREATE_METHOD_TABLE(StringConstructor) };
42
43/* Source for StringConstructor.lut.h
44@begin stringConstructorTable
45  fromCharCode          stringFromCharCode         DontEnum|Function 1
46@end
47*/
48
49STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(StringConstructor);
50
51StringConstructor::StringConstructor(VM& vm, Structure* structure)
52    : InternalFunction(vm, structure)
53{
54}
55
56void StringConstructor::finishCreation(VM& vm, StringPrototype* stringPrototype)
57{
58    Base::finishCreation(vm, stringPrototype->classInfo()->className);
59    putDirectWithoutTransition(vm, vm.propertyNames->prototype, stringPrototype, ReadOnly | DontEnum | DontDelete);
60    putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), ReadOnly | DontEnum | DontDelete);
61}
62
63bool StringConstructor::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot &slot)
64{
65    return getStaticFunctionSlot<InternalFunction>(exec, ExecState::stringConstructorTable(exec->vm()), jsCast<StringConstructor*>(object), propertyName, slot);
66}
67
68// ------------------------------ Functions --------------------------------
69
70static NEVER_INLINE JSValue stringFromCharCodeSlowCase(ExecState* exec)
71{
72    unsigned length = exec->argumentCount();
73    UChar* buf;
74    PassRefPtr<StringImpl> impl = StringImpl::createUninitialized(length, buf);
75    for (unsigned i = 0; i < length; ++i)
76        buf[i] = static_cast<UChar>(exec->uncheckedArgument(i).toUInt32(exec));
77    return jsString(exec, impl);
78}
79
80static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState* exec)
81{
82    if (LIKELY(exec->argumentCount() == 1))
83        return JSValue::encode(jsSingleCharacterString(exec, exec->uncheckedArgument(0).toUInt32(exec)));
84    return JSValue::encode(stringFromCharCodeSlowCase(exec));
85}
86
87JSCell* JSC_HOST_CALL stringFromCharCode(ExecState* exec, int32_t arg)
88{
89    return jsSingleCharacterString(exec, arg);
90}
91
92static EncodedJSValue JSC_HOST_CALL constructWithStringConstructor(ExecState* exec)
93{
94    JSGlobalObject* globalObject = asInternalFunction(exec->callee())->globalObject();
95    VM& vm = exec->vm();
96
97    if (!exec->argumentCount())
98        return JSValue::encode(StringObject::create(vm, globalObject->stringObjectStructure()));
99
100    return JSValue::encode(StringObject::create(vm, globalObject->stringObjectStructure(), exec->uncheckedArgument(0).toString(exec)));
101}
102
103ConstructType StringConstructor::getConstructData(JSCell*, ConstructData& constructData)
104{
105    constructData.native.function = constructWithStringConstructor;
106    return ConstructTypeHost;
107}
108
109static EncodedJSValue JSC_HOST_CALL callStringConstructor(ExecState* exec)
110{
111    if (!exec->argumentCount())
112        return JSValue::encode(jsEmptyString(exec));
113    return JSValue::encode(exec->uncheckedArgument(0).toString(exec));
114}
115
116CallType StringConstructor::getCallData(JSCell*, CallData& callData)
117{
118    callData.native.function = callStringConstructor;
119    return CallTypeHost;
120}
121
122} // namespace JSC
123