1/*
2 * Copyright (C) 2003, 2006 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. ``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 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
28#if ENABLE(NETSCAPE_PLUGIN_API)
29
30#include "c_class.h"
31
32#include "c_instance.h"
33#include "c_runtime.h"
34#include "npruntime_impl.h"
35#include <runtime/Identifier.h>
36#include <runtime/JSGlobalObject.h>
37#include <runtime/JSObject.h>
38#include <wtf/text/StringHash.h>
39
40namespace JSC { namespace Bindings {
41
42CClass::CClass(NPClass* aClass)
43{
44    m_isa = aClass;
45}
46
47CClass::~CClass()
48{
49    m_methods.clear();
50    m_fields.clear();
51}
52
53typedef HashMap<NPClass*, CClass*> ClassesByIsAMap;
54static ClassesByIsAMap* classesByIsA = 0;
55
56CClass* CClass::classForIsA(NPClass* isa)
57{
58    if (!classesByIsA)
59        classesByIsA = new ClassesByIsAMap;
60
61    CClass* aClass = classesByIsA->get(isa);
62    if (!aClass) {
63        aClass = new CClass(isa);
64        classesByIsA->set(isa, aClass);
65    }
66
67    return aClass;
68}
69
70Method* CClass::methodNamed(PropertyName propertyName, Instance* instance) const
71{
72    String name(propertyName.publicName());
73
74    if (Method* method = m_methods.get(name.impl()))
75        return method;
76
77    NPIdentifier ident = _NPN_GetStringIdentifier(name.ascii().data());
78    const CInstance* inst = static_cast<const CInstance*>(instance);
79    NPObject* obj = inst->getObject();
80    if (m_isa->hasMethod && m_isa->hasMethod(obj, ident)) {
81        Method* method = new CMethod(ident);
82        m_methods.set(name.impl(), adoptPtr(method));
83        return method;
84    }
85
86    return 0;
87}
88
89Field* CClass::fieldNamed(PropertyName propertyName, Instance* instance) const
90{
91    String name(propertyName.publicName());
92
93    if (Field* field = m_fields.get(name.impl()))
94        return field;
95
96    NPIdentifier ident = _NPN_GetStringIdentifier(name.ascii().data());
97    const CInstance* inst = static_cast<const CInstance*>(instance);
98    NPObject* obj = inst->getObject();
99    if (m_isa->hasProperty && m_isa->hasProperty(obj, ident)) {
100        Field* field = new CField(ident);
101        m_fields.set(name.impl(), adoptPtr(field));
102        return field;
103    }
104
105    return 0;
106}
107
108} } // namespace JSC::Bindings
109
110#endif // ENABLE(NETSCAPE_PLUGIN_API)
111