Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Script::Interface Class Reference

#include <Interface.h>

Collaboration diagram for Script::Interface:

Classes

struct  CmptPrivate
 
struct  CustomType
 

Public Types

enum  JSObjectReservedSlots { PRIVATE = 0 }
 Name the reserved slots we may need to use in custom JSObjects. More...
 

Public Member Functions

 Interface (const char *nativeScopeName, const char *debugName, Context &context, std::function< bool(const VfsPath &)> allowModule={})
 Constructor. More...
 
template<typename Context >
 Interface (const char *nativeScopeName, const char *debugName, Context &&context, std::function< bool(const VfsPath &)> allowModule={})
 
 Interface (const char *nativeScopeName, const char *debugName, const Interface &neighbor, std::function< bool(const VfsPath &)> allowModule={})
 Alternate constructor. More...
 
 ~Interface ()
 
void SetCallbackData (void *pCBData)
 
ModuleLoader & GetModuleLoader () const
 
JSContext * GetGeneralJSContext () const
 GetGeneralJSContext returns the context without starting a GC request and without entering the Interface compartment. More...
 
Context & GetContext () const
 
bool LoadGlobalScripts ()
 Load global scripts that most script interfaces need, located in the /globalscripts directory. More...
 
bool ReplaceNondeterministicRNG (boost::random::rand48 &rng)
 Replace the default JS random number generator with a seeded, network-synced 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, unsigned int minArgs, JSPropertySpec *ps, JSFunctionSpec *fs, JSPropertySpec *static_ps, JSFunctionSpec *static_fs)
 
template<typename T >
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. More...
 
bool SetPrototype (JS::HandleValue obj, JS::HandleValue proto)
 
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::string &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...
 
bool Eval (const std::string_view code) const
 Evaluate some JS code in the global scope. More...
 
bool Eval (const std::string_view code, JS::MutableHandleValue out) const
 
template<typename T >
bool Eval (const std::string_view code, T &out) const
 
bool MathRandom (double &nbr) const
 Calls the random number generator assigned to this Interface instance and returns the generated number. More...
 

Static Public Member Functions

template<typename T >
static T * ObjectFromCBData (const Request &rq)
 Convert the CmptPrivate callback data to T*. More...
 
template<typename T >
static T * ObjectFromCBData (const Request &rq, JS::CallArgs &)
 Variant for the function wrapper. More...
 
static bool GetGlobalProperty (const Request &rq, const std::string &name, JS::MutableHandleValue out)
 Get an object from the global scope or any lexical scope. More...
 
static bool Math_random (JSContext *cx, unsigned int argc, JS::Value *vp)
 JSNative wrapper of the above. More...
 
template<typename T >
static T * GetPrivate (const Request &rq, JS::HandleObject thisobj)
 Retrieve the private data field of a JSObject. More...
 
template<typename T >
static T * GetPrivate (const Request &rq, JS::CallArgs &callArgs)
 Retrieve the private data field of a JS Object. More...
 
template<>
void * ObjectFromCBData (const Request &rq)
 

Private Member Functions

 NONCOPYABLE (Interface)
 
bool SetGlobal_ (const char *name, JS::HandleValue value, bool replace, bool constant, bool enumerate)
 

Private Attributes

CmptPrivate m_CmptPrivate
 
std::unique_ptr< Interface_impl > m
 
std::unordered_map< std::string, CustomType > m_CustomObjectTypes
 

Friends

class Request
 

Member Enumeration Documentation

◆ JSObjectReservedSlots

Name the reserved slots we may need to use in custom JSObjects.

When using JSCLASS_HAS_RESERVED_SLOTS in the definition of your JSClass, use the number of the highest slot you need plus 1.

Enumerator
PRIVATE 

Constructor & Destructor Documentation

◆ Interface() [1/3]

Script::Interface::Interface ( const char *  nativeScopeName,
const char *  debugName,
Context &  context,
std::function< bool(const VfsPath &)>  allowModule = {} 
)

Constructor.

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

◆ Interface() [2/3]

template<typename Context >
Script::Interface::Interface ( const char *  nativeScopeName,
const char *  debugName,
Context &&  context,
std::function< bool(const VfsPath &)>  allowModule = {} 
)
inline

◆ Interface() [3/3]

Script::Interface::Interface ( const char *  nativeScopeName,
const char *  debugName,
const Interface &  neighbor,
std::function< bool(const VfsPath &)>  allowModule = {} 
)

Alternate constructor.

This creates the new Realm in the same Compartment as the neighbor scriptInterface. This means that data can be freely exchanged between these two script interfaces without cloning.

Parameters
nativeScopeNameName of global object that functions (via Script::Function::Register) will be placed into, as a scoping mechanism; typically "Engine"
debugNameName of this interface for CScriptStats purposes.
scriptInterface'Neighbor' scriptInterface to share a compartment with.
allowModuleA predicate deciding wheter to import the given module.

◆ ~Interface()

Script::Interface::~Interface ( )

Member Function Documentation

◆ CallConstructor()

void Script::Interface::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).

◆ CreateCustomObject()

JSObject * Script::Interface::CreateCustomObject ( const std::string &  typeName) const

◆ DefineCustomObjectType()

void Script::Interface::DefineCustomObjectType ( JSClass *  clasp,
JSNative  constructor,
unsigned int  minArgs,
JSPropertySpec *  ps,
JSFunctionSpec *  fs,
JSPropertySpec *  static_ps,
JSFunctionSpec *  static_fs 
)

◆ Eval() [1/3]

bool Script::Interface::Eval ( const std::string_view  code) const

Evaluate some JS code in the global scope.

Returns
true on successful compilation and execution; false otherwise

◆ Eval() [2/3]

bool Script::Interface::Eval ( const std::string_view  code,
JS::MutableHandleValue  out 
) const

◆ Eval() [3/3]

template<typename T >
bool Script::Interface::Eval ( const std::string_view  code,
T &  out 
) const

◆ GetContext()

Context & Script::Interface::GetContext ( ) const

◆ GetGeneralJSContext()

JSContext * Script::Interface::GetGeneralJSContext ( ) const

GetGeneralJSContext returns the context without starting a GC request and without entering the Interface compartment.

It should only be used in specific situations, for instance when initializing a persistent rooted. If you need the compartmented context of the Interface, you should create a Interface::Request and use the context from that.

◆ GetGlobalProperty()

bool Script::Interface::GetGlobalProperty ( const Request &  rq,
const std::string &  name,
JS::MutableHandleValue  out 
)
static

Get an object from the global scope or any lexical scope.

This can return globally accessible objects even if they are not properties of the global object (e.g. ES6 class definitions).

Parameters
name- Name of the property.
outThe object or null.

◆ GetModuleLoader()

ModuleLoader & Script::Interface::GetModuleLoader ( ) const

◆ GetPrivate() [1/2]

template<typename T >
static T * Script::Interface::GetPrivate ( const Request &  rq,
JS::CallArgs &  callArgs 
)
inlinestatic

Retrieve the private data field of a JS Object.

If an error occurs, GetPrivate will report it with the according stack.

◆ GetPrivate() [2/2]

template<typename T >
static T * Script::Interface::GetPrivate ( const Request &  rq,
JS::HandleObject  thisobj 
)
inlinestatic

Retrieve the private data field of a JSObject.

◆ LoadGlobalScript()

bool Script::Interface::LoadGlobalScript ( const VfsPath &  filename,
const std::string &  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

◆ LoadGlobalScriptFile()

bool Script::Interface::LoadGlobalScriptFile ( const VfsPath &  path) const

Load and execute the given script in the global scope.

Returns
true on successful compilation and execution; false otherwise

◆ LoadGlobalScripts()

bool Script::Interface::LoadGlobalScripts ( )

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

VFS must be initialized.

◆ LoadScript()

bool Script::Interface::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

◆ Math_random()

bool Script::Interface::Math_random ( JSContext *  cx,
unsigned int  argc,
JS::Value *  vp 
)
static

JSNative wrapper of the above.

◆ MathRandom()

bool Script::Interface::MathRandom ( double &  nbr) const

Calls the random number generator assigned to this Interface instance and returns the generated number.

◆ NONCOPYABLE()

Script::Interface::NONCOPYABLE ( Interface  )
private

◆ ObjectFromCBData() [1/3]

template<>
void * Script::Interface::ObjectFromCBData ( const Request &  rq)
static

◆ ObjectFromCBData() [2/3]

template<typename T >
static T * Script::Interface::ObjectFromCBData ( const Request &  rq)
inlinestatic

Convert the CmptPrivate callback data to T*.

◆ ObjectFromCBData() [3/3]

template<typename T >
static T * Script::Interface::ObjectFromCBData ( const Request &  rq,
JS::CallArgs &   
)
inlinestatic

Variant for the function wrapper.

◆ ReplaceNondeterministicRNG()

bool Script::Interface::ReplaceNondeterministicRNG ( boost::random::rand48 &  rng)

Replace the default JS random number generator with a seeded, network-synced one.

◆ SetCallbackData()

void Script::Interface::SetCallbackData ( void *  pCBData)

◆ SetGlobal()

template<typename T >
bool Script::Interface::SetGlobal ( const char *  name,
const T &  value,
bool  replace = false,
bool  constant = true,
bool  enumerate = true 
)

Set the named property on the global object.

Optionally makes it {ReadOnly, DontEnum}. We do not allow to make it DontDelete, so that it can be hotloaded by deleting it and re-creating it, which is done by setting replace to true.

◆ SetGlobal_()

bool Script::Interface::SetGlobal_ ( const char *  name,
JS::HandleValue  value,
bool  replace,
bool  constant,
bool  enumerate 
)
private

◆ SetPrototype()

bool Script::Interface::SetPrototype ( JS::HandleValue  obj,
JS::HandleValue  proto 
)

Friends And Related Function Documentation

◆ Request

friend class Request
friend

Member Data Documentation

◆ m

std::unique_ptr<Interface_impl> Script::Interface::m
private

◆ m_CmptPrivate

CmptPrivate Script::Interface::m_CmptPrivate
private

◆ m_CustomObjectTypes

std::unordered_map<std::string, CustomType> Script::Interface::m_CustomObjectTypes
private

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