18 #ifndef INCLUDED_SCRIPTINTERFACE 19 #define INCLUDED_SCRIPTINTERFACE 42 ERROR_TYPE(Scripting_DefineType, AlreadyExists);
43 ERROR_TYPE(Scripting_DefineType, CreationFailed);
48 #define SCRIPT_INTERFACE_MAX_ARGS 8 58 namespace boost {
namespace random {
class rand48; } }
86 ScriptInterface(
const char* nativeScopeName,
const char* debugName,
const std::shared_ptr<ScriptContext>& context);
105 static void* GetCBData(JSContext* cx);
116 template <
typename T>
119 static_assert(!std::is_same_v<void, T>);
121 return static_cast<T*
>(ObjectFromCBData<void>(rq));
127 template <
typename T>
130 return ObjectFromCBData<T>(rq);
141 std::shared_ptr<ScriptContext>
GetContext()
const;
160 void CallConstructor(JS::HandleValue ctor, JS::HandleValueArray argv, JS::MutableHandleValue
out)
const;
163 void DefineCustomObjectType(JSClass *clasp, JSNative constructor,
uint minArgs, JSPropertySpec *ps, JSFunctionSpec *
fs, JSPropertySpec *static_ps, JSFunctionSpec *static_fs);
171 bool SetGlobal(
const char* name,
const T& value,
bool replace =
false,
bool constant =
true,
bool enumerate =
true);
182 bool SetPrototype(JS::HandleValue obj, JS::HandleValue proto);
210 bool Eval(
const char* code)
const;
211 bool Eval(
const char* code, JS::MutableHandleValue out)
const;
212 template<
typename T>
bool Eval(
const char* code,
T& out)
const;
227 template <
typename T>
230 T* value =
static_cast<T*
>(JS_GetInstancePrivate(rq.
cx, thisobj, jsClass,
nullptr));
232 if (value ==
nullptr)
242 template <
typename T>
245 if (!callArgs.thisv().isObject())
251 JS::RootedObject thisObj(rq.
cx, &callArgs.thisv().toObject());
252 T* value =
static_cast<T*
>(JS_GetInstancePrivate(rq.
cx, thisObj, jsClass, &callArgs));
254 if (value ==
nullptr)
261 bool SetGlobal_(
const char* name, JS::HandleValue value,
bool replace,
bool constant,
bool enumerate);
274 std::unique_ptr<ScriptInterface_impl>
m;
287 JS::RootedValue val(rq.
cx);
289 return SetGlobal_(name, val, replace, constant, enumerate);
296 JS::RootedValue rval(rq.
cx);
297 if (!Eval(code, &rval))
302 #endif // INCLUDED_SCRIPTINTERFACE #define NONCOPYABLE(className)
Indicates that a class is noncopyable (usually due to const or reference members, or because the clas...
Definition: code_annotation.h:227
JSContext * cx
Definition: ScriptRequest.h:92
thread_local std::shared_ptr< ScriptContext > g_ScriptContext
Definition: GameSetup.cpp:108
static T * ObjectFromCBData(const ScriptRequest &rq)
Convert the CmptPrivate callback data to T*.
Definition: ScriptInterface.h:117
bool Eval(const char *code) const
Evaluate some JS code in the global scope.
Definition: ScriptInterface.cpp:687
Definition: pch_boost.h:50
ScriptInterface * pScriptInterface
Definition: ScriptInterface.h:107
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:421
std::unique_ptr< ScriptInterface_impl > m
Definition: ScriptInterface.h:274
bool LoadGlobalScript(const VfsPath &filename, const std::string &code) const
Load and execute the given script in the global scope.
Definition: ScriptInterface.cpp:644
ERROR_SUBGROUP(Scripting, LoadFile)
Abstraction around a SpiderMonkey JSContext.
Definition: ScriptContext.h:40
bool LoadGlobalScripts()
Load global scripts that most script interfaces need, located in the /globalscripts directory...
Definition: ScriptInterface.cpp:415
static bool GetGlobalProperty(const ScriptRequest &rq, const std::string &name, JS::MutableHandleValue out)
Get an object from the global scope or any lexical scope.
Definition: ScriptInterface.cpp:570
static T * GetPrivate(const ScriptRequest &rq, JS::CallArgs &callArgs, JSClass *jsClass)
Retrieve the private data field of a JS Object that is an instance of the given JSClass.
Definition: ScriptInterface.h:243
Config::Value_type Value
Definition: json_spirit_value.h:182
bool SetGlobal(const char *name, const T &value, bool replace=false, bool constant=true, bool enumerate=true)
Set the named property on the global object.
Definition: ScriptInterface.h:284
static bool Math_random(JSContext *cx, uint argc, JS::Value *vp)
JSNative wrapper of the above.
Definition: ScriptInterface.cpp:291
void ToJSVal(const ScriptRequest &rq, JS::MutableHandleValue ret, T const &val)
Convert a C++ type to a JS::Value.
bool LoadScript(const VfsPath &filename, const std::string &code) const
Load and execute the given script in a new function scope.
Definition: ScriptInterface.cpp:610
bool SetGlobal_(const char *name, JS::HandleValue value, bool replace, bool constant, bool enumerate)
Definition: ScriptInterface.cpp:526
bool LoadGlobalScriptFile(const VfsPath &path) const
Load and execute the given script in the global scope.
Definition: ScriptInterface.cpp:664
bool ReplaceNondeterministicRNG(boost::random::rand48 &rng)
Replace the default JS random number generator with a seeded, network-synced one. ...
Definition: ScriptInterface.cpp:434
std::shared_ptr< ScriptContext > GetContext() const
Definition: ScriptInterface.cpp:461
static T * GetPrivate(const ScriptRequest &rq, JS::HandleObject thisobj, JSClass *jsClass)
Retrieve the private data field of a JSObject that is an instance of the given JSClass.
Definition: ScriptInterface.h:228
#define T(string_literal)
Definition: secure_crt.cpp:77
JS::PersistentRootedObject m_Prototype
Definition: ScriptInterface.h:265
void SetCallbackData(void *pCBData)
Definition: ScriptInterface.cpp:404
bool SetPrototype(JS::HandleValue obj, JS::HandleValue proto)
Definition: ScriptInterface.cpp:600
JSObject * CreateCustomObject(const std::string &typeName) const
Definition: ScriptInterface.cpp:514
Definition: pch_boost.h:51
bool MathRandom(double &nbr) const
Calls the random number generator assigned to this ScriptInterface instance and returns the generated...
Definition: ScriptInterface.cpp:283
Definition: ScriptInterface.h:263
std::map< std::string, CustomType > m_CustomObjectTypes
Definition: ScriptInterface.h:276
void * pCBData
Definition: ScriptInterface.h:108
unsigned int uint
Definition: types.h:42
JSContext * GetGeneralJSContext() const
GetGeneralJSContext returns the context without starting a GC request and without entering the Script...
Definition: ScriptInterface.cpp:456
Abstraction around a SpiderMonkey JS::Realm.
Definition: ScriptInterface.h:71
void Raise(const ScriptRequest &rq, const char *format,...)
Raise a JS exception from C++ code.
Definition: ScriptExceptions.cpp:95
Definition: ScriptInterface.h:100
JSClass * m_Class
Definition: ScriptInterface.h:266
static T * ObjectFromCBData(const ScriptRequest &rq, JS::CallArgs &)
Variant for the function wrapper.
Definition: ScriptInterface.h:128
Path()
Definition: path.h:84
Spidermonkey maintains some 'local' state via the JSContext* object.
Definition: ScriptRequest.h:59
JSNative m_Constructor
Definition: ScriptInterface.h:267
void CallConstructor(JS::HandleValue ctor, JS::HandleValueArray argv, JS::MutableHandleValue out) const
Call a constructor function, equivalent to JS "new ctor(arg)".
Definition: ScriptInterface.cpp:466
void DefineCustomObjectType(JSClass *clasp, JSNative constructor, uint minArgs, JSPropertySpec *ps, JSFunctionSpec *fs, JSPropertySpec *static_ps, JSFunctionSpec *static_fs)
Definition: ScriptInterface.cpp:483
static void * GetCBData(JSContext *cx)
Definition: ScriptInterface.cpp:398
Definition: ScriptInterface.cpp:52
ERROR_TYPE(Scripting, SetupFailed)
bool FromJSVal(const ScriptRequest &rq, const JS::HandleValue val, T &ret)
Convert a JS::Value to a C++ type.
CmptPrivate m_CmptPrivate
Definition: ScriptInterface.h:270