]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/StringConstructor.cpp
JavaScriptCore-903.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"
9dae56ea
A
28#include "StringPrototype.h"
29
30namespace JSC {
31
14957cd0
A
32static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState*);
33
34}
35
36#include "StringConstructor.lut.h"
37
38namespace JSC {
39
40const 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
48ASSERT_CLASS_FITS_IN_CELL(StringConstructor);
49
50StringConstructor::StringConstructor(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, StringPrototype* stringPrototype)
51 : InternalFunction(&exec->globalData(), globalObject, structure, Identifier(exec, stringPrototype->classInfo()->className))
9dae56ea 52{
14957cd0
A
53 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().prototype, stringPrototype, ReadOnly | DontEnum | DontDelete);
54 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(1), ReadOnly | DontEnum | DontDelete);
9dae56ea
A
55}
56
14957cd0 57bool StringConstructor::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
9dae56ea 58{
14957cd0 59 return getStaticFunctionSlot<InternalFunction>(exec, ExecState::stringConstructorTable(exec), this, propertyName, slot);
9dae56ea
A
60}
61
14957cd0
A
62bool StringConstructor::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
63{
64 return getStaticFunctionDescriptor<InternalFunction>(exec, ExecState::stringConstructorTable(exec), this, propertyName, descriptor);
65}
66
67// ------------------------------ Functions --------------------------------
68
69static NEVER_INLINE JSValue stringFromCharCodeSlowCase(ExecState* exec)
70{
71 unsigned length = exec->argumentCount();
72 UChar* buf;
73 PassRefPtr<StringImpl> impl = StringImpl::createUninitialized(length, buf);
74 for (unsigned i = 0; i < length; ++i)
75 buf[i] = static_cast<UChar>(exec->argument(i).toUInt32(exec));
76 return jsString(exec, impl);
77}
9dae56ea 78
14957cd0 79static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState* exec)
9dae56ea 80{
14957cd0
A
81 if (LIKELY(exec->argumentCount() == 1))
82 return JSValue::encode(jsSingleCharacterString(exec, exec->argument(0).toUInt32(exec)));
83 return JSValue::encode(stringFromCharCodeSlowCase(exec));
9dae56ea
A
84}
85
14957cd0 86static EncodedJSValue JSC_HOST_CALL constructWithStringConstructor(ExecState* exec)
9dae56ea 87{
14957cd0
A
88 JSGlobalObject* globalObject = asInternalFunction(exec->callee())->globalObject();
89 if (!exec->argumentCount())
90 return JSValue::encode(new (exec) StringObject(exec, globalObject->stringObjectStructure()));
91 return JSValue::encode(new (exec) StringObject(exec, globalObject->stringObjectStructure(), exec->argument(0).toString(exec)));
9dae56ea
A
92}
93
94ConstructType StringConstructor::getConstructData(ConstructData& constructData)
95{
96 constructData.native.function = constructWithStringConstructor;
97 return ConstructTypeHost;
98}
99
14957cd0 100static EncodedJSValue JSC_HOST_CALL callStringConstructor(ExecState* exec)
9dae56ea 101{
14957cd0
A
102 if (!exec->argumentCount())
103 return JSValue::encode(jsEmptyString(exec));
104 return JSValue::encode(jsString(exec, exec->argument(0).toString(exec)));
9dae56ea
A
105}
106
107CallType StringConstructor::getCallData(CallData& callData)
108{
109 callData.native.function = callStringConstructor;
110 return CallTypeHost;
111}
112
113} // namespace JSC