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