initial import
[vuplus_webkit] / 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 };
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(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
63 {
64     return getStaticFunctionSlot<InternalFunction>(exec, ExecState::stringConstructorTable(exec), this, propertyName, slot);
65 }
66
67 bool StringConstructor::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
68 {
69     return getStaticFunctionDescriptor<InternalFunction>(exec, ExecState::stringConstructorTable(exec), this, 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     return JSValue::encode(StringObject::create(exec, globalObject->stringObjectStructure(), exec->argument(0).toString(exec)));
97 }
98
99 ConstructType StringConstructor::getConstructData(ConstructData& constructData)
100 {
101     constructData.native.function = constructWithStringConstructor;
102     return ConstructTypeHost;
103 }
104
105 static EncodedJSValue JSC_HOST_CALL callStringConstructor(ExecState* exec)
106 {
107     if (!exec->argumentCount())
108         return JSValue::encode(jsEmptyString(exec));
109     return JSValue::encode(jsString(exec, exec->argument(0).toString(exec)));
110 }
111
112 CallType StringConstructor::getCallData(CallData& callData)
113 {
114     callData.native.function = callStringConstructor;
115     return CallTypeHost;
116 }
117
118 } // namespace JSC