We should support CreateThis in the FTL
[WebKit-https.git] / Source / JavaScriptCore / runtime / StringConstructor.cpp
1 /*
2  *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
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, stringPrototype->classInfo()->className);
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     UChar* buf;
87     auto impl = StringImpl::createUninitialized(length, buf);
88     for (unsigned i = 0; i < length; ++i) {
89         buf[i] = static_cast<UChar>(exec->uncheckedArgument(i).toUInt32(exec));
90         RETURN_IF_EXCEPTION(scope, encodedJSValue());
91     }
92     scope.release();
93     return JSValue::encode(jsString(exec, WTFMove(impl)));
94 }
95
96 JSCell* JSC_HOST_CALL stringFromCharCode(ExecState* exec, int32_t arg)
97 {
98     return jsSingleCharacterString(exec, arg);
99 }
100
101 static EncodedJSValue JSC_HOST_CALL stringFromCodePoint(ExecState* exec)
102 {
103     VM& vm = exec->vm();
104     auto scope = DECLARE_THROW_SCOPE(vm);
105
106     unsigned length = exec->argumentCount();
107     StringBuilder builder;
108     builder.reserveCapacity(length);
109
110     for (unsigned i = 0; i < length; ++i) {
111         double codePointAsDouble = exec->uncheckedArgument(i).toNumber(exec);
112         RETURN_IF_EXCEPTION(scope, encodedJSValue());
113
114         uint32_t codePoint = static_cast<uint32_t>(codePointAsDouble);
115
116         if (codePoint != codePointAsDouble || codePoint > UCHAR_MAX_VALUE)
117             return throwVMError(exec, scope, createRangeError(exec, "Arguments contain a value that is out of range of code points"_s));
118
119         if (U_IS_BMP(codePoint))
120             builder.append(static_cast<UChar>(codePoint));
121         else {
122             builder.append(U16_LEAD(codePoint));
123             builder.append(U16_TRAIL(codePoint));
124         }
125     }
126
127     scope.release();
128     return JSValue::encode(jsString(exec, builder.toString()));
129 }
130
131 static EncodedJSValue JSC_HOST_CALL constructWithStringConstructor(ExecState* exec)
132 {
133     VM& vm = exec->vm();
134     JSGlobalObject* globalObject = jsCast<InternalFunction*>(exec->jsCallee())->globalObject(vm);
135     auto scope = DECLARE_THROW_SCOPE(vm);
136
137     Structure* structure = InternalFunction::createSubclassStructure(exec, exec->newTarget(), globalObject->stringObjectStructure());
138     RETURN_IF_EXCEPTION(scope, encodedJSValue());
139
140     if (!exec->argumentCount())
141         return JSValue::encode(StringObject::create(vm, structure));
142     JSString* str = exec->uncheckedArgument(0).toString(exec);
143     RETURN_IF_EXCEPTION(scope, encodedJSValue());
144     return JSValue::encode(StringObject::create(vm, structure, str));
145 }
146
147 JSCell* stringConstructor(ExecState* exec, JSValue argument)
148 {
149     if (argument.isSymbol())
150         return jsNontrivialString(exec, asSymbol(argument)->descriptiveString());
151     return argument.toString(exec);
152 }
153
154 static EncodedJSValue JSC_HOST_CALL callStringConstructor(ExecState* exec)
155 {
156     if (!exec->argumentCount())
157         return JSValue::encode(jsEmptyString(exec));
158     return JSValue::encode(stringConstructor(exec, exec->uncheckedArgument(0)));
159 }
160
161 } // namespace JSC