1 | /* |
2 | * Copyright (C) 1999-2001 Harri Porten ([email protected]) |
3 | * Copyright (C) 2004-2008, 2016 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 "Error.h" |
25 | #include "JITCode.h" |
26 | #include "JSFunction.h" |
27 | #include "JSGlobalObject.h" |
28 | #include "JSCInlines.h" |
29 | #include "StringPrototype.h" |
30 | #include <wtf/text/StringBuilder.h> |
31 | |
32 | namespace JSC { |
33 | |
34 | static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState*); |
35 | static EncodedJSValue JSC_HOST_CALL stringFromCodePoint(ExecState*); |
36 | |
37 | } |
38 | |
39 | #include "StringConstructor.lut.h" |
40 | |
41 | namespace JSC { |
42 | |
43 | const ClassInfo StringConstructor::s_info = { "Function" , &InternalFunction::s_info, &stringConstructorTable, nullptr, CREATE_METHOD_TABLE(StringConstructor) }; |
44 | |
45 | /* Source for StringConstructor.lut.h |
46 | @begin stringConstructorTable |
47 | fromCharCode stringFromCharCode DontEnum|Function 1 FromCharCodeIntrinsic |
48 | fromCodePoint stringFromCodePoint DontEnum|Function 1 |
49 | raw JSBuiltin DontEnum|Function 1 |
50 | @end |
51 | */ |
52 | |
53 | STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(StringConstructor); |
54 | |
55 | |
56 | static EncodedJSValue JSC_HOST_CALL callStringConstructor(ExecState*); |
57 | static EncodedJSValue JSC_HOST_CALL constructWithStringConstructor(ExecState*); |
58 | |
59 | StringConstructor::StringConstructor(VM& vm, Structure* structure) |
60 | : InternalFunction(vm, structure, callStringConstructor, constructWithStringConstructor) |
61 | { |
62 | } |
63 | |
64 | void StringConstructor::finishCreation(VM& vm, StringPrototype* stringPrototype) |
65 | { |
66 | Base::finishCreation(vm, vm.propertyNames->String.string(), NameVisibility::Visible, NameAdditionMode::WithoutStructureTransition); |
67 | putDirectWithoutTransition(vm, vm.propertyNames->prototype, stringPrototype, PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum | PropertyAttribute::DontDelete); |
68 | putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum); |
69 | } |
70 | |
71 | // ------------------------------ Functions -------------------------------- |
72 | |
73 | static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState* exec) |
74 | { |
75 | VM& vm = exec->vm(); |
76 | auto scope = DECLARE_THROW_SCOPE(vm); |
77 | |
78 | unsigned length = exec->argumentCount(); |
79 | if (LIKELY(length == 1)) { |
80 | scope.release(); |
81 | unsigned code = exec->uncheckedArgument(0).toUInt32(exec); |
82 | // Not checking for an exception here is ok because jsSingleCharacterString will just fetch an unused string if there's an exception. |
83 | return JSValue::encode(jsSingleCharacterString(exec, code)); |
84 | } |
85 | |
86 | LChar* buf8Bit; |
87 | auto impl8Bit = StringImpl::createUninitialized(length, buf8Bit); |
88 | for (unsigned i = 0; i < length; ++i) { |
89 | UChar character = static_cast<UChar>(exec->uncheckedArgument(i).toUInt32(exec)); |
90 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
91 | if (UNLIKELY(!isLatin1(character))) { |
92 | UChar* buf16Bit; |
93 | auto impl16Bit = StringImpl::createUninitialized(length, buf16Bit); |
94 | StringImpl::copyCharacters(buf16Bit, buf8Bit, i); |
95 | buf16Bit[i] = character; |
96 | ++i; |
97 | for (; i < length; ++i) { |
98 | buf16Bit[i] = static_cast<UChar>(exec->uncheckedArgument(i).toUInt32(exec)); |
99 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
100 | } |
101 | RELEASE_AND_RETURN(scope, JSValue::encode(jsString(exec, WTFMove(impl16Bit)))); |
102 | } |
103 | buf8Bit[i] = static_cast<LChar>(character); |
104 | } |
105 | RELEASE_AND_RETURN(scope, JSValue::encode(jsString(exec, WTFMove(impl8Bit)))); |
106 | } |
107 | |
108 | JSString* JSC_HOST_CALL stringFromCharCode(ExecState* exec, int32_t arg) |
109 | { |
110 | return jsSingleCharacterString(exec, arg); |
111 | } |
112 | |
113 | static EncodedJSValue JSC_HOST_CALL stringFromCodePoint(ExecState* exec) |
114 | { |
115 | VM& vm = exec->vm(); |
116 | auto scope = DECLARE_THROW_SCOPE(vm); |
117 | |
118 | unsigned length = exec->argumentCount(); |
119 | StringBuilder builder; |
120 | builder.reserveCapacity(length); |
121 | |
122 | for (unsigned i = 0; i < length; ++i) { |
123 | double codePointAsDouble = exec->uncheckedArgument(i).toNumber(exec); |
124 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
125 | |
126 | uint32_t codePoint = static_cast<uint32_t>(codePointAsDouble); |
127 | |
128 | if (codePoint != codePointAsDouble || codePoint > UCHAR_MAX_VALUE) |
129 | return throwVMError(exec, scope, createRangeError(exec, "Arguments contain a value that is out of range of code points"_s )); |
130 | |
131 | if (U_IS_BMP(codePoint)) |
132 | builder.append(static_cast<UChar>(codePoint)); |
133 | else { |
134 | builder.append(U16_LEAD(codePoint)); |
135 | builder.append(U16_TRAIL(codePoint)); |
136 | } |
137 | } |
138 | |
139 | RELEASE_AND_RETURN(scope, JSValue::encode(jsString(exec, builder.toString()))); |
140 | } |
141 | |
142 | static EncodedJSValue JSC_HOST_CALL constructWithStringConstructor(ExecState* exec) |
143 | { |
144 | VM& vm = exec->vm(); |
145 | JSGlobalObject* globalObject = jsCast<InternalFunction*>(exec->jsCallee())->globalObject(vm); |
146 | auto scope = DECLARE_THROW_SCOPE(vm); |
147 | |
148 | Structure* structure = InternalFunction::createSubclassStructure(exec, exec->newTarget(), globalObject->stringObjectStructure()); |
149 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
150 | |
151 | if (!exec->argumentCount()) |
152 | return JSValue::encode(StringObject::create(vm, structure)); |
153 | JSString* str = exec->uncheckedArgument(0).toString(exec); |
154 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
155 | return JSValue::encode(StringObject::create(vm, structure, str)); |
156 | } |
157 | |
158 | JSString* stringConstructor(ExecState* exec, JSValue argument) |
159 | { |
160 | if (argument.isSymbol()) |
161 | return jsNontrivialString(exec, asSymbol(argument)->descriptiveString()); |
162 | return argument.toString(exec); |
163 | } |
164 | |
165 | static EncodedJSValue JSC_HOST_CALL callStringConstructor(ExecState* exec) |
166 | { |
167 | if (!exec->argumentCount()) |
168 | return JSValue::encode(jsEmptyString(exec)); |
169 | return JSValue::encode(stringConstructor(exec, exec->uncheckedArgument(0))); |
170 | } |
171 | |
172 | } // namespace JSC |
173 | |