18 #ifndef INCLUDED_SCRIPTINTERFACE 19 #define INCLUDED_SCRIPTINTERFACE 21 #include <boost/random/linear_congruential.hpp> 45 ERROR_TYPE(Scripting_DefineType, CreationFailed);
50 #define SCRIPT_INTERFACE_MAX_ARGS 8 53 #define DEFAULT_RUNTIME_SIZE 16 * 1024 * 1024 54 #define DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER 2 * 1024 *1024 95 ScriptInterface(
const char* nativeScopeName,
const char* debugName,
const shared_ptr<ScriptRuntime>& runtime);
132 void CallConstructor(JS::HandleValue ctor, JS::HandleValueArray argv, JS::MutableHandleValue
out)
const;
135 void DefineCustomObjectType(JSClass *clasp, JSNative constructor,
uint minArgs, JSPropertySpec *ps, JSFunctionSpec *fs, JSPropertySpec *static_ps, JSFunctionSpec *static_fs);
147 bool SetGlobal(
const char* name,
const T& value,
bool replace =
false);
154 bool SetProperty(JS::HandleValue obj,
const char* name,
const T& value,
bool constant =
false,
bool enumerate =
true);
161 bool SetProperty(JS::HandleValue obj,
const wchar_t* name,
const T& value,
bool constant =
false,
bool enumerate =
true);
168 bool SetPropertyInt(JS::HandleValue obj,
int name,
const T& value,
bool constant =
false,
bool enumerate =
true);
179 bool GetProperty(JS::HandleValue obj,
const char* name, JS::MutableHandleValue
out)
const;
180 bool GetProperty(JS::HandleValue obj,
const char* name, JS::MutableHandleObject
out)
const;
191 bool GetPropertyInt(JS::HandleValue obj,
int name, JS::MutableHandleValue
out)
const;
196 bool HasProperty(JS::HandleValue obj,
const char* name)
const;
200 bool SetPrototype(JS::HandleValue obj, JS::HandleValue proto);
202 bool FreezeObject(JS::HandleValue objVal,
bool deep)
const;
204 bool Eval(
const char* code)
const;
206 template<
typename CHAR>
bool Eval(
const CHAR* code, JS::MutableHandleValue
out)
const;
207 template<
typename T,
typename CHAR>
bool Eval(
const CHAR* code,
T&
out)
const;
215 std::string
ToString(JS::MutableHandleValue obj,
bool pretty =
false)
const;
222 bool ParseJSON(
const std::string& string_utf8, JS::MutableHandleValue
out)
const;
232 std::string
StringifyJSON(JS::MutableHandleValue obj,
bool indent =
true)
const;
274 template<
typename T>
static bool FromJSVal(JSContext* cx,
const JS::HandleValue val,
T& ret);
283 template<
typename T>
static void ToJSVal(JSContext* cx, JS::MutableHandleValue ret,
T const& val);
323 void ReadStructuredClone(
const shared_ptr<StructuredClone>& ptr, JS::MutableHandleValue ret);
333 template <
typename T>
343 template <
typename T>
354 template <
typename T>
359 bool CallFunction_(JS::HandleValue val,
const char* name, JS::HandleValueArray argv, JS::MutableHandleValue ret)
const;
360 bool Eval_(
const char* code, JS::MutableHandleValue ret)
const;
361 bool Eval_(
const wchar_t* code, JS::MutableHandleValue ret)
const;
362 bool SetGlobal_(
const char* name, JS::HandleValue value,
bool replace);
363 bool SetProperty_(JS::HandleValue obj,
const char* name, JS::HandleValue value,
bool readonly,
bool enumerate);
364 bool SetProperty_(JS::HandleValue obj,
const wchar_t* name, JS::HandleValue value,
bool readonly,
bool enumerate);
365 bool SetPropertyInt_(JS::HandleValue obj,
int name, JS::HandleValue value,
bool readonly,
bool enumerate);
366 bool GetProperty_(JS::HandleValue obj,
const char* name, JS::MutableHandleValue
out)
const;
367 bool GetPropertyInt_(JS::HandleValue obj,
int name, JS::MutableHandleValue value)
const;
369 static const JSClass*
GetClass(JS::HandleObject obj);
370 static void*
GetPrivate(JS::HandleObject obj);
381 m_Prototype = std::move(other.m_Prototype);
382 m_Class = std::move(other.m_Class);
383 m_Constructor = std::move(other.m_Constructor);
388 m_Prototype = std::move(other.m_Prototype);
389 m_Class = std::move(other.m_Class);
390 m_Constructor = std::move(other.m_Constructor);
397 void Register(
const char* name, JSNative fptr,
size_t nargs);
401 std::unique_ptr<ScriptInterface_impl>
m;
449 inline void ScriptInterface::AssignOrToJSVal<JS::PersistentRootedValue>(JSContext*
UNUSED(cx), JS::MutableHandleValue
handle,
const JS::PersistentRootedValue& a)
455 inline void ScriptInterface::AssignOrToJSVal<JS::RootedValue>(JSContext*
UNUSED(cx), JS::MutableHandleValue
handle,
const JS::RootedValue& a)
461 inline void ScriptInterface::AssignOrToJSVal<JS::HandleValue>(JSContext*
UNUSED(cx), JS::MutableHandleValue
handle,
const JS::HandleValue& a)
467 inline void ScriptInterface::AssignOrToJSValUnrooted<JS::Value>(JSContext*
UNUSED(cx), JS::MutableHandleValue
handle,
const JS::Value& a)
481 inline JS::HandleValue ScriptInterface::AssignOrFromJSVal<JS::HandleValue>(JSContext*
UNUSED(cx),
const JS::HandleValue& val,
bool& ret)
502 return SetProperty_(obj, name, val, readonly, enumerate);
511 return SetProperty_(obj, name, val, readonly, enumerate);
527 JSAutoRequest rq(cx);
528 JS::RootedValue val(cx);
544 template<
typename CHAR>
547 if (!
Eval_(code, ret))
552 template<
typename T,
typename CHAR>
557 if (!
Eval_(code, &rval))
562 #endif // INCLUDED_SCRIPTINTERFACE 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:512
void ReadJSONFile(const VfsPath &path, JS::MutableHandleValue out) const
Read a JSON file.
Definition: ScriptInterface.cpp:970
bool ReplaceNondeterministicRNG(boost::rand48 &rng)
Replace the default JS random number geenrator with a seeded, network-sync'd one. ...
Definition: ScriptInterface.cpp:471
JS::Value GetCachedValue(CACHED_VAL valueIdentifier) const
Definition: ScriptInterface.cpp:437
bool FreezeObject(JS::HandleValue objVal, bool deep) const
Definition: ScriptInterface.cpp:814
Definition: ScriptInterface.h:118
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
Definition: code_annotation.h:38
bool HasProperty(JS::HandleValue obj, const char *name) const
Check the named property has been defined on the given object.
Definition: ScriptInterface.cpp:723
Definition: ScriptInterface.h:118
void ForceGC()
Triggers a full non-incremental garbage collection immediately.
Definition: ScriptInterface.cpp:1100
jsval GetGlobalObject()
Definition: ScriptInterface.cpp:583
CACHED_VAL
Definition: ScriptInterface.h:118
static CStr prefix
Definition: DllLoader.cpp:46
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:419
NONCOPYABLE(ScriptInterface)
void Register(const char *name, JSNative fptr, size_t nargs)
Definition: ScriptInterface.cpp:492
std::unique_ptr< ScriptInterface_impl > m
Definition: ScriptInterface.h:401
shared_ptr< ScriptRuntime > g_ScriptRuntime
Definition: GameSetup.cpp:122
static void AssignOrToJSVal(JSContext *cx, JS::MutableHandleValue handle, const T &a)
Converts |a| if needed and assigns it to |handle|.
Definition: ScriptInterface.h:443
ERROR_SUBGROUP(Scripting, LoadFile)
bool LoadGlobalScripts()
Load global scripts that most script contexts need, located in the /globalscripts directory...
Definition: ScriptInterface.cpp:445
shared_ptr< ScriptRuntime > GetRuntime() const
Definition: ScriptInterface.cpp:507
static const JSClass * GetClass(JS::HandleObject obj)
Definition: ScriptInterface.cpp:1084
u64 * m_Data
Definition: ScriptInterface.h:318
struct ScriptInterface::CxPrivate m_CxPrivate
char CHAR
Definition: wgl.h:60
uint64_t u64
Definition: types.h:40
bool SetPropertyInt_(JS::HandleValue obj, int name, JS::HandleValue value, bool readonly, bool enumerate)
Definition: ScriptInterface.cpp:652
Structured clones are a way to serialize 'simple' JS values into a buffer that can safely be passed b...
Definition: ScriptInterface.h:312
bool CallFunction_(JS::HandleValue val, const char *name, JS::HandleValueArray argv, JS::MutableHandleValue ret) const
Definition: ScriptInterface.cpp:565
static bool IsExceptionPending(JSContext *cx)
Definition: ScriptInterface.cpp:1078
ScriptInterface(const char *nativeScopeName, const char *debugName, const shared_ptr< ScriptRuntime > &runtime)
Constructor.
Definition: ScriptInterface.cpp:403
static size_t nargs()
Definition: ScriptInterface.h:93
bool GetPropertyInt(JS::HandleValue obj, int name, T &out) const
Get the integer-named property on the given object.
Definition: ScriptInterface.h:535
bool SetPropertyInt(JS::HandleValue obj, int name, const T &value, bool constant=false, bool enumerate=true)
Set the integer-named property on the given object.
Definition: ScriptInterface.h:515
JS::Value CloneValueFromOtherContext(ScriptInterface &otherContext, JS::HandleValue val)
Construct a new value (usable in this ScriptInterface's context) by cloning a value from a different ...
Definition: ScriptInterface.cpp:1106
Config::Value_type Value
Definition: json_spirit_value.h:181
~ScriptInterface()
Definition: ScriptInterface.cpp:417
bool SetProperty(JS::HandleValue obj, const char *name, const T &value, bool constant=false, bool enumerate=true)
Set the named property on the given object.
Definition: ScriptInterface.h:497
CustomType()
Definition: ScriptInterface.h:378
boost::mt19937 rng
Random number generator (Boost Mersenne Twister)
Definition: Noise.cpp:34
static CxPrivate * GetScriptInterfaceAndCBData(JSContext *cx)
Definition: ScriptInterface.cpp:431
void * pCBData
Definition: ScriptInterface.h:102
bool ParseJSON(const std::string &string_utf8, JS::MutableHandleValue out) const
Parse a UTF-8-encoded JSON string.
Definition: ScriptInterface.cpp:938
#define DEFAULT_RUNTIME_SIZE
Definition: ScriptInterface.h:53
#define T(string_literal)
Definition: secure_crt.cpp:76
CustomType & operator=(CustomType &&other)
Definition: ScriptInterface.h:379
CustomType(CustomType &&other)
Definition: ScriptInterface.h:386
bool SetGlobal_(const char *name, JS::HandleValue value, bool replace)
Definition: ScriptInterface.cpp:594
JS::PersistentRootedObject m_Prototype
Definition: ScriptInterface.h:393
#define DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER
Definition: ScriptInterface.h:54
void SetCallbackData(void *pCBData)
Definition: ScriptInterface.cpp:426
bool SetPrototype(JS::HandleValue obj, JS::HandleValue proto)
Definition: ScriptInterface.cpp:804
JSObject * CreateCustomObject(const std::string &typeName) const
Definition: ScriptInterface.cpp:554
boost::rand48 * m_rng
Definition: ScriptInterface.h:403
static shared_ptr< ScriptRuntime > CreateRuntime(shared_ptr< ScriptRuntime > parentRuntime=shared_ptr< ScriptRuntime >(), int runtimeSize=DEFAULT_RUNTIME_SIZE, int heapGrowthBytesGCTrigger=DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER)
Returns a runtime, which can used to initialise any number of ScriptInterfaces contexts.
Definition: ScriptInterface.cpp:852
bool SetProperty_(JS::HandleValue obj, const char *name, JS::HandleValue value, bool readonly, bool enumerate)
Definition: ScriptInterface.cpp:615
bool MathRandom(double &nbr)
MathRandom (this function) calls the random number generator assigned to this ScriptInterface instanc...
Definition: ScriptInterface.cpp:324
bool GetProperty(JS::HandleValue obj, const char *name, T &out) const
Get the named property on the given object.
Definition: ScriptInterface.h:524
void ReportError(const char *msg) const
Report the given error message through the JS error reporting mechanism, and throw a JS exception...
Definition: ScriptInterface.cpp:1064
shared_ptr< StructuredClone > WriteStructuredClone(JS::HandleValue v)
Definition: ScriptInterface.cpp:1127
Definition: ScriptInterface.h:372
bool GetPropertyInt_(JS::HandleValue obj, int name, JS::MutableHandleValue value) const
Definition: ScriptInterface.cpp:710
bool EnumeratePropertyNamesWithPrefix(JS::HandleValue objVal, const char *prefix, std::vector< std::string > &out) const
Definition: ScriptInterface.cpp:737
bool LoadGlobalScriptFile(const VfsPath &path) const
Load and execute the given script in the global scope.
Definition: ScriptInterface.cpp:874
std::map< std::string, CustomType > m_CustomObjectTypes
Definition: ScriptInterface.h:404
unsigned int uint
Definition: types.h:42
bool SetGlobal(const char *name, const T &value, bool replace=false)
Set the named property on the global object.
Definition: ScriptInterface.h:488
JSClass * GetGlobalClass()
Definition: ScriptInterface.cpp:589
Definition: ScriptInterface.h:99
static void AssignOrToJSValUnrooted(JSContext *cx, JS::MutableHandleValue handle, const T &a)
The same as AssignOrToJSVal, but also allows JS::Value for T.
Definition: ScriptInterface.h:344
static bool FromJSVal(JSContext *cx, const JS::HandleValue val, T &ret)
Convert a jsval to a C++ type.
static Handle handle(size_t idx, u64 tag)
Definition: h_mgr.cpp:121
Abstraction around a SpiderMonkey JSContext.
Definition: ScriptInterface.h:71
bool Eval(const char *code) const
Definition: ScriptInterface.cpp:909
void ReadStructuredClone(const shared_ptr< StructuredClone > &ptr, JS::MutableHandleValue ret)
Definition: ScriptInterface.cpp:1144
bool Eval_(const char *code, JS::MutableHandleValue ret) const
Definition: ScriptInterface.cpp:916
JSClass * m_Class
Definition: ScriptInterface.h:394
Abstraction around a SpiderMonkey JSRuntime.
Definition: ScriptRuntime.h:38
std::string ToString(JS::MutableHandleValue obj, bool pretty=false) const
Convert an object to a UTF-8 encoded string, either with JSON (if pretty == true and there is no JSON...
Definition: ScriptInterface.cpp:1027
bool LoadScript(const VfsPath &filename, const std::string &code) const
Load and execute the given script in a new function scope.
Definition: ScriptInterface.cpp:828
static T AssignOrFromJSVal(JSContext *cx, const JS::HandleValue &val, bool &ret)
Converts |val| to T if needed or just returns it if it's a handle.
Definition: ScriptInterface.h:473
JSNative m_Constructor
Definition: ScriptInterface.h:395
static void * GetPrivate(JS::HandleObject obj)
Definition: ScriptInterface.cpp:1089
ScriptInterface * pScriptInterface
Definition: ScriptInterface.h:101
JSContext * GetContext() const
Definition: ScriptInterface.cpp:497
void MaybeGC()
MaybeGC tries to determine whether garbage collection in cx's runtime would free up enough memory to ...
Definition: ScriptInterface.cpp:1095
JSRuntime * GetJSRuntime() const
Definition: ScriptInterface.cpp:502
void DefineCustomObjectType(JSClass *clasp, JSNative constructor, uint minArgs, JSPropertySpec *ps, JSFunctionSpec *fs, JSPropertySpec *static_ps, JSFunctionSpec *static_fs)
Definition: ScriptInterface.cpp:526
size_t m_Size
Definition: ScriptInterface.h:319
std::string StringifyJSON(JS::MutableHandleValue obj, bool indent=true) const
Stringify to a JSON string, UTF-8 encoded.
Definition: ScriptInterface.cpp:1011
Definition: ScriptInterface.cpp:55
ERROR_TYPE(Scripting, SetupFailed)
bool LoadGlobalScript(const VfsPath &filename, const std::wstring &code) const
Load and execute the given script in the global scope.
Definition: ScriptInterface.cpp:857
bool GetProperty_(JS::HandleValue obj, const char *name, JS::MutableHandleValue out) const
Definition: ScriptInterface.cpp:698
static void ToJSVal(JSContext *cx, JS::MutableHandleValue ret, T const &val)
Convert a C++ type to a jsval.