Get rid of HeapRootVisitor and make SlotVisitor less painful to use
[WebKit-https.git] / Source / JavaScriptCore / runtime / IntlDateTimeFormatConstructor.cpp
1 /*
2  * Copyright (C) 2015 Andy VanWagoner (thetalecrafter@gmail.com)
3  * Copyright (C) 2016 Apple Inc. All Rights Reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28 #include "IntlDateTimeFormatConstructor.h"
29
30 #if ENABLE(INTL)
31
32 #include "Error.h"
33 #include "IntlDateTimeFormat.h"
34 #include "IntlDateTimeFormatPrototype.h"
35 #include "IntlObject.h"
36 #include "IntlObjectInlines.h"
37 #include "JSCInlines.h"
38 #include "Lookup.h"
39
40 namespace JSC {
41
42 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(IntlDateTimeFormatConstructor);
43
44 static EncodedJSValue JSC_HOST_CALL IntlDateTimeFormatConstructorFuncSupportedLocalesOf(ExecState*);
45
46 }
47
48 #include "IntlDateTimeFormatConstructor.lut.h"
49
50 namespace JSC {
51
52 const ClassInfo IntlDateTimeFormatConstructor::s_info = { "Function", &InternalFunction::s_info, &dateTimeFormatConstructorTable, CREATE_METHOD_TABLE(IntlDateTimeFormatConstructor) };
53
54 /* Source for IntlDateTimeFormatConstructor.lut.h
55 @begin dateTimeFormatConstructorTable
56   supportedLocalesOf             IntlDateTimeFormatConstructorFuncSupportedLocalesOf             DontEnum|Function 1
57 @end
58 */
59
60 IntlDateTimeFormatConstructor* IntlDateTimeFormatConstructor::create(VM& vm, Structure* structure, IntlDateTimeFormatPrototype* dateTimeFormatPrototype, Structure* dateTimeFormatStructure)
61 {
62     IntlDateTimeFormatConstructor* constructor = new (NotNull, allocateCell<IntlDateTimeFormatConstructor>(vm.heap)) IntlDateTimeFormatConstructor(vm, structure);
63     constructor->finishCreation(vm, dateTimeFormatPrototype, dateTimeFormatStructure);
64     return constructor;
65 }
66
67 Structure* IntlDateTimeFormatConstructor::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
68 {
69     return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
70 }
71
72 IntlDateTimeFormatConstructor::IntlDateTimeFormatConstructor(VM& vm, Structure* structure)
73     : InternalFunction(vm, structure)
74 {
75 }
76
77 void IntlDateTimeFormatConstructor::finishCreation(VM& vm, IntlDateTimeFormatPrototype* dateTimeFormatPrototype, Structure* dateTimeFormatStructure)
78 {
79     Base::finishCreation(vm, ASCIILiteral("DateTimeFormat"));
80     putDirectWithoutTransition(vm, vm.propertyNames->prototype, dateTimeFormatPrototype, DontEnum | DontDelete | ReadOnly);
81     putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(0), ReadOnly | DontEnum | DontDelete);
82     m_dateTimeFormatStructure.set(vm, this, dateTimeFormatStructure);
83 }
84
85 static EncodedJSValue JSC_HOST_CALL constructIntlDateTimeFormat(ExecState* state)
86 {
87     VM& vm = state->vm();
88     auto scope = DECLARE_THROW_SCOPE(vm);
89     // 12.1.2 Intl.DateTimeFormat ([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 dateTimeFormat be OrdinaryCreateFromConstructor(newTarget, %DateTimeFormatPrototype%).
92     // 3. ReturnIfAbrupt(dateTimeFormat).
93     Structure* structure = InternalFunction::createSubclassStructure(state, state->newTarget(), jsCast<IntlDateTimeFormatConstructor*>(state->jsCallee())->dateTimeFormatStructure());
94     RETURN_IF_EXCEPTION(scope, encodedJSValue());
95     IntlDateTimeFormat* dateTimeFormat = IntlDateTimeFormat::create(vm, structure);
96     ASSERT(dateTimeFormat);
97
98     // 4. Return InitializeDateTimeFormat(dateTimeFormat, locales, options).
99     scope.release();
100     dateTimeFormat->initializeDateTimeFormat(*state, state->argument(0), state->argument(1));
101     return JSValue::encode(dateTimeFormat);
102 }
103
104 static EncodedJSValue JSC_HOST_CALL callIntlDateTimeFormat(ExecState* state)
105 {
106     // 12.1.2 Intl.DateTimeFormat ([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     IntlDateTimeFormatConstructor* callee = jsCast<IntlDateTimeFormatConstructor*>(state->jsCallee());
111
112     // FIXME: Workaround to provide compatibility with ECMA-402 1.0 call/apply patterns.
113     // https://bugs.webkit.org/show_bug.cgi?id=153679
114     return JSValue::encode(constructIntlInstanceWithWorkaroundForLegacyIntlConstructor<IntlDateTimeFormat>(*state, state->thisValue(), callee, [&] (VM& vm) {
115         // 2. Let dateTimeFormat be OrdinaryCreateFromConstructor(newTarget, %DateTimeFormatPrototype%).
116         // 3. ReturnIfAbrupt(dateTimeFormat).
117         IntlDateTimeFormat* dateTimeFormat = IntlDateTimeFormat::create(vm, callee->dateTimeFormatStructure());
118         ASSERT(dateTimeFormat);
119
120         // 4. Return InitializeDateTimeFormat(dateTimeFormat, locales, options).
121         dateTimeFormat->initializeDateTimeFormat(*state, state->argument(0), state->argument(1));
122         return dateTimeFormat;
123     }));
124 }
125
126 ConstructType IntlDateTimeFormatConstructor::getConstructData(JSCell*, ConstructData& constructData)
127 {
128     constructData.native.function = constructIntlDateTimeFormat;
129     return ConstructType::Host;
130 }
131
132 CallType IntlDateTimeFormatConstructor::getCallData(JSCell*, CallData& callData)
133 {
134     callData.native.function = callIntlDateTimeFormat;
135     return CallType::Host;
136 }
137
138 EncodedJSValue JSC_HOST_CALL IntlDateTimeFormatConstructorFuncSupportedLocalesOf(ExecState* state)
139 {
140     VM& vm = state->vm();
141     auto scope = DECLARE_THROW_SCOPE(vm);
142     // 12.2.2 Intl.DateTimeFormat.supportedLocalesOf(locales [, options]) (ECMA-402 2.0)
143
144     // 1. Let availableLocales be %DateTimeFormat%.[[availableLocales]].
145     JSGlobalObject* globalObject = state->jsCallee()->globalObject();
146     const HashSet<String> availableLocales = globalObject->intlDateTimeFormatAvailableLocales();
147
148     // 2. Let requestedLocales be CanonicalizeLocaleList(locales).
149     Vector<String> requestedLocales = canonicalizeLocaleList(*state, state->argument(0));
150     RETURN_IF_EXCEPTION(scope, encodedJSValue());
151
152     // 3. Return SupportedLocales(availableLocales, requestedLocales, options).
153     scope.release();
154     return JSValue::encode(supportedLocales(*state, availableLocales, requestedLocales, state->argument(1)));
155 }
156
157 void IntlDateTimeFormatConstructor::visitChildren(JSCell* cell, SlotVisitor& visitor)
158 {
159     IntlDateTimeFormatConstructor* thisObject = jsCast<IntlDateTimeFormatConstructor*>(cell);
160     ASSERT_GC_OBJECT_INHERITS(thisObject, info());
161
162     Base::visitChildren(thisObject, visitor);
163
164     visitor.append(thisObject->m_dateTimeFormatStructure);
165 }
166
167 } // namespace JSC
168
169 #endif // ENABLE(INTL)