1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
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 "JSDOMStringMap.h"
28
29#include "DOMStringMap.h"
30#include <wtf/text/AtomicString.h>
31
32using namespace JSC;
33
34namespace WebCore {
35
36bool JSDOMStringMap::canGetItemsForName(ExecState*, DOMStringMap* impl, const Identifier& propertyName)
37{
38    return impl->contains(identifierToAtomicString(propertyName));
39}
40
41JSValue JSDOMStringMap::nameGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
42{
43    JSDOMStringMap* thisObj = static_cast<JSDOMStringMap*>(asObject(slotBase));
44    return jsString(exec, thisObj->impl()->item(identifierToAtomicString(propertyName)));
45}
46
47void JSDOMStringMap::getOwnPropertyNames(ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
48{
49    Vector<String> names;
50    m_impl->getNames(names);
51    size_t length = names.size();
52    for (size_t i = 0; i < length; ++i)
53        propertyNames.add(Identifier(exec, stringToUString(names[i])));
54
55    Base::getOwnPropertyNames(exec, propertyNames, mode);
56}
57
58bool JSDOMStringMap::deleteProperty(ExecState* exec, const Identifier& propertyName)
59{
60    // Only perform the custom delete if the object doesn't have a native property by this name.
61    // Since hasProperty() would end up calling canGetItemsForName() and be fooled, we need to check
62    // the native property slots manually.
63    PropertySlot slot;
64    if (getStaticValueSlot<JSDOMStringMap, Base>(exec, s_info.propHashTable(exec), this, propertyName, slot))
65        return false;
66
67    JSValue prototype = this->prototype();
68    if (prototype.isObject() && asObject(prototype)->hasProperty(exec, propertyName))
69        return false;
70
71    ExceptionCode ec = 0;
72    m_impl->deleteItem(identifierToString(propertyName), ec);
73    setDOMException(exec, ec);
74
75    return true;
76}
77
78bool JSDOMStringMap::putDelegate(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot&)
79{
80    // Only perform the custom put if the object doesn't have a native property by this name.
81    // Since hasProperty() would end up calling canGetItemsForName() and be fooled, we need to check
82    // the native property slots manually.
83    PropertySlot slot;
84    if (getStaticValueSlot<JSDOMStringMap, Base>(exec, s_info.propHashTable(exec), this, propertyName, slot))
85        return false;
86
87    JSValue prototype = this->prototype();
88    if (prototype.isObject() && asObject(prototype)->hasProperty(exec, propertyName))
89        return false;
90
91    String stringValue = ustringToString(value.toString(exec));
92    if (exec->hadException())
93        return true;
94
95    ExceptionCode ec = 0;
96    impl()->setItem(identifierToString(propertyName), stringValue, ec);
97    setDOMException(exec, ec);
98
99    return true;
100}
101
102} // namespace WebCore
103