We should support CreateThis in the FTL
[WebKit-https.git] / Source / JavaScriptCore / runtime / IntlCollatorConstructor.cpp
1 /*
2  * Copyright (C) 2015 Andy VanWagoner (andy@vanwagoner.family)
3  * Copyright (C) 2015 Sukolsak Sakshuwong (sukolsak@gmail.com)
4  * Copyright (C) 2016 Apple Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
19  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25  * THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "config.h"
29 #include "IntlCollatorConstructor.h"
30
31 #if ENABLE(INTL)
32
33 #include "Error.h"
34 #include "IntlCollator.h"
35 #include "IntlCollatorPrototype.h"
36 #include "IntlObject.h"
37 #include "JSCInlines.h"
38 #include "Lookup.h"
39
40 namespace JSC {
41
42 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(IntlCollatorConstructor);
43
44 static EncodedJSValue JSC_HOST_CALL IntlCollatorConstructorFuncSupportedLocalesOf(ExecState*);
45
46 }
47
48 #include "IntlCollatorConstructor.lut.h"
49
50 namespace JSC {
51
52 static EncodedJSValue JSC_HOST_CALL callIntlCollator(ExecState*);
53 static EncodedJSValue JSC_HOST_CALL constructIntlCollator(ExecState*);
54
55 const ClassInfo IntlCollatorConstructor::s_info = { "Function", &InternalFunction::s_info, &collatorConstructorTable, nullptr, CREATE_METHOD_TABLE(IntlCollatorConstructor) };
56
57 /* Source for IntlCollatorConstructor.lut.h
58 @begin collatorConstructorTable
59   supportedLocalesOf             IntlCollatorConstructorFuncSupportedLocalesOf             DontEnum|Function 1
60 @end
61 */
62
63 IntlCollatorConstructor* IntlCollatorConstructor::create(VM& vm, Structure* structure, IntlCollatorPrototype* collatorPrototype, Structure* collatorStructure)
64 {
65     IntlCollatorConstructor* constructor = new (NotNull, allocateCell<IntlCollatorConstructor>(vm.heap)) IntlCollatorConstructor(vm, structure);
66     constructor->finishCreation(vm, collatorPrototype, collatorStructure);
67     return constructor;
68 }
69
70 Structure* IntlCollatorConstructor::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
71 {
72     return Structure::create(vm, globalObject, prototype, TypeInfo(InternalFunctionType, StructureFlags), info());
73 }
74
75 IntlCollatorConstructor::IntlCollatorConstructor(VM& vm, Structure* structure)
76     : InternalFunction(vm, structure, callIntlCollator, constructIntlCollator)
77 {
78 }
79
80 void IntlCollatorConstructor::finishCreation(VM& vm, IntlCollatorPrototype* collatorPrototype, Structure* collatorStructure)
81 {
82     Base::finishCreation(vm, "Collator"_s);
83     putDirectWithoutTransition(vm, vm.propertyNames->prototype, collatorPrototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);
84     putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(0), PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum | PropertyAttribute::DontDelete);
85     collatorPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, this, static_cast<unsigned>(PropertyAttribute::DontEnum));
86     m_collatorStructure.set(vm, this, collatorStructure);
87 }
88
89 static EncodedJSValue JSC_HOST_CALL constructIntlCollator(ExecState* state)
90 {
91     VM& vm = state->vm();
92     auto scope = DECLARE_THROW_SCOPE(vm);
93     // 10.1.2 Intl.Collator ([locales [, options]]) (ECMA-402 2.0)
94     // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
95     // 2. Let collator be OrdinaryCreateFromConstructor(newTarget, %CollatorPrototype%).
96     // 3. ReturnIfAbrupt(collator).
97     Structure* structure = InternalFunction::createSubclassStructure(state, state->newTarget(), jsCast<IntlCollatorConstructor*>(state->jsCallee())->collatorStructure());
98     RETURN_IF_EXCEPTION(scope, encodedJSValue());
99     IntlCollator* collator = IntlCollator::create(vm, structure);
100     ASSERT(collator);
101
102     // 4. Return InitializeCollator(collator, locales, options).
103     scope.release();
104     collator->initializeCollator(*state, state->argument(0), state->argument(1));
105     return JSValue::encode(collator);
106 }
107
108 static EncodedJSValue JSC_HOST_CALL callIntlCollator(ExecState* state)
109 {
110     // 10.1.2 Intl.Collator ([locales [, options]]) (ECMA-402 2.0)
111     // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
112     // NewTarget is always undefined when called as a function.
113
114     VM& vm = state->vm();
115     IntlCollatorConstructor* callee = jsCast<IntlCollatorConstructor*>(state->jsCallee());
116
117     // FIXME: Collator does not get the workaround for ECMA-402 1.0 compatibility.
118     // https://bugs.webkit.org/show_bug.cgi?id=153679
119
120     // 2. Let collator be OrdinaryCreateFromConstructor(newTarget, %CollatorPrototype%).
121     // 3. ReturnIfAbrupt(collator).
122     IntlCollator* collator = IntlCollator::create(vm, callee->collatorStructure());
123     ASSERT(collator);
124
125     // 4. Return InitializeCollator(collator, locales, options).
126     collator->initializeCollator(*state, state->argument(0), state->argument(1));
127     return JSValue::encode(collator);
128 }
129
130 EncodedJSValue JSC_HOST_CALL IntlCollatorConstructorFuncSupportedLocalesOf(ExecState* state)
131 {
132     VM& vm = state->vm();
133     auto scope = DECLARE_THROW_SCOPE(vm);
134     // 10.2.2 Intl.Collator.supportedLocalesOf(locales [, options]) (ECMA-402 2.0)
135
136     // 1. Let requestedLocales be CanonicalizeLocaleList(locales).
137     Vector<String> requestedLocales = canonicalizeLocaleList(*state, state->argument(0));
138
139     // 2. ReturnIfAbrupt(requestedLocales).
140     RETURN_IF_EXCEPTION(scope, encodedJSValue());
141
142     // 3. Return SupportedLocales(%Collator%.[[availableLocales]], requestedLocales, options).
143     JSGlobalObject* globalObject = state->jsCallee()->globalObject(vm);
144     scope.release();
145     return JSValue::encode(supportedLocales(*state, globalObject->intlCollatorAvailableLocales(), requestedLocales, state->argument(1)));
146 }
147
148 void IntlCollatorConstructor::visitChildren(JSCell* cell, SlotVisitor& visitor)
149 {
150     IntlCollatorConstructor* thisObject = jsCast<IntlCollatorConstructor*>(cell);
151     ASSERT_GC_OBJECT_INHERITS(thisObject, info());
152
153     Base::visitChildren(thisObject, visitor);
154
155     visitor.append(thisObject->m_collatorStructure);
156 }
157
158 } // namespace JSC
159
160 #endif // ENABLE(INTL)