18#ifndef INCLUDED_SCRIPTINTERFACE
19#define INCLUDED_SCRIPTINTERFACE
48#define SCRIPT_INTERFACE_MAX_ARGS 8
58namespace boost {
namespace random {
class rand48; } }
88 template<
typename Context>
89 ScriptInterface(
const char* nativeScopeName,
const char* debugName, Context&& context) :
92 static_assert(std::is_lvalue_reference_v<Context>,
"`ScriptInterface` doesn't take ownership "
124 template <
typename T>
127 static_assert(!std::is_same_v<void, T>);
128 return static_cast<T*
>(ObjectFromCBData<void>(rq));
134 template <
typename T>
137 return ObjectFromCBData<T>(rq);
167 void CallConstructor(JS::HandleValue ctor, JS::HandleValueArray argv, JS::MutableHandleValue
out)
const;
170 void DefineCustomObjectType(JSClass *clasp, JSNative constructor,
uint minArgs, JSPropertySpec *ps, JSFunctionSpec *
fs, JSPropertySpec *static_ps, JSFunctionSpec *static_fs);
178 bool SetGlobal(
const char* name,
const T& value,
bool replace =
false,
bool constant =
true,
bool enumerate =
true);
189 bool SetPrototype(JS::HandleValue obj, JS::HandleValue proto);
217 bool Eval(
const char* code)
const;
218 bool Eval(
const char* code, JS::MutableHandleValue
out)
const;
219 template<
typename T>
bool Eval(
const char* code,
T&
out)
const;
234 template <
typename T>
237 T* value =
static_cast<T*
>(JS_GetInstancePrivate(rq.
cx, thisobj, jsClass,
nullptr));
239 if (value ==
nullptr)
249 template <
typename T>
252 if (!callArgs.thisv().isObject())
258 JS::RootedObject thisObj(rq.
cx, &callArgs.thisv().toObject());
259 T* value =
static_cast<T*
>(JS_GetInstancePrivate(rq.
cx, thisObj, jsClass, &callArgs));
261 if (value ==
nullptr)
268 bool SetGlobal_(
const char* name, JS::HandleValue value,
bool replace,
bool constant,
bool enumerate);
281 std::unique_ptr<ScriptInterface_impl>
m;
294 JS::RootedValue val(rq.
cx);
296 return SetGlobal_(name, val, replace, constant, enumerate);
303 JS::RootedValue rval(rq.
cx);
304 if (!
Eval(code, &rval))
thread_local std::shared_ptr< ScriptContext > g_ScriptContext
Definition: GameSetup.cpp:108
ERROR_SUBGROUP(Scripting, LoadFile)
ERROR_TYPE(Scripting, SetupFailed)
Path()
Definition: path.h:84
Abstraction around a SpiderMonkey JSContext.
Definition: ScriptContext.h:46
Abstraction around a SpiderMonkey JS::Realm.
Definition: ScriptInterface.h:72
void SetCallbackData(void *pCBData)
Definition: ScriptInterface.cpp:408
NONCOPYABLE(ScriptInterface)
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:291
bool MathRandom(double &nbr) const
Calls the random number generator assigned to this ScriptInterface instance and returns the generated...
Definition: ScriptInterface.cpp:284
std::map< std::string, CustomType > m_CustomObjectTypes
Definition: ScriptInterface.h:283
bool ReplaceNondeterministicRNG(boost::random::rand48 &rng)
Replace the default JS random number generator with a seeded, network-synced one.
Definition: ScriptInterface.cpp:438
JSContext * GetGeneralJSContext() const
GetGeneralJSContext returns the context without starting a GC request and without entering the Script...
Definition: ScriptInterface.cpp:460
static T * ObjectFromCBData(const ScriptRequest &rq)
Convert the CmptPrivate callback data to T*.
Definition: ScriptInterface.h:125
JSObject * CreateCustomObject(const std::string &typeName) const
Definition: ScriptInterface.cpp:518
bool Eval(const char *code) const
Evaluate some JS code in the global scope.
Definition: ScriptInterface.cpp:691
bool SetGlobal_(const char *name, JS::HandleValue value, bool replace, bool constant, bool enumerate)
Definition: ScriptInterface.cpp:530
ScriptInterface(const char *nativeScopeName, const char *debugName, Context &&context)
Definition: ScriptInterface.h:89
static bool Math_random(JSContext *cx, uint argc, JS::Value *vp)
JSNative wrapper of the above.
Definition: ScriptInterface.cpp:292
bool SetPrototype(JS::HandleValue obj, JS::HandleValue proto)
Definition: ScriptInterface.cpp:604
CmptPrivate m_CmptPrivate
Definition: ScriptInterface.h:277
std::unique_ptr< ScriptInterface_impl > m
Definition: ScriptInterface.h:281
void DefineCustomObjectType(JSClass *clasp, JSNative constructor, uint minArgs, JSPropertySpec *ps, JSFunctionSpec *fs, JSPropertySpec *static_ps, JSFunctionSpec *static_fs)
Definition: ScriptInterface.cpp:487
static T * ObjectFromCBData(const ScriptRequest &rq, JS::CallArgs &)
Variant for the function wrapper.
Definition: ScriptInterface.h:135
bool LoadGlobalScripts()
Load global scripts that most script interfaces need, located in the /globalscripts directory.
Definition: ScriptInterface.cpp:419
ScriptInterface(const char *nativeScopeName, const char *debugName, ScriptContext &context)
Constructor.
Definition: ScriptInterface.cpp:352
bool LoadGlobalScriptFile(const VfsPath &path) const
Load and execute the given script in the global scope.
Definition: ScriptInterface.cpp:668
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:574
~ScriptInterface()
Definition: ScriptInterface.cpp:385
bool LoadScript(const VfsPath &filename, const std::string &code) const
Load and execute the given script in a new function scope.
Definition: ScriptInterface.cpp:614
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:250
ScriptContext & GetContext() const
Definition: ScriptInterface.cpp:465
bool LoadGlobalScript(const VfsPath &filename, const std::string &code) const
Load and execute the given script in the global scope.
Definition: ScriptInterface.cpp:648
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:470
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:235
Spidermonkey maintains some 'local' state via the JSContext* object.
Definition: ScriptRequest.h:60
JSContext * cx
Definition: ScriptRequest.h:92
void Raise(const ScriptRequest &rq, const char *format,...)
Raise a JS exception from C++ code.
Definition: ScriptExceptions.cpp:95
bool FromJSVal(const ScriptRequest &rq, const JS::HandleValue val, T &ret)
Convert a JS::Value to a C++ type.
void ToJSVal(const ScriptRequest &rq, JS::MutableHandleValue ret, T const &val)
Convert a C++ type to a JS::Value.
Definition: pch_boost.h:51
Definition: pch_boost.h:50
#define T(string_literal)
Definition: secure_crt.cpp:77
Definition: ScriptInterface.h:109
ScriptInterface * pScriptInterface
Definition: ScriptInterface.h:115
void * pCBData
Definition: ScriptInterface.h:116
static void * GetCBData(JSContext *cx)
Definition: ScriptInterface.cpp:402
static const ScriptInterface & GetScriptInterface(JSContext *cx)
Definition: ScriptInterface.cpp:395
Definition: ScriptInterface.h:271
JSNative m_Constructor
Definition: ScriptInterface.h:274
JS::PersistentRootedObject m_Prototype
Definition: ScriptInterface.h:272
JSClass * m_Class
Definition: ScriptInterface.h:273
Definition: ScriptInterface.cpp:54
unsigned int uint
Definition: types.h:42
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:407