Changeset View
Changeset View
Standalone View
Standalone View
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 | ||||
class JSStructuredCloneData; | |||||
class ScriptInterface; | class ScriptInterface; | ||||
struct ScriptInterface_impl; | struct ScriptInterface_impl; | ||||
class ScriptContext; | class ScriptContext; | ||||
// Using a global object for the context 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 contexts. | // use their own threads and also their own contexts. | ||||
extern thread_local shared_ptr<ScriptContext> g_ScriptContext; | extern thread_local shared_ptr<ScriptContext> g_ScriptContext; | ||||
▲ Show 20 Lines • Show All 249 Lines • ▼ Show 20 Lines | public: | ||||
* MathRandom (this function) calls the random number generator assigned to this ScriptInterface instance and | * MathRandom (this function) calls the random number generator assigned to this ScriptInterface instance and | ||||
* returns the generated number. | * returns the generated number. | ||||
* Math_random (with underscore, not this function) is a global function, but different random number generators can be | * Math_random (with underscore, not this function) is a global function, but different random number generators can be | ||||
* stored per ScriptInterface. It calls MathRandom of the current ScriptInterface instance. | * stored per ScriptInterface. It calls MathRandom of the current ScriptInterface instance. | ||||
*/ | */ | ||||
bool MathRandom(double& nbr); | bool MathRandom(double& nbr); | ||||
/** | /** | ||||
* Structured clones are a way to serialize 'simple' JS::Values into a buffer | |||||
* that can safely be passed between compartments and between threads. | |||||
* A StructuredClone can be stored and read multiple times if desired. | |||||
* We wrap them in shared_ptr so memory management is automatic and | |||||
* thread-safe. | |||||
*/ | |||||
using StructuredClone = shared_ptr<JSStructuredCloneData>; | |||||
StructuredClone WriteStructuredClone(JS::HandleValue v) const; | |||||
void ReadStructuredClone(const 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) 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)); | ||||
if (value == nullptr) | if (value == nullptr) | ||||
▲ Show 20 Lines • Show All 223 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator