HoverRace  2.0
Classes | Public Types | Public Member Functions | Public Attributes | Static Public Attributes | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
HoverRace::Script::Core Class Reference

A script environment. More...

#include <Core.h>

+ Inheritance diagram for HoverRace::Script::Core:

Classes

struct  Chunk
 
struct  PassReturn
 Return policy that leaves the return values on the stack. More...
 
struct  PrintReturn
 Return policy that calls Lua's print() function on each value. More...
 
struct  StackRestore
 Ensures that the Lua stack is unchanged in a block. More...
 

Public Types

using OutHandle = outs_t::iterator
 

Public Member Functions

 Core ()
 
 Core (const Core &core)=delete
 
virtual ~Core ()
 
void ActivateSandbox ()
 Activate the security sandbox. More...
 
OutHandle AddOutput (std::shared_ptr< std::ostream > out)
 Redirect output to a stream. More...
 
int Call (int numParams=0, Help::HelpHandler *helpHandler=nullptr)
 Pop a function off the stack and execute it. More...
 
void CallAndPrint (int numParams=0, Help::HelpHandler *helpHandler=nullptr)
 
void Compile (const Chunk &chunk)
 Compile a chunk of code. More...
 
template<class ReturnPolicy = PrintReturn>
void Execute (const Chunk &chunk, Help::HelpHandler *helpHandler=nullptr, ReturnPolicy rp=ReturnPolicy())
 Safely compile and execute a chunk of code. More...
 
lua_State * GetState () const
 
std::string GetVersionString () const
 Retrieve the full scripting version string (name and version). More...
 
template<class ReturnPolicy = PrintReturn>
int Invoke (int numParams=0, Help::HelpHandler *helpHandler=nullptr, ReturnPolicy rp=ReturnPolicy())
 Pop a function off the stack and execute it, printing any return values. More...
 
Coreoperator= (const Core &)=delete
 
void Print (const std::string &s)
 Print a message to all registered output streams. More...
 
void PrintStack ()
 
void RemoveOutput (const OutHandle &handle)
 
void ReqHelp (const std::string &className)
 Request the documentation for a scripting API class. More...
 
void ReqHelp (const std::string &className, const std::string &methodName)
 Request the documentation for a scripting API class or method. More...
 
virtual CoreReset ()
 Reset changes to the global environment. More...
 

Public Attributes

std::unique_ptr< luabind::object > NIL
 

Static Public Attributes

static const std::string DEFAULT_CHUNK_NAME
 

Private Types

using helpClasses_t = std::map< const std::string, std::shared_ptr< Help::Class >>
 
using outs_t = std::list< std::shared_ptr< std::ostream >>
 

Private Member Functions

void LoadClassHelp (const std::string &className)
 

Static Private Member Functions

static int ErrorFunc (lua_State *L)
 The error function for Luabind. More...
 
static int LPrint (lua_State *state)
 
static int LSandboxedFunction (lua_State *state)
 
static std::string PopError (lua_State *state)
 Pop the error message off the stack. More...
 
static void PrintFromStack (lua_State *state, int n)
 

Private Attributes

Help::HelpHandlercurHelpHandler
 
helpClasses_t helpClasses
 
outs_t outs
 
lua_State * state
 

Detailed Description

A script environment.

Author
Michael Imamura

Member Typedef Documentation

using HoverRace::Script::Core::helpClasses_t = std::map< const std::string, std::shared_ptr<Help::Class>>
private
using HoverRace::Script::Core::OutHandle = outs_t::iterator
using HoverRace::Script::Core::outs_t = std::list<std::shared_ptr<std::ostream>>
private

Constructor & Destructor Documentation

HoverRace::Script::Core::Core ( )
HoverRace::Script::Core::Core ( const Core core)
delete
HoverRace::Script::Core::~Core ( )
virtual

Member Function Documentation

void HoverRace::Script::Core::ActivateSandbox ( )

Activate the security sandbox.

This guards against methods a script may use to access the filesystem or break out of the sandbox.

Core::OutHandle HoverRace::Script::Core::AddOutput ( std::shared_ptr< std::ostream >  out)

Redirect output to a stream.

Parameters
outThe output stream (wrapped in a shared pointer). May be NULL to use the system default.
Returns
A handle for removing the stream later.
int HoverRace::Script::Core::Call ( int  numParams = 0,
Help::HelpHandler helpHandler = nullptr 
)

Pop a function off the stack and execute it.

Parameters
numParamsThe number of params being passed to the function.
helpHandlerOptional callback for when a script requests API help.
Returns
The number of return values from the function that remain on the stack.
Exceptions
ScriptExnThe code signaled an error while executing.
void HoverRace::Script::Core::CallAndPrint ( int  numParams = 0,
Help::HelpHandler helpHandler = nullptr 
)
inline
Deprecated:
Use Invoke instead.
void HoverRace::Script::Core::Compile ( const Chunk chunk)

Compile a chunk of code.

Upon successful execution, the compiled chunk will be pushed to the stack.

Parameters
chunkThe chunk to compile.
Exceptions
IncompleteExnIf the code does not complete a statement; i.e., expecting more tokens. Callers can catch this to keep reading more data to finish the statement.
ScriptExnThe code failed to compile.
int HoverRace::Script::Core::ErrorFunc ( lua_State *  L)
staticprivate

The error function for Luabind.

We use the default Lua function for non-Luabind calls.

Parameters
LLua state.
Returns
template<class ReturnPolicy = PrintReturn>
void HoverRace::Script::Core::Execute ( const Chunk chunk,
Help::HelpHandler helpHandler = nullptr,
ReturnPolicy  rp = ReturnPolicy() 
)
inline

Safely compile and execute a chunk of code.

This is a convenience function.

Parameters
chunkThe code to execute.
helpHandlerOptional callback for when a script requests API help.
rpOptional policy to handle return values.
Exceptions
IncompleteExnIf the code does not complete a statement; i.e., expecting more tokens. Callers can catch this to keep reading more data to finish the statement.
ScriptExnThe code either failed to compile or signaled an error while executing.
lua_State* HoverRace::Script::Core::GetState ( ) const
inline
std::string HoverRace::Script::Core::GetVersionString ( ) const

Retrieve the full scripting version string (name and version).

Returns
The string (never empty).
template<class ReturnPolicy = PrintReturn>
int HoverRace::Script::Core::Invoke ( int  numParams = 0,
Help::HelpHandler helpHandler = nullptr,
ReturnPolicy  rp = ReturnPolicy() 
)
inline

Pop a function off the stack and execute it, printing any return values.

Parameters
numParamsThe number of params being passed to the function.
helpHandlerOptional callback for when a script requests API help.
rpOptional policy to handle return values.
Returns
The number of return values remaining on the stack.
Exceptions
ScriptExnThe code signaled an error while executing.
void HoverRace::Script::Core::LoadClassHelp ( const std::string &  className)
private
int HoverRace::Script::Core::LPrint ( lua_State *  state)
staticprivate
int HoverRace::Script::Core::LSandboxedFunction ( lua_State *  state)
staticprivate
Core& HoverRace::Script::Core::operator= ( const Core )
delete
std::string HoverRace::Script::Core::PopError ( lua_State *  state)
staticprivate

Pop the error message off the stack.

Only call this if you KNOW that there is an error on the top of the stack.

Parameters
stateThe Lua context.
Returns
The error as a string.
void HoverRace::Script::Core::Print ( const std::string &  s)

Print a message to all registered output streams.

Parameters
sThe message to broadcast (a newline will be appended automatically).
void HoverRace::Script::Core::PrintFromStack ( lua_State *  state,
int  n 
)
staticprivate
void HoverRace::Script::Core::PrintStack ( )
void HoverRace::Script::Core::RemoveOutput ( const OutHandle handle)
void HoverRace::Script::Core::ReqHelp ( const std::string &  className)

Request the documentation for a scripting API class.

This must only be called from a script peer as the result of a Lua invocation!

Parameters
classNameThe name of the class (may not be blank).
void HoverRace::Script::Core::ReqHelp ( const std::string &  className,
const std::string &  methodName 
)

Request the documentation for a scripting API class or method.

This must only be called from a script peer as the result of a Lua invocation!

Parameters
classNameThe name of the class (may not be blank).
methodNameThe name of the method within the class (may be blank to retrieve the class documentation instead of the method documentation).
Core * HoverRace::Script::Core::Reset ( )
virtual

Reset changes to the global environment.

When creating a new instance, this member function must be called at least once before executing any scripts. This is used for fixing accidental changes to globals. Note that this will effectively deactivate the security sandbox. Call ActivateSandbox() to reactivate if necessary. The state returned by GetState() is otherwise unchanged.

Returns
The same instance.

Reimplemented in HoverRace::Client::HoverScript::ClientScriptCore.

Member Data Documentation

Help::HelpHandler* HoverRace::Script::Core::curHelpHandler
private
const std::string HoverRace::Script::Core::DEFAULT_CHUNK_NAME
static
helpClasses_t HoverRace::Script::Core::helpClasses
private
std::unique_ptr<luabind::object> HoverRace::Script::Core::NIL
outs_t HoverRace::Script::Core::outs
private
lua_State* HoverRace::Script::Core::state
private

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