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 "Executable.h"
26 #include "JSFunction.h"
27 #include "JSGlobalObject.h"
28 #include "Operations.h"
29 #include "StringPrototype.h"
33 static EncodedJSValue JSC_HOST_CALL
stringFromCharCode(ExecState
*);
37 #include "StringConstructor.lut.h"
41 const ClassInfo
StringConstructor::s_info
= { "Function", &InternalFunction::s_info
, 0, ExecState::stringConstructorTable
, CREATE_METHOD_TABLE(StringConstructor
) };
43 /* Source for StringConstructor.lut.h
44 @begin stringConstructorTable
45 fromCharCode stringFromCharCode DontEnum|Function 1
49 ASSERT_HAS_TRIVIAL_DESTRUCTOR(StringConstructor
);
51 StringConstructor::StringConstructor(JSGlobalObject
* globalObject
, Structure
* structure
)
52 : InternalFunction(globalObject
, structure
)
56 void StringConstructor::finishCreation(ExecState
* exec
, StringPrototype
* stringPrototype
)
58 Base::finishCreation(exec
->vm(), stringPrototype
->classInfo()->className
);
59 putDirectWithoutTransition(exec
->vm(), exec
->propertyNames().prototype
, stringPrototype
, ReadOnly
| DontEnum
| DontDelete
);
60 putDirectWithoutTransition(exec
->vm(), exec
->propertyNames().length
, jsNumber(1), ReadOnly
| DontEnum
| DontDelete
);
63 bool StringConstructor::getOwnPropertySlot(JSCell
* cell
, ExecState
* exec
, PropertyName propertyName
, PropertySlot
&slot
)
65 return getStaticFunctionSlot
<InternalFunction
>(exec
, ExecState::stringConstructorTable(exec
), jsCast
<StringConstructor
*>(cell
), propertyName
, slot
);
68 bool StringConstructor::getOwnPropertyDescriptor(JSObject
* object
, ExecState
* exec
, PropertyName propertyName
, PropertyDescriptor
& descriptor
)
70 return getStaticFunctionDescriptor
<InternalFunction
>(exec
, ExecState::stringConstructorTable(exec
), jsCast
<StringConstructor
*>(object
), propertyName
, descriptor
);
73 // ------------------------------ Functions --------------------------------
75 static NEVER_INLINE JSValue
stringFromCharCodeSlowCase(ExecState
* exec
)
77 unsigned length
= exec
->argumentCount();
79 PassRefPtr
<StringImpl
> impl
= StringImpl::createUninitialized(length
, buf
);
80 for (unsigned i
= 0; i
< length
; ++i
)
81 buf
[i
] = static_cast<UChar
>(exec
->argument(i
).toUInt32(exec
));
82 return jsString(exec
, impl
);
85 static EncodedJSValue JSC_HOST_CALL
stringFromCharCode(ExecState
* exec
)
87 if (LIKELY(exec
->argumentCount() == 1))
88 return JSValue::encode(jsSingleCharacterString(exec
, exec
->argument(0).toUInt32(exec
)));
89 return JSValue::encode(stringFromCharCodeSlowCase(exec
));
92 JSCell
* JSC_HOST_CALL
stringFromCharCode(ExecState
* exec
, int32_t arg
)
94 return jsSingleCharacterString(exec
, arg
);
97 static EncodedJSValue JSC_HOST_CALL
constructWithStringConstructor(ExecState
* exec
)
99 JSGlobalObject
* globalObject
= asInternalFunction(exec
->callee())->globalObject();
100 if (!exec
->argumentCount())
101 return JSValue::encode(StringObject::create(exec
, globalObject
->stringObjectStructure()));
103 return JSValue::encode(StringObject::create(exec
, globalObject
->stringObjectStructure(), exec
->argument(0).toString(exec
)));
106 ConstructType
StringConstructor::getConstructData(JSCell
*, ConstructData
& constructData
)
108 constructData
.native
.function
= constructWithStringConstructor
;
109 return ConstructTypeHost
;
112 static EncodedJSValue JSC_HOST_CALL
callStringConstructor(ExecState
* exec
)
114 if (!exec
->argumentCount())
115 return JSValue::encode(jsEmptyString(exec
));
116 return JSValue::encode(exec
->argument(0).toString(exec
));
119 CallType
StringConstructor::getCallData(JSCell
*, CallData
& callData
)
121 callData
.native
.function
= callStringConstructor
;