23 #ifndef INCLUDED_SCRIPTABLEOBJECT
24 #define INCLUDED_SCRIPTABLEOBJECT
26 #include <boost/unordered_map.hpp>
32 #define ALLOW_NONSHARED_NATIVES
41 virtual void Set( JSContext* cx,
IJSObject* obj, jsval value ) = 0;
57 virtual bool GetProperty( JSContext* cx,
const CStrW& PropertyName, jsval* vp ) = 0;
60 virtual void AddProperty(
const CStrW& PropertyName, jsval Value ) = 0;
66 template<
typename T,
bool ReadOnly = false>
class CJSObject;
88 #ifdef ALLOW_NONSHARED_NATIVES
141 template<
typename Q,
bool ReadOnly>
friend class CJSObject;
157 if( JSVAL_IS_GCTHING(
m_Data ) )
162 if( JSVAL_IS_GCTHING(
m_Data ))
179 template<
typename T,
bool ReadOnly,
typename RType, RType (T::*NativeFunction)( JSContext* cx, u
intN argc, jsval* argv )>
class CNativeFunction
182 static JSBool
JSFunction( JSContext* cx, uintN argc, jsval* vp )
184 T* Native = ToNative<T>( cx, JS_THIS_OBJECT( cx, vp ) );
188 jsval rval = ToJSVal<RType>( (Native->*NativeFunction)( cx, argc, JS_ARGV(cx, vp) ) );
189 JS_SET_RVAL( cx, vp, rval );
195 template<
typename T,
bool ReadOnly,
void (T::*NativeFunction)( JSContext* cx, u
intN argc, jsval* argv )>
199 static JSBool
JSFunction( JSContext* cx, uintN argc, jsval* vp )
201 T* Native = ToNative<T>( cx, JS_THIS_OBJECT( cx, vp ) );
205 (Native->*NativeFunction)( cx, argc, JS_ARGV(cx, vp) );
207 JS_SET_RVAL( cx, vp, JSVAL_VOID );
220 #ifdef ALLOW_NONSHARED_NATIVES
235 typedef void (
T::*
TSetFn)( JSContext*, jsval value );
239 static void ScriptingInit(
const char* ClassName, JSNative Constructor = NULL, uintN ConstructorMinArgs = 0 )
243 for( MethodID = 0; MethodID <
m_Methods.size(); ++MethodID )
244 JSI_methods[MethodID] =
m_Methods[MethodID];
246 JSI_methods[MethodID].name = 0;
257 PropertyTable::iterator it;
259 delete( it->second );
264 bool GetProperty( JSContext* cx,
const CStrW& PropertyName, jsval* vp )
268 *vp = Property->
Get( cx,
this );
272 void SetProperty( JSContext* cx,
const CStrW& PropertyName, jsval* vp )
281 prop->
Set( cx,
this, *vp );
293 PropertyTable::iterator it;
298 return( it->second );
301 #ifdef ALLOW_NONSHARED_NATIVES
304 return( it->second );
309 return( it->second );
325 template<
typename ReturnType, ReturnType (T::*NativeFunction)( JSContext* cx, u
intN argc, jsval* argv )>
326 static void AddMethod(
const char* Name, uintN MinArgs )
331 template<
typename PropType>
static void AddProperty(
const CStrW& PropertyName, PropType
T::*Native,
bool PropReadOnly = ReadOnly )
344 #ifdef ALLOW_NONSHARED_NATIVES
345 template<
typename PropType>
void AddLocalProperty(
const CStrW& PropertyName, PropType* Native,
bool PropReadOnly = ReadOnly )
393 static JSBool
JSGetProperty( JSContext* cx, JSObject* obj, jsid
id, jsval* vp )
395 T* Instance = ToNative<T>( cx, obj );
400 if( !JS_IdToValue( cx,
id, &idval ) )
405 if( !Instance->GetProperty( cx, PropName, vp ) )
412 T* Instance = ToNative<T>( cx, obj );
417 if( !JS_IdToValue( cx,
id, &idval ) )
422 Instance->SetProperty( cx, PropName, vp );
428 T* Instance = ToNative<T>( cx, obj );
429 if( !Instance || Instance->m_EngineOwned )
433 JS_SetPrivate( cx, obj, NULL );
453 PropertyTable::iterator it;
455 delete( it->second );
458 #ifdef ALLOW_NONSHARED_NATIVES
460 delete( it->second );
467 NULL, JSCLASS_HAS_PRIVATE,
468 JS_PropertyStub, JS_PropertyStub,
470 JS_EnumerateStub, JS_ResolveStub,
472 NULL, NULL, NULL, NULL,
473 NULL, NULL, NULL, NULL
477 { NULL, 0, 0, NULL, NULL },
CJSFunctionProperty(IJSObject::GetFn Getter, IJSObject::SetFn Setter)
void SetProperty(JSContext *cx, const CStrW &PropertyName, jsval *vp)
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
CJSProperty(T IJSObject::*Data)
PropertyTable m_NonsharedProperties
static JSBool JSFunction(JSContext *cx, uintN argc, jsval *vp)
IJSProperty * HasProperty(const CStrW &PropertyName)
CJSValProperty(jsval Data)
virtual void Set(JSContext *cx, IJSObject *obj, jsval value)=0
void CreateScriptObject()
static JSBool JSSetProperty(JSContext *cx, JSObject *obj, jsid id, JSBool strict, jsval *vp)
static void ScriptingInit(const char *ClassName, JSNative Constructor=NULL, uintN ConstructorMinArgs=0)
jsval Get(JSContext *cx, IJSObject *obj)
void ReleaseScriptObject()
virtual jsval Get(JSContext *cx, IJSObject *obj)=0
void Set(JSContext *cx, IJSObject *owner, jsval Value)
static void ScriptingShutdown()
static void AddProperty(const CStrW &PropertyName, PropType T::*Native, bool PropReadOnly=ReadOnly)
IJSObject::GetFn m_Getter
static std::vector< JSFunctionSpec > m_Methods
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
CJSNonsharedProperty(T *Data)
jsval Get(JSContext *cx, IJSObject *object)
static JSBool JSFunction(JSContext *cx, uintN argc, jsval *vp)
void(T::* TSetFn)(JSContext *, jsval value)
static void AddProperty(const CStrW &PropertyName, TGetFn Getter, TSetFn Setter=NULL)
void AddLocalProperty(const CStrW &PropertyName, PropType *Native, bool PropReadOnly=ReadOnly)
static void DefaultFinalize(JSContext *cx, JSObject *obj)
JSFunctionSpec JSI_methods[]
boost::unordered_map< CStrW, IJSProperty * > PropertyTable
bool GetProperty(JSContext *cx, const CStrW &PropertyName, jsval *vp)
void(IJSObject::* SetFn)(JSContext *cx, jsval value)
bool ToPrimitive(JSContext *cx, jsval v, T &Storage)
void Set(JSContext *cx, IJSObject *owner, jsval Value)
jsval Get(JSContext *cx, IJSObject *owner)
#define T(string_literal)
virtual bool GetProperty(JSContext *cx, const CStrW &PropertyName, jsval *vp)=0
void AddProperty(const CStrW &PropertyName, jsval Value)
jsval(IJSObject::* GetFn)(JSContext *cx)
void Set(JSContext *cx, IJSObject *owner, jsval value)
static PropertyTable m_NativeProperties
static void AddMethod(const char *Name, uintN MinArgs)
jsval Get(JSContext *cx, IJSObject *owner)
void Set(JSContext *cx, IJSObject *obj, jsval value)
jsval(T::* TGetFn)(JSContext *)
static JSPropertySpec JSI_props[]
static JSBool JSGetProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
PropertyTable m_ScriptProperties
virtual IJSProperty * HasProperty(const CStrW &PropertyName)=0
virtual void AddProperty(const CStrW &PropertyName, jsval Value)=0
IJSObject::SetFn m_Setter