Pyrogenesis  trunk
Classes | Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
ScriptInterface Class Reference

Abstraction around a SpiderMonkey JSContext. More...

#include <ScriptInterface.h>

Collaboration diagram for ScriptInterface:
Collaboration graph
[legend]

Classes

struct  CustomType
 
struct  CxPrivate
 
struct  MaybeRef
 
class  StructuredClone
 Structured clones are a way to serialize 'simple' JS values into a buffer that can safely be passed between contexts and runtimes and threads. More...
 

Public Types

enum  CACHED_VAL { CACHE_VECTOR2DPROTO, CACHE_VECTOR3DPROTO }
 

Public Member Functions

 ScriptInterface (const char *nativeScopeName, const char *debugName, const shared_ptr< ScriptRuntime > &runtime)
 Constructor. More...
 
 ~ScriptInterface ()
 
void SetCallbackData (void *pCBData)
 
JSContext * GetContext () const
 
JSRuntime * GetJSRuntime () const
 
shared_ptr< ScriptRuntimeGetRuntime () const
 
bool LoadGlobalScripts ()
 Load global scripts that most script contexts need, located in the /globalscripts directory. More...
 
JS::Value GetCachedValue (CACHED_VAL valueIdentifier) const
 
bool ReplaceNondeterministicRNG (boost::rand48 &rng)
 Replace the default JS random number geenrator with a seeded, network-sync'd one. More...
 
void CallConstructor (JS::HandleValue ctor, JS::HandleValueArray argv, JS::MutableHandleValue out) const
 Call a constructor function, equivalent to JS "new ctor(arg)". More...
 
JSObject * CreateCustomObject (const std::string &typeName) const
 
void DefineCustomObjectType (JSClass *clasp, JSNative constructor, uint minArgs, JSPropertySpec *ps, JSFunctionSpec *fs, JSPropertySpec *static_ps, JSFunctionSpec *static_fs)
 
jsval GetGlobalObject ()
 
JSClass * GetGlobalClass ()
 
template<typename T >
bool SetGlobal (const char *name, const T &value, bool replace=false)
 Set the named property on the global object. More...
 
template<typename T >
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. More...
 
template<typename T >
bool SetProperty (JS::HandleValue obj, const wchar_t *name, const T &value, bool constant=false, bool enumerate=true)
 Set the named property on the given object. More...
 
template<typename T >
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. More...
 
template<typename T >
bool GetProperty (JS::HandleValue obj, const char *name, T &out) const
 Get the named property on the given object. More...
 
bool GetProperty (JS::HandleValue obj, const char *name, JS::MutableHandleValue out) const
 Get the named property of the given object. More...
 
bool GetProperty (JS::HandleValue obj, const char *name, JS::MutableHandleObject out) const
 
template<typename T >
bool GetPropertyInt (JS::HandleValue obj, int name, T &out) const
 Get the integer-named property on the given object. More...
 
bool GetPropertyInt (JS::HandleValue obj, int name, JS::MutableHandleValue out) const
 Get the named property of the given object. More...
 
bool HasProperty (JS::HandleValue obj, const char *name) const
 Check the named property has been defined on the given object. More...
 
bool EnumeratePropertyNamesWithPrefix (JS::HandleValue objVal, const char *prefix, std::vector< std::string > &out) const
 
bool SetPrototype (JS::HandleValue obj, JS::HandleValue proto)
 
bool FreezeObject (JS::HandleValue objVal, bool deep) const
 
bool Eval (const char *code) const
 
template<typename CHAR >
bool Eval (const CHAR *code, JS::MutableHandleValue out) const
 
template<typename T , typename CHAR >
bool Eval (const CHAR *code, T &out) const
 
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 error) or with toSource(). More...
 
bool ParseJSON (const std::string &string_utf8, JS::MutableHandleValue out) const
 Parse a UTF-8-encoded JSON string. More...
 
void ReadJSONFile (const VfsPath &path, JS::MutableHandleValue out) const
 Read a JSON file. More...
 
std::string StringifyJSON (JS::MutableHandleValue obj, bool indent=true) const
 Stringify to a JSON string, UTF-8 encoded. More...
 
void ReportError (const char *msg) const
 Report the given error message through the JS error reporting mechanism, and throw a JS exception. More...
 
bool LoadScript (const VfsPath &filename, const std::string &code) const
 Load and execute the given script in a new function scope. More...
 
bool LoadGlobalScript (const VfsPath &filename, const std::wstring &code) const
 Load and execute the given script in the global scope. More...
 
bool LoadGlobalScriptFile (const VfsPath &path) const
 Load and execute the given script in the global scope. More...
 
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 context. More...
 
void MaybeGC ()
 MaybeGC tries to determine whether garbage collection in cx's runtime would free up enough memory to be worth the amount of time it would take. More...
 
void ForceGC ()
 Triggers a full non-incremental garbage collection immediately. More...
 
bool MathRandom (double &nbr)
 MathRandom (this function) calls the random number generator assigned to this ScriptInterface instance and returns the generated number. More...
 
shared_ptr< StructuredCloneWriteStructuredClone (JS::HandleValue v)
 
void ReadStructuredClone (const shared_ptr< StructuredClone > &ptr, JS::MutableHandleValue ret)
 
template<typename R , typename... Ts>
bool CallFunction (JS::HandleValue val, const char *name, R &ret, const Ts &...params) const
 
template<typename R , typename... Ts>
bool CallFunction (JS::HandleValue val, const char *name, JS::Rooted< R > *ret, const Ts &...params) const
 
template<typename R , typename... Ts>
bool CallFunction (JS::HandleValue val, const char *name, JS::MutableHandle< R > ret, const Ts &...params) const
 
template<typename... Ts>
bool CallFunctionVoid (JS::HandleValue val, const char *name, const Ts &...params) const
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, SDL_Event_ const &val)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, IGUIObject *const &val)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, const CGUIString &val)
 
template<>
bool FromJSVal (JSContext *cx, JS::HandleValue v, CGUIString &out)
 
template<>
bool FromJSVal (JSContext *cx, JS::HandleValue v, bool &out)
 
template<>
bool FromJSVal (JSContext *cx, JS::HandleValue v, float &out)
 
template<>
bool FromJSVal (JSContext *cx, JS::HandleValue v, double &out)
 
template<>
bool FromJSVal (JSContext *cx, JS::HandleValue v, i32 &out)
 
template<>
bool FromJSVal (JSContext *cx, JS::HandleValue v, u32 &out)
 
template<>
bool FromJSVal (JSContext *cx, JS::HandleValue v, u16 &out)
 
template<>
bool FromJSVal (JSContext *cx, JS::HandleValue v, u8 &out)
 
template<>
bool FromJSVal (JSContext *cx, JS::HandleValue v, Path &out)
 
template<>
bool FromJSVal (JSContext *cx, JS::HandleValue v, CStr8 &out)
 
template<>
bool FromJSVal (JSContext *cx, JS::HandleValue v, CStrW &out)
 
template<>
bool FromJSVal (JSContext *cx, JS::HandleValue v, Entity &out)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, const bool &val)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, const float &val)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, const double &val)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, const i32 &val)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, const u16 &val)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, const u8 &val)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, const u32 &val)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, const Path &val)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, const wchar_t *const &val)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, const char *const &val)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, const CStrW &val)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, const CStr8 &val)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, const CVector2D &val)
 
template<>
bool FromJSVal (JSContext *cx, JS::HandleValue v, CVector2D &out)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, IComponent *const &val)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, CParamNode const &val)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, const CParamNode *const &val)
 
template<>
bool FromJSVal (JSContext *cx, JS::HandleValue v, CColor &out)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, CColor const &val)
 
template<>
bool FromJSVal (JSContext *cx, JS::HandleValue v, fixed &out)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, const fixed &val)
 
template<>
bool FromJSVal (JSContext *cx, JS::HandleValue v, CFixedVector3D &out)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, const CFixedVector3D &val)
 
template<>
bool FromJSVal (JSContext *cx, JS::HandleValue v, CFixedVector2D &out)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, const CFixedVector2D &val)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, const Grid< u8 > &val)
 
template<>
void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, const Grid< u16 > &val)
 
template<>
bool FromJSVal (JSContext *cx, JS::HandleValue v, TNSpline &out)
 
template<>
bool FromJSVal (JSContext *cx, JS::HandleValue v, CCinemaPath &out)
 

Static Public Member Functions

static shared_ptr< ScriptRuntimeCreateRuntime (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. More...
 
static CxPrivateGetScriptInterfaceAndCBData (JSContext *cx)
 
template<typename T >
static bool FromJSVal (JSContext *cx, const JS::HandleValue val, T &ret)
 Convert a jsval to a C++ type. More...
 
template<typename T >
static void ToJSVal (JSContext *cx, JS::MutableHandleValue ret, T const &val)
 Convert a C++ type to a jsval. More...
 
template<typename T >
static void AssignOrToJSVal (JSContext *cx, JS::MutableHandleValue handle, const T &a)
 Converts |a| if needed and assigns it to |handle|. More...
 
template<typename T >
static void AssignOrToJSValUnrooted (JSContext *cx, JS::MutableHandleValue handle, const T &a)
 The same as AssignOrToJSVal, but also allows JS::Value for T. More...
 
template<typename T >
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. More...
 
template<typename... Ts>
static size_t nargs ()
 

Public Attributes

struct ScriptInterface::CxPrivate m_CxPrivate
 

Private Member Functions

 NONCOPYABLE (ScriptInterface)
 
bool CallFunction_ (JS::HandleValue val, const char *name, JS::HandleValueArray argv, JS::MutableHandleValue ret) const
 
bool Eval_ (const char *code, JS::MutableHandleValue ret) const
 
bool Eval_ (const wchar_t *code, JS::MutableHandleValue ret) const
 
bool SetGlobal_ (const char *name, JS::HandleValue value, bool replace)
 
bool SetProperty_ (JS::HandleValue obj, const char *name, JS::HandleValue value, bool readonly, bool enumerate)
 
bool SetProperty_ (JS::HandleValue obj, const wchar_t *name, JS::HandleValue value, bool readonly, bool enumerate)
 
bool SetPropertyInt_ (JS::HandleValue obj, int name, JS::HandleValue value, bool readonly, bool enumerate)
 
bool GetProperty_ (JS::HandleValue obj, const char *name, JS::MutableHandleValue out) const
 
bool GetPropertyInt_ (JS::HandleValue obj, int name, JS::MutableHandleValue value) const
 
void Register (const char *name, JSNative fptr, size_t nargs)
 

Static Private Member Functions

static bool IsExceptionPending (JSContext *cx)
 
static const JSClass * GetClass (JS::HandleObject obj)
 
static void * GetPrivate (JS::HandleObject obj)
 

Private Attributes

std::unique_ptr< ScriptInterface_implm
 
boost::rand48 * m_rng
 
std::map< std::string, CustomTypem_CustomObjectTypes
 

Detailed Description

Abstraction around a SpiderMonkey JSContext.

Thread-safety:

Member Enumeration Documentation

Enumerator
CACHE_VECTOR2DPROTO 
CACHE_VECTOR3DPROTO 

Constructor & Destructor Documentation

ScriptInterface::ScriptInterface ( const char *  nativeScopeName,
const char *  debugName,
const shared_ptr< ScriptRuntime > &  runtime 
)

Constructor.

Parameters
nativeScopeNameName of global object that functions (via RegisterFunction) will be placed into, as a scoping mechanism; typically "Engine"
debugNameName of this interface for CScriptStats purposes.
runtimeScriptRuntime to use when initializing this interface.
ScriptInterface::~ScriptInterface ( )

Member Function Documentation

template<typename T >
T ScriptInterface::AssignOrFromJSVal ( JSContext *  cx,
const JS::HandleValue &  val,
bool &  ret 
)
inlinestatic

Converts |val| to T if needed or just returns it if it's a handle.

This is meant for use in other templates where we want to use the same code for JS::HandleValue and other types.

template<typename T >
void ScriptInterface::AssignOrToJSVal ( JSContext *  cx,
JS::MutableHandleValue  handle,
const T a 
)
inlinestatic

Converts |a| if needed and assigns it to |handle|.

This is meant for use in other templates where we want to use the same code for JS::RootedValue&/JS::HandleValue and other types. Note that functions are meant to take JS::HandleValue instead of JS::RootedValue&, but this implicit conversion does not work for templates (exact type matches required for type deduction). A similar functionality could also be implemented as a ToJSVal specialization. The current approach was preferred because "conversions" from JS::HandleValue to JS::MutableHandleValue are unusual and should not happen "by accident".

template<typename T >
static void ScriptInterface::AssignOrToJSValUnrooted ( JSContext *  cx,
JS::MutableHandleValue  handle,
const T a 
)
inlinestatic

The same as AssignOrToJSVal, but also allows JS::Value for T.

In most cases it's not safe to use the plain (unrooted) JS::Value type, but this can happen quite easily with template functions. The idea is that the linker prints an error if AssignOrToJSVal is used with JS::Value. If the specialization for JS::Value should be allowed, you can use this "unrooted" version of AssignOrToJSVal.

void ScriptInterface::CallConstructor ( JS::HandleValue  ctor,
JS::HandleValueArray  argv,
JS::MutableHandleValue  out 
) const

Call a constructor function, equivalent to JS "new ctor(arg)".

Parameters
ctorAn object that can be used as constructor
argvConstructor arguments
outThe new object; On error an error message gets logged and out is Null (out.isNull() == true).
template<typename R , typename... Ts>
bool ScriptInterface::CallFunction ( JS::HandleValue  val,
const char *  name,
R ret,
const Ts &...  params 
) const
template<typename R , typename... Ts>
bool ScriptInterface::CallFunction ( JS::HandleValue  val,
const char *  name,
JS::Rooted< R > *  ret,
const Ts &...  params 
) const
template<typename R , typename... Ts>
bool ScriptInterface::CallFunction ( JS::HandleValue  val,
const char *  name,
JS::MutableHandle< R ret,
const Ts &...  params 
) const
bool ScriptInterface::CallFunction_ ( JS::HandleValue  val,
const char *  name,
JS::HandleValueArray  argv,
JS::MutableHandleValue  ret 
) const
private
template<typename... Ts>
bool ScriptInterface::CallFunctionVoid ( JS::HandleValue  val,
const char *  name,
const Ts &...  params 
) const
JS::Value ScriptInterface::CloneValueFromOtherContext ( ScriptInterface otherContext,
JS::HandleValue  val 
)

Construct a new value (usable in this ScriptInterface's context) by cloning a value from a different context.

Complex values (functions, XML, etc) won't be cloned correctly, but basic types and cyclic references should be fine.

JSObject * ScriptInterface::CreateCustomObject ( const std::string &  typeName) const
shared_ptr< ScriptRuntime > ScriptInterface::CreateRuntime ( shared_ptr< ScriptRuntime parentRuntime = shared_ptr<ScriptRuntime>(),
int  runtimeSize = DEFAULT_RUNTIME_SIZE,
int  heapGrowthBytesGCTrigger = DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER 
)
static

Returns a runtime, which can used to initialise any number of ScriptInterfaces contexts.

Values created in one context may be used in any other context from the same runtime (but not any other runtime). Each runtime should only ever be used on a single thread.

Parameters
runtimeSizeMaximum size in bytes of the new runtime
void ScriptInterface::DefineCustomObjectType ( JSClass *  clasp,
JSNative  constructor,
uint  minArgs,
JSPropertySpec *  ps,
JSFunctionSpec *  fs,
JSPropertySpec *  static_ps,
JSFunctionSpec *  static_fs 
)
bool ScriptInterface::EnumeratePropertyNamesWithPrefix ( JS::HandleValue  objVal,
const char *  prefix,
std::vector< std::string > &  out 
) const
bool ScriptInterface::Eval ( const char *  code) const
template<typename CHAR >
bool ScriptInterface::Eval ( const CHAR code,
JS::MutableHandleValue  out 
) const
template<typename T , typename CHAR >
bool ScriptInterface::Eval ( const CHAR code,
T out 
) const
bool ScriptInterface::Eval_ ( const char *  code,
JS::MutableHandleValue  ret 
) const
private
bool ScriptInterface::Eval_ ( const wchar_t code,
JS::MutableHandleValue  ret 
) const
private
void ScriptInterface::ForceGC ( )

Triggers a full non-incremental garbage collection immediately.

That should only be required in special cases and normally you should try to use MaybeIncrementalRuntimeGC instead.

bool ScriptInterface::FreezeObject ( JS::HandleValue  objVal,
bool  deep 
) const
template<>
bool ScriptInterface::FromJSVal ( JSContext *  cx,
JS::HandleValue  v,
bool &  out 
)
template<>
bool ScriptInterface::FromJSVal ( JSContext *  cx,
JS::HandleValue  v,
float &  out 
)
template<>
bool ScriptInterface::FromJSVal ( JSContext *  cx,
JS::HandleValue  v,
double &  out 
)
template<>
bool ScriptInterface::FromJSVal ( JSContext *  cx,
JS::HandleValue  v,
i32 out 
)
template<>
bool ScriptInterface::FromJSVal ( JSContext *  cx,
JS::HandleValue  v,
CColor out 
)
template<>
bool ScriptInterface::FromJSVal ( JSContext *  cx,
JS::HandleValue  v,
u32 out 
)
template<>
bool ScriptInterface::FromJSVal ( JSContext *  cx,
JS::HandleValue  v,
u16 out 
)
template<>
bool ScriptInterface::FromJSVal ( JSContext *  cx,
JS::HandleValue  v,
u8 out 
)
template<>
bool ScriptInterface::FromJSVal ( JSContext *  cx,
JS::HandleValue  v,
CGUIString out 
)
template<>
bool ScriptInterface::FromJSVal ( JSContext *  cx,
JS::HandleValue  v,
fixed out 
)
template<>
bool ScriptInterface::FromJSVal ( JSContext *  cx,
JS::HandleValue  v,
Path out 
)
template<>
bool ScriptInterface::FromJSVal ( JSContext *  cx,
JS::HandleValue  v,
CFixedVector3D out 
)
template<>
bool ScriptInterface::FromJSVal ( JSContext *  cx,
JS::HandleValue  v,
CStr8 &  out 
)
template<>
bool ScriptInterface::FromJSVal ( JSContext *  cx,
JS::HandleValue  v,
CStrW &  out 
)
template<>
bool ScriptInterface::FromJSVal ( JSContext *  cx,
JS::HandleValue  v,
Entity out 
)
template<>
bool ScriptInterface::FromJSVal ( JSContext *  cx,
JS::HandleValue  v,
CFixedVector2D out 
)
template<typename T >
static bool ScriptInterface::FromJSVal ( JSContext *  cx,
const JS::HandleValue  val,
T ret 
)
static

Convert a jsval to a C++ type.

(This might trigger GC.)

template<>
bool ScriptInterface::FromJSVal ( JSContext *  cx,
JS::HandleValue  v,
TNSpline out 
)
template<>
bool ScriptInterface::FromJSVal ( JSContext *  cx,
JS::HandleValue  v,
CVector2D out 
)
template<>
bool ScriptInterface::FromJSVal ( JSContext *  cx,
JS::HandleValue  v,
CCinemaPath out 
)
JS::Value ScriptInterface::GetCachedValue ( CACHED_VAL  valueIdentifier) const
const JSClass * ScriptInterface::GetClass ( JS::HandleObject  obj)
staticprivate
JSContext * ScriptInterface::GetContext ( ) const
JSClass * ScriptInterface::GetGlobalClass ( )
jsval ScriptInterface::GetGlobalObject ( )
JSRuntime * ScriptInterface::GetJSRuntime ( ) const
void * ScriptInterface::GetPrivate ( JS::HandleObject  obj)
staticprivate
template<typename T >
bool ScriptInterface::GetProperty ( JS::HandleValue  obj,
const char *  name,
T out 
) const

Get the named property on the given object.

bool ScriptInterface::GetProperty ( JS::HandleValue  obj,
const char *  name,
JS::MutableHandleValue  out 
) const

Get the named property of the given object.

bool ScriptInterface::GetProperty ( JS::HandleValue  obj,
const char *  name,
JS::MutableHandleObject  out 
) const
bool ScriptInterface::GetProperty_ ( JS::HandleValue  obj,
const char *  name,
JS::MutableHandleValue  out 
) const
private
template<typename T >
bool ScriptInterface::GetPropertyInt ( JS::HandleValue  obj,
int  name,
T out 
) const

Get the integer-named property on the given object.

bool ScriptInterface::GetPropertyInt ( JS::HandleValue  obj,
int  name,
JS::MutableHandleValue  out 
) const

Get the named property of the given object.

bool ScriptInterface::GetPropertyInt_ ( JS::HandleValue  obj,
int  name,
JS::MutableHandleValue  value 
) const
private
shared_ptr< ScriptRuntime > ScriptInterface::GetRuntime ( ) const
ScriptInterface::CxPrivate * ScriptInterface::GetScriptInterfaceAndCBData ( JSContext *  cx)
static
bool ScriptInterface::HasProperty ( JS::HandleValue  obj,
const char *  name 
) const

Check the named property has been defined on the given object.

bool ScriptInterface::IsExceptionPending ( JSContext *  cx)
staticprivate
bool ScriptInterface::LoadGlobalScript ( const VfsPath filename,
const std::wstring &  code 
) const

Load and execute the given script in the global scope.

Parameters
filenameName for debugging purposes (not used to load the file)
codeJS code to execute
Returns
true on successful compilation and execution; false otherwise
bool ScriptInterface::LoadGlobalScriptFile ( const VfsPath path) const

Load and execute the given script in the global scope.

Returns
true on successful compilation and execution; false otherwise
bool ScriptInterface::LoadGlobalScripts ( )

Load global scripts that most script contexts need, located in the /globalscripts directory.

VFS must be initialized.

bool ScriptInterface::LoadScript ( const VfsPath filename,
const std::string &  code 
) const

Load and execute the given script in a new function scope.

Parameters
filenameName for debugging purposes (not used to load the file)
codeJS code to execute
Returns
true on successful compilation and execution; false otherwise
bool ScriptInterface::MathRandom ( double &  nbr)

MathRandom (this function) calls the random number generator assigned to this ScriptInterface instance and returns the generated number.

Math_random (with underscore, not this function) is a global function, but different random number generators can be stored per ScriptInterface. It calls MathRandom of the current ScriptInterface instance.

void ScriptInterface::MaybeGC ( )

MaybeGC tries to determine whether garbage collection in cx's runtime would free up enough memory to be worth the amount of time it would take.

This calls JS_MaybeGC directly, which does not do incremental GC. Usually you should prefer MaybeIncrementalRuntimeGC.

template<typename... Ts>
static size_t ScriptInterface::nargs ( )
inlinestatic
ScriptInterface::NONCOPYABLE ( ScriptInterface  )
private
bool ScriptInterface::ParseJSON ( const std::string &  string_utf8,
JS::MutableHandleValue  out 
) const

Parse a UTF-8-encoded JSON string.

Returns the unmodified value on error and prints an error message.

Returns
true on success; false otherwise
void ScriptInterface::ReadJSONFile ( const VfsPath path,
JS::MutableHandleValue  out 
) const

Read a JSON file.

Returns the unmodified value on error and prints an error message.

void ScriptInterface::ReadStructuredClone ( const shared_ptr< StructuredClone > &  ptr,
JS::MutableHandleValue  ret 
)
void ScriptInterface::Register ( const char *  name,
JSNative  fptr,
size_t  nargs 
)
private
bool ScriptInterface::ReplaceNondeterministicRNG ( boost::rand48 &  rng)

Replace the default JS random number geenrator with a seeded, network-sync'd one.

void ScriptInterface::ReportError ( const char *  msg) const

Report the given error message through the JS error reporting mechanism, and throw a JS exception.

(Callers can check IsPendingException, and must return false in that case to propagate the exception.)

void ScriptInterface::SetCallbackData ( void *  pCBData)
template<typename T >
bool ScriptInterface::SetGlobal ( const char *  name,
const T value,
bool  replace = false 
)

Set the named property on the global object.

If replace is true, an existing property will be overwritten; otherwise attempts to set an already-defined value will fail.

bool ScriptInterface::SetGlobal_ ( const char *  name,
JS::HandleValue  value,
bool  replace 
)
private
template<typename T >
bool ScriptInterface::SetProperty ( JS::HandleValue  obj,
const char *  name,
const T value,
bool  constant = false,
bool  enumerate = true 
)

Set the named property on the given object.

Optionally makes it {ReadOnly, DontDelete, DontEnum}.

template<typename T >
bool ScriptInterface::SetProperty ( JS::HandleValue  obj,
const wchar_t name,
const T value,
bool  constant = false,
bool  enumerate = true 
)

Set the named property on the given object.

Optionally makes it {ReadOnly, DontDelete, DontEnum}.

bool ScriptInterface::SetProperty_ ( JS::HandleValue  obj,
const char *  name,
JS::HandleValue  value,
bool  readonly,
bool  enumerate 
)
private
bool ScriptInterface::SetProperty_ ( JS::HandleValue  obj,
const wchar_t name,
JS::HandleValue  value,
bool  readonly,
bool  enumerate 
)
private
template<typename T >
bool ScriptInterface::SetPropertyInt ( JS::HandleValue  obj,
int  name,
const T value,
bool  constant = false,
bool  enumerate = true 
)

Set the integer-named property on the given object.

Optionally makes it {ReadOnly, DontDelete, DontEnum}.

bool ScriptInterface::SetPropertyInt_ ( JS::HandleValue  obj,
int  name,
JS::HandleValue  value,
bool  readonly,
bool  enumerate 
)
private
bool ScriptInterface::SetPrototype ( JS::HandleValue  obj,
JS::HandleValue  proto 
)
std::string ScriptInterface::StringifyJSON ( JS::MutableHandleValue  obj,
bool  indent = true 
) const

Stringify to a JSON string, UTF-8 encoded.

Returns an empty string on error.

template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
SDL_Event_ const &  val 
)
template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
IComponent *const &  val 
)
template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
CParamNode const &  val 
)
template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
const CParamNode *const &  val 
)
template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
CColor const &  val 
)
template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
IGUIObject *const &  val 
)
template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
const CGUIString val 
)
template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
const fixed val 
)
template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
const CFixedVector3D val 
)
template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
const bool &  val 
)
template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
const float &  val 
)
template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
const CFixedVector2D val 
)
template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
const double &  val 
)
template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
const i32 val 
)
template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
const u16 val 
)
template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
const u8 val 
)
template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
const u32 val 
)
template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
const Grid< u8 > &  val 
)
template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
const Path val 
)
template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
const wchar_t *const &  val 
)
template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
const Grid< u16 > &  val 
)
template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
const char *const &  val 
)
template<typename T >
static void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
T const &  val 
)
static

Convert a C++ type to a jsval.

(This might trigger GC. The return value must be rooted if you don't want it to be collected.) NOTE: We are passing the JS::Value by reference instead of returning it by value. The reason is a memory corruption problem that appears to be caused by a bug in Visual Studio. Details here: http://www.wildfiregames.com/forum/index.php?showtopic=17289&p=285921

template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
const CStrW &  val 
)
template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
const CStr8 &  val 
)
template<>
void ScriptInterface::ToJSVal ( JSContext *  cx,
JS::MutableHandleValue  ret,
const CVector2D val 
)
std::string ScriptInterface::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 error) or with toSource().

We have to use a mutable handle because JS_Stringify requires that for unknown reasons.

shared_ptr< ScriptInterface::StructuredClone > ScriptInterface::WriteStructuredClone ( JS::HandleValue  v)

Member Data Documentation

std::unique_ptr<ScriptInterface_impl> ScriptInterface::m
private
std::map<std::string, CustomType> ScriptInterface::m_CustomObjectTypes
private
struct ScriptInterface::CxPrivate ScriptInterface::m_CxPrivate
boost::rand48* ScriptInterface::m_rng
private

The documentation for this class was generated from the following files: