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
22 #include "StringConstructor.h"
24 #include "JSGlobalObject.h"
25 #include "PrototypeFunction.h"
26 #include "StringPrototype.h"
30 static NEVER_INLINE JSValuePtr
stringFromCharCodeSlowCase(ExecState
* exec
, const ArgList
& args
)
32 UChar
* buf
= static_cast<UChar
*>(fastMalloc(args
.size() * sizeof(UChar
)));
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));
40 static JSValuePtr
stringFromCharCode(ExecState
* exec
, JSObject
*, JSValuePtr
, const ArgList
& args
)
42 if (LIKELY(args
.size() == 1))
43 return jsSingleCharacterString(exec
, args
.at(exec
, 0).toUInt32(exec
));
44 return stringFromCharCodeSlowCase(exec
, args
);
47 ASSERT_CLASS_FITS_IN_CELL(StringConstructor
);
49 StringConstructor::StringConstructor(ExecState
* exec
, PassRefPtr
<Structure
> structure
, Structure
* prototypeFunctionStructure
, StringPrototype
* stringPrototype
)
50 : InternalFunction(&exec
->globalData(), structure
, Identifier(exec
, stringPrototype
->classInfo()->className
))
52 // ECMA 15.5.3.1 String.prototype
53 putDirectWithoutTransition(exec
->propertyNames().prototype
, stringPrototype
, ReadOnly
| DontEnum
| DontDelete
);
55 // ECMA 15.5.3.2 fromCharCode()
56 putDirectFunctionWithoutTransition(exec
, new (exec
) PrototypeFunction(exec
, prototypeFunctionStructure
, 1, exec
->propertyNames().fromCharCode
, stringFromCharCode
), DontEnum
);
58 // no. of arguments for constructor
59 putDirectWithoutTransition(exec
->propertyNames().length
, jsNumber(exec
, 1), ReadOnly
| DontEnum
| DontDelete
);
63 static JSObject
* constructWithStringConstructor(ExecState
* exec
, JSObject
*, const ArgList
& args
)
66 return new (exec
) StringObject(exec
, exec
->lexicalGlobalObject()->stringObjectStructure());
67 return new (exec
) StringObject(exec
, exec
->lexicalGlobalObject()->stringObjectStructure(), args
.at(exec
, 0).toString(exec
));
70 ConstructType
StringConstructor::getConstructData(ConstructData
& constructData
)
72 constructData
.native
.function
= constructWithStringConstructor
;
73 return ConstructTypeHost
;
77 static JSValuePtr
callStringConstructor(ExecState
* exec
, JSObject
*, JSValuePtr
, const ArgList
& args
)
80 return jsEmptyString(exec
);
81 return jsString(exec
, args
.at(exec
, 0).toString(exec
));
84 CallType
StringConstructor::getCallData(CallData
& callData
)
86 callData
.native
.function
= callStringConstructor
;