8fca42ad3c250604971980c36a5eb85c9a21162a
[WebKit-https.git] / Source / JavaScriptCore / runtime / IntlCollatorConstructor.cpp
1 /*
2  * Copyright (C) 2015 Andy VanWagoner (thetalecrafter@gmail.com)
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 "IntlCollatorConstructor.h"
28
29 #if ENABLE(INTL)
30
31 #include "Error.h"
32 #include "IntlCollator.h"
33 #include "IntlCollatorPrototype.h"
34 #include "IntlObject.h"
35 #include "JSCJSValueInlines.h"
36 #include "JSCellInlines.h"
37 #include "Lookup.h"
38 #include "SlotVisitorInlines.h"
39 #include "StructureInlines.h"
40
41 namespace JSC {
42
43 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(IntlCollatorConstructor);
44
45 static EncodedJSValue JSC_HOST_CALL IntlCollatorConstructorFuncSupportedLocalesOf(ExecState*);
46
47 }
48
49 #include "IntlCollatorConstructor.lut.h"
50
51 namespace JSC {
52
53 const ClassInfo IntlCollatorConstructor::s_info = { "Function", &InternalFunction::s_info, &collatorConstructorTable, CREATE_METHOD_TABLE(IntlCollatorConstructor) };
54
55 /* Source for IntlCollatorConstructor.lut.h
56 @begin collatorConstructorTable
57   supportedLocalesOf             IntlCollatorConstructorFuncSupportedLocalesOf             DontEnum|Function 1
58 @end
59 */
60
61 IntlCollatorConstructor* IntlCollatorConstructor::create(VM& vm, Structure* structure, IntlCollatorPrototype* collatorPrototype, Structure* collatorStructure)
62 {
63     IntlCollatorConstructor* constructor = new (NotNull, allocateCell<IntlCollatorConstructor>(vm.heap)) IntlCollatorConstructor(vm, structure);
64     constructor->finishCreation(vm, collatorPrototype, collatorStructure);
65     return constructor;
66 }
67
68 Structure* IntlCollatorConstructor::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
69 {
70     return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
71 }
72
73 IntlCollatorConstructor::IntlCollatorConstructor(VM& vm, Structure* structure)
74     : InternalFunction(vm, structure)
75 {
76 }
77
78 void IntlCollatorConstructor::finishCreation(VM& vm, IntlCollatorPrototype* collatorPrototype, Structure* collatorStructure)
79 {
80     Base::finishCreation(vm, ASCIILiteral("Collator"));
81     putDirectWithoutTransition(vm, vm.propertyNames->prototype, collatorPrototype, DontEnum | DontDelete | ReadOnly);
82     putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(0), ReadOnly | DontEnum | DontDelete);
83     m_collatorStructure.set(vm, this, collatorStructure);
84 }
85
86 static EncodedJSValue JSC_HOST_CALL constructIntlCollator(ExecState* exec)
87 {
88     // 10.1.2 Intl.Collator ([locales [, options]]) (ECMA-402 2.0)
89     // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
90     JSValue newTarget = exec->newTarget();
91     if (newTarget.isUndefined())
92         newTarget = exec->callee();
93
94     // 2. Let collator be OrdinaryCreateFromConstructor(newTarget, %CollatorPrototype%).
95     VM& vm = exec->vm();
96     IntlCollator* collator = IntlCollator::create(vm, jsCast<IntlCollatorConstructor*>(exec->callee()));
97     if (collator && !jsDynamicCast<IntlCollatorConstructor*>(newTarget)) {
98         JSValue proto = asObject(newTarget)->getDirect(vm, vm.propertyNames->prototype);
99         asObject(collator)->setPrototypeWithCycleCheck(exec, proto);
100     }
101
102     // 3. ReturnIfAbrupt(collator).
103     ASSERT(collator);
104
105     // 4. Return InitializeCollator(collator, locales, options).
106     // FIXME: return JSValue::encode(InitializeCollator(collator, locales, options));
107
108     return JSValue::encode(collator);
109 }
110
111 static EncodedJSValue JSC_HOST_CALL callIntlCollator(ExecState* exec)
112 {
113     // 10.1.2 Intl.Collator ([locales [, options]]) (ECMA-402 2.0)
114     // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
115     // NewTarget is always undefined when called as a function.
116
117     // 2. Let collator be OrdinaryCreateFromConstructor(newTarget, %CollatorPrototype%).
118     VM& vm = exec->vm();
119     IntlCollator* collator = IntlCollator::create(vm, jsCast<IntlCollatorConstructor*>(exec->callee()));
120
121     // 3. ReturnIfAbrupt(collator).
122     ASSERT(collator);
123
124     // 4. Return InitializeCollator(collator, locales, options).
125     // FIXME: return JSValue::encode(InitializeCollator(collator, locales, options));
126
127     return JSValue::encode(collator);
128 }
129
130 ConstructType IntlCollatorConstructor::getConstructData(JSCell*, ConstructData& constructData)
131 {
132     constructData.native.function = constructIntlCollator;
133     return ConstructTypeHost;
134 }
135
136 CallType IntlCollatorConstructor::getCallData(JSCell*, CallData& callData)
137 {
138     callData.native.function = callIntlCollator;
139     return CallTypeHost;
140 }
141
142 bool IntlCollatorConstructor::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
143 {
144     return getStaticFunctionSlot<InternalFunction>(exec, collatorConstructorTable, jsCast<IntlCollatorConstructor*>(object), propertyName, slot);
145 }
146
147 EncodedJSValue JSC_HOST_CALL IntlCollatorConstructorFuncSupportedLocalesOf(ExecState* exec)
148 {
149     // 10.2.2 Intl.Collator.supportedLocalesOf(locales [, options]) (ECMA-402 2.0)
150
151     // 1. Let requestedLocales be CanonicalizeLocaleList(locales).
152     Vector<String> requestedLocales = canonicalizeLocaleList(exec, exec->argument(0));
153
154     // 2. ReturnIfAbrupt(requestedLocales).
155     if (exec->hadException())
156         return JSValue::encode(jsUndefined());
157
158     // 3. Return SupportedLocales(%Collator%.[[availableLocales]], requestedLocales, options).
159     JSGlobalObject* globalObject = exec->callee()->globalObject();
160     return JSValue::encode(supportedLocales(exec, globalObject->intlCollatorAvailableLocales(), requestedLocales, exec->argument(1)));
161 }
162
163 void IntlCollatorConstructor::visitChildren(JSCell* cell, SlotVisitor& visitor)
164 {
165     IntlCollatorConstructor* thisObject = jsCast<IntlCollatorConstructor*>(cell);
166     ASSERT_GC_OBJECT_INHERITS(thisObject, info());
167
168     Base::visitChildren(thisObject, visitor);
169
170     visitor.append(&thisObject->m_collatorStructure);
171 }
172
173 } // namespace JSC
174
175 #endif // ENABLE(INTL)