2009-04-18 19:00:33 +02: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-05-30 02:46:58 +02:00
|
|
|
|
2007-05-07 18:33:24 +02:00
|
|
|
#ifndef INCLUDED_SCRIPTINGHOST
|
|
|
|
#define INCLUDED_SCRIPTINGHOST
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2005-01-07 01:47:44 +01:00
|
|
|
#include "ps/Errors.h"
|
2004-07-31 17:57:18 +02:00
|
|
|
|
2004-07-18 13:51:15 +02:00
|
|
|
ERROR_GROUP(Scripting);
|
2005-07-22 20:41:49 +02:00
|
|
|
ERROR_TYPE(Scripting, SetupFailed);
|
2004-07-18 13:51:15 +02:00
|
|
|
|
|
|
|
ERROR_SUBGROUP(Scripting, LoadFile);
|
|
|
|
ERROR_TYPE(Scripting_LoadFile, OpenFailed);
|
|
|
|
ERROR_TYPE(Scripting_LoadFile, EvalErrors);
|
|
|
|
|
|
|
|
ERROR_TYPE(Scripting, ConversionFailed);
|
|
|
|
ERROR_TYPE(Scripting, CallFunctionFailed);
|
|
|
|
ERROR_TYPE(Scripting, RegisterFunctionFailed);
|
|
|
|
ERROR_TYPE(Scripting, DefineConstantFailed);
|
|
|
|
ERROR_TYPE(Scripting, CreateObjectFailed);
|
|
|
|
ERROR_TYPE(Scripting, TypeDoesNotExist);
|
|
|
|
|
|
|
|
ERROR_SUBGROUP(Scripting, DefineType);
|
|
|
|
ERROR_TYPE(Scripting_DefineType, AlreadyExists);
|
|
|
|
ERROR_TYPE(Scripting_DefineType, CreationFailed);
|
2004-07-17 19:09:33 +02:00
|
|
|
|
2005-06-22 04:23:46 +02:00
|
|
|
#include "scripting/SpiderMonkey.h"
|
2009-11-03 22:46:35 +01:00
|
|
|
#include "lib/file/vfs/vfs_path.h"
|
2004-09-04 16:43:23 +02:00
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "ps/Singleton.h"
|
|
|
|
#include "ps/CStr.h"
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
class ScriptInterface;
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
class IPropertyOwner
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
class ScriptingHost : public Singleton < ScriptingHost >
|
|
|
|
{
|
|
|
|
private:
|
2010-01-09 20:20:14 +01:00
|
|
|
class CustomType
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
JSObject * m_Object;
|
|
|
|
JSClass * m_Class;
|
|
|
|
};
|
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
JSContext * m_Context;
|
|
|
|
JSObject * m_GlobalObject;
|
|
|
|
|
|
|
|
JSErrorReport m_ErrorReport;
|
|
|
|
|
|
|
|
std::map < std::string, CustomType > m_CustomObjectTypes;
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
// The long-term plan is to migrate from ScriptingHost to the newer shinier ScriptInterface.
|
|
|
|
// For now, just have a ScriptInterface that hooks onto the ScriptingHost's context so they
|
|
|
|
// can both be used.
|
|
|
|
ScriptInterface* m_ScriptInterface;
|
2004-05-30 02:46:58 +02:00
|
|
|
public:
|
|
|
|
|
|
|
|
ScriptingHost();
|
|
|
|
~ScriptingHost();
|
2007-06-03 17:37:26 +02:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
ScriptInterface& GetScriptInterface();
|
|
|
|
|
2007-06-03 17:37:26 +02:00
|
|
|
static void FinalShutdown();
|
2004-06-11 00:24:03 +02:00
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
// Helpers:
|
|
|
|
|
2004-12-21 19:41:58 +01:00
|
|
|
// TODO: Remove one of these
|
|
|
|
inline JSContext *getContext() { return m_Context; }
|
|
|
|
inline JSContext *GetContext() { return m_Context; }
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2005-04-15 06:23:33 +02:00
|
|
|
inline JSObject* GetGlobalObject() { return m_GlobalObject; }
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2005-08-09 17:55:44 +02:00
|
|
|
void RunMemScript(const char* script, size_t size, const char* filename = 0, int line = 0, JSObject* globalObject = 0);
|
2009-11-03 22:46:35 +01:00
|
|
|
void RunScript(const VfsPath& filename, JSObject* globalObject = 0);
|
2005-08-09 17:55:44 +02:00
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
|
|
|
|
jsval CallFunction(const std::string & functionName, jsval * params, int numParams);
|
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
jsval ExecuteScript(const CStrW& script, const CStrW& calledFrom = L"Console", JSObject* contextObject = NULL );
|
2004-05-30 02:46:58 +02:00
|
|
|
|
|
|
|
void DefineConstant(const std::string & name, int value);
|
|
|
|
|
|
|
|
void DefineCustomObjectType(JSClass *clasp, JSNative constructor, uintN nargs, JSPropertySpec *ps, JSFunctionSpec *fs, JSPropertySpec *static_ps, JSFunctionSpec *static_fs);
|
|
|
|
|
|
|
|
JSObject * CreateCustomObject(const std::string & typeName);
|
|
|
|
|
2004-07-07 12:52:45 +02:00
|
|
|
void SetObjectProperty(JSObject * object, const std::string & propertyName, jsval value);
|
|
|
|
jsval GetObjectProperty(JSObject * object, const std::string & propertyName);
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2004-09-03 16:10:48 +02:00
|
|
|
void SetObjectProperty_Double(JSObject* object, const char* propertyName, double value);
|
|
|
|
double GetObjectProperty_Double(JSObject* object, const char* propertyName);
|
|
|
|
|
2004-07-04 17:35:04 +02:00
|
|
|
void SetGlobal(const std::string& globalName, jsval value);
|
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
std::string ValueToString(const jsval value);
|
2010-01-09 20:20:14 +01:00
|
|
|
CStrW ValueToUCString(const jsval value);
|
2006-07-20 16:37:58 +02:00
|
|
|
jsval UCStringToValue(const CStrW& str);
|
2004-05-30 02:46:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#define g_ScriptingHost ScriptingHost::GetSingleton()
|
|
|
|
|
|
|
|
#endif
|