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, output&: buf); |
36 | for (unsigned i = 0; i < length; ++i) |
37 | buf[i] = static_cast<UChar>(args.at(idx: i).toUInt32(exec)); |
38 | return jsString(exec, s: 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, c: args.at(idx: 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(propertyName: exec->propertyNames().prototype, value: stringPrototype, attributes: ReadOnly | DontEnum | DontDelete); |
55 | |
56 | // ECMA 15.5.3.2 fromCharCode() |
57 | putDirectFunctionWithoutTransition(exec, function: new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 1, exec->propertyNames().fromCharCode, stringFromCharCode), attr: DontEnum); |
58 | |
59 | // no. of arguments for constructor |
60 | putDirectWithoutTransition(propertyName: exec->propertyNames().length, value: jsNumber(exec, i: 1), attributes: ReadOnly | DontEnum | DontDelete); |
61 | } |
62 | |
63 | // ECMA 15.5.2 |
64 | static JSObject* constructWithStringConstructor(ExecState* exec, JSObject*, const ArgList& args) |
65 | { |
66 | if (args.isEmpty()) |
67 | return new (exec) StringObject(exec, exec->lexicalGlobalObject()->stringObjectStructure()); |
68 | return new (exec) StringObject(exec, exec->lexicalGlobalObject()->stringObjectStructure(), args.at(idx: 0).toString(exec)); |
69 | } |
70 | |
71 | ConstructType StringConstructor::getConstructData(ConstructData& constructData) |
72 | { |
73 | constructData.native.function = constructWithStringConstructor; |
74 | return ConstructTypeHost; |
75 | } |
76 | |
77 | // ECMA 15.5.1 |
78 | static JSValue JSC_HOST_CALL callStringConstructor(ExecState* exec, JSObject*, JSValue, const ArgList& args) |
79 | { |
80 | if (args.isEmpty()) |
81 | return jsEmptyString(exec); |
82 | return jsString(exec, s: args.at(idx: 0).toString(exec)); |
83 | } |
84 | |
85 | CallType StringConstructor::getCallData(CallData& callData) |
86 | { |
87 | callData.native.function = callStringConstructor; |
88 | return CallTypeHost; |
89 | } |
90 | |
91 | } // namespace JSC |
92 | |