1 /*****************************************************************************
2 * runtime.cpp: support for NPRuntime API for Netscape Script-able plugins
3 * FYI: http://www.mozilla.org/projects/plugins/npruntime.html
4 *****************************************************************************
5 * Copyright (C) 2002-2005 the VideoLAN team
6 * $Id: RuntimeNPObject.h 14466 2006-02-22 23:34:54Z dionoea $
8 * Authors: Damien Fouilleul <damien.fouilleul@laposte.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #ifndef __NPORUNTIME_H__
26 #define __NPORUNTIME_H__
29 ** support framework for runtime script objects
33 #include <npruntime.h>
35 static void RuntimeNPClassDeallocate(NPObject *npobj);
36 static void RuntimeNPClassInvalidate(NPObject *npobj);
37 static bool RuntimeNPClassInvokeDefault(NPObject *npobj,
38 const NPVariant *args,
42 class RuntimeNPObject : public NPObject
50 static bool isNumberValue(const NPVariant &v)
52 return NPVARIANT_IS_INT32(v)
53 || NPVARIANT_IS_DOUBLE(v);
56 static int numberValue(const NPVariant &v)
59 case NPVariantType_Int32:
60 return NPVARIANT_TO_INT32(v);
61 case NPVariantType_Double:
62 return(int)NPVARIANT_TO_DOUBLE(v);
68 static char* stringValue(const NPString &v);
69 static char* stringValue(const NPVariant &v);
72 void *operator new(size_t n)
74 return NPN_MemAlloc(n);
77 void operator delete(void *p)
80 ** Some memory scribble happens occasionally on freed object
81 ** when used on Firefox (MacOS X) and may cause crash, a leak
82 ** sounds like the better option.
87 RuntimeNPObject(NPP instance, const NPClass *aClass) :
90 _class = const_cast<NPClass *>(aClass);
93 virtual ~RuntimeNPObject() {};
97 INVOKERESULT_NO_ERROR = 0, /* returns no error */
98 INVOKERESULT_GENERIC_ERROR = 1, /* returns error */
99 INVOKERESULT_NO_SUCH_METHOD = 2, /* throws method does not exist */
100 INVOKERESULT_INVALID_ARGS = 3, /* throws invalid arguments */
101 INVOKERESULT_INVALID_VALUE = 4, /* throws invalid value in assignment */
102 INVOKERESULT_OUT_OF_MEMORY = 5, /* throws out of memory */
105 friend void RuntimeNPClassDeallocate(NPObject *npobj);
106 friend void RuntimeNPClassInvalidate(NPObject *npobj);
107 template <class RuntimeNPObject> friend bool RuntimeNPClassGetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result);
108 template <class RuntimeNPObject> friend bool RuntimeNPClassSetProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value);
109 template <class RuntimeNPObject> friend bool RuntimeNPClassRemoveProperty(NPObject *npobj, NPIdentifier name);
110 template <class RuntimeNPObject> friend bool RuntimeNPClassInvoke(NPObject *npobj, NPIdentifier name,
111 const NPVariant *args, uint32_t argCount,
113 friend bool RuntimeNPClassInvokeDefault(NPObject *npobj,
114 const NPVariant *args,
118 virtual InvokeResult getProperty(int index, NPVariant &result);
119 virtual InvokeResult setProperty(int index, const NPVariant &value);
120 virtual InvokeResult removeProperty(int index);
121 virtual InvokeResult invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result);
122 virtual InvokeResult invokeDefault(const NPVariant *args, uint32_t argCount, NPVariant &result);
124 bool returnInvokeResult(InvokeResult result);
129 template<class T> class RuntimeNPClass : public NPClass
132 static NPClass *getClass()
134 static NPClass *singleton = new RuntimeNPClass<T>;
140 virtual ~RuntimeNPClass();
142 template <class RuntimeNPObject> friend NPObject *RuntimeNPClassAllocate(NPP instance, NPClass *aClass);
143 template <class RuntimeNPObject> friend bool RuntimeNPClassHasMethod(NPObject *npobj, NPIdentifier name);
144 template <class RuntimeNPObject> friend bool RuntimeNPClassHasProperty(NPObject *npobj, NPIdentifier name);
145 template <class RuntimeNPObject> friend bool RuntimeNPClassGetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result);
146 template <class RuntimeNPObject> friend bool RuntimeNPClassSetProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value);
147 template <class RuntimeNPObject> friend bool RuntimeNPClassRemoveProperty(NPObject *npobj, NPIdentifier name);
148 template <class RuntimeNPObject> friend bool RuntimeNPClassInvoke(NPObject *npobj, NPIdentifier name,
149 const NPVariant *args, uint32_t argCount,
152 RuntimeNPObject *create(NPP instance) const;
154 int indexOfMethod(NPIdentifier name) const;
155 int indexOfProperty(NPIdentifier name) const;
158 NPIdentifier *propertyIdentifiers;
159 NPIdentifier *methodIdentifiers;
163 static NPObject *RuntimeNPClassAllocate(NPP instance, NPClass *aClass)
165 const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(aClass);
166 return vClass->create(instance);
169 static void RuntimeNPClassDeallocate(NPObject *npobj)
171 RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
176 static void RuntimeNPClassInvalidate(NPObject *npobj)
178 RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
179 vObj->_instance = NULL;
183 static bool RuntimeNPClassHasMethod(NPObject *npobj, NPIdentifier name)
185 const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
186 return vClass->indexOfMethod(name) != -1;
190 static bool RuntimeNPClassHasProperty(NPObject *npobj, NPIdentifier name)
192 const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
193 return vClass->indexOfProperty(name) != -1;
197 static bool RuntimeNPClassGetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result)
199 RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
200 if( vObj->_instance )
202 const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
203 int index = vClass->indexOfProperty(name);
206 return vObj->returnInvokeResult(vObj->getProperty(index, *result));
213 static bool RuntimeNPClassSetProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value)
215 RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
216 if( vObj->_instance )
218 const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
219 int index = vClass->indexOfProperty(name);
222 return vObj->returnInvokeResult(vObj->setProperty(index, *value));
229 static bool RuntimeNPClassRemoveProperty(NPObject *npobj, NPIdentifier name)
231 RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
232 if( vObj->_instance )
234 const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
235 int index = vClass->indexOfProperty(name);
238 return vObj->returnInvokeResult(vObj->removeProperty(index));
245 static bool RuntimeNPClassInvoke(NPObject *npobj, NPIdentifier name,
246 const NPVariant *args, uint32_t argCount,
249 RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
250 if( vObj->_instance )
252 const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
253 int index = vClass->indexOfMethod(name);
256 return vObj->returnInvokeResult(vObj->invoke(index, args, argCount, *result));
263 static bool RuntimeNPClassInvokeDefault(NPObject *npobj,
264 const NPVariant *args,
268 RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
269 if( vObj->_instance )
271 return vObj->returnInvokeResult(vObj->invokeDefault(args, argCount, *result));
277 RuntimeNPClass<T>::RuntimeNPClass()
279 // retreive property identifiers from names
280 if( T::propertyCount > 0 )
282 propertyIdentifiers = new NPIdentifier[T::propertyCount];
283 if( propertyIdentifiers )
284 NPN_GetStringIdentifiers(const_cast<const NPUTF8**>(T::propertyNames),
285 T::propertyCount, propertyIdentifiers);
288 // retreive method identifiers from names
289 if( T::methodCount > 0 )
291 methodIdentifiers = new NPIdentifier[T::methodCount];
292 if( methodIdentifiers )
293 NPN_GetStringIdentifiers(const_cast<const NPUTF8**>(T::methodNames),
294 T::methodCount, methodIdentifiers);
297 // fill in NPClass structure
298 structVersion = NP_CLASS_STRUCT_VERSION;
299 allocate = &RuntimeNPClassAllocate<T>;
300 deallocate = &RuntimeNPClassDeallocate;
301 invalidate = &RuntimeNPClassInvalidate;
302 hasMethod = &RuntimeNPClassHasMethod<T>;
303 invoke = &RuntimeNPClassInvoke<T>;
304 invokeDefault = &RuntimeNPClassInvokeDefault;
305 hasProperty = &RuntimeNPClassHasProperty<T>;
306 getProperty = &RuntimeNPClassGetProperty<T>;
307 setProperty = &RuntimeNPClassSetProperty<T>;
308 removeProperty = &RuntimeNPClassRemoveProperty<T>;
312 RuntimeNPClass<T>::~RuntimeNPClass()
314 delete propertyIdentifiers;
315 delete methodIdentifiers;
319 RuntimeNPObject *RuntimeNPClass<T>::create(NPP instance) const
321 return new T(instance, this);
325 int RuntimeNPClass<T>::indexOfMethod(NPIdentifier name) const
327 if( methodIdentifiers )
329 for(int c=0; c< T::methodCount; ++c )
331 if( name == methodIdentifiers[c] )
339 int RuntimeNPClass<T>::indexOfProperty(NPIdentifier name) const
341 if( propertyIdentifiers )
343 for(int c=0; c< T::propertyCount; ++c )
345 if( name == propertyIdentifiers[c] )