We should support CreateThis in the FTL
[WebKit-https.git] / Source / JavaScriptCore / runtime / IntlPluralRulesConstructor.cpp
1 /*
2  * Copyright (C) 2018 Andy VanWagoner (andy@vanwagoner.family)
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "IntlPluralRulesConstructor.h"
28
29 #if ENABLE(INTL)
30
31 #include "Error.h"
32 #include "IntlObject.h"
33 #include "IntlPluralRules.h"
34 #include "IntlPluralRulesPrototype.h"
35 #include "JSCInlines.h"
36 #include "Lookup.h"
37
38 namespace JSC {
39
40 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(IntlPluralRulesConstructor);
41
42 static EncodedJSValue JSC_HOST_CALL IntlPluralRulesConstructorFuncSupportedLocalesOf(ExecState*);
43
44 }
45
46 #include "IntlPluralRulesConstructor.lut.h"
47
48 namespace JSC {
49
50 const ClassInfo IntlPluralRulesConstructor::s_info = { "Function", &InternalFunction::s_info, &pluralRulesConstructorTable, nullptr, CREATE_METHOD_TABLE(IntlPluralRulesConstructor) };
51
52 /* Source for IntlPluralRulesConstructor.lut.h
53 @begin pluralRulesConstructorTable
54   supportedLocalesOf             IntlPluralRulesConstructorFuncSupportedLocalesOf             DontEnum|Function 1
55 @end
56 */
57
58 IntlPluralRulesConstructor* IntlPluralRulesConstructor::create(VM& vm, Structure* structure, IntlPluralRulesPrototype* pluralRulesPrototype, Structure* pluralRulesStructure)
59 {
60     IntlPluralRulesConstructor* constructor = new (NotNull, allocateCell<IntlPluralRulesConstructor>(vm.heap)) IntlPluralRulesConstructor(vm, structure);
61     constructor->finishCreation(vm, pluralRulesPrototype, pluralRulesStructure);
62     return constructor;
63 }
64
65 Structure* IntlPluralRulesConstructor::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
66 {
67     return Structure::create(vm, globalObject, prototype, TypeInfo(InternalFunctionType, StructureFlags), info());
68 }
69
70 static EncodedJSValue JSC_HOST_CALL callIntlPluralRules(ExecState*);
71 static EncodedJSValue JSC_HOST_CALL constructIntlPluralRules(ExecState*);
72
73 IntlPluralRulesConstructor::IntlPluralRulesConstructor(VM& vm, Structure* structure)
74     : InternalFunction(vm, structure, callIntlPluralRules, constructIntlPluralRules)
75 {
76 }
77
78 void IntlPluralRulesConstructor::finishCreation(VM& vm, IntlPluralRulesPrototype* pluralRulesPrototype, Structure* pluralRulesStructure)
79 {
80     Base::finishCreation(vm, "PluralRules"_s);
81     putDirectWithoutTransition(vm, vm.propertyNames->prototype, pluralRulesPrototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);
82     putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(0), PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum | PropertyAttribute::DontDelete);
83     pluralRulesPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, this, static_cast<unsigned>(PropertyAttribute::DontEnum));
84     m_pluralRulesStructure.set(vm, this, pluralRulesStructure);
85 }
86
87 static EncodedJSValue JSC_HOST_CALL constructIntlPluralRules(ExecState* state)
88 {
89     VM& vm = state->vm();
90     auto scope = DECLARE_THROW_SCOPE(vm);
91
92     // 13.2.1 Intl.PluralRules ([ locales [ , options ] ])
93     // https://tc39.github.io/ecma402/#sec-intl.pluralrules
94     Structure* structure = InternalFunction::createSubclassStructure(state, state->newTarget(), jsCast<IntlPluralRulesConstructor*>(state->jsCallee())->pluralRulesStructure());
95     RETURN_IF_EXCEPTION(scope, encodedJSValue());
96     IntlPluralRules* pluralRules = IntlPluralRules::create(vm, structure);
97     ASSERT(pluralRules);
98
99     scope.release();
100     pluralRules->initializePluralRules(*state, state->argument(0), state->argument(1));
101     return JSValue::encode(pluralRules);
102 }
103
104 static EncodedJSValue JSC_HOST_CALL callIntlPluralRules(ExecState* state)
105 {
106     VM& vm = state->vm();
107     auto scope = DECLARE_THROW_SCOPE(vm);
108
109     // 13.2.1 Intl.PluralRules ([ locales [ , options ] ])
110     // https://tc39.github.io/ecma402/#sec-intl.pluralrules
111     return JSValue::encode(throwConstructorCannotBeCalledAsFunctionTypeError(state, scope, "PluralRules"));
112 }
113
114 EncodedJSValue JSC_HOST_CALL IntlPluralRulesConstructorFuncSupportedLocalesOf(ExecState* state)
115 {
116     VM& vm = state->vm();
117     auto scope = DECLARE_THROW_SCOPE(vm);
118
119     // 13.3.2 Intl.PluralRules.supportedLocalesOf (locales [, options ])
120     // https://tc39.github.io/ecma402/#sec-intl.pluralrules.supportedlocalesof
121     JSGlobalObject* globalObject = state->jsCallee()->globalObject(vm);
122     const HashSet<String> availableLocales = globalObject->intlNumberFormatAvailableLocales();
123
124     Vector<String> requestedLocales = canonicalizeLocaleList(*state, state->argument(0));
125     RETURN_IF_EXCEPTION(scope, encodedJSValue());
126
127     scope.release();
128     return JSValue::encode(supportedLocales(*state, availableLocales, requestedLocales, state->argument(1)));
129 }
130
131 void IntlPluralRulesConstructor::visitChildren(JSCell* cell, SlotVisitor& visitor)
132 {
133     IntlPluralRulesConstructor* thisObject = jsCast<IntlPluralRulesConstructor*>(cell);
134     ASSERT_GC_OBJECT_INHERITS(thisObject, info());
135
136     Base::visitChildren(thisObject, visitor);
137
138     visitor.append(thisObject->m_pluralRulesStructure);
139 }
140
141 } // namespace JSC
142
143 #endif // ENABLE(INTL)