Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/scriptinterface/ScriptInterface.h
Show First 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | |||||
// 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 | ||||
struct ScriptInterface_impl; | struct ScriptInterface_impl; | ||||
class ScriptRuntime; | class ScriptContext; | ||||
// Using a global object for the runtime is a workaround until Simulation, AI, etc, | // Using a global object for the context is a workaround until Simulation, AI, etc, | ||||
// use their own threads and also their own runtimes. | // use their own threads and also their own contexts. | ||||
extern thread_local shared_ptr<ScriptRuntime> g_ScriptRuntime; | extern thread_local shared_ptr<ScriptContext> g_ScriptContext; | ||||
/** | /** | ||||
* Abstraction around a SpiderMonkey JSCompartment. | * Abstraction around a SpiderMonkey JSCompartment. | ||||
* | * | ||||
* 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: | ||||
/** | /** | ||||
* 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 context ScriptContext 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<ScriptContext>& context); | ||||
~ScriptInterface(); | ~ScriptInterface(); | ||||
struct CmptPrivate | struct CmptPrivate | ||||
{ | { | ||||
ScriptInterface* pScriptInterface; // the ScriptInterface object the compartment belongs to | ScriptInterface* pScriptInterface; // the ScriptInterface object the compartment belongs to | ||||
void* pCBData; // meant to be used as the "this" object for callback functions | void* pCBData; // meant to be used as the "this" object for callback functions | ||||
} m_CmptPrivate; | } m_CmptPrivate; | ||||
void SetCallbackData(void* pCBData); | void SetCallbackData(void* pCBData); | ||||
static CmptPrivate* GetScriptInterfaceAndCBData(JSContext* cx); | static CmptPrivate* GetScriptInterfaceAndCBData(JSContext* cx); | ||||
JSRuntime* GetJSRuntime() const; | JSRuntime* GetJSRuntime() const; | ||||
shared_ptr<ScriptRuntime> GetRuntime() const; | shared_ptr<ScriptContext> GetContext() const; | ||||
/** | /** | ||||
* RAII structure which encapsulates an access to the context and compartment of a ScriptInterface. | * RAII structure which encapsulates an access to the context and compartment of a ScriptInterface. | ||||
* This struct provides: | * This struct provides: | ||||
* - a pointer to the context, while acting like JSAutoRequest | * - a pointer to the context, while acting like JSAutoRequest | ||||
* - a pointer to the global object of the compartment, while acting like JSAutoCompartment | * - a pointer to the global object of the compartment, while acting like JSAutoCompartment | ||||
* | * | ||||
* This way, getting and using those pointers is safe with respect to the GC | * This way, getting and using those pointers is safe with respect to the GC | ||||
▲ Show 20 Lines • Show All 514 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator