We should support CreateThis in the FTL
[WebKit-https.git] / Source / JavaScriptCore / runtime / IntlNumberFormatConstructor.cpp
1 /*
2  * Copyright (C) 2015 Andy VanWagoner (andy@vanwagoner.family)
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 "IntlNumberFormatConstructor.h"
29
30 #if ENABLE(INTL)
31
32 #include "Error.h"
33 #include "IntlNumberFormat.h"
34 #include "IntlNumberFormatPrototype.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(IntlNumberFormatConstructor);
43
44 static EncodedJSValue JSC_HOST_CALL IntlNumberFormatConstructorFuncSupportedLocalesOf(ExecState*);
45
46 }
47
48 #include "IntlNumberFormatConstructor.lut.h"
49
50 namespace JSC {
51
52 const ClassInfo IntlNumberFormatConstructor::s_info = { "Function", &Base::s_info, &numberFormatConstructorTable, nullptr, CREATE_METHOD_TABLE(IntlNumberFormatConstructor) };
53
54 /* Source for IntlNumberFormatConstructor.lut.h
55 @begin numberFormatConstructorTable
56   supportedLocalesOf             IntlNumberFormatConstructorFuncSupportedLocalesOf             DontEnum|Function 1
57 @end
58 */
59
60 IntlNumberFormatConstructor* IntlNumberFormatConstructor::create(VM& vm, Structure* structure, IntlNumberFormatPrototype* numberFormatPrototype, Structure* numberFormatStructure)
61 {
62     IntlNumberFormatConstructor* constructor = new (NotNull, allocateCell<IntlNumberFormatConstructor>(vm.heap)) IntlNumberFormatConstructor(vm, structure);
63     constructor->finishCreation(vm, numberFormatPrototype, numberFormatStructure);
64     return constructor;
65 }
66
67 Structure* IntlNumberFormatConstructor::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
68 {
69     return Structure::create(vm, globalObject, prototype, TypeInfo(InternalFunctionType, StructureFlags), info());
70 }
71
72 static EncodedJSValue JSC_HOST_CALL callIntlNumberFormat(ExecState*);
73 static EncodedJSValue JSC_HOST_CALL constructIntlNumberFormat(ExecState*);
74
75 IntlNumberFormatConstructor::IntlNumberFormatConstructor(VM& vm, Structure* structure)
76     : InternalFunction(vm, structure, callIntlNumberFormat, constructIntlNumberFormat)
77 {
78 }
79
80 void IntlNumberFormatConstructor::finishCreation(VM& vm, IntlNumberFormatPrototype* numberFormatPrototype, Structure* numberFormatStructure)
81 {
82     Base::finishCreation(vm, "NumberFormat"_s);
83     putDirectWithoutTransition(vm, vm.propertyNames->prototype, numberFormatPrototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);
84     putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(0), PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum | PropertyAttribute::DontDelete);
85     numberFormatPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, this, static_cast<unsigned>(PropertyAttribute::DontEnum));
86     m_numberFormatStructure.set(vm, this, numberFormatStructure);
87 }
88
89 static EncodedJSValue JSC_HOST_CALL constructIntlNumberFormat(ExecState* state)
90 {
91     VM& vm = state->vm();
92     auto scope = DECLARE_THROW_SCOPE(vm);
93     // 11.1.2 Intl.NumberFormat ([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 numberFormat be OrdinaryCreateFromConstructor(newTarget, %NumberFormatPrototype%).
96     // 3. ReturnIfAbrupt(numberFormat).
97     Structure* structure = InternalFunction::createSubclassStructure(state, state->newTarget(), jsCast<IntlNumberFormatConstructor*>(state->jsCallee())->numberFormatStructure());
98     RETURN_IF_EXCEPTION(scope, encodedJSValue());
99     IntlNumberFormat* numberFormat = IntlNumberFormat::create(vm, structure);
100     ASSERT(numberFormat);
101
102     // 4. Return InitializeNumberFormat(numberFormat, locales, options).
103     scope.release();
104     numberFormat->initializeNumberFormat(*state, state->argument(0), state->argument(1));
105     return JSValue::encode(numberFormat);
106 }
107
108 static EncodedJSValue JSC_HOST_CALL callIntlNumberFormat(ExecState* state)
109 {
110     // 11.1.2 Intl.NumberFormat ([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     IntlNumberFormatConstructor* callee = jsCast<IntlNumberFormatConstructor*>(state->jsCallee());
115
116     // FIXME: Workaround to provide compatibility with ECMA-402 1.0 call/apply patterns.
117     // https://bugs.webkit.org/show_bug.cgi?id=153679
118     return JSValue::encode(constructIntlInstanceWithWorkaroundForLegacyIntlConstructor<IntlNumberFormat>(*state, state->thisValue(), callee, [&] (VM& vm) {
119         // 2. Let numberFormat be OrdinaryCreateFromConstructor(newTarget, %NumberFormatPrototype%).
120         // 3. ReturnIfAbrupt(numberFormat).
121         IntlNumberFormat* numberFormat = IntlNumberFormat::create(vm, callee->numberFormatStructure());
122         ASSERT(numberFormat);
123
124         // 4. Return InitializeNumberFormat(numberFormat, locales, options).
125         numberFormat->initializeNumberFormat(*state, state->argument(0), state->argument(1));
126         return numberFormat;
127     }));
128 }
129
130 EncodedJSValue JSC_HOST_CALL IntlNumberFormatConstructorFuncSupportedLocalesOf(ExecState* state)
131 {
132     VM& vm = state->vm();
133     auto scope = DECLARE_THROW_SCOPE(vm);
134     // 11.2.2 Intl.NumberFormat.supportedLocalesOf(locales [, options]) (ECMA-402 2.0)
135
136     // 1. Let availableLocales be %NumberFormat%.[[availableLocales]].
137     JSGlobalObject* globalObject = state->jsCallee()->globalObject(vm);
138     const HashSet<String> availableLocales = globalObject->intlNumberFormatAvailableLocales();
139
140     // 2. Let requestedLocales be CanonicalizeLocaleList(locales).
141     Vector<String> requestedLocales = canonicalizeLocaleList(*state, state->argument(0));
142     RETURN_IF_EXCEPTION(scope, encodedJSValue());
143
144     // 3. Return SupportedLocales(availableLocales, requestedLocales, options).
145     scope.release();
146     return JSValue::encode(supportedLocales(*state, availableLocales, requestedLocales, state->argument(1)));
147 }
148
149 void IntlNumberFormatConstructor::visitChildren(JSCell* cell, SlotVisitor& visitor)
150 {
151     IntlNumberFormatConstructor* thisObject = jsCast<IntlNumberFormatConstructor*>(cell);
152     ASSERT_GC_OBJECT_INHERITS(thisObject, info());
153
154     Base::visitChildren(thisObject, visitor);
155
156     visitor.append(thisObject->m_numberFormatStructure);
157 }
158
159 } // namespace JSC
160
161 #endif // ENABLE(INTL)