]> git.saurik.com Git - apple/javascriptcore.git/blob - bindings/npruntime.cpp
JavaScriptCore-466.1.6.tar.gz
[apple/javascriptcore.git] / bindings / npruntime.cpp
1 /*
2 * Copyright (C) 2004, 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_API)
29
30 #include "npruntime_internal.h"
31 #include "npruntime_impl.h"
32 #include "npruntime_priv.h"
33
34 #include "JSLock.h"
35 #include "c_utility.h"
36 #include "identifier.h"
37 #include <wtf/Assertions.h>
38 #include <wtf/HashMap.h>
39
40 using namespace KJS::Bindings;
41
42 typedef HashMap<RefPtr<KJS::UString::Rep>, PrivateIdentifier*> StringIdentifierMap;
43
44 static StringIdentifierMap* getStringIdentifierMap()
45 {
46 static StringIdentifierMap* stringIdentifierMap = 0;
47 if (!stringIdentifierMap)
48 stringIdentifierMap = new StringIdentifierMap;
49 return stringIdentifierMap;
50 }
51
52 typedef HashMap<int, PrivateIdentifier*> IntIdentifierMap;
53
54 static IntIdentifierMap* getIntIdentifierMap()
55 {
56 static IntIdentifierMap* intIdentifierMap = 0;
57 if (!intIdentifierMap)
58 intIdentifierMap = new IntIdentifierMap;
59 return intIdentifierMap;
60 }
61
62 NPIdentifier _NPN_GetStringIdentifier(const NPUTF8* name)
63 {
64 ASSERT(name);
65
66 if (name) {
67 PrivateIdentifier* identifier = 0;
68
69 KJS::JSLock lock;
70
71 identifier = getStringIdentifierMap()->get(identifierFromNPIdentifier(name).ustring().rep());
72 if (identifier == 0) {
73 identifier = (PrivateIdentifier*)malloc(sizeof(PrivateIdentifier));
74 // We never release identifier names, so this dictionary will grow, as will
75 // the memory for the identifier name strings.
76 identifier->isString = true;
77 identifier->value.string = strdup(name);
78
79 getStringIdentifierMap()->set(identifierFromNPIdentifier(name).ustring().rep(), identifier);
80 }
81 return (NPIdentifier)identifier;
82 }
83
84 return 0;
85 }
86
87 void _NPN_GetStringIdentifiers(const NPUTF8** names, int32_t nameCount, NPIdentifier* identifiers)
88 {
89 ASSERT(names);
90 ASSERT(identifiers);
91
92 if (names && identifiers)
93 for (int i = 0; i < nameCount; i++)
94 identifiers[i] = _NPN_GetStringIdentifier(names[i]);
95 }
96
97 NPIdentifier _NPN_GetIntIdentifier(int32_t intid)
98 {
99 PrivateIdentifier* identifier;
100
101 if (intid == 0 || intid == -1) {
102 static PrivateIdentifier* negativeOneAndZeroIdentifiers[2];
103
104 identifier = negativeOneAndZeroIdentifiers[intid + 1];
105 if (!identifier) {
106 identifier = (PrivateIdentifier*)malloc(sizeof(PrivateIdentifier));
107 identifier->isString = false;
108 identifier->value.number = intid;
109
110 negativeOneAndZeroIdentifiers[intid + 1] = identifier;
111 }
112 } else {
113 identifier = getIntIdentifierMap()->get(intid);
114 if (!identifier) {
115 identifier = (PrivateIdentifier*)malloc(sizeof(PrivateIdentifier));
116 // We never release identifier names, so this dictionary will grow.
117 identifier->isString = false;
118 identifier->value.number = intid;
119
120 getIntIdentifierMap()->set(intid, identifier);
121 }
122 }
123 return (NPIdentifier)identifier;
124 }
125
126 bool _NPN_IdentifierIsString(NPIdentifier identifier)
127 {
128 PrivateIdentifier* i = (PrivateIdentifier*)identifier;
129 return i->isString;
130 }
131
132 NPUTF8 *_NPN_UTF8FromIdentifier(NPIdentifier identifier)
133 {
134 PrivateIdentifier* i = (PrivateIdentifier*)identifier;
135 if (!i->isString || !i->value.string)
136 return NULL;
137
138 return (NPUTF8 *)strdup(i->value.string);
139 }
140
141 int32_t _NPN_IntFromIdentifier(NPIdentifier identifier)
142 {
143 PrivateIdentifier* i = (PrivateIdentifier*)identifier;
144 if (!i->isString)
145 return 0;
146 return i->value.number;
147 }
148
149 void NPN_InitializeVariantWithStringCopy(NPVariant* variant, const NPString* value)
150 {
151 variant->type = NPVariantType_String;
152 variant->value.stringValue.UTF8Length = value->UTF8Length;
153 variant->value.stringValue.UTF8Characters = (NPUTF8 *)malloc(sizeof(NPUTF8) * value->UTF8Length);
154 memcpy((void*)variant->value.stringValue.UTF8Characters, value->UTF8Characters, sizeof(NPUTF8) * value->UTF8Length);
155 }
156
157 void _NPN_ReleaseVariantValue(NPVariant* variant)
158 {
159 ASSERT(variant);
160
161 if (variant->type == NPVariantType_Object) {
162 _NPN_ReleaseObject(variant->value.objectValue);
163 variant->value.objectValue = 0;
164 } else if (variant->type == NPVariantType_String) {
165 free((void*)variant->value.stringValue.UTF8Characters);
166 variant->value.stringValue.UTF8Characters = 0;
167 variant->value.stringValue.UTF8Length = 0;
168 }
169
170 variant->type = NPVariantType_Void;
171 }
172
173 NPObject *_NPN_CreateObject(NPP npp, NPClass* aClass)
174 {
175 ASSERT(aClass);
176
177 if (aClass) {
178 NPObject* obj;
179 if (aClass->allocate != NULL)
180 obj = aClass->allocate(npp, aClass);
181 else
182 obj = (NPObject*)malloc(sizeof(NPObject));
183
184 obj->_class = aClass;
185 obj->referenceCount = 1;
186
187 return obj;
188 }
189
190 return 0;
191 }
192
193 NPObject* _NPN_RetainObject(NPObject* obj)
194 {
195 ASSERT(obj);
196
197 if (obj)
198 obj->referenceCount++;
199
200 return obj;
201 }
202
203 void _NPN_ReleaseObject(NPObject* obj)
204 {
205 ASSERT(obj);
206 ASSERT(obj->referenceCount >= 1);
207
208 if (obj && obj->referenceCount >= 1) {
209 if (--obj->referenceCount == 0)
210 _NPN_DeallocateObject(obj);
211 }
212 }
213
214 void _NPN_DeallocateObject(NPObject *obj)
215 {
216 ASSERT(obj);
217
218 if (obj) {
219 if (obj->_class->deallocate)
220 obj->_class->deallocate(obj);
221 else
222 free(obj);
223 }
224 }
225
226 #endif // ENABLE(NETSCAPE_API)