18 #ifndef INCLUDED_SCRIPTINTERFACE
19 #define INCLUDED_SCRIPTINTERFACE
34 #include <boost/random/linear_congruential.hpp>
41 #define SCRIPT_INTERFACE_MAX_ARGS 8
44 #define DEFAULT_RUNTIME_SIZE 16 * 1024 * 1024
80 ScriptInterface(
const char* nativeScopeName,
const char* debugName,
const shared_ptr<ScriptRuntime>& runtime);
128 template<
typename T0>
134 template<
typename T0,
typename T1>
140 template<
typename T0,
typename T1,
typename T2>
141 bool CallFunctionVoid(jsval val,
const char* name,
const T0& a0,
const T1& a1,
const T2& a2);
152 template<
typename T0,
typename R>
153 bool CallFunction(jsval val,
const char* name,
const T0& a0, R& ret);
158 template<
typename T0,
typename T1,
typename R>
159 bool CallFunction(jsval val,
const char* name,
const T0& a0,
const T1& a1, R& ret);
164 template<
typename T0,
typename T1,
typename T2,
typename R>
165 bool CallFunction(jsval val,
const char* name,
const T0& a0,
const T1& a1,
const T2& a2, R& ret);
170 template<
typename T0,
typename T1,
typename T2,
typename T3,
typename R>
171 bool CallFunction(jsval val,
const char* name,
const T0& a0,
const T1& a1,
const T2& a2,
const T3& a3, R& ret);
183 bool SetGlobal(
const char* name,
const T& value,
bool replace =
false);
190 bool SetProperty(jsval obj,
const char* name,
const T& value,
bool constant =
false,
bool enumerate =
true);
197 bool SetPropertyInt(jsval obj,
int name,
const T& value,
bool constant =
false,
bool enumerate =
true);
222 bool Eval(
const char* code);
224 template<
typename T,
typename CHAR>
bool Eval(
const CHAR* code,
T&
out);
226 std::wstring
ToString(jsval obj,
bool pretty =
false);
283 template<
typename T>
static bool FromJSVal(JSContext* cx, jsval val,
T& ret);
289 template<
typename T>
static jsval
ToJSVal(JSContext* cx,
T const& val);
325 bool CallFunction_(jsval val,
const char* name,
size_t argc, jsval* argv, jsval& ret);
326 bool Eval_(
const char* code, jsval& ret);
327 bool Eval_(
const wchar_t* code, jsval& ret);
328 bool SetGlobal_(
const char* name, jsval value,
bool replace);
329 bool SetProperty_(jsval obj,
const char* name, jsval value,
bool readonly,
bool enumerate);
330 bool SetPropertyInt_(jsval obj,
int name, jsval value,
bool readonly,
bool enumerate);
331 bool GetProperty_(jsval obj,
const char* name, jsval& value);
334 static JSClass*
GetClass(JSContext* cx, JSObject* obj);
335 static void*
GetPrivate(JSContext* cx, JSObject* obj);
337 void Register(
const char* name, JSNative fptr,
size_t nargs);
338 std::auto_ptr<ScriptInterface_impl>
m;
371 template<
typename T0>
380 template<
typename T0,
typename T1>
390 template<
typename T0,
typename T1,
typename T2>
401 template<
typename T0,
typename R>
413 template<
typename T0,
typename T1,
typename R>
426 template<
typename T0,
typename T1,
typename T2,
typename R>
440 template<
typename T0,
typename T1,
typename T2,
typename T3,
typename R>
491 template<
typename T,
typename CHAR>
495 if (!
Eval_(code, rval))
500 #endif // INCLUDED_SCRIPTINTERFACE
bool SetPrototype(jsval obj, jsval proto)
bool ReplaceNondeterministicRNG(boost::rand48 &rng)
Replace the default JS random number geenrator with a seeded, network-sync'd one. ...
bool CallFunction_(jsval val, const char *name, size_t argc, jsval *argv, jsval &ret)
bool LoadGlobalScript(const VfsPath &filename, const std::string &code)
Load and execute the given script in the global scope.
bool SetGlobal_(const char *name, jsval value, bool replace)
bool HasProperty(jsval obj, const char *name)
Check the named property has been defined on the given object.
void SetCallbackData(void *cbdata)
static void out(const wchar_t *fmt,...)
bool CallFunctionVoid(jsval val, const char *name)
Call the named property on the given object, with void return type and 0 arguments.
void Register(const char *name, JSNative fptr, size_t nargs)
JSRuntime * GetRuntime() const
static void ShutDown()
Shut down the JS system to clean up memory.
bool GetProperty_(jsval obj, const char *name, jsval &value)
bool FreezeObject(jsval obj, bool deep)
bool LoadScript(const VfsPath &filename, const std::string &code)
Load and execute the given script in a new function scope.
bool LoadGlobalScripts()
Load global scripts that most script contexts need, located in the /globalscripts directory...
std::string StringifyJSON(jsval obj, bool indent=true)
Stringify to a JSON string, UTF-8 encoded.
CScriptValRooted ParseJSON(const std::string &string_utf8)
Parse a UTF-8-encoded JSON string.
bool SetPropertyInt(jsval obj, int name, const T &value, bool constant=false, bool enumerate=true)
Set the integer-named property on the given object.
bool SetPropertyInt_(jsval obj, int name, jsval value, bool readonly, bool enumerate)
Structured clones are a way to serialize 'simple' JS values into a buffer that can safely be passed b...
AutoGCRooter * ReplaceAutoGCRooter(AutoGCRooter *rooter)
void DumpHeap()
Dump some memory heap debugging information to stderr.
static bool IsExceptionPending(JSContext *cx)
jsval NewObjectFromConstructor(jsval ctor)
Create an object as with CallConstructor except don't actually execute the constructor function...
ScriptInterface(const char *nativeScopeName, const char *debugName, const shared_ptr< ScriptRuntime > &runtime)
Constructor.
static bool FromJSVal(JSContext *cx, jsval val, T &ret)
Convert a jsval to a C++ type.
static void * GetCallbackData(JSContext *cx)
boost::mt19937 rng
Random number generator (Boost Mersenne Twister)
CScriptValRooted ReadJSONFile(const VfsPath &path)
Read a JSON file.
bool LoadGlobalScriptFile(const VfsPath &path)
Load and execute the given script in the global scope.
static JSClass * GetClass(JSContext *cx, JSObject *obj)
bool Eval(const char *code)
static jsval ToJSVal(JSContext *cx, T const &val)
Convert a C++ type to a jsval.
#define DEFAULT_RUNTIME_SIZE
#define T(string_literal)
NONCOPYABLE(StructuredClone)
jsval CloneValueFromOtherContext(ScriptInterface &otherContext, jsval val)
Construct a new value (usable in this ScriptInterface's context) by cloning a value from a different ...
bool GetProperty(jsval obj, const char *name, T &out)
Get the named property on the given object.
bool CallFunction(jsval val, const char *name, R &ret)
Call the named property on the given object, with return type R and 0 arguments.
bool SetProperty_(jsval obj, const char *name, jsval value, bool readonly, bool enumerate)
jsval CallConstructor(jsval ctor, jsval arg)
Call a constructor function, equivalent to JS "new ctor(arg)".
bool SetGlobal(const char *name, const T &value, bool replace=false)
Set the named property on the global object.
bool GetPropertyInt(jsval obj, int name, T &out)
Get the integer-named property on the given object.
JSClass * GetGlobalClass()
static void * GetPrivate(JSContext *cx, JSObject *obj)
Abstraction around a SpiderMonkey JSContext.
std::wstring ToString(jsval obj, bool pretty=false)
bool GetPropertyInt_(jsval obj, int name, jsval &value)
bool Eval_(const char *code, jsval &ret)
bool SetProperty(jsval obj, const char *name, const T &value, bool constant=false, bool enumerate=true)
Set the named property on the given object.
Abstraction around a SpiderMonkey JSRuntime.
jsval ReadStructuredClone(const shared_ptr< StructuredClone > &ptr)
bool EnumeratePropertyNamesWithPrefix(jsval obj, const char *prefix, std::vector< std::string > &out)
static shared_ptr< ScriptRuntime > CreateRuntime(int runtimeSize=DEFAULT_RUNTIME_SIZE)
Returns a runtime, which can used to initialise any number of ScriptInterfaces contexts.
JSContext * GetContext() const
void MaybeGC()
MaybeGC tries to determine whether garbage collection in cx's runtime would free up enough memory to ...
Helper for rooting large groups of script values.
void ReportError(const char *msg)
Report the given error message through the JS error reporting mechanism, and throw a JS exception...
std::auto_ptr< ScriptInterface_impl > m
shared_ptr< StructuredClone > WriteStructuredClone(jsval v)