1/*
2 * Copyright (C) 2007, 2008, 2009 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 COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "JSNamedNodeMap.h"
28
29#include "JSNode.h"
30#include "Element.h"
31#include "NamedNodeMap.h"
32
33using namespace JSC;
34
35namespace WebCore {
36
37class JSNamedNodeMapOwner : public JSC::WeakHandleOwner {
38    virtual bool isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown>, void* context, JSC::MarkStack&);
39    virtual void finalize(JSC::Handle<JSC::Unknown>, void* context);
40};
41
42bool JSNamedNodeMapOwner::isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown> handle, void*, MarkStack& markStack)
43{
44    JSNamedNodeMap* jsNamedNodeMap = static_cast<JSNamedNodeMap*>(handle.get().asCell());
45    if (!jsNamedNodeMap->hasCustomProperties())
46        return false;
47    Element* element = jsNamedNodeMap->impl()->element();
48    if (!element)
49        return false;
50    return markStack.containsOpaqueRoot(root(element));
51}
52
53void JSNamedNodeMapOwner::finalize(JSC::Handle<JSC::Unknown> handle, void* context)
54{
55    JSNamedNodeMap* jsNamedNodeMap = static_cast<JSNamedNodeMap*>(handle.get().asCell());
56    DOMWrapperWorld* world = static_cast<DOMWrapperWorld*>(context);
57    uncacheWrapper(world, jsNamedNodeMap->impl(), jsNamedNodeMap);
58}
59
60inline JSC::WeakHandleOwner* wrapperOwner(DOMWrapperWorld*, NamedNodeMap*)
61{
62    DEFINE_STATIC_LOCAL(JSNamedNodeMapOwner, jsNamedNodeMapOwner, ());
63    return &jsNamedNodeMapOwner;
64}
65
66inline void* wrapperContext(DOMWrapperWorld* world, NamedNodeMap*)
67{
68    return world;
69}
70
71bool JSNamedNodeMap::canGetItemsForName(ExecState*, NamedNodeMap* impl, const Identifier& propertyName)
72{
73    return impl->getNamedItem(identifierToString(propertyName));
74}
75
76JSValue JSNamedNodeMap::nameGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
77{
78    JSNamedNodeMap* thisObj = static_cast<JSNamedNodeMap*>(asObject(slotBase));
79    return toJS(exec, thisObj->impl()->getNamedItem(identifierToString(propertyName)));
80}
81
82void JSNamedNodeMap::markChildren(MarkStack& markStack)
83{
84    Base::markChildren(markStack);
85
86    // We need to keep the wrapper for our underlying NamedNodeMap's element
87    // alive because NamedNodeMap and Attr rely on the element for data, and
88    // don't know how to keep it alive correctly.
89    // FIXME: Fix this lifetime issue in the DOM, and remove this.
90    Element* element = impl()->element();
91    if (!element)
92        return;
93    markStack.addOpaqueRoot(root(element));
94}
95
96JSC::JSValue toJS(JSC::ExecState* exec, JSDOMGlobalObject* globalObject, NamedNodeMap* impl)
97{
98    return wrap<JSNamedNodeMap>(exec, globalObject, impl);
99}
100
101} // namespace WebCore
102