1/*
2 * Copyright (C) 2003, 2006 Apple Computer, 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
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/ScopeChain.h>
36#include <runtime/Identifier.h>
37#include <runtime/JSLock.h>
38#include <runtime/JSObject.h>
39#include <wtf/text/StringHash.h>
40
41namespace JSC { namespace Bindings {
42
43CClass::CClass(NPClass* aClass)
44{
45    _isa = aClass;
46}
47
48CClass::~CClass()
49{
50    JSLock lock(SilenceAssertionsOnly);
51
52    deleteAllValues(_methods);
53    _methods.clear();
54
55    deleteAllValues(_fields);
56    _fields.clear();
57}
58
59typedef HashMap<NPClass*, CClass*> ClassesByIsAMap;
60static ClassesByIsAMap* classesByIsA = 0;
61
62CClass* CClass::classForIsA(NPClass* isa)
63{
64    if (!classesByIsA)
65        classesByIsA = new ClassesByIsAMap;
66
67    CClass* aClass = classesByIsA->get(isa);
68    if (!aClass) {
69        aClass = new CClass(isa);
70        classesByIsA->set(isa, aClass);
71    }
72
73    return aClass;
74}
75
76MethodList CClass::methodsNamed(const Identifier& identifier, Instance* instance) const
77{
78    MethodList methodList;
79
80    Method* method = _methods.get(identifier.ustring().impl());
81    if (method) {
82        methodList.append(method);
83        return methodList;
84    }
85
86    NPIdentifier ident = _NPN_GetStringIdentifier(identifier.ascii().data());
87    const CInstance* inst = static_cast<const CInstance*>(instance);
88    NPObject* obj = inst->getObject();
89    if (_isa->hasMethod && _isa->hasMethod(obj, ident)){
90        Method* aMethod = new CMethod(ident); // deleted in the CClass destructor
91        {
92            JSLock lock(SilenceAssertionsOnly);
93            _methods.set(identifier.ustring().impl(), aMethod);
94        }
95        methodList.append(aMethod);
96    }
97
98    return methodList;
99}
100
101Field* CClass::fieldNamed(const Identifier& identifier, Instance* instance) const
102{
103    Field* aField = _fields.get(identifier.ustring().impl());
104    if (aField)
105        return aField;
106
107    NPIdentifier ident = _NPN_GetStringIdentifier(identifier.ascii().data());
108    const CInstance* inst = static_cast<const CInstance*>(instance);
109    NPObject* obj = inst->getObject();
110    if (_isa->hasProperty && _isa->hasProperty(obj, ident)){
111        aField = new CField(ident); // deleted in the CClass destructor
112        {
113            JSLock lock(SilenceAssertionsOnly);
114            _fields.set(identifier.ustring().impl(), aField);
115        }
116    }
117    return aField;
118}
119
120} } // namespace JSC::Bindings
121
122#endif // ENABLE(NETSCAPE_PLUGIN_API)
123