1/*
2 * Copyright (C) 1999-2001 Harri Porten ([email protected])
3 * Copyright (C) 2004-2019 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
32namespace JSC {
33
34static EncodedJSValue JSC_HOST_CALL stringFromCharCode(JSGlobalObject*, CallFrame*);
35static EncodedJSValue JSC_HOST_CALL stringFromCodePoint(JSGlobalObject*, CallFrame*);
36
37}
38
39#include "StringConstructor.lut.h"
40
41namespace JSC {
42
43const 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
53STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(StringConstructor);
54
55
56static EncodedJSValue JSC_HOST_CALL callStringConstructor(JSGlobalObject*, CallFrame*);
57static EncodedJSValue JSC_HOST_CALL constructWithStringConstructor(JSGlobalObject*, CallFrame*);
58
59StringConstructor::StringConstructor(VM& vm, Structure* structure)
60 : InternalFunction(vm, structure, callStringConstructor, constructWithStringConstructor)
61{
62}
63
64void StringConstructor::finishCreation(VM& vm, StringPrototype* stringPrototype)
65{
66 Base::finishCreation(vm, vm.propertyNames->String.string(), 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
73static EncodedJSValue JSC_HOST_CALL stringFromCharCode(JSGlobalObject* globalObject, CallFrame* callFrame)
74{
75 VM& vm = globalObject->vm();
76 auto scope = DECLARE_THROW_SCOPE(vm);
77
78 unsigned length = callFrame->argumentCount();
79 if (LIKELY(length == 1)) {
80 scope.release();
81 unsigned code = callFrame->uncheckedArgument(0).toUInt32(globalObject);
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(vm, 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>(callFrame->uncheckedArgument(i).toUInt32(globalObject));
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>(callFrame->uncheckedArgument(i).toUInt32(globalObject));
99 RETURN_IF_EXCEPTION(scope, encodedJSValue());
100 }
101 RELEASE_AND_RETURN(scope, JSValue::encode(jsString(vm, WTFMove(impl16Bit))));
102 }
103 buf8Bit[i] = static_cast<LChar>(character);
104 }
105 RELEASE_AND_RETURN(scope, JSValue::encode(jsString(vm, WTFMove(impl8Bit))));
106}
107
108JSString* JSC_HOST_CALL stringFromCharCode(JSGlobalObject* globalObject, int32_t arg)
109{
110 return jsSingleCharacterString(globalObject->vm(), arg);
111}
112
113static EncodedJSValue JSC_HOST_CALL stringFromCodePoint(JSGlobalObject* globalObject, CallFrame* callFrame)
114{
115 VM& vm = globalObject->vm();
116 auto scope = DECLARE_THROW_SCOPE(vm);
117
118 unsigned length = callFrame->argumentCount();
119 StringBuilder builder;
120 builder.reserveCapacity(length);
121
122 for (unsigned i = 0; i < length; ++i) {
123 double codePointAsDouble = callFrame->uncheckedArgument(i).toNumber(globalObject);
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(globalObject, scope, createRangeError(globalObject, "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(vm, builder.toString())));
140}
141
142static EncodedJSValue JSC_HOST_CALL constructWithStringConstructor(JSGlobalObject* globalObject, CallFrame* callFrame)
143{
144 VM& vm = globalObject->vm();
145 auto scope = DECLARE_THROW_SCOPE(vm);
146
147 Structure* structure = InternalFunction::createSubclassStructure(globalObject, callFrame->jsCallee(), callFrame->newTarget(), globalObject->stringObjectStructure());
148 RETURN_IF_EXCEPTION(scope, encodedJSValue());
149
150 if (!callFrame->argumentCount())
151 return JSValue::encode(StringObject::create(vm, structure));
152 JSString* str = callFrame->uncheckedArgument(0).toString(globalObject);
153 RETURN_IF_EXCEPTION(scope, encodedJSValue());
154 return JSValue::encode(StringObject::create(vm, structure, str));
155}
156
157JSString* stringConstructor(JSGlobalObject* globalObject, JSValue argument)
158{
159 VM& vm = globalObject->vm();
160 if (argument.isSymbol())
161 return jsNontrivialString(vm, asSymbol(argument)->descriptiveString());
162 return argument.toString(globalObject);
163}
164
165static EncodedJSValue JSC_HOST_CALL callStringConstructor(JSGlobalObject* globalObject, CallFrame* callFrame)
166{
167 VM& vm = globalObject->vm();
168 if (!callFrame->argumentCount())
169 return JSValue::encode(jsEmptyString(vm));
170 return JSValue::encode(stringConstructor(globalObject, callFrame->uncheckedArgument(0)));
171}
172
173} // namespace JSC
174