2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
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.
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.
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
22 #include "StringConstructor.h"
24 #include "Executable.h"
26 #include "JSFunction.h"
27 #include "JSGlobalObject.h"
28 #include "StringPrototype.h"
32 static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState*);
36 #include "StringConstructor.lut.h"
40 const ClassInfo StringConstructor::s_info = { "Function", &InternalFunction::s_info, 0, ExecState::stringConstructorTable };
42 /* Source for StringConstructor.lut.h
43 @begin stringConstructorTable
44 fromCharCode stringFromCharCode DontEnum|Function 1
48 ASSERT_CLASS_FITS_IN_CELL(StringConstructor);
50 StringConstructor::StringConstructor(JSGlobalObject* globalObject, Structure* structure)
51 : InternalFunction(globalObject, structure)
55 void StringConstructor::finishCreation(ExecState* exec, StringPrototype* stringPrototype)
57 Base::finishCreation(exec->globalData(), Identifier(exec, stringPrototype->classInfo()->className));
58 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().prototype, stringPrototype, ReadOnly | DontEnum | DontDelete);
59 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(1), ReadOnly | DontEnum | DontDelete);
62 bool StringConstructor::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
64 return getStaticFunctionSlot<InternalFunction>(exec, ExecState::stringConstructorTable(exec), this, propertyName, slot);
67 bool StringConstructor::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
69 return getStaticFunctionDescriptor<InternalFunction>(exec, ExecState::stringConstructorTable(exec), this, propertyName, descriptor);
72 // ------------------------------ Functions --------------------------------
74 static NEVER_INLINE JSValue stringFromCharCodeSlowCase(ExecState* exec)
76 unsigned length = exec->argumentCount();
78 PassRefPtr<StringImpl> impl = StringImpl::createUninitialized(length, buf);
79 for (unsigned i = 0; i < length; ++i)
80 buf[i] = static_cast<UChar>(exec->argument(i).toUInt32(exec));
81 return jsString(exec, impl);
84 static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState* exec)
86 if (LIKELY(exec->argumentCount() == 1))
87 return JSValue::encode(jsSingleCharacterString(exec, exec->argument(0).toUInt32(exec)));
88 return JSValue::encode(stringFromCharCodeSlowCase(exec));
91 static EncodedJSValue JSC_HOST_CALL constructWithStringConstructor(ExecState* exec)
93 JSGlobalObject* globalObject = asInternalFunction(exec->callee())->globalObject();
94 if (!exec->argumentCount())
95 return JSValue::encode(StringObject::create(exec, globalObject->stringObjectStructure()));
96 return JSValue::encode(StringObject::create(exec, globalObject->stringObjectStructure(), exec->argument(0).toString(exec)));
99 ConstructType StringConstructor::getConstructData(ConstructData& constructData)
101 constructData.native.function = constructWithStringConstructor;
102 return ConstructTypeHost;
105 static EncodedJSValue JSC_HOST_CALL callStringConstructor(ExecState* exec)
107 if (!exec->argumentCount())
108 return JSValue::encode(jsEmptyString(exec));
109 return JSValue::encode(jsString(exec, exec->argument(0).toString(exec)));
112 CallType StringConstructor::getCallData(CallData& callData)
114 callData.native.function = callStringConstructor;