1/*
2 * Copyright (C) 2018 Andy VanWagoner ([email protected])
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(JSGlobalObject*, CallFrame*);
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(JSGlobalObject*, CallFrame*);
71static EncodedJSValue JSC_HOST_CALL constructIntlPluralRules(JSGlobalObject*, CallFrame*);
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, 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(JSGlobalObject* globalObject, CallFrame* callFrame)
87{
88 VM& vm = globalObject->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(globalObject, callFrame->jsCallee(), callFrame->newTarget(), jsCast<IntlPluralRulesConstructor*>(callFrame->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(globalObject, callFrame->argument(0), callFrame->argument(1));
100 return JSValue::encode(pluralRules);
101}
102
103static EncodedJSValue JSC_HOST_CALL callIntlPluralRules(JSGlobalObject* globalObject, CallFrame*)
104{
105 VM& vm = globalObject->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(globalObject, scope, "PluralRules"));
111}
112
113EncodedJSValue JSC_HOST_CALL IntlPluralRulesConstructorFuncSupportedLocalesOf(JSGlobalObject* globalObject, CallFrame* callFrame)
114{
115 VM& vm = globalObject->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 const HashSet<String> availableLocales = globalObject->intlNumberFormatAvailableLocales();
121
122 Vector<String> requestedLocales = canonicalizeLocaleList(globalObject, callFrame->argument(0));
123 RETURN_IF_EXCEPTION(scope, encodedJSValue());
124
125 RELEASE_AND_RETURN(scope, JSValue::encode(supportedLocales(globalObject, availableLocales, requestedLocales, callFrame->argument(1))));
126}
127
128} // namespace JSC
129
130#endif // ENABLE(INTL)
131