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