Rubber-stamped by Brady Eidson.
[webbrowser.git] / JavaScriptCore / runtime / StringConstructor.cpp
blob2f3adbef641d4392a4c1b7b8c7d4338ace6c30fb
1 /*
2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
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.
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.
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
21 #include "config.h"
22 #include "StringConstructor.h"
24 #include "JSFunction.h"
25 #include "JSGlobalObject.h"
26 #include "PrototypeFunction.h"
27 #include "StringPrototype.h"
29 namespace JSC {
31 static NEVER_INLINE JSValue stringFromCharCodeSlowCase(ExecState* exec, const ArgList& args)
33 UChar* buf = static_cast<UChar*>(fastMalloc(args.size() * sizeof(UChar)));
34 UChar* p = buf;
35 ArgList::const_iterator end = args.end();
36 for (ArgList::const_iterator it = args.begin(); it != end; ++it)
37 *p++ = static_cast<UChar>((*it).toUInt32(exec));
38 return jsString(exec, UString(buf, p - buf, false));
41 static JSValue JSC_HOST_CALL stringFromCharCode(ExecState* exec, JSObject*, JSValue, const ArgList& args)
43 if (LIKELY(args.size() == 1))
44 return jsSingleCharacterString(exec, args.at(0).toUInt32(exec));
45 return stringFromCharCodeSlowCase(exec, args);
48 ASSERT_CLASS_FITS_IN_CELL(StringConstructor);
50 StringConstructor::StringConstructor(ExecState* exec, NonNullPassRefPtr<Structure> structure, Structure* prototypeFunctionStructure, StringPrototype* stringPrototype)
51 : InternalFunction(&exec->globalData(), structure, Identifier(exec, stringPrototype->classInfo()->className))
53 // ECMA 15.5.3.1 String.prototype
54 putDirectWithoutTransition(exec->propertyNames().prototype, stringPrototype, ReadOnly | DontEnum | DontDelete);
56 // ECMA 15.5.3.2 fromCharCode()
57 putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 1, exec->propertyNames().fromCharCode, stringFromCharCode), DontEnum);
59 // no. of arguments for constructor
60 putDirectWithoutTransition(exec->propertyNames().length, jsNumber(exec, 1), ReadOnly | DontEnum | DontDelete);
63 // ECMA 15.5.2
64 static JSObject* constructWithStringConstructor(ExecState* exec, JSObject*, const ArgList& args)
66 if (args.isEmpty())
67 return new (exec) StringObject(exec, exec->lexicalGlobalObject()->stringObjectStructure());
68 return new (exec) StringObject(exec, exec->lexicalGlobalObject()->stringObjectStructure(), args.at(0).toString(exec));
71 ConstructType StringConstructor::getConstructData(ConstructData& constructData)
73 constructData.native.function = constructWithStringConstructor;
74 return ConstructTypeHost;
77 // ECMA 15.5.1
78 static JSValue JSC_HOST_CALL callStringConstructor(ExecState* exec, JSObject*, JSValue, const ArgList& args)
80 if (args.isEmpty())
81 return jsEmptyString(exec);
82 return jsString(exec, args.at(0).toString(exec));
85 CallType StringConstructor::getCallData(CallData& callData)
87 callData.native.function = callStringConstructor;
88 return CallTypeHost;
91 } // namespace JSC