Changeset View
Changeset View
Standalone View
Standalone View
source/scriptinterface/ScriptInterface.h
Show First 20 Lines • Show All 116 Lines • ▼ Show 20 Lines | public: | ||||
{ | { | ||||
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; | /** | ||||
* GetGeneralJSContext returns the context without starting a GC request and without | |||||
* entering the ScriptInterface compartment. It should only be used in specific situations, | |||||
* for instance when initializing a persistent rooted. | |||||
* If you need the compartmented context of the ScriptInterface, you should create a | |||||
* ScriptInterface::Request and use the context from that. | |||||
*/ | |||||
JSContext* GetGeneralJSContext() const; | |||||
shared_ptr<ScriptContext> GetContext() const; | shared_ptr<ScriptContext> GetContext() const; | ||||
/** | /** | ||||
* Load global scripts that most script interfaces need, | * Load global scripts that most script interfaces need, | ||||
* located in the /globalscripts directory. VFS must be initialized. | * located in the /globalscripts directory. VFS must be initialized. | ||||
*/ | */ | ||||
bool LoadGlobalScripts(); | bool LoadGlobalScripts(); | ||||
▲ Show 20 Lines • Show All 154 Lines • ▼ Show 20 Lines | public: | ||||
/** | /** | ||||
* Load and execute the given script in the global scope. | * Load and execute the given script in the global scope. | ||||
* @return true on successful compilation and execution; false otherwise | * @return true on successful compilation and execution; false otherwise | ||||
*/ | */ | ||||
bool LoadGlobalScriptFile(const VfsPath& path) const; | bool LoadGlobalScriptFile(const VfsPath& path) const; | ||||
/** | /** | ||||
* Construct a new value (usable in this ScriptInterface's compartment) by cloning | |||||
* a value from a different compartment. | |||||
* Complex values (functions, XML, etc) won't be cloned correctly, but basic | |||||
* types and cyclic references should be fine. | |||||
*/ | |||||
JS::Value CloneValueFromOtherCompartment(const ScriptInterface& otherCompartment, JS::HandleValue val) const; | |||||
/** | |||||
* Convert a JS::Value to a C++ type. (This might trigger GC.) | * Convert a JS::Value to a C++ type. (This might trigger GC.) | ||||
*/ | */ | ||||
template<typename T> static bool FromJSVal(const ScriptRequest& rq, const JS::HandleValue val, T& ret); | template<typename T> static bool FromJSVal(const ScriptRequest& rq, const JS::HandleValue val, T& ret); | ||||
/** | /** | ||||
* Convert a C++ type to a JS::Value. (This might trigger GC. The return | * Convert a C++ type to a JS::Value. (This might trigger GC. The return | ||||
* value must be rooted if you don't want it to be collected.) | * value must be rooted if you don't want it to be collected.) | ||||
* NOTE: We are passing the JS::Value by reference instead of returning it by value. | * NOTE: We are passing the JS::Value by reference instead of returning it by value. | ||||
Show All 17 Lines | public: | ||||
/** | /** | ||||
* Structured clones are a way to serialize 'simple' JS::Values into a buffer | * Structured clones are a way to serialize 'simple' JS::Values into a buffer | ||||
* that can safely be passed between compartments and between threads. | * that can safely be passed between compartments and between threads. | ||||
* A StructuredClone can be stored and read multiple times if desired. | * A StructuredClone can be stored and read multiple times if desired. | ||||
* We wrap them in shared_ptr so memory management is automatic and | * We wrap them in shared_ptr so memory management is automatic and | ||||
* thread-safe. | * thread-safe. | ||||
*/ | */ | ||||
class StructuredClone | using StructuredClone = shared_ptr<JSStructuredCloneData>; | ||||
{ | |||||
NONCOPYABLE(StructuredClone); | StructuredClone WriteStructuredClone(JS::HandleValue v, bool sameThread) const; | ||||
public: | void ReadStructuredClone(const StructuredClone& ptr, JS::MutableHandleValue ret) const; | ||||
StructuredClone(); | |||||
~StructuredClone(); | |||||
u64* m_Data; | |||||
size_t m_Size; | |||||
}; | |||||
shared_ptr<StructuredClone> WriteStructuredClone(JS::HandleValue v) const; | /** | ||||
void ReadStructuredClone(const shared_ptr<StructuredClone>& ptr, JS::MutableHandleValue ret) const; | * Construct a new value (usable in this ScriptInterface's compartment) by cloning | ||||
* a value from a different compartment. | |||||
* Complex values (functions, XML, etc) won't be cloned correctly, but basic | |||||
* types and cyclic references should be fine. | |||||
*/ | |||||
JS::Value CloneValueFromOtherCompartment(const ScriptInterface& otherCompartment, JS::HandleValue val, bool sameThread) const; | |||||
/** | /** | ||||
* Retrieve the private data field of a JSObject that is an instance of the given JSClass. | * Retrieve the private data field of a JSObject that is an instance of the given JSClass. | ||||
*/ | */ | ||||
template <typename T> | template <typename T> | ||||
static T* GetPrivate(const ScriptRequest& rq, JS::HandleObject thisobj, JSClass* jsClass) | static T* GetPrivate(const ScriptRequest& rq, JS::HandleObject thisobj, JSClass* jsClass) | ||||
{ | { | ||||
T* value = static_cast<T*>(JS_GetInstancePrivate(rq.cx, thisobj, jsClass, nullptr)); | T* value = static_cast<T*>(JS_GetInstancePrivate(rq.cx, thisobj, jsClass, nullptr)); | ||||
▲ Show 20 Lines • Show All 261 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator