]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/StringConstructor.cpp
JavaScriptCore-721.26.tar.gz
[apple/javascriptcore.git] / runtime / StringConstructor.cpp
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
24 #include "JSFunction.h"
25 #include "JSGlobalObject.h"
26 #include "PrototypeFunction.h"
27 #include "StringPrototype.h"
28
29 namespace JSC {
30
31 static NEVER_INLINE JSValue stringFromCharCodeSlowCase(ExecState* exec, const ArgList& args)
32 {
33 unsigned length = args.size();
34 UChar* buf;
35 PassRefPtr<UStringImpl> impl = UStringImpl::createUninitialized(length, buf);
36 for (unsigned i = 0; i < length; ++i)
37 buf[i] = static_cast<UChar>(args.at(i).toUInt32(exec));
38 return jsString(exec, impl);
39 }
40
41 static JSValue JSC_HOST_CALL stringFromCharCode(ExecState* exec, JSObject*, JSValue, const ArgList& args)
42 {
43 if (LIKELY(args.size() == 1))
44 return jsSingleCharacterString(exec, args.at(0).toUInt32(exec));
45 return stringFromCharCodeSlowCase(exec, args);
46 }
47
48 ASSERT_CLASS_FITS_IN_CELL(StringConstructor);
49
50 StringConstructor::StringConstructor(ExecState* exec, NonNullPassRefPtr<Structure> structure, Structure* prototypeFunctionStructure, StringPrototype* stringPrototype)
51 : InternalFunction(&exec->globalData(), structure, Identifier(exec, stringPrototype->classInfo()->className))
52 {
53 // ECMA 15.5.3.1 String.prototype
54 putDirectWithoutTransition(exec->propertyNames().prototype, stringPrototype, ReadOnly | DontEnum | DontDelete);
55
56 // ECMA 15.5.3.2 fromCharCode()
57 #if ENABLE(JIT) && ENABLE(JIT_OPTIMIZE_NATIVE_CALL)
58 putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 1, exec->propertyNames().fromCharCode, exec->globalData().getThunk(fromCharCodeThunkGenerator), stringFromCharCode), DontEnum);
59 #else
60 putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 1, exec->propertyNames().fromCharCode, stringFromCharCode), DontEnum);
61 #endif
62 // no. of arguments for constructor
63 putDirectWithoutTransition(exec->propertyNames().length, jsNumber(exec, 1), ReadOnly | DontEnum | DontDelete);
64 }
65
66 // ECMA 15.5.2
67 static JSObject* constructWithStringConstructor(ExecState* exec, JSObject*, const ArgList& args)
68 {
69 if (args.isEmpty())
70 return new (exec) StringObject(exec, exec->lexicalGlobalObject()->stringObjectStructure());
71 return new (exec) StringObject(exec, exec->lexicalGlobalObject()->stringObjectStructure(), args.at(0).toString(exec));
72 }
73
74 ConstructType StringConstructor::getConstructData(ConstructData& constructData)
75 {
76 constructData.native.function = constructWithStringConstructor;
77 return ConstructTypeHost;
78 }
79
80 // ECMA 15.5.1
81 static JSValue JSC_HOST_CALL callStringConstructor(ExecState* exec, JSObject*, JSValue, const ArgList& args)
82 {
83 if (args.isEmpty())
84 return jsEmptyString(exec);
85 return jsString(exec, args.at(0).toString(exec));
86 }
87
88 CallType StringConstructor::getCallData(CallData& callData)
89 {
90 callData.native.function = callStringConstructor;
91 return CallTypeHost;
92 }
93
94 } // namespace JSC