]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/StringConstructor.cpp
JavaScriptCore-7601.1.46.3.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
ed1e77d3 24#include "Error.h"
14957cd0
A
25#include "Executable.h"
26#include "JITCode.h"
ba379fdc 27#include "JSFunction.h"
9dae56ea 28#include "JSGlobalObject.h"
81345200 29#include "JSCInlines.h"
9dae56ea
A
30#include "StringPrototype.h"
31
32namespace JSC {
33
14957cd0 34static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState*);
ed1e77d3 35static EncodedJSValue JSC_HOST_CALL stringFromCodePoint(ExecState*);
14957cd0
A
36
37}
38
39#include "StringConstructor.lut.h"
40
41namespace JSC {
42
ed1e77d3 43const ClassInfo StringConstructor::s_info = { "Function", &InternalFunction::s_info, &stringConstructorTable, CREATE_METHOD_TABLE(StringConstructor) };
14957cd0
A
44
45/* Source for StringConstructor.lut.h
46@begin stringConstructorTable
47 fromCharCode stringFromCharCode DontEnum|Function 1
ed1e77d3
A
48 fromCodePoint stringFromCodePoint DontEnum|Function 1
49 raw stringRaw DontEnum|Function 1
14957cd0
A
50@end
51*/
52
81345200 53STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(StringConstructor);
14957cd0 54
81345200
A
55StringConstructor::StringConstructor(VM& vm, Structure* structure)
56 : InternalFunction(vm, structure)
9dae56ea 57{
6fe7ccc8
A
58}
59
81345200 60void StringConstructor::finishCreation(VM& vm, StringPrototype* stringPrototype)
6fe7ccc8 61{
81345200
A
62 Base::finishCreation(vm, stringPrototype->classInfo()->className);
63 putDirectWithoutTransition(vm, vm.propertyNames->prototype, stringPrototype, ReadOnly | DontEnum | DontDelete);
64 putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), ReadOnly | DontEnum | DontDelete);
9dae56ea
A
65}
66
81345200 67bool StringConstructor::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot &slot)
9dae56ea 68{
ed1e77d3 69 return getStaticFunctionSlot<InternalFunction>(exec, stringConstructorTable, jsCast<StringConstructor*>(object), propertyName, slot);
14957cd0
A
70}
71
72// ------------------------------ Functions --------------------------------
73
74static NEVER_INLINE JSValue stringFromCharCodeSlowCase(ExecState* exec)
75{
76 unsigned length = exec->argumentCount();
77 UChar* buf;
78 PassRefPtr<StringImpl> impl = StringImpl::createUninitialized(length, buf);
79 for (unsigned i = 0; i < length; ++i)
81345200 80 buf[i] = static_cast<UChar>(exec->uncheckedArgument(i).toUInt32(exec));
14957cd0
A
81 return jsString(exec, impl);
82}
9dae56ea 83
14957cd0 84static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState* exec)
9dae56ea 85{
14957cd0 86 if (LIKELY(exec->argumentCount() == 1))
81345200 87 return JSValue::encode(jsSingleCharacterString(exec, exec->uncheckedArgument(0).toUInt32(exec)));
14957cd0 88 return JSValue::encode(stringFromCharCodeSlowCase(exec));
9dae56ea
A
89}
90
93a37866
A
91JSCell* JSC_HOST_CALL stringFromCharCode(ExecState* exec, int32_t arg)
92{
93 return jsSingleCharacterString(exec, arg);
94}
95
ed1e77d3
A
96static EncodedJSValue JSC_HOST_CALL stringFromCodePoint(ExecState* exec)
97{
98 unsigned length = exec->argumentCount();
99 StringBuilder builder;
100 builder.reserveCapacity(length);
101
102 for (unsigned i = 0; i < length; ++i) {
103 double codePointAsDouble = exec->uncheckedArgument(i).toNumber(exec);
104 if (exec->hadException())
105 return JSValue::encode(jsUndefined());
106
107 uint32_t codePoint = static_cast<uint32_t>(codePointAsDouble);
108
109 if (codePoint != codePointAsDouble || codePoint > UCHAR_MAX_VALUE)
110 return throwVMError(exec, createRangeError(exec, ASCIILiteral("Arguments contain a value that is out of range of code points")));
111
112 if (U_IS_BMP(codePoint))
113 builder.append(static_cast<UChar>(codePoint));
114 else {
115 builder.append(U16_LEAD(codePoint));
116 builder.append(U16_TRAIL(codePoint));
117 }
118 }
119
120 return JSValue::encode(jsString(exec, builder.toString()));
121}
122
14957cd0 123static EncodedJSValue JSC_HOST_CALL constructWithStringConstructor(ExecState* exec)
9dae56ea 124{
14957cd0 125 JSGlobalObject* globalObject = asInternalFunction(exec->callee())->globalObject();
81345200
A
126 VM& vm = exec->vm();
127
14957cd0 128 if (!exec->argumentCount())
81345200 129 return JSValue::encode(StringObject::create(vm, globalObject->stringObjectStructure()));
ed1e77d3 130
81345200 131 return JSValue::encode(StringObject::create(vm, globalObject->stringObjectStructure(), exec->uncheckedArgument(0).toString(exec)));
9dae56ea
A
132}
133
6fe7ccc8 134ConstructType StringConstructor::getConstructData(JSCell*, ConstructData& constructData)
9dae56ea
A
135{
136 constructData.native.function = constructWithStringConstructor;
137 return ConstructTypeHost;
138}
139
ed1e77d3
A
140JSCell* stringConstructor(ExecState* exec, JSValue argument)
141{
142 if (argument.isSymbol())
143 return jsNontrivialString(exec, asSymbol(argument)->descriptiveString());
144 return argument.toString(exec);
145}
146
14957cd0 147static EncodedJSValue JSC_HOST_CALL callStringConstructor(ExecState* exec)
9dae56ea 148{
14957cd0
A
149 if (!exec->argumentCount())
150 return JSValue::encode(jsEmptyString(exec));
ed1e77d3 151 return JSValue::encode(stringConstructor(exec, exec->uncheckedArgument(0)));
9dae56ea
A
152}
153
6fe7ccc8 154CallType StringConstructor::getCallData(JSCell*, CallData& callData)
9dae56ea
A
155{
156 callData.native.function = callStringConstructor;
157 return CallTypeHost;
158}
159
160} // namespace JSC