]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/StringConstructor.cpp
JavaScriptCore-1097.3.3.tar.gz
[apple/javascriptcore.git] / 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 ASSERT_HAS_TRIVIAL_DESTRUCTOR(StringConstructor);
50
51 StringConstructor::StringConstructor(JSGlobalObject* globalObject, Structure* structure)
52 : InternalFunction(globalObject, structure)
53 {
54 }
55
56 void StringConstructor::finishCreation(ExecState* exec, StringPrototype* stringPrototype)
57 {
58 Base::finishCreation(exec->globalData(), Identifier(exec, stringPrototype->classInfo()->className));
59 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().prototype, stringPrototype, ReadOnly | DontEnum | DontDelete);
60 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(1), ReadOnly | DontEnum | DontDelete);
61 }
62
63 bool StringConstructor::getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
64 {
65 return getStaticFunctionSlot<InternalFunction>(exec, ExecState::stringConstructorTable(exec), jsCast<StringConstructor*>(cell), propertyName, slot);
66 }
67
68 bool StringConstructor::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
69 {
70 return getStaticFunctionDescriptor<InternalFunction>(exec, ExecState::stringConstructorTable(exec), jsCast<StringConstructor*>(object), propertyName, descriptor);
71 }
72
73 // ------------------------------ Functions --------------------------------
74
75 static NEVER_INLINE JSValue stringFromCharCodeSlowCase(ExecState* exec)
76 {
77 unsigned length = exec->argumentCount();
78 UChar* buf;
79 PassRefPtr<StringImpl> impl = StringImpl::createUninitialized(length, buf);
80 for (unsigned i = 0; i < length; ++i)
81 buf[i] = static_cast<UChar>(exec->argument(i).toUInt32(exec));
82 return jsString(exec, impl);
83 }
84
85 static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState* exec)
86 {
87 if (LIKELY(exec->argumentCount() == 1))
88 return JSValue::encode(jsSingleCharacterString(exec, exec->argument(0).toUInt32(exec)));
89 return JSValue::encode(stringFromCharCodeSlowCase(exec));
90 }
91
92 static EncodedJSValue JSC_HOST_CALL constructWithStringConstructor(ExecState* exec)
93 {
94 JSGlobalObject* globalObject = asInternalFunction(exec->callee())->globalObject();
95 if (!exec->argumentCount())
96 return JSValue::encode(StringObject::create(exec, globalObject->stringObjectStructure()));
97
98 return JSValue::encode(StringObject::create(exec, globalObject->stringObjectStructure(), exec->argument(0).toString(exec)));
99 }
100
101 ConstructType StringConstructor::getConstructData(JSCell*, ConstructData& constructData)
102 {
103 constructData.native.function = constructWithStringConstructor;
104 return ConstructTypeHost;
105 }
106
107 static EncodedJSValue JSC_HOST_CALL callStringConstructor(ExecState* exec)
108 {
109 if (!exec->argumentCount())
110 return JSValue::encode(jsEmptyString(exec));
111 return JSValue::encode(exec->argument(0).toString(exec));
112 }
113
114 CallType StringConstructor::getCallData(JSCell*, CallData& callData)
115 {
116 callData.native.function = callStringConstructor;
117 return CallTypeHost;
118 }
119
120 } // namespace JSC