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
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)
75 ** Assume that browser has a smarter memory allocator
76 ** than plain old malloc() and use it instead.
78 return NPN_MemAlloc(n);
81 void operator delete(void *p)
88 return _instance != NULL;
91 RuntimeNPObject(NPP instance, const NPClass *aClass) :
94 _class = const_cast<NPClass *>(aClass);
97 virtual ~RuntimeNPObject() {};
101 INVOKERESULT_NO_ERROR = 0, /* returns no error */
102 INVOKERESULT_GENERIC_ERROR = 1, /* returns error */
103 INVOKERESULT_NO_SUCH_METHOD = 2, /* throws method does not exist */
104 INVOKERESULT_INVALID_ARGS = 3, /* throws invalid arguments */
105 INVOKERESULT_INVALID_VALUE = 4, /* throws invalid value in assignment */
106 INVOKERESULT_OUT_OF_MEMORY = 5, /* throws out of memory */
109 friend void RuntimeNPClassDeallocate(NPObject *npobj);
110 friend void RuntimeNPClassInvalidate(NPObject *npobj);
111 template <class RuntimeNPObject> friend bool RuntimeNPClassGetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result);
112 template <class RuntimeNPObject> friend bool RuntimeNPClassSetProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value);
113 template <class RuntimeNPObject> friend bool RuntimeNPClassRemoveProperty(NPObject *npobj, NPIdentifier name);
114 template <class RuntimeNPObject> friend bool RuntimeNPClassInvoke(NPObject *npobj, NPIdentifier name,
115 const NPVariant *args, uint32_t argCount,
117 friend bool RuntimeNPClassInvokeDefault(NPObject *npobj,
118 const NPVariant *args,
122 virtual InvokeResult getProperty(int index, NPVariant &result);
123 virtual InvokeResult setProperty(int index, const NPVariant &value);
124 virtual InvokeResult removeProperty(int index);
125 virtual InvokeResult invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result);
126 virtual InvokeResult invokeDefault(const NPVariant *args, uint32_t argCount, NPVariant &result);
128 bool returnInvokeResult(InvokeResult result);
133 template<class T> class RuntimeNPClass : public NPClass
136 static NPClass *getClass()
138 static NPClass *singleton = new RuntimeNPClass<T>;
144 virtual ~RuntimeNPClass();
146 template <class RuntimeNPObject> friend NPObject *RuntimeNPClassAllocate(NPP instance, NPClass *aClass);
147 template <class RuntimeNPObject> friend bool RuntimeNPClassHasMethod(NPObject *npobj, NPIdentifier name);
148 template <class RuntimeNPObject> friend bool RuntimeNPClassHasProperty(NPObject *npobj, NPIdentifier name);
149 template <class RuntimeNPObject> friend bool RuntimeNPClassGetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result);
150 template <class RuntimeNPObject> friend bool RuntimeNPClassSetProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value);
151 template <class RuntimeNPObject> friend bool RuntimeNPClassRemoveProperty(NPObject *npobj, NPIdentifier name);
152 template <class RuntimeNPObject> friend bool RuntimeNPClassInvoke(NPObject *npobj, NPIdentifier name,
153 const NPVariant *args, uint32_t argCount,
156 RuntimeNPObject *create(NPP instance) const;
158 int indexOfMethod(NPIdentifier name) const;
159 int indexOfProperty(NPIdentifier name) const;
162 NPIdentifier *propertyIdentifiers;
163 NPIdentifier *methodIdentifiers;
167 static NPObject *RuntimeNPClassAllocate(NPP instance, NPClass *aClass)
169 const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(aClass);
170 return vClass->create(instance);
173 static void RuntimeNPClassDeallocate(NPObject *npobj)
175 RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
180 static void RuntimeNPClassInvalidate(NPObject *npobj)
182 RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
183 vObj->_instance = NULL;
187 static bool RuntimeNPClassHasMethod(NPObject *npobj, NPIdentifier name)
189 const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
190 return vClass->indexOfMethod(name) != -1;
194 static bool RuntimeNPClassHasProperty(NPObject *npobj, NPIdentifier name)
196 const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
197 return vClass->indexOfProperty(name) != -1;
201 static bool RuntimeNPClassGetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result)
203 RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
204 if( vObj->isValid() )
206 const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
207 int index = vClass->indexOfProperty(name);
210 return vObj->returnInvokeResult(vObj->getProperty(index, *result));
217 static bool RuntimeNPClassSetProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value)
219 RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
220 if( vObj->isValid() )
222 const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
223 int index = vClass->indexOfProperty(name);
226 return vObj->returnInvokeResult(vObj->setProperty(index, *value));
233 static bool RuntimeNPClassRemoveProperty(NPObject *npobj, NPIdentifier name)
235 RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
236 if( vObj->isValid() )
238 const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
239 int index = vClass->indexOfProperty(name);
242 return vObj->returnInvokeResult(vObj->removeProperty(index));
249 static bool RuntimeNPClassInvoke(NPObject *npobj, NPIdentifier name,
250 const NPVariant *args, uint32_t argCount,
253 RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
254 if( vObj->isValid() )
256 const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class);
257 int index = vClass->indexOfMethod(name);
260 return vObj->returnInvokeResult(vObj->invoke(index, args, argCount, *result));
267 static bool RuntimeNPClassInvokeDefault(NPObject *npobj,
268 const NPVariant *args,
272 RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
273 if( vObj->isValid() )
275 return vObj->returnInvokeResult(vObj->invokeDefault(args, argCount, *result));
281 RuntimeNPClass<T>::RuntimeNPClass()
283 // retreive property identifiers from names
284 if( T::propertyCount > 0 )
286 propertyIdentifiers = new NPIdentifier[T::propertyCount];
287 if( propertyIdentifiers )
288 NPN_GetStringIdentifiers(const_cast<const NPUTF8**>(T::propertyNames),
289 T::propertyCount, propertyIdentifiers);
292 // retreive method identifiers from names
293 if( T::methodCount > 0 )
295 methodIdentifiers = new NPIdentifier[T::methodCount];
296 if( methodIdentifiers )
297 NPN_GetStringIdentifiers(const_cast<const NPUTF8**>(T::methodNames),
298 T::methodCount, methodIdentifiers);
301 // fill in NPClass structure
302 structVersion = NP_CLASS_STRUCT_VERSION;
303 allocate = &RuntimeNPClassAllocate<T>;
304 deallocate = &RuntimeNPClassDeallocate;
305 invalidate = &RuntimeNPClassInvalidate;
306 hasMethod = &RuntimeNPClassHasMethod<T>;
307 invoke = &RuntimeNPClassInvoke<T>;
308 invokeDefault = &RuntimeNPClassInvokeDefault;
309 hasProperty = &RuntimeNPClassHasProperty<T>;
310 getProperty = &RuntimeNPClassGetProperty<T>;
311 setProperty = &RuntimeNPClassSetProperty<T>;
312 removeProperty = &RuntimeNPClassRemoveProperty<T>;
316 RuntimeNPClass<T>::~RuntimeNPClass()
318 delete propertyIdentifiers;
319 delete methodIdentifiers;
323 RuntimeNPObject *RuntimeNPClass<T>::create(NPP instance) const
325 return new T(instance, this);
329 int RuntimeNPClass<T>::indexOfMethod(NPIdentifier name) const
331 if( methodIdentifiers )
333 for(int c=0; c< T::methodCount; ++c )
335 if( name == methodIdentifiers[c] )
343 int RuntimeNPClass<T>::indexOfProperty(NPIdentifier name) const
345 if( propertyIdentifiers )
347 for(int c=0; c< T::propertyCount; ++c )
349 if( name == propertyIdentifiers[c] )