2009-04-18 10:00:33 -07:00
|
|
|
/* Copyright (C) 2009 Wildfire Games.
|
|
|
|
|
* This file is part of 0 A.D.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2004-10-07 12:35:08 -07:00
|
|
|
// A generic type and some helper functions
|
|
|
|
|
// for scripts
|
|
|
|
|
|
2007-05-07 09:33:24 -07:00
|
|
|
#ifndef INCLUDED_SCRIPTOBJECT
|
|
|
|
|
#define INCLUDED_SCRIPTOBJECT
|
2004-10-07 12:35:08 -07:00
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|