Remove excessive headers from JavaScriptCore
[WebKit-https.git] / Source / JavaScriptCore / runtime / IntlCollatorConstructor.cpp
1 /*
2  * Copyright (C) 2015 Andy VanWagoner (thetalecrafter@gmail.com)
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 const ClassInfo IntlCollatorConstructor::s_info = { "Function", &InternalFunction::s_info, &collatorConstructorTable, nullptr, CREATE_METHOD_TABLE(IntlCollatorConstructor) };
53
54 /* Source for IntlCollatorConstructor.lut.h
55 @begin collatorConstructorTable
56   supportedLocalesOf             IntlCollatorConstructorFuncSupportedLocalesOf             DontEnum|Function 1
57 @end
58 */
59
60 IntlCollatorConstructor* IntlCollatorConstructor::create(VM& vm, Structure* structure, IntlCollatorPrototype* collatorPrototype, Structure* collatorStructure)
61 {
62     IntlCollatorConstructor* constructor = new (NotNull, allocateCell<IntlCollatorConstructor>(vm.heap)) IntlCollatorConstructor(vm, structure);
63     constructor->finishCreation(vm, collatorPrototype, collatorStructure);
64     return constructor;
65 }
66
67 Structure* IntlCollatorConstructor::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
68 {
69     return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
70 }
71
72 IntlCollatorConstructor::IntlCollatorConstructor(VM& vm, Structure* structure)
73     : InternalFunction(vm, structure)
74 {
75 }
76
77 void IntlCollatorConstructor::finishCreation(VM& vm, IntlCollatorPrototype* collatorPrototype, Structure* collatorStructure)
78 {
79     Base::finishCreation(vm, ASCIILiteral("Collator"));
80     putDirectWithoutTransition(vm, vm.propertyNames->prototype, collatorPrototype, DontEnum | DontDelete | ReadOnly);
81     putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(0), ReadOnly | DontEnum | DontDelete);
82     m_collatorStructure.set(vm, this, collatorStructure);
83 }
84
85 static EncodedJSValue JSC_HOST_CALL constructIntlCollator(ExecState* state)
86 {
87     VM& vm = state->vm();
88     auto scope = DECLARE_THROW_SCOPE(vm);
89     // 10.1.2 Intl.Collator ([locales [, options]]) (ECMA-402 2.0)
90     // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
91     // 2. Let collator be OrdinaryCreateFromConstructor(newTarget, %CollatorPrototype%).
92     // 3. ReturnIfAbrupt(collator).
93     Structure* structure = InternalFunction::createSubclassStructure(state, state->newTarget(), jsCast<IntlCollatorConstructor*>(state->jsCallee())->collatorStructure());
94     RETURN_IF_EXCEPTION(scope, encodedJSValue());
95     IntlCollator* collator = IntlCollator::create(vm, structure);
96     ASSERT(collator);
97
98     // 4. Return InitializeCollator(collator, locales, options).
99     scope.release();
100     collator->initializeCollator(*state, state->argument(0), state->argument(1));
101     return JSValue::encode(collator);
102 }
103
104 static EncodedJSValue JSC_HOST_CALL callIntlCollator(ExecState* state)
105 {
106     // 10.1.2 Intl.Collator ([locales [, options]]) (ECMA-402 2.0)
107     // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
108     // NewTarget is always undefined when called as a function.
109
110     VM& vm = state->vm();
111     IntlCollatorConstructor* callee = jsCast<IntlCollatorConstructor*>(state->jsCallee());
112
113     // FIXME: Collator does not get the workaround for ECMA-402 1.0 compatibility.
114     // https://bugs.webkit.org/show_bug.cgi?id=153679
115
116     // 2. Let collator be OrdinaryCreateFromConstructor(newTarget, %CollatorPrototype%).
117     // 3. ReturnIfAbrupt(collator).
118     IntlCollator* collator = IntlCollator::create(vm, callee->collatorStructure());
119     ASSERT(collator);
120
121     // 4. Return InitializeCollator(collator, locales, options).
122     collator->initializeCollator(*state, state->argument(0), state->argument(1));
123     return JSValue::encode(collator);
124 }
125
126 ConstructType IntlCollatorConstructor::getConstructData(JSCell*, ConstructData& constructData)
127 {
128     constructData.native.function = constructIntlCollator;
129     return ConstructType::Host;
130 }
131
132 CallType IntlCollatorConstructor::getCallData(JSCell*, CallData& callData)
133 {
134     callData.native.function = callIntlCollator;
135     return CallType::Host;
136 }
137
138 EncodedJSValue JSC_HOST_CALL IntlCollatorConstructorFuncSupportedLocalesOf(ExecState* state)
139 {
140     VM& vm = state->vm();
141     auto scope = DECLARE_THROW_SCOPE(vm);
142     // 10.2.2 Intl.Collator.supportedLocalesOf(locales [, options]) (ECMA-402 2.0)
143
144     // 1. Let requestedLocales be CanonicalizeLocaleList(locales).
145     Vector<String> requestedLocales = canonicalizeLocaleList(*state, state->argument(0));
146
147     // 2. ReturnIfAbrupt(requestedLocales).
148     RETURN_IF_EXCEPTION(scope, encodedJSValue());
149
150     // 3. Return SupportedLocales(%Collator%.[[availableLocales]], requestedLocales, options).
151     JSGlobalObject* globalObject = state->jsCallee()->globalObject();
152     scope.release();
153     return JSValue::encode(supportedLocales(*state, globalObject->intlCollatorAvailableLocales(), requestedLocales, state->argument(1)));
154 }
155
156 void IntlCollatorConstructor::visitChildren(JSCell* cell, SlotVisitor& visitor)
157 {
158     IntlCollatorConstructor* thisObject = jsCast<IntlCollatorConstructor*>(cell);
159     ASSERT_GC_OBJECT_INHERITS(thisObject, info());
160
161     Base::visitChildren(thisObject, visitor);
162
163     visitor.append(thisObject->m_collatorStructure);
164 }
165
166 } // namespace JSC
167
168 #endif // ENABLE(INTL)