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 <JavaScriptCore/Identifier.h>
36#include <JavaScriptCore/JSGlobalObject.h>
37#include <JavaScriptCore/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 if (name.isNull())
74 return nullptr;
75
76 if (Method* method = m_methods.get(name.impl()))
77 return method;
78
79 NPIdentifier ident = _NPN_GetStringIdentifier(name.ascii().data());
80 const CInstance* inst = static_cast<const CInstance*>(instance);
81 NPObject* obj = inst->getObject();
82 if (m_isa->hasMethod && m_isa->hasMethod(obj, ident)) {
83 auto method = std::make_unique<CMethod>(ident);
84 CMethod* ret = method.get();
85 m_methods.set(name.impl(), WTFMove(method));
86 return ret;
87 }
88
89 return nullptr;
90}
91
92Field* CClass::fieldNamed(PropertyName propertyName, Instance* instance) const
93{
94 String name(propertyName.publicName());
95 if (name.isNull())
96 return nullptr;
97
98 if (Field* field = m_fields.get(name.impl()))
99 return field;
100
101 NPIdentifier ident = _NPN_GetStringIdentifier(name.ascii().data());
102 const CInstance* inst = static_cast<const CInstance*>(instance);
103 NPObject* obj = inst->getObject();
104 if (m_isa->hasProperty && m_isa->hasProperty(obj, ident)) {
105 auto field = std::make_unique<CField>(ident);
106 CField* ret = field.get();
107 m_fields.set(name.impl(), WTFMove(field));
108 return ret;
109 }
110
111 return nullptr;
112}
113
114} } // namespace JSC::Bindings
115
116#endif // ENABLE(NETSCAPE_PLUGIN_API)
117