blob: 79c523c65b99a96a93d3c5a62924a1fb50be61a6 [file] [log] [blame]
commit-queue@webkit.org77e4cc22015-07-30 03:33:32 +00001/*
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 "IntlCollator.h"
28
29#if ENABLE(INTL)
30
31#include "Error.h"
32#include "IntlCollatorConstructor.h"
33#include "IntlObject.h"
34#include "JSBoundFunction.h"
35#include "JSCJSValueInlines.h"
36#include "JSCellInlines.h"
37#include "SlotVisitorInlines.h"
38#include "StructureInlines.h"
39
40namespace JSC {
41
42const ClassInfo IntlCollator::s_info = { "Object", &Base::s_info, 0, CREATE_METHOD_TABLE(IntlCollator) };
43
44IntlCollator* IntlCollator::create(VM& vm, IntlCollatorConstructor* constructor)
45{
46 IntlCollator* format = new (NotNull, allocateCell<IntlCollator>(vm.heap)) IntlCollator(vm, constructor->collatorStructure());
47 format->finishCreation(vm);
48 return format;
49}
50
51Structure* IntlCollator::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
52{
53 return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
54}
55
56IntlCollator::IntlCollator(VM& vm, Structure* structure)
57 : JSDestructibleObject(vm, structure)
58{
59}
60
61void IntlCollator::finishCreation(VM& vm)
62{
63 Base::finishCreation(vm);
64 ASSERT(inherits(info()));
65}
66
67void IntlCollator::destroy(JSCell* cell)
68{
69 static_cast<IntlCollator*>(cell)->IntlCollator::~IntlCollator();
70}
71
72void IntlCollator::visitChildren(JSCell* cell, SlotVisitor& visitor)
73{
74 IntlCollator* thisObject = jsCast<IntlCollator*>(cell);
75 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
76
77 Base::visitChildren(thisObject, visitor);
78
79 visitor.append(&thisObject->m_boundCompare);
80}
81
82void IntlCollator::setBoundCompare(VM& vm, JSBoundFunction* format)
83{
84 m_boundCompare.set(vm, this, format);
85}
86
sukolsak@gmail.com47152dd2015-11-30 22:21:18 +000087EncodedJSValue JSC_HOST_CALL IntlCollatorFuncCompare(ExecState* state)
commit-queue@webkit.org77e4cc22015-07-30 03:33:32 +000088{
89 // 10.3.4 Collator Compare Functions (ECMA-402 2.0)
90 // 1. Let collator be the this value.
sukolsak@gmail.com47152dd2015-11-30 22:21:18 +000091 IntlCollator* collator = jsDynamicCast<IntlCollator*>(state->thisValue());
commit-queue@webkit.org77e4cc22015-07-30 03:33:32 +000092
93 // 2. Assert: Type(collator) is Object and collator has an [[initializedCollator]] internal slot whose value is true.
94 if (!collator)
sukolsak@gmail.com47152dd2015-11-30 22:21:18 +000095 return JSValue::encode(throwTypeError(state));
commit-queue@webkit.org77e4cc22015-07-30 03:33:32 +000096
97 // 3. If x is not provided, let x be undefined.
98 // 4. If y is not provided, let y be undefined.
99 // 5. Let X be ToString(x).
sukolsak@gmail.com47152dd2015-11-30 22:21:18 +0000100 JSString* a = state->argument(0).toString(state);
commit-queue@webkit.org77e4cc22015-07-30 03:33:32 +0000101 // 6. ReturnIfAbrupt(X).
sukolsak@gmail.com47152dd2015-11-30 22:21:18 +0000102 if (state->hadException())
commit-queue@webkit.org77e4cc22015-07-30 03:33:32 +0000103 return JSValue::encode(jsUndefined());
104
105 // 7. Let Y be ToString(y).
sukolsak@gmail.com47152dd2015-11-30 22:21:18 +0000106 JSString* b = state->argument(1).toString(state);
commit-queue@webkit.org77e4cc22015-07-30 03:33:32 +0000107 // 8. ReturnIfAbrupt(Y).
sukolsak@gmail.com47152dd2015-11-30 22:21:18 +0000108 if (state->hadException())
commit-queue@webkit.org77e4cc22015-07-30 03:33:32 +0000109 return JSValue::encode(jsUndefined());
110
111 // 9. Return CompareStrings(collator, X, Y).
112
113 // 10.3.4 CompareStrings abstract operation (ECMA-402 2.0)
114 // FIXME: Implement CompareStrings.
115
116 // Return simple check until properly implemented.
sukolsak@gmail.com47152dd2015-11-30 22:21:18 +0000117 return JSValue::encode(jsNumber(codePointCompare(a->value(state).impl(), b->value(state).impl())));
commit-queue@webkit.org77e4cc22015-07-30 03:33:32 +0000118}
119
120} // namespace JSC
121
122#endif // ENABLE(INTL)