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 "IntlPluralRulesPrototype.h"
28
29#if ENABLE(INTL)
30
31#include "Error.h"
32#include "IntlPluralRules.h"
33#include "JSCInlines.h"
34#include "JSObjectInlines.h"
35
36namespace JSC {
37
38static EncodedJSValue JSC_HOST_CALL IntlPluralRulesPrototypeFuncSelect(ExecState*);
39static EncodedJSValue JSC_HOST_CALL IntlPluralRulesPrototypeFuncResolvedOptions(ExecState*);
40
41}
42
43#include "IntlPluralRulesPrototype.lut.h"
44
45namespace JSC {
46
47const ClassInfo IntlPluralRulesPrototype::s_info = { "Object", &Base::s_info, &pluralRulesPrototypeTable, nullptr, CREATE_METHOD_TABLE(IntlPluralRulesPrototype) };
48
49/* Source for IntlPluralRulesPrototype.lut.h
50@begin pluralRulesPrototypeTable
51 select IntlPluralRulesPrototypeFuncSelect DontEnum|Function 1
52 resolvedOptions IntlPluralRulesPrototypeFuncResolvedOptions DontEnum|Function 0
53@end
54*/
55
56IntlPluralRulesPrototype* IntlPluralRulesPrototype::create(VM& vm, JSGlobalObject*, Structure* structure)
57{
58 IntlPluralRulesPrototype* object = new (NotNull, allocateCell<IntlPluralRulesPrototype>(vm.heap)) IntlPluralRulesPrototype(vm, structure);
59 object->finishCreation(vm, structure);
60 return object;
61}
62
63Structure* IntlPluralRulesPrototype::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
64{
65 return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
66}
67
68IntlPluralRulesPrototype::IntlPluralRulesPrototype(VM& vm, Structure* structure)
69 : Base(vm, structure)
70{
71}
72
73void IntlPluralRulesPrototype::finishCreation(VM& vm, Structure*)
74{
75 Base::finishCreation(vm);
76
77 putDirectWithoutTransition(vm, vm.propertyNames->toStringTagSymbol, jsString(&vm, "Object"), PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly);
78}
79
80EncodedJSValue JSC_HOST_CALL IntlPluralRulesPrototypeFuncSelect(ExecState* state)
81{
82 VM& vm = state->vm();
83 auto scope = DECLARE_THROW_SCOPE(vm);
84
85 // 13.4.3 Intl.PluralRules.prototype.select (value)
86 // https://tc39.github.io/ecma402/#sec-intl.pluralrules.prototype.select
87 IntlPluralRules* pluralRules = jsDynamicCast<IntlPluralRules*>(vm, state->thisValue());
88
89 if (!pluralRules)
90 return JSValue::encode(throwTypeError(state, scope, "Intl.PluralRules.prototype.select called on value that's not an object initialized as a PluralRules"_s));
91
92 double value = state->argument(0).toNumber(state);
93 RETURN_IF_EXCEPTION(scope, encodedJSValue());
94
95 RELEASE_AND_RETURN(scope, JSValue::encode(pluralRules->select(*state, value)));
96}
97
98EncodedJSValue JSC_HOST_CALL IntlPluralRulesPrototypeFuncResolvedOptions(ExecState* state)
99{
100 VM& vm = state->vm();
101 auto scope = DECLARE_THROW_SCOPE(vm);
102
103 // 13.4.4 Intl.PluralRules.prototype.resolvedOptions ()
104 // https://tc39.github.io/ecma402/#sec-intl.pluralrules.prototype.resolvedoptions
105 IntlPluralRules* pluralRules = jsDynamicCast<IntlPluralRules*>(vm, state->thisValue());
106
107 if (!pluralRules)
108 return JSValue::encode(throwTypeError(state, scope, "Intl.PluralRules.prototype.resolvedOptions called on value that's not an object initialized as a PluralRules"_s));
109
110 RELEASE_AND_RETURN(scope, JSValue::encode(pluralRules->resolvedOptions(*state)));
111}
112
113} // namespace JSC
114
115#endif // ENABLE(INTL)
116