Fix exception scope verification failures in IteratorOperations.h.
[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, 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->callee())->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     collator->initializeCollator(*state, state->argument(0), state->argument(1));
100     return JSValue::encode(collator);
101 }
102
103 static EncodedJSValue JSC_HOST_CALL callIntlCollator(ExecState* state)
104 {
105     // 10.1.2 Intl.Collator ([locales [, options]]) (ECMA-402 2.0)
106     // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
107     // NewTarget is always undefined when called as a function.
108
109     VM& vm = state->vm();
110     IntlCollatorConstructor* callee = jsCast<IntlCollatorConstructor*>(state->callee());
111
112     // FIXME: Collator does not get the workaround for ECMA-402 1.0 compatibility.
113     // https://bugs.webkit.org/show_bug.cgi?id=153679
114
115     // 2. Let collator be OrdinaryCreateFromConstructor(newTarget, %CollatorPrototype%).
116     // 3. ReturnIfAbrupt(collator).
117     IntlCollator* collator = IntlCollator::create(vm, callee->collatorStructure());
118     ASSERT(collator);
119
120     // 4. Return InitializeCollator(collator, locales, options).
121     collator->initializeCollator(*state, state->argument(0), state->argument(1));
122     return JSValue::encode(collator);
123 }
124
125 ConstructType IntlCollatorConstructor::getConstructData(JSCell*, ConstructData& constructData)
126 {
127     constructData.native.function = constructIntlCollator;
128     return ConstructType::Host;
129 }
130
131 CallType IntlCollatorConstructor::getCallData(JSCell*, CallData& callData)
132 {
133     callData.native.function = callIntlCollator;
134     return CallType::Host;
135 }
136
137 EncodedJSValue JSC_HOST_CALL IntlCollatorConstructorFuncSupportedLocalesOf(ExecState* state)
138 {
139     VM& vm = state->vm();
140     auto scope = DECLARE_THROW_SCOPE(vm);
141     // 10.2.2 Intl.Collator.supportedLocalesOf(locales [, options]) (ECMA-402 2.0)
142
143     // 1. Let requestedLocales be CanonicalizeLocaleList(locales).
144     Vector<String> requestedLocales = canonicalizeLocaleList(*state, state->argument(0));
145
146     // 2. ReturnIfAbrupt(requestedLocales).
147     RETURN_IF_EXCEPTION(scope, encodedJSValue());
148
149     // 3. Return SupportedLocales(%Collator%.[[availableLocales]], requestedLocales, options).
150     JSGlobalObject* globalObject = state->callee()->globalObject();
151     return JSValue::encode(supportedLocales(*state, globalObject->intlCollatorAvailableLocales(), requestedLocales, state->argument(1)));
152 }
153
154 void IntlCollatorConstructor::visitChildren(JSCell* cell, SlotVisitor& visitor)
155 {
156     IntlCollatorConstructor* thisObject = jsCast<IntlCollatorConstructor*>(cell);
157     ASSERT_GC_OBJECT_INHERITS(thisObject, info());
158
159     Base::visitChildren(thisObject, visitor);
160
161     visitor.append(&thisObject->m_collatorStructure);
162 }
163
164 } // namespace JSC
165
166 #endif // ENABLE(INTL)