18#ifndef INCLUDED_SCRIPT_INTERFACE
19#define INCLUDED_SCRIPT_INTERFACE
28#include <js/CallArgs.h>
30#include <js/RootingAPI.h>
31#include <js/TypeDecls.h>
33#include <js/ValueArray.h>
38#include <unordered_map>
43namespace Script {
class Context; }
44namespace Script {
class ModuleLoader; }
45namespace Script {
struct Interface_impl; }
46namespace boost {
namespace random {
class rand48; } }
72#define SCRIPT_INTERFACE_MAX_ARGS 8
107 Interface(
const char* nativeScopeName,
const char* debugName,
Context& context,
108 std::function<
bool(
const VfsPath&)> allowModule = {});
110 template<
typename Context>
112 std::function<
bool(
const VfsPath&)> allowModule = {}) :
113 Interface{nativeScopeName, debugName, *context, std::move(allowModule)}
115 static_assert(std::is_lvalue_reference_v<Context>,
"`Interface` doesn't take ownership "
130 std::function<
bool(
const VfsPath&)> allowModule = {});
150 template <
typename T>
153 static_assert(!std::is_same_v<void, T>);
154 return static_cast<T*
>(ObjectFromCBData<void>(rq));
160 template <
typename T>
163 return ObjectFromCBData<T>(rq);
195 void CallConstructor(JS::HandleValue ctor, JS::HandleValueArray argv, JS::MutableHandleValue
out)
const;
199 JSPropertySpec *ps, JSFunctionSpec *fs, JSPropertySpec *static_ps, JSFunctionSpec *static_fs);
207 bool SetGlobal(
const char* name,
const T& value,
bool replace =
false,
bool constant =
true,
bool enumerate =
true);
218 bool SetPrototype(JS::HandleValue obj, JS::HandleValue proto);
246 bool Eval(
const std::string_view code)
const;
247 bool Eval(
const std::string_view code, JS::MutableHandleValue
out)
const;
248 template<
typename T>
bool Eval(
const std::string_view code,
T&
out)
const;
258 static bool Math_random(JSContext* cx,
unsigned int argc, JS::Value* vp);
272 template <
typename T>
275 T* value = JS::GetMaybePtrFromReservedSlot<T>(thisobj, JSObjectReservedSlots::PRIVATE);
277 if (value ==
nullptr)
287 template <
typename T>
290 if (!callArgs.thisv().isObject())
292 Exception::Raise(rq,
"Cannot retrieve private JS object data because from a non-object value!");
296 JS::RootedObject thisObj(rq.
cx, &callArgs.thisv().toObject());
297 T* value = JS::GetMaybePtrFromReservedSlot<T>(thisObj, JSObjectReservedSlots::PRIVATE);
299 if (value ==
nullptr)
306 bool SetGlobal_(
const char* name, JS::HandleValue value,
bool replace,
bool constant,
bool enumerate);
319 std::unique_ptr<Interface_impl>
m;
334 JS::RootedValue val(rq.
cx);
336 return SetGlobal_(name, val, replace, constant, enumerate);
343 JS::RootedValue rval(rq.
cx);
344 if (!Eval(code, &rval))
Definition: Interface.h:93
void DefineCustomObjectType(JSClass *clasp, JSNative constructor, unsigned int minArgs, JSPropertySpec *ps, JSFunctionSpec *fs, JSPropertySpec *static_ps, JSFunctionSpec *static_fs)
Definition: Interface.cpp:531
bool LoadGlobalScriptFile(const VfsPath &path) const
Load and execute the given script in the global scope.
Definition: Interface.cpp:715
bool SetPrototype(JS::HandleValue obj, JS::HandleValue proto)
Definition: Interface.cpp:649
bool SetGlobal_(const char *name, JS::HandleValue value, bool replace, bool constant, bool enumerate)
Definition: Interface.cpp:575
bool ReplaceNondeterministicRNG(boost::random::rand48 &rng)
Replace the default JS random number generator with a seeded, network-synced one.
Definition: Interface.cpp:477
Context & GetContext() const
Definition: Interface.cpp:504
JSObject * CreateCustomObject(const std::string &typeName) const
Definition: Interface.cpp:563
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: Interface.h:331
bool MathRandom(double &nbr) const
Calls the random number generator assigned to this Interface instance and returns the generated numbe...
Definition: Interface.cpp:318
static bool Math_random(JSContext *cx, unsigned int argc, JS::Value *vp)
JSNative wrapper of the above.
Definition: Interface.cpp:326
static T * ObjectFromCBData(const Request &rq)
Convert the CmptPrivate callback data to T*.
Definition: Interface.h:151
JSContext * GetGeneralJSContext() const
GetGeneralJSContext returns the context without starting a GC request and without entering the Interf...
Definition: Interface.cpp:499
bool LoadGlobalScript(const VfsPath &filename, const std::string &code) const
Load and execute the given script in the global scope.
Definition: Interface.cpp:694
bool LoadGlobalScripts()
Load global scripts that most script interfaces need, located in the /globalscripts directory.
Definition: Interface.cpp:458
static T * GetPrivate(const Request &rq, JS::HandleObject thisobj)
Retrieve the private data field of a JSObject.
Definition: Interface.h:273
Interface(const char *nativeScopeName, const char *debugName, const Interface &neighbor, std::function< bool(const VfsPath &)> allowModule={})
Alternate constructor.
CmptPrivate m_CmptPrivate
Definition: Interface.h:315
static bool GetGlobalProperty(const Request &rq, const std::string &name, JS::MutableHandleValue out)
Get an object from the global scope or any lexical scope.
Definition: Interface.cpp:619
Interface(const char *nativeScopeName, const char *debugName, Context &context, std::function< bool(const VfsPath &)> allowModule={})
Constructor.
Definition: Interface.cpp:387
bool LoadScript(const VfsPath &filename, const std::string &code) const
Load and execute the given script in a new function scope.
Definition: Interface.cpp:659
std::unordered_map< std::string, CustomType > m_CustomObjectTypes
Definition: Interface.h:321
~Interface()
Definition: Interface.cpp:424
Interface(const char *nativeScopeName, const char *debugName, Context &&context, std::function< bool(const VfsPath &)> allowModule={})
Definition: Interface.h:111
std::unique_ptr< Interface_impl > m
Definition: Interface.h:319
ModuleLoader & GetModuleLoader() const
Definition: Interface.cpp:509
bool Eval(const std::string_view code) const
Evaluate some JS code in the global scope.
Definition: Interface.cpp:738
static T * GetPrivate(const Request &rq, JS::CallArgs &callArgs)
Retrieve the private data field of a JS Object.
Definition: Interface.h:288
static T * ObjectFromCBData(const Request &rq, JS::CallArgs &)
Variant for the function wrapper.
Definition: Interface.h:161
void SetCallbackData(void *pCBData)
Definition: Interface.cpp:447
void CallConstructor(JS::HandleValue ctor, JS::HandleValueArray argv, JS::MutableHandleValue out) const
Call a constructor function, equivalent to JS "new ctor(arg)".
Definition: Interface.cpp:514
JSObjectReservedSlots
Name the reserved slots we may need to use in custom JSObjects.
Definition: Interface.h:265
@ PRIVATE
Definition: Interface.h:266
Definition: ModuleLoader.h:46
JSContext * cx
Definition: Request.h:83
void Raise(const Request &rq, const char *format,...)
Raise a JS exception from C++ code.
Definition: Exceptions.cpp:111
Abstraction around a SpiderMonkey JSContext.
Definition: DapInterface.h:32
bool FromJSVal(const Request &rq, const JS::HandleValue val, T &ret)
Convert a JS::Value to a C++ type.
void ToJSVal(const Request &rq, JS::MutableHandleValue ret, T const &val)
Convert a C++ type to a JS::Value.
Definition: Interface.h:46
ERROR_SUBGROUP(Scripting, LoadFile)
ERROR_TYPE(Scripting, SetupFailed)
thread_local std::shared_ptr< Script::Context > g_ScriptContext
Definition: GameSetup.cpp:133
#define T(string_literal)
Definition: secure_crt.cpp:81
Definition: Interface.h:135
void * pCBData
Definition: Interface.h:142
Interface * pScriptInterface
Definition: Interface.h:141
static void * GetCBData(JSContext *cx)
Definition: Interface.cpp:441
static const Interface & GetScriptInterface(JSContext *cx)
Definition: Interface.cpp:434
Definition: Interface.h:309
JSClass * m_Class
Definition: Interface.h:311
JSNative m_Constructor
Definition: Interface.h:312
JS::PersistentRootedObject m_Prototype
Definition: Interface.h:310
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:407