]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/StringConstructor.cpp
JavaScriptCore-1218.33.tar.gz
[apple/javascriptcore.git] / runtime / StringConstructor.cpp
CommitLineData
9dae56ea
A
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
14957cd0
A
24#include "Executable.h"
25#include "JITCode.h"
ba379fdc 26#include "JSFunction.h"
9dae56ea 27#include "JSGlobalObject.h"
93a37866 28#include "Operations.h"
9dae56ea
A
29#include "StringPrototype.h"
30
31namespace JSC {
32
14957cd0
A
33static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState*);
34
35}
36
37#include "StringConstructor.lut.h"
38
39namespace JSC {
40
6fe7ccc8 41const ClassInfo StringConstructor::s_info = { "Function", &InternalFunction::s_info, 0, ExecState::stringConstructorTable, CREATE_METHOD_TABLE(StringConstructor) };
14957cd0
A
42
43/* Source for StringConstructor.lut.h
44@begin stringConstructorTable
45 fromCharCode stringFromCharCode DontEnum|Function 1
46@end
47*/
48
6fe7ccc8 49ASSERT_HAS_TRIVIAL_DESTRUCTOR(StringConstructor);
14957cd0 50
6fe7ccc8
A
51StringConstructor::StringConstructor(JSGlobalObject* globalObject, Structure* structure)
52 : InternalFunction(globalObject, structure)
9dae56ea 53{
6fe7ccc8
A
54}
55
56void StringConstructor::finishCreation(ExecState* exec, StringPrototype* stringPrototype)
57{
93a37866
A
58 Base::finishCreation(exec->vm(), stringPrototype->classInfo()->className);
59 putDirectWithoutTransition(exec->vm(), exec->propertyNames().prototype, stringPrototype, ReadOnly | DontEnum | DontDelete);
60 putDirectWithoutTransition(exec->vm(), exec->propertyNames().length, jsNumber(1), ReadOnly | DontEnum | DontDelete);
9dae56ea
A
61}
62
93a37866 63bool StringConstructor::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot &slot)
9dae56ea 64{
6fe7ccc8 65 return getStaticFunctionSlot<InternalFunction>(exec, ExecState::stringConstructorTable(exec), jsCast<StringConstructor*>(cell), propertyName, slot);
9dae56ea
A
66}
67
93a37866 68bool StringConstructor::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
14957cd0 69{
6fe7ccc8 70 return getStaticFunctionDescriptor<InternalFunction>(exec, ExecState::stringConstructorTable(exec), jsCast<StringConstructor*>(object), propertyName, descriptor);
14957cd0
A
71}
72
73// ------------------------------ Functions --------------------------------
74
75static 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}
9dae56ea 84
14957cd0 85static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState* exec)
9dae56ea 86{
14957cd0
A
87 if (LIKELY(exec->argumentCount() == 1))
88 return JSValue::encode(jsSingleCharacterString(exec, exec->argument(0).toUInt32(exec)));
89 return JSValue::encode(stringFromCharCodeSlowCase(exec));
9dae56ea
A
90}
91
93a37866
A
92JSCell* JSC_HOST_CALL stringFromCharCode(ExecState* exec, int32_t arg)
93{
94 return jsSingleCharacterString(exec, arg);
95}
96
14957cd0 97static EncodedJSValue JSC_HOST_CALL constructWithStringConstructor(ExecState* exec)
9dae56ea 98{
14957cd0
A
99 JSGlobalObject* globalObject = asInternalFunction(exec->callee())->globalObject();
100 if (!exec->argumentCount())
6fe7ccc8
A
101 return JSValue::encode(StringObject::create(exec, globalObject->stringObjectStructure()));
102
103 return JSValue::encode(StringObject::create(exec, globalObject->stringObjectStructure(), exec->argument(0).toString(exec)));
9dae56ea
A
104}
105
6fe7ccc8 106ConstructType StringConstructor::getConstructData(JSCell*, ConstructData& constructData)
9dae56ea
A
107{
108 constructData.native.function = constructWithStringConstructor;
109 return ConstructTypeHost;
110}
111
14957cd0 112static EncodedJSValue JSC_HOST_CALL callStringConstructor(ExecState* exec)
9dae56ea 113{
14957cd0
A
114 if (!exec->argumentCount())
115 return JSValue::encode(jsEmptyString(exec));
6fe7ccc8 116 return JSValue::encode(exec->argument(0).toString(exec));
9dae56ea
A
117}
118
6fe7ccc8 119CallType StringConstructor::getCallData(JSCell*, CallData& callData)
9dae56ea
A
120{
121 callData.native.function = callStringConstructor;
122 return CallTypeHost;
123}
124
125} // namespace JSC