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
38namespace JSC {
39
40STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(IntlPluralRulesConstructor);
41
42static EncodedJSValue JSC_HOST_CALL IntlPluralRulesConstructorFuncSupportedLocalesOf(ExecState*);
43
44}
45
46#include "IntlPluralRulesConstructor.lut.h"
47
48namespace JSC {
49
50const 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
58IntlPluralRulesConstructor* IntlPluralRulesConstructor::create(VM& vm, Structure* structure, IntlPluralRulesPrototype* pluralRulesPrototype)
59{
60 IntlPluralRulesConstructor* constructor = new (NotNull, allocateCell<IntlPluralRulesConstructor>(vm.heap)) IntlPluralRulesConstructor(vm, structure);
61 constructor->finishCreation(vm, pluralRulesPrototype);
62 return constructor;
63}
64
65Structure* IntlPluralRulesConstructor::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
66{
67 return Structure::create(vm, globalObject, prototype, TypeInfo(InternalFunctionType, StructureFlags), info());
68}
69
70static EncodedJSValue JSC_HOST_CALL callIntlPluralRules(ExecState*);
71static EncodedJSValue JSC_HOST_CALL constructIntlPluralRules(ExecState*);
72
73IntlPluralRulesConstructor::IntlPluralRulesConstructor(VM& vm, Structure* structure)
74 : InternalFunction(vm, structure, callIntlPluralRules, constructIntlPluralRules)
75{
76}
77
78void IntlPluralRulesConstructor::finishCreation(VM& vm, IntlPluralRulesPrototype* pluralRulesPrototype)
79{
80 Base::finishCreation(vm, "PluralRules"_s, NameVisibility::Visible, NameAdditionMode::WithoutStructureTransition);
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);
83 pluralRulesPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, this, static_cast<unsigned>(PropertyAttribute::DontEnum));
84}
85
86static EncodedJSValue JSC_HOST_CALL constructIntlPluralRules(ExecState* state)
87{
88 VM& vm = state->vm();
89 auto scope = DECLARE_THROW_SCOPE(vm);
90
91 // 13.2.1 Intl.PluralRules ([ locales [ , options ] ])
92 // https://tc39.github.io/ecma402/#sec-intl.pluralrules
93 Structure* structure = InternalFunction::createSubclassStructure(state, state->newTarget(), jsCast<IntlPluralRulesConstructor*>(state->jsCallee())->pluralRulesStructure(vm));
94 RETURN_IF_EXCEPTION(scope, encodedJSValue());
95 IntlPluralRules* pluralRules = IntlPluralRules::create(vm, structure);
96 ASSERT(pluralRules);
97
98 scope.release();
99 pluralRules->initializePluralRules(*state, state->argument(0), state->argument(1));
100 return JSValue::encode(pluralRules);
101}
102
103static EncodedJSValue JSC_HOST_CALL callIntlPluralRules(ExecState* state)
104{
105 VM& vm = state->vm();
106 auto scope = DECLARE_THROW_SCOPE(vm);
107
108 // 13.2.1 Intl.PluralRules ([ locales [ , options ] ])
109 // https://tc39.github.io/ecma402/#sec-intl.pluralrules
110 return JSValue::encode(throwConstructorCannotBeCalledAsFunctionTypeError(state, scope, "PluralRules"));
111}
112
113EncodedJSValue JSC_HOST_CALL IntlPluralRulesConstructorFuncSupportedLocalesOf(ExecState* state)
114{
115 VM& vm = state->vm();
116 auto scope = DECLARE_THROW_SCOPE(vm);
117
118 // 13.3.2 Intl.PluralRules.supportedLocalesOf (locales [, options ])
119 // https://tc39.github.io/ecma402/#sec-intl.pluralrules.supportedlocalesof
120 JSGlobalObject* globalObject = state->jsCallee()->globalObject(vm);
121 const HashSet<String> availableLocales = globalObject->intlNumberFormatAvailableLocales();
122
123 Vector<String> requestedLocales = canonicalizeLocaleList(*state, state->argument(0));
124 RETURN_IF_EXCEPTION(scope, encodedJSValue());
125
126 RELEASE_AND_RETURN(scope, JSValue::encode(supportedLocales(*state, availableLocales, requestedLocales, state->argument(1))));
127}
128
129} // namespace JSC
130
131#endif // ENABLE(INTL)
132