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