1/*
2 * Copyright (C) 2015 Andy VanWagoner ([email protected])
3 * Copyright (C) 2015 Sukolsak Sakshuwong ([email protected])
4 * Copyright (C) 2016 Apple Inc. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
19 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25 * THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "config.h"
29#include "IntlCollatorConstructor.h"
30
31#if ENABLE(INTL)
32
33#include "Error.h"
34#include "IntlCollator.h"
35#include "IntlCollatorPrototype.h"
36#include "IntlObject.h"
37#include "JSCInlines.h"
38#include "Lookup.h"
39
40namespace JSC {
41
42STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(IntlCollatorConstructor);
43
44static EncodedJSValue JSC_HOST_CALL IntlCollatorConstructorFuncSupportedLocalesOf(ExecState*);
45
46}
47
48#include "IntlCollatorConstructor.lut.h"
49
50namespace JSC {
51
52static EncodedJSValue JSC_HOST_CALL callIntlCollator(ExecState*);
53static EncodedJSValue JSC_HOST_CALL constructIntlCollator(ExecState*);
54
55const ClassInfo IntlCollatorConstructor::s_info = { "Function", &InternalFunction::s_info, &collatorConstructorTable, nullptr, CREATE_METHOD_TABLE(IntlCollatorConstructor) };
56
57/* Source for IntlCollatorConstructor.lut.h
58@begin collatorConstructorTable
59 supportedLocalesOf IntlCollatorConstructorFuncSupportedLocalesOf DontEnum|Function 1
60@end
61*/
62
63IntlCollatorConstructor* IntlCollatorConstructor::create(VM& vm, Structure* structure, IntlCollatorPrototype* collatorPrototype)
64{
65 IntlCollatorConstructor* constructor = new (NotNull, allocateCell<IntlCollatorConstructor>(vm.heap)) IntlCollatorConstructor(vm, structure);
66 constructor->finishCreation(vm, collatorPrototype);
67 return constructor;
68}
69
70Structure* IntlCollatorConstructor::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
71{
72 return Structure::create(vm, globalObject, prototype, TypeInfo(InternalFunctionType, StructureFlags), info());
73}
74
75IntlCollatorConstructor::IntlCollatorConstructor(VM& vm, Structure* structure)
76 : InternalFunction(vm, structure, callIntlCollator, constructIntlCollator)
77{
78}
79
80void IntlCollatorConstructor::finishCreation(VM& vm, IntlCollatorPrototype* collatorPrototype)
81{
82 Base::finishCreation(vm, "Collator"_s, NameVisibility::Visible, NameAdditionMode::WithoutStructureTransition);
83 putDirectWithoutTransition(vm, vm.propertyNames->prototype, collatorPrototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);
84 putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(0), PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum);
85 collatorPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, this, static_cast<unsigned>(PropertyAttribute::DontEnum));
86}
87
88static EncodedJSValue JSC_HOST_CALL constructIntlCollator(ExecState* state)
89{
90 VM& vm = state->vm();
91 auto scope = DECLARE_THROW_SCOPE(vm);
92 // 10.1.2 Intl.Collator ([locales [, options]]) (ECMA-402 2.0)
93 // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
94 // 2. Let collator be OrdinaryCreateFromConstructor(newTarget, %CollatorPrototype%).
95 // 3. ReturnIfAbrupt(collator).
96 Structure* structure = InternalFunction::createSubclassStructure(state, state->newTarget(), jsCast<IntlCollatorConstructor*>(state->jsCallee())->collatorStructure(vm));
97 RETURN_IF_EXCEPTION(scope, encodedJSValue());
98 IntlCollator* collator = IntlCollator::create(vm, structure);
99 ASSERT(collator);
100
101 // 4. Return InitializeCollator(collator, locales, options).
102 scope.release();
103 collator->initializeCollator(*state, state->argument(0), state->argument(1));
104 return JSValue::encode(collator);
105}
106
107static EncodedJSValue JSC_HOST_CALL callIntlCollator(ExecState* state)
108{
109 // 10.1.2 Intl.Collator ([locales [, options]]) (ECMA-402 2.0)
110 // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
111 // NewTarget is always undefined when called as a function.
112
113 VM& vm = state->vm();
114 IntlCollatorConstructor* callee = jsCast<IntlCollatorConstructor*>(state->jsCallee());
115
116 // FIXME: Collator does not get the workaround for ECMA-402 1.0 compatibility.
117 // https://bugs.webkit.org/show_bug.cgi?id=153679
118
119 // 2. Let collator be OrdinaryCreateFromConstructor(newTarget, %CollatorPrototype%).
120 // 3. ReturnIfAbrupt(collator).
121 IntlCollator* collator = IntlCollator::create(vm, callee->collatorStructure(vm));
122 ASSERT(collator);
123
124 // 4. Return InitializeCollator(collator, locales, options).
125 collator->initializeCollator(*state, state->argument(0), state->argument(1));
126 return JSValue::encode(collator);
127}
128
129EncodedJSValue JSC_HOST_CALL IntlCollatorConstructorFuncSupportedLocalesOf(ExecState* state)
130{
131 VM& vm = state->vm();
132 auto scope = DECLARE_THROW_SCOPE(vm);
133 // 10.2.2 Intl.Collator.supportedLocalesOf(locales [, options]) (ECMA-402 2.0)
134
135 // 1. Let requestedLocales be CanonicalizeLocaleList(locales).
136 Vector<String> requestedLocales = canonicalizeLocaleList(*state, state->argument(0));
137
138 // 2. ReturnIfAbrupt(requestedLocales).
139 RETURN_IF_EXCEPTION(scope, encodedJSValue());
140
141 // 3. Return SupportedLocales(%Collator%.[[availableLocales]], requestedLocales, options).
142 JSGlobalObject* globalObject = state->jsCallee()->globalObject(vm);
143 RELEASE_AND_RETURN(scope, JSValue::encode(supportedLocales(*state, globalObject->intlCollatorAvailableLocales(), requestedLocales, state->argument(1))));
144}
145
146} // namespace JSC
147
148#endif // ENABLE(INTL)
149