StringConstructor.cpp 4.23 KB
Newer Older
weinig@apple.com's avatar
weinig@apple.com committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
 *  Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "config.h"
#include "StringConstructor.h"

24 25
#include "Executable.h"
#include "JITCode.h"
26
#include "JSFunction.h"
27
#include "JSGlobalObject.h"
28
#include "Operations.h"
weinig@apple.com's avatar
weinig@apple.com committed
29 30
#include "StringPrototype.h"

31
namespace JSC {
weinig@apple.com's avatar
weinig@apple.com committed
32

33 34 35 36 37 38 39 40
static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState*);

}

#include "StringConstructor.lut.h"

namespace JSC {

41
const ClassInfo StringConstructor::s_info = { "Function", &InternalFunction::s_info, 0, ExecState::stringConstructorTable, CREATE_METHOD_TABLE(StringConstructor) };
42 43 44 45 46 47 48

/* Source for StringConstructor.lut.h
@begin stringConstructorTable
  fromCharCode          stringFromCharCode         DontEnum|Function 1
@end
*/

49
STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(StringConstructor);
50

51 52
StringConstructor::StringConstructor(VM& vm, Structure* structure)
    : InternalFunction(vm, structure)
53
{
54 55
}

56
void StringConstructor::finishCreation(VM& vm, StringPrototype* stringPrototype)
57
{
58 59 60
    Base::finishCreation(vm, stringPrototype->classInfo()->className);
    putDirectWithoutTransition(vm, vm.propertyNames->prototype, stringPrototype, ReadOnly | DontEnum | DontDelete);
    putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), ReadOnly | DontEnum | DontDelete);
61 62
}

63
bool StringConstructor::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot &slot)
64
{
65
    return getStaticFunctionSlot<InternalFunction>(exec, ExecState::stringConstructorTable(exec), jsCast<StringConstructor*>(object), propertyName, slot);
66 67 68 69
}

// ------------------------------ Functions --------------------------------

70
static NEVER_INLINE JSValue stringFromCharCodeSlowCase(ExecState* exec)
weinig@apple.com's avatar
weinig@apple.com committed
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)
76
        buf[i] = static_cast<UChar>(exec->uncheckedArgument(i).toUInt32(exec));
77
    return jsString(exec, impl);
darin@apple.com's avatar
darin@apple.com committed
78
}
weinig@apple.com's avatar
weinig@apple.com committed
79

80
static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState* exec)
darin@apple.com's avatar
darin@apple.com committed
81
{
82
    if (LIKELY(exec->argumentCount() == 1))
83
        return JSValue::encode(jsSingleCharacterString(exec, exec->uncheckedArgument(0).toUInt32(exec)));
84
    return JSValue::encode(stringFromCharCodeSlowCase(exec));
weinig@apple.com's avatar
weinig@apple.com committed
85 86
}

87 88 89 90 91
JSCell* JSC_HOST_CALL stringFromCharCode(ExecState* exec, int32_t arg)
{
    return jsSingleCharacterString(exec, arg);
}

92
static EncodedJSValue JSC_HOST_CALL constructWithStringConstructor(ExecState* exec)
weinig@apple.com's avatar
weinig@apple.com committed
93
{
94
    JSGlobalObject* globalObject = asInternalFunction(exec->callee())->globalObject();
95 96
    VM& vm = exec->vm();

97
    if (!exec->argumentCount())
98
        return JSValue::encode(StringObject::create(vm, globalObject->stringObjectStructure()));
99
    
100
    return JSValue::encode(StringObject::create(vm, globalObject->stringObjectStructure(), exec->uncheckedArgument(0).toString(exec)));
weinig@apple.com's avatar
weinig@apple.com committed
101 102
}

103
ConstructType StringConstructor::getConstructData(JSCell*, ConstructData& constructData)
weinig@apple.com's avatar
weinig@apple.com committed
104 105
{
    constructData.native.function = constructWithStringConstructor;
ggaren@apple.com's avatar
ggaren@apple.com committed
106
    return ConstructTypeHost;
weinig@apple.com's avatar
weinig@apple.com committed
107 108
}

109
static EncodedJSValue JSC_HOST_CALL callStringConstructor(ExecState* exec)
weinig@apple.com's avatar
weinig@apple.com committed
110
{
111
    if (!exec->argumentCount())
112
        return JSValue::encode(jsEmptyString(exec));
113
    return JSValue::encode(exec->uncheckedArgument(0).toString(exec));
weinig@apple.com's avatar
weinig@apple.com committed
114 115
}

116
CallType StringConstructor::getCallData(JSCell*, CallData& callData)
weinig@apple.com's avatar
weinig@apple.com committed
117 118
{
    callData.native.function = callStringConstructor;
ggaren@apple.com's avatar
ggaren@apple.com committed
119
    return CallTypeHost;
weinig@apple.com's avatar
weinig@apple.com committed
120 121
}

122
} // namespace JSC