]> git.saurik.com Git - apple/javascriptcore.git/blob - bindings/NP_jsobject.cpp
d68a8e69ecdce185a9468978103cce322f6e32fc
[apple/javascriptcore.git] / bindings / NP_jsobject.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 "NP_jsobject.h"
31
32 #include "JSGlobalObject.h"
33 #include "PropertyNameArray.h"
34 #include "c_utility.h"
35 #include "interpreter.h"
36 #include "npruntime_impl.h"
37 #include "npruntime_priv.h"
38 #include "object.h"
39 #include "runtime_root.h"
40
41 using namespace KJS;
42 using namespace KJS::Bindings;
43
44 static void getListFromVariantArgs(ExecState* exec, const NPVariant* args, unsigned argCount, RootObject* rootObject, List& aList)
45 {
46 for (unsigned i = 0; i < argCount; i++)
47 aList.append(convertNPVariantToValue(exec, &args[i], rootObject));
48 }
49
50 static NPObject* jsAllocate(NPP, NPClass*)
51 {
52 return (NPObject*)malloc(sizeof(JavaScriptObject));
53 }
54
55 static void jsDeallocate(NPObject* npObj)
56 {
57 JavaScriptObject* obj = (JavaScriptObject*)npObj;
58
59 if (obj->rootObject && obj->rootObject->isValid())
60 obj->rootObject->gcUnprotect(obj->imp);
61
62 if (obj->rootObject)
63 obj->rootObject->deref();
64
65 free(obj);
66 }
67
68 static NPClass javascriptClass = { 1, jsAllocate, jsDeallocate, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
69 static NPClass noScriptClass = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
70
71 NPClass* NPScriptObjectClass = &javascriptClass;
72 static NPClass* NPNoScriptObjectClass = &noScriptClass;
73
74 NPObject* _NPN_CreateScriptObject(NPP npp, JSObject* imp, PassRefPtr<RootObject> rootObject)
75 {
76 JavaScriptObject* obj = (JavaScriptObject*)_NPN_CreateObject(npp, NPScriptObjectClass);
77
78 obj->rootObject = rootObject.releaseRef();
79
80 if (obj->rootObject)
81 obj->rootObject->gcProtect(imp);
82 obj->imp = imp;
83
84 return (NPObject*)obj;
85 }
86
87 NPObject *_NPN_CreateNoScriptObject(void)
88 {
89 return _NPN_CreateObject(0, NPNoScriptObjectClass);
90 }
91
92 bool _NPN_InvokeDefault(NPP, NPObject* o, const NPVariant* args, uint32_t argCount, NPVariant* result)
93 {
94 if (o->_class == NPScriptObjectClass) {
95 JavaScriptObject* obj = (JavaScriptObject*)o;
96
97 VOID_TO_NPVARIANT(*result);
98
99 // Lookup the function object.
100 RootObject* rootObject = obj->rootObject;
101 if (!rootObject || !rootObject->isValid())
102 return false;
103
104 ExecState* exec = rootObject->globalObject()->globalExec();
105 JSLock lock;
106
107 // Call the function object.
108 JSObject *funcImp = static_cast<JSObject*>(obj->imp);
109 if (!funcImp->implementsCall())
110 return false;
111
112 List argList;
113 getListFromVariantArgs(exec, args, argCount, rootObject, argList);
114 rootObject->globalObject()->startTimeoutCheck();
115 JSValue *resultV = funcImp->call (exec, funcImp, argList);
116 rootObject->globalObject()->stopTimeoutCheck();
117
118 // Convert and return the result of the function call.
119 convertValueToNPVariant(exec, resultV, result);
120 return true;
121 }
122
123 if (o->_class->invokeDefault)
124 return o->_class->invokeDefault(o, args, argCount, result);
125 VOID_TO_NPVARIANT(*result);
126 return true;
127 }
128
129 bool _NPN_Invoke(NPP npp, NPObject* o, NPIdentifier methodName, const NPVariant* args, uint32_t argCount, NPVariant* result)
130 {
131 if (o->_class == NPScriptObjectClass) {
132 JavaScriptObject* obj = (JavaScriptObject*)o;
133
134 PrivateIdentifier* i = (PrivateIdentifier*)methodName;
135 if (!i->isString)
136 return false;
137
138 // Special case the "eval" method.
139 if (methodName == _NPN_GetStringIdentifier("eval")) {
140 if (argCount != 1)
141 return false;
142 if (args[0].type != NPVariantType_String)
143 return false;
144 return _NPN_Evaluate(npp, o, (NPString *)&args[0].value.stringValue, result);
145 }
146
147 // Lookup the function object.
148 RootObject* rootObject = obj->rootObject;
149 if (!rootObject || !rootObject->isValid())
150 return false;
151
152 ExecState* exec = rootObject->globalObject()->globalExec();
153 JSLock lock;
154 JSValue* func = obj->imp->get(exec, identifierFromNPIdentifier(i->value.string));
155 if (func->isNull()) {
156 NULL_TO_NPVARIANT(*result);
157 return false;
158 }
159 if (func->isUndefined()) {
160 VOID_TO_NPVARIANT(*result);
161 return false;
162 }
163 // Call the function object.
164 JSObject *funcImp = static_cast<JSObject*>(func);
165 JSObject *thisObj = const_cast<JSObject*>(obj->imp);
166 List argList;
167 getListFromVariantArgs(exec, args, argCount, rootObject, argList);
168 rootObject->globalObject()->startTimeoutCheck();
169 JSValue *resultV = funcImp->call (exec, thisObj, argList);
170 rootObject->globalObject()->stopTimeoutCheck();
171
172 // Convert and return the result of the function call.
173 convertValueToNPVariant(exec, resultV, result);
174 return true;
175 }
176
177 if (o->_class->invoke)
178 return o->_class->invoke(o, methodName, args, argCount, result);
179
180 VOID_TO_NPVARIANT(*result);
181 return true;
182 }
183
184 bool _NPN_Evaluate(NPP, NPObject* o, NPString* s, NPVariant* variant)
185 {
186 if (o->_class == NPScriptObjectClass) {
187 JavaScriptObject* obj = (JavaScriptObject*)o;
188
189 RootObject* rootObject = obj->rootObject;
190 if (!rootObject || !rootObject->isValid())
191 return false;
192
193 ExecState* exec = rootObject->globalObject()->globalExec();
194
195 JSLock lock;
196 NPUTF16* scriptString;
197 unsigned int UTF16Length;
198 convertNPStringToUTF16(s, &scriptString, &UTF16Length); // requires free() of returned memory
199 rootObject->globalObject()->startTimeoutCheck();
200 Completion completion = Interpreter::evaluate(rootObject->globalObject()->globalExec(), UString(), 0, UString(reinterpret_cast<const UChar*>(scriptString), UTF16Length));
201 rootObject->globalObject()->stopTimeoutCheck();
202 ComplType type = completion.complType();
203
204 JSValue* result;
205 if (type == Normal) {
206 result = completion.value();
207 if (!result)
208 result = jsUndefined();
209 } else
210 result = jsUndefined();
211
212 free(scriptString);
213
214 convertValueToNPVariant(exec, result, variant);
215
216 return true;
217 }
218
219 VOID_TO_NPVARIANT(*variant);
220 return false;
221 }
222
223 bool _NPN_GetProperty(NPP, NPObject* o, NPIdentifier propertyName, NPVariant* variant)
224 {
225 if (o->_class == NPScriptObjectClass) {
226 JavaScriptObject* obj = (JavaScriptObject*)o;
227
228 RootObject* rootObject = obj->rootObject;
229 if (!rootObject || !rootObject->isValid())
230 return false;
231
232 ExecState* exec = rootObject->globalObject()->globalExec();
233 PrivateIdentifier* i = (PrivateIdentifier*)propertyName;
234
235 JSLock lock;
236 JSValue *result;
237 if (i->isString)
238 result = obj->imp->get(exec, identifierFromNPIdentifier(i->value.string));
239 else
240 result = obj->imp->get(exec, i->value.number);
241
242 convertValueToNPVariant(exec, result, variant);
243 return true;
244 }
245
246 if (o->_class->hasProperty && o->_class->getProperty) {
247 if (o->_class->hasProperty(o, propertyName))
248 return o->_class->getProperty(o, propertyName, variant);
249 return false;
250 }
251
252 VOID_TO_NPVARIANT(*variant);
253 return false;
254 }
255
256 bool _NPN_SetProperty(NPP, NPObject* o, NPIdentifier propertyName, const NPVariant* variant)
257 {
258 if (o->_class == NPScriptObjectClass) {
259 JavaScriptObject* obj = (JavaScriptObject*)o;
260
261 RootObject* rootObject = obj->rootObject;
262 if (!rootObject || !rootObject->isValid())
263 return false;
264
265 ExecState* exec = rootObject->globalObject()->globalExec();
266 JSLock lock;
267 PrivateIdentifier* i = (PrivateIdentifier*)propertyName;
268 if (i->isString)
269 obj->imp->put(exec, identifierFromNPIdentifier(i->value.string), convertNPVariantToValue(exec, variant, rootObject));
270 else
271 obj->imp->put(exec, i->value.number, convertNPVariantToValue(exec, variant, rootObject));
272 return true;
273 }
274
275 if (o->_class->setProperty)
276 return o->_class->setProperty(o, propertyName, variant);
277
278 return false;
279 }
280
281 bool _NPN_RemoveProperty(NPP, NPObject* o, NPIdentifier propertyName)
282 {
283 if (o->_class == NPScriptObjectClass) {
284 JavaScriptObject* obj = (JavaScriptObject*)o;
285
286 RootObject* rootObject = obj->rootObject;
287 if (!rootObject || !rootObject->isValid())
288 return false;
289
290 ExecState* exec = rootObject->globalObject()->globalExec();
291 PrivateIdentifier* i = (PrivateIdentifier*)propertyName;
292 if (i->isString) {
293 if (!obj->imp->hasProperty(exec, identifierFromNPIdentifier(i->value.string)))
294 return false;
295 } else {
296 if (!obj->imp->hasProperty(exec, i->value.number))
297 return false;
298 }
299
300 JSLock lock;
301 if (i->isString)
302 obj->imp->deleteProperty(exec, identifierFromNPIdentifier(i->value.string));
303 else
304 obj->imp->deleteProperty(exec, i->value.number);
305
306 return true;
307 }
308 return false;
309 }
310
311 bool _NPN_HasProperty(NPP, NPObject* o, NPIdentifier propertyName)
312 {
313 if (o->_class == NPScriptObjectClass) {
314 JavaScriptObject* obj = (JavaScriptObject*)o;
315
316 RootObject* rootObject = obj->rootObject;
317 if (!rootObject || !rootObject->isValid())
318 return false;
319
320 ExecState* exec = rootObject->globalObject()->globalExec();
321 PrivateIdentifier* i = (PrivateIdentifier*)propertyName;
322 JSLock lock;
323 if (i->isString)
324 return obj->imp->hasProperty(exec, identifierFromNPIdentifier(i->value.string));
325 return obj->imp->hasProperty(exec, i->value.number);
326 }
327
328 if (o->_class->hasProperty)
329 return o->_class->hasProperty(o, propertyName);
330
331 return false;
332 }
333
334 bool _NPN_HasMethod(NPP, NPObject* o, NPIdentifier methodName)
335 {
336 if (o->_class == NPScriptObjectClass) {
337 JavaScriptObject* obj = (JavaScriptObject*)o;
338
339 PrivateIdentifier* i = (PrivateIdentifier*)methodName;
340 if (!i->isString)
341 return false;
342
343 RootObject* rootObject = obj->rootObject;
344 if (!rootObject || !rootObject->isValid())
345 return false;
346
347 ExecState* exec = rootObject->globalObject()->globalExec();
348 JSLock lock;
349 JSValue* func = obj->imp->get(exec, identifierFromNPIdentifier(i->value.string));
350 return !func->isUndefined();
351 }
352
353 if (o->_class->hasMethod)
354 return o->_class->hasMethod(o, methodName);
355
356 return false;
357 }
358
359 void _NPN_SetException(NPObject* o, const NPUTF8* message)
360 {
361 if (o->_class == NPScriptObjectClass) {
362 JavaScriptObject* obj = (JavaScriptObject*)o;
363 RootObject* rootObject = obj->rootObject;
364 if (!rootObject || !rootObject->isValid())
365 return;
366
367 ExecState* exec = rootObject->globalObject()->globalExec();
368 JSLock lock;
369 throwError(exec, GeneralError, message);
370 }
371 }
372
373 bool _NPN_Enumerate(NPP, NPObject *o, NPIdentifier **identifier, uint32_t *count)
374 {
375 if (o->_class == NPScriptObjectClass) {
376 JavaScriptObject* obj = (JavaScriptObject*)o;
377
378 RootObject* rootObject = obj->rootObject;
379 if (!rootObject || !rootObject->isValid())
380 return false;
381
382 ExecState* exec = rootObject->globalObject()->globalExec();
383 JSLock lock;
384 PropertyNameArray propertyNames;
385
386 obj->imp->getPropertyNames(exec, propertyNames);
387 unsigned size = static_cast<unsigned>(propertyNames.size());
388 // FIXME: This should really call NPN_MemAlloc but that's in WebKit
389 NPIdentifier *identifiers = static_cast<NPIdentifier*>(malloc(sizeof(NPIdentifier) * size));
390
391 for (unsigned i = 0; i < size; i++)
392 identifiers[i] = _NPN_GetStringIdentifier(propertyNames[i].ustring().UTF8String().c_str());
393
394 *identifier = identifiers;
395 *count = size;
396
397 return true;
398 }
399
400 if (NP_CLASS_STRUCT_VERSION_HAS_ENUM(o->_class) && o->_class->enumerate)
401 return o->_class->enumerate(o, identifier, count);
402
403 return false;
404 }
405
406 #endif // ENABLE(NETSCAPE_API)