Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/scriptinterface/ScriptInterface.h
Show First 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | |||||
ERROR_TYPE(Scripting_DefineType, AlreadyExists); | ERROR_TYPE(Scripting_DefineType, AlreadyExists); | ||||
ERROR_TYPE(Scripting_DefineType, CreationFailed); | ERROR_TYPE(Scripting_DefineType, CreationFailed); | ||||
// Set the maximum number of function arguments that can be handled | // Set the maximum number of function arguments that can be handled | ||||
// (This should be as small as possible (for compiler efficiency), | // (This should be as small as possible (for compiler efficiency), | ||||
// but as large as necessary for all wrapped functions) | // but as large as necessary for all wrapped functions) | ||||
#define SCRIPT_INTERFACE_MAX_ARGS 8 | #define SCRIPT_INTERFACE_MAX_ARGS 8 | ||||
// TODO: what's a good default? | |||||
#define DEFAULT_RUNTIME_SIZE 16 * 1024 * 1024 | |||||
#define DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER 2 * 1024 *1024 | |||||
struct ScriptInterface_impl; | struct ScriptInterface_impl; | ||||
class ScriptRuntime; | class ScriptRuntime; | ||||
extern shared_ptr<ScriptRuntime> g_ScriptRuntime; | // Using a global object for the runtime is a workaround until Simulation, AI, etc, | ||||
// use their own threads and also their own runtimes. | |||||
extern thread_local shared_ptr<ScriptRuntime> g_ScriptRuntime; | |||||
/** | /** | ||||
* Abstraction around a SpiderMonkey JSContext. | * Abstraction around a SpiderMonkey JSContext. | ||||
* | * | ||||
* Thread-safety: | * Thread-safety: | ||||
* - May be used in non-main threads. | * - May be used in non-main threads. | ||||
* - Each ScriptInterface must be created, used, and destroyed, all in a single thread | * - Each ScriptInterface must be created, used, and destroyed, all in a single thread | ||||
* (it must never be shared between threads). | * (it must never be shared between threads). | ||||
*/ | */ | ||||
class ScriptInterface | class ScriptInterface | ||||
{ | { | ||||
NONCOPYABLE(ScriptInterface); | NONCOPYABLE(ScriptInterface); | ||||
public: | public: | ||||
/** | /** | ||||
* Returns a runtime, which can used to initialise any number of | |||||
* ScriptInterfaces contexts. Values created in one context may be used | |||||
* in any other context from the same runtime (but not any other runtime). | |||||
* Each runtime should only ever be used on a single thread. | |||||
* @param runtimeSize Maximum size in bytes of the new runtime | |||||
*/ | |||||
static shared_ptr<ScriptRuntime> CreateRuntime(shared_ptr<ScriptRuntime> parentRuntime = shared_ptr<ScriptRuntime>(), int runtimeSize = DEFAULT_RUNTIME_SIZE, | |||||
int heapGrowthBytesGCTrigger = DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER); | |||||
/** | |||||
* Constructor. | * Constructor. | ||||
* @param nativeScopeName Name of global object that functions (via RegisterFunction) will | * @param nativeScopeName Name of global object that functions (via RegisterFunction) will | ||||
* be placed into, as a scoping mechanism; typically "Engine" | * be placed into, as a scoping mechanism; typically "Engine" | ||||
* @param debugName Name of this interface for CScriptStats purposes. | * @param debugName Name of this interface for CScriptStats purposes. | ||||
* @param runtime ScriptRuntime to use when initializing this interface. | * @param runtime ScriptRuntime to use when initializing this interface. | ||||
*/ | */ | ||||
ScriptInterface(const char* nativeScopeName, const char* debugName, const shared_ptr<ScriptRuntime>& runtime); | ScriptInterface(const char* nativeScopeName, const char* debugName, const shared_ptr<ScriptRuntime>& runtime); | ||||
▲ Show 20 Lines • Show All 341 Lines • ▼ Show 20 Lines | private: | ||||
{ | { | ||||
JS::PersistentRootedObject m_Prototype; | JS::PersistentRootedObject m_Prototype; | ||||
JSClass* m_Class; | JSClass* m_Class; | ||||
JSNative m_Constructor; | JSNative m_Constructor; | ||||
}; | }; | ||||
void Register(const char* name, JSNative fptr, size_t nargs) const; | void Register(const char* name, JSNative fptr, size_t nargs) const; | ||||
// Take care to keep this declaration before heap rooted members. Destructors of heap rooted | // Take care to keep this declaration before heap rooted members. Destructors of heap rooted | ||||
// members have to be called before the runtime destructor. | // members have to be called before the custom destructor of ScriptInterface_impl. | ||||
std::unique_ptr<ScriptInterface_impl> m; | std::unique_ptr<ScriptInterface_impl> m; | ||||
boost::rand48* m_rng; | boost::rand48* m_rng; | ||||
std::map<std::string, CustomType> m_CustomObjectTypes; | std::map<std::string, CustomType> m_CustomObjectTypes; | ||||
// The nasty macro/template bits are split into a separate file so you don't have to look at them | // The nasty macro/template bits are split into a separate file so you don't have to look at them | ||||
public: | public: | ||||
#include "NativeWrapperDecls.h" | #include "NativeWrapperDecls.h" | ||||
▲ Show 20 Lines • Show All 160 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator