1/*
2 * Copyright (C) 2007 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 "JSNodeList.h"
28
29#include "DynamicNodeList.h"
30#include "JSNode.h"
31#include "Node.h"
32#include "NodeList.h"
33#include <wtf/text/AtomicString.h>
34
35using namespace JSC;
36
37namespace WebCore {
38
39class JSNodeListOwner : public JSC::WeakHandleOwner {
40    virtual bool isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown>, void* context, JSC::MarkStack&);
41    virtual void finalize(JSC::Handle<JSC::Unknown>, void* context);
42};
43
44bool JSNodeListOwner::isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown> handle, void*, MarkStack& markStack)
45{
46    JSNodeList* jsNodeList = static_cast<JSNodeList*>(handle.get().asCell());
47    if (!jsNodeList->hasCustomProperties())
48        return false;
49    if (!jsNodeList->impl()->isDynamicNodeList())
50        return false;
51    return markStack.containsOpaqueRoot(root(static_cast<DynamicNodeList*>(jsNodeList->impl())->rootNode()));
52}
53
54void JSNodeListOwner::finalize(JSC::Handle<JSC::Unknown> handle, void* context)
55{
56    JSNodeList* jsNodeList = static_cast<JSNodeList*>(handle.get().asCell());
57    DOMWrapperWorld* world = static_cast<DOMWrapperWorld*>(context);
58    uncacheWrapper(world, jsNodeList->impl(), jsNodeList);
59}
60
61inline JSC::WeakHandleOwner* wrapperOwner(DOMWrapperWorld*, NodeList*)
62{
63    DEFINE_STATIC_LOCAL(JSNodeListOwner, jsNodeListOwner, ());
64    return &jsNodeListOwner;
65}
66
67inline void* wrapperContext(DOMWrapperWorld* world, NodeList*)
68{
69    return world;
70}
71
72JSC::JSValue toJS(JSC::ExecState* exec, JSDOMGlobalObject* globalObject, NodeList* impl)
73{
74    return wrap<JSNodeList>(exec, globalObject, impl);
75}
76
77// Need to support call so that list(0) works.
78static EncodedJSValue JSC_HOST_CALL callNodeList(ExecState* exec)
79{
80    bool ok;
81    unsigned index = Identifier::toUInt32(exec->argument(0).toString(exec), ok);
82    if (!ok)
83        return JSValue::encode(jsUndefined());
84    return JSValue::encode(toJS(exec, static_cast<JSNodeList*>(exec->callee())->impl()->item(index)));
85}
86
87CallType JSNodeList::getCallData(CallData& callData)
88{
89    callData.native.function = callNodeList;
90    return CallTypeHost;
91}
92
93bool JSNodeList::canGetItemsForName(ExecState*, NodeList* impl, const Identifier& propertyName)
94{
95    return impl->itemWithName(identifierToAtomicString(propertyName));
96}
97
98JSValue JSNodeList::nameGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
99{
100    JSNodeList* thisObj = static_cast<JSNodeList*>(asObject(slotBase));
101    return toJS(exec, thisObj->impl()->itemWithName(identifierToAtomicString(propertyName)));
102}
103
104} // namespace WebCore
105