tizen beta release
[framework/web/webkit-efl.git] / Source / JavaScriptCore / runtime / StringConstructor.cpp
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
30 namespace JSC {
31
32 static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState*);
33
34 }
35
36 #include "StringConstructor.lut.h"
37
38 namespace JSC {
39
40 const ClassInfo StringConstructor::s_info = { "Function", &InternalFunction::s_info, 0, ExecState::stringConstructorTable, CREATE_METHOD_TABLE(StringConstructor) };
41
42 /* Source for StringConstructor.lut.h
43 @begin stringConstructorTable
44   fromCharCode          stringFromCharCode         DontEnum|Function 1
45 @end
46 */
47
48 ASSERT_CLASS_FITS_IN_CELL(StringConstructor);
49
50 StringConstructor::StringConstructor(JSGlobalObject* globalObject, Structure* structure)
51     : InternalFunction(globalObject, structure)
52 {
53 }
54
55 void StringConstructor::finishCreation(ExecState* exec, StringPrototype* stringPrototype)
56 {
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);
60 }
61
62 bool StringConstructor::getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
63 {
64     return getStaticFunctionSlot<InternalFunction>(exec, ExecState::stringConstructorTable(exec), jsCast<StringConstructor*>(cell), propertyName, slot);
65 }
66
67 bool StringConstructor::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
68 {
69     return getStaticFunctionDescriptor<InternalFunction>(exec, ExecState::stringConstructorTable(exec), jsCast<StringConstructor*>(object), propertyName, descriptor);
70 }
71
72 // ------------------------------ Functions --------------------------------
73
74 static NEVER_INLINE JSValue stringFromCharCodeSlowCase(ExecState* exec)
75 {
76     unsigned length = exec->argumentCount();
77     UChar* buf;
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);
82 }
83
84 static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState* exec)
85 {
86     if (LIKELY(exec->argumentCount() == 1))
87         return JSValue::encode(jsSingleCharacterString(exec, exec->argument(0).toUInt32(exec)));
88     return JSValue::encode(stringFromCharCodeSlowCase(exec));
89 }
90
91 static EncodedJSValue JSC_HOST_CALL constructWithStringConstructor(ExecState* exec)
92 {
93     JSGlobalObject* globalObject = asInternalFunction(exec->callee())->globalObject();
94     if (!exec->argumentCount())
95         return JSValue::encode(StringObject::create(exec, globalObject->stringObjectStructure()));
96     
97     JSString* string = exec->argument(0).isString()
98         ? asString(exec->argument(0))
99         : jsString(exec, exec->argument(0).toString(exec));
100     return JSValue::encode(StringObject::create(exec, globalObject->stringObjectStructure(), string));
101 }
102
103 ConstructType StringConstructor::getConstructData(JSCell*, ConstructData& constructData)
104 {
105     constructData.native.function = constructWithStringConstructor;
106     return ConstructTypeHost;
107 }
108
109 static EncodedJSValue JSC_HOST_CALL callStringConstructor(ExecState* exec)
110 {
111     if (!exec->argumentCount())
112         return JSValue::encode(jsEmptyString(exec));
113     return JSValue::encode(jsString(exec, exec->argument(0).toString(exec)));
114 }
115
116 CallType StringConstructor::getCallData(JSCell*, CallData& callData)
117 {
118     callData.native.function = callStringConstructor;
119     return CallTypeHost;
120 }
121
122 } // namespace JSC