2004-10-07 12:35:08 -07:00
|
|
|
// A generic type and some helper functions
|
|
|
|
|
// for scripts
|
|
|
|
|
|
|
|
|
|
// Mark Thompson (mark@wildfiregames.com / mot20@cam.ac.uk)
|
|
|
|
|
|
|
|
|
|
#ifndef SCRIPTOBJECT_INCLUDED
|
|
|
|
|
#define SCRIPTOBJECT_INCLUDED
|
|
|
|
|
|
2006-11-11 20:02:36 -08:00
|
|
|
#include "scripting/SpiderMonkey.h"
|
|
|
|
|
|
2006-06-09 11:32:00 -07:00
|
|
|
class CStrW;
|
|
|
|
|
class CScriptEvent;
|
2004-10-07 12:35:08 -07:00
|
|
|
|
2006-09-30 08:46:40 -07:00
|
|
|
#include "scripting/SpiderMonkey.h"
|
|
|
|
|
|
2004-10-07 12:35:08 -07:00
|
|
|
class CScriptObject
|
|
|
|
|
{
|
|
|
|
|
JSFunction* Function;
|
2005-01-17 16:46:18 -08:00
|
|
|
JSObject* FunctionObject;
|
|
|
|
|
void Root();
|
|
|
|
|
void Uproot();
|
|
|
|
|
|
2004-10-23 07:39:28 -07:00
|
|
|
public:
|
2004-10-07 12:35:08 -07:00
|
|
|
|
|
|
|
|
CScriptObject();
|
|
|
|
|
CScriptObject( JSFunction* _Function );
|
|
|
|
|
CScriptObject( jsval v );
|
2005-05-10 00:13:25 -07:00
|
|
|
CScriptObject( const CScriptObject& copy );
|
2004-10-23 07:39:28 -07:00
|
|
|
|
2005-01-17 16:46:18 -08:00
|
|
|
~CScriptObject();
|
|
|
|
|
|
2004-10-23 07:39:28 -07:00
|
|
|
// Initialize in various ways: from a JS function, a string to be compiled, or a jsval containing either.
|
2004-10-07 12:35:08 -07:00
|
|
|
void SetFunction( JSFunction* _Function );
|
|
|
|
|
void SetJSVal( jsval v );
|
2006-06-09 11:32:00 -07:00
|
|
|
void Compile( const CStrW& FileNameTag, const CStrW& FunctionBody );
|
2004-10-23 07:39:28 -07:00
|
|
|
|
2005-01-16 20:52:02 -08:00
|
|
|
inline bool Defined()
|
|
|
|
|
{
|
|
|
|
|
return( Function != NULL );
|
|
|
|
|
}
|
2004-10-23 07:39:28 -07:00
|
|
|
|
2005-04-22 00:12:55 -07:00
|
|
|
inline operator bool() { return( Function != NULL ); }
|
|
|
|
|
inline bool operator!() { return( !Function ); }
|
|
|
|
|
inline bool operator==( const CScriptObject& compare ) { return( Function == compare.Function ); }
|
|
|
|
|
|
2004-10-23 07:39:28 -07:00
|
|
|
// JSObject wrapping the function if it's defined, NULL if it isn't.
|
|
|
|
|
JSObject* GetFunctionObject();
|
|
|
|
|
|
2004-10-07 12:35:08 -07:00
|
|
|
// Executes a script attached to a JS object.
|
|
|
|
|
// Returns false if the script isn't defined, if the script can't be executed,
|
|
|
|
|
// otherwise true. Script return value is in rval.
|
2005-04-22 00:12:55 -07:00
|
|
|
bool Run( JSObject* Context, jsval* rval, uintN argc = 0, jsval* argv = NULL );
|
2004-10-07 12:35:08 -07:00
|
|
|
// This variant casts script return value to a boolean, and passes it back.
|
2005-04-22 00:12:55 -07:00
|
|
|
bool Run( JSObject* Context, uintN argc = 0, jsval* argv = NULL );
|
2004-10-23 07:39:28 -07:00
|
|
|
|
|
|
|
|
// Treat this as an event handler and dispatch an event to it.
|
2004-11-10 23:09:32 -08:00
|
|
|
bool DispatchEvent( JSObject* Context, CScriptEvent* evt );
|
2004-10-07 12:35:08 -07:00
|
|
|
};
|
|
|
|
|
|
2004-11-07 13:30:47 -08:00
|
|
|
#endif
|