Gorgon Game Engine
VirtualMachine Class Reference

This class defines a virtual environment for scripts to run. More...

Public Member Functions

 VirtualMachine (bool automaticreset=true, std::ostream &out=std::cout, std::istream &in=std::cin)
 Default constructor. More...
 
 ~VirtualMachine ()
 
void Activate ()
 Activate this VM for this thread. More...
 
void AddLibrary (const Library &library)
 Includes a new library to be used in this virtual machine. More...
 
void AttachCommandConsole ()
 Creates a new InputSource using a console input provider. More...
 
void Begin (InputProvider &input)
 This method begins a new execution scope without starting execution. More...
 
void CompileCurrent ()
 Commands virtual machine to compile current execution scope. More...
 
ScopeInstanceCurrentScopeInstance () const
 Returns the current exection scope. More...
 
bool DetachCommandConsole ()
 If there is an attached command console, this function detaches that console, stops execution and returns true. More...
 
Data ExecuteFunction (const Function *fn, const std::vector< Data > &params, bool method)
 Executes a function in the current scope. More...
 
bool ExecuteStatement (const std::string &code, InputProvider::Dialect dialect=InputProvider::Programming)
 Executes a single statement in this virtual machine. More...
 
Data FindSymbol (const std::string &original, bool reference=false, bool allownull=false)
 Finds the given symbol and resolves its value. More...
 
std::istream & GetInput () const
 Returns the input stream. More...
 
SourceMarker GetMarkerForNext () const
 Returns the code marker for the next line. More...
 
std::ostream & GetOutput () const
 Returns the output stream. More...
 
Data GetReturnValue () const
 Returns the data returned from the last executed script. More...
 
unsigned GetScopeInstanceCount () const
 Returns the number of active execution scopes. More...
 
Variable GetVariable (const std::string &name)
 
Variablegetvarref (const std::string &var)
 Internal, returns pointer to the variable. Can return nullptr. Only searches in VM variables. More...
 
bool IsVariableSet (const std::string &name)
 
void Jump (SourceMarker marker)
 Changes the current executing line. More...
 
void Jump (unsigned long line)
 Changes the current executing line. More...
 
void LongJump (SourceMarker marker)
 Changes current executing line. More...
 
void RemoveLibrary (const Library &library)
 Removes a library. More...
 
void Reset ()
 Resets any runtime information that this VM has. More...
 
void ResetInput ()
 Resets the input stream to default stream that is given in the constructor. More...
 
void ResetOutput ()
 Resets the output stream to default stream that is given in the constructor. More...
 
void Return (Data value=Data::Invalid())
 Returns from the currently running script and sets return data to the given value. More...
 
void Run ()
 This method starts the virtual machine. More...
 
void Run (std::shared_ptr< ScopeInstance > scope)
 This method starts the virtual machine with the given scopeinstance. More...
 
void Run (unsigned executiontarget)
 This method starts the virtual machine. More...
 
void SetInput (std::istream &in)
 Redirects input stream to the given stream. More...
 
void SetOutput (std::ostream &out, bool deleteonchange=false)
 Redirects the output stream to the given stream. More...
 
void SetSpecialIdentifierHandler (std::function< Data(char, std::string)> handler)
 Sets the handler for special identifiers. More...
 
void SetVariable (const std::string &name, Data data, bool ref=false)
 
void Start (InputProvider &input)
 This method starts the virtual machine with the given input source. More...
 
void UnsetVariable (const std::string &name)
 
void UsingNamespace (const Namespace &name)
 Imports symbols of a namespace to the list of global symbols. More...
 
void UsingNamespace (const std::string &name)
 Imports symbols of a namespace to the list of global symbols. More...
 

Static Public Member Functions

static bool Exists ()
 Returns the current VM for this thread. More...
 
static VirtualMachineGet ()
 Returns the current VM for this thread. More...
 

Public Attributes

const Containers::Collection< const Library > & Libraries
 Allows read-only access to libraries. More...
 
ReferenceCounter References
 This system allows objects of automatic lifetime. More...
 

Detailed Description

This class defines a virtual environment for scripts to run.

Constructor & Destructor Documentation

◆ VirtualMachine()

VirtualMachine ( bool  automaticreset = true,
std::ostream &  out = std::cout,
std::istream &  in = std::cin 
)
explicit

◆ ~VirtualMachine()

Member Function Documentation

◆ Activate()

void Activate ( )

Activate this VM for this thread.

This VM will automatically activate when Start is issued, therefore, this is mostly used for debugging

◆ AddLibrary()

void AddLibrary ( const Library library)

Includes a new library to be used in this virtual machine.

References Member::GetName(), and VirtualMachine::GetScopeInstanceCount().

◆ AttachCommandConsole()

void AttachCommandConsole ( )

Creates a new InputSource using a console input provider.

Also creates an activates a new execution scope using this input source.

◆ Begin()

void Begin ( InputProvider input)

This method begins a new execution scope without starting execution.

References VirtualMachine::Activate(), and InputProvider::GetName().

◆ CompileCurrent()

void CompileCurrent ( )

Commands virtual machine to compile current execution scope.

Might cause issues with interactive input sources.

◆ CurrentScopeInstance()

ScopeInstance& CurrentScopeInstance ( ) const

Returns the current exection scope.

◆ DetachCommandConsole()

bool DetachCommandConsole ( )

If there is an attached command console, this function detaches that console, stops execution and returns true.

Otherwise, it returns false.

◆ ExecuteFunction()

Data ExecuteFunction ( const Function fn,
const std::vector< Data > &  params,
bool  method 
)

Executes a function in the current scope.

References Value::SetLiteral().

◆ ExecuteStatement()

bool ExecuteStatement ( const std::string &  code,
InputProvider::Dialect  dialect = InputProvider::Programming 
)

Executes a single statement in this virtual machine.

This operation will create a new input scope and run the code within that scope.

References VirtualMachine::Start().

◆ Exists()

static bool Exists ( )
static

Returns the current VM for this thread.

◆ FindSymbol()

◆ Get()

static VirtualMachine& Get ( )
static

Returns the current VM for this thread.

◆ GetInput()

std::istream& GetInput ( ) const

Returns the input stream.

◆ GetMarkerForNext()

SourceMarker GetMarkerForNext ( ) const

Returns the code marker for the next line.

◆ GetOutput()

std::ostream& GetOutput ( ) const

Returns the output stream.

◆ GetReturnValue()

Data GetReturnValue ( ) const

Returns the data returned from the last executed script.

◆ GetScopeInstanceCount()

unsigned GetScopeInstanceCount ( ) const

Returns the number of active execution scopes.

If this number is 0, VM cannot be started without providing additional code source.

◆ GetVariable()

Variable GetVariable ( const std::string &  name)

References ASSERT, and Data::IsValid().

◆ getvarref()

Variable * getvarref ( const std::string &  var)

Internal, returns pointer to the variable. Can return nullptr. Only searches in VM variables.

References ASSERT.

◆ IsVariableSet()

bool IsVariableSet ( const std::string &  name)

References ASSERT.

◆ Jump() [1/2]

void Jump ( SourceMarker  marker)

Changes the current executing line.

This function checks if the marker is in this execution scope. If not, it throws.

References SourceMarker::GetLine(), and SourceMarker::GetSource().

◆ Jump() [2/2]

void Jump ( unsigned long  line)

Changes the current executing line.

◆ LongJump()

void LongJump ( SourceMarker  marker)

Changes current executing line.

This function can jump to a previous execution point. Useful for keywords like try/catch

◆ RemoveLibrary()

void RemoveLibrary ( const Library library)

Removes a library.

Update symbol table

◆ Reset()

void Reset ( )

Resets any runtime information that this VM has.

This includes all scopes and global variables

◆ ResetInput()

void ResetInput ( )

Resets the input stream to default stream that is given in the constructor.

◆ ResetOutput()

void ResetOutput ( )

Resets the output stream to default stream that is given in the constructor.

◆ Return()

void Return ( Data  value = Data::Invalid())

Returns from the currently running script and sets return data to the given value.

◆ Run() [1/3]

void Run ( )

This method starts the virtual machine.

◆ Run() [2/3]

void Run ( std::shared_ptr< ScopeInstance scope)

This method starts the virtual machine with the given scopeinstance.

References VirtualMachine::Run().

◆ Run() [3/3]

void Run ( unsigned  executiontarget)

This method starts the virtual machine.

Parameters
executiontargetdepth of aimed execution. This value should be less than the current.

References Gorgon::Graphics::Color::Black, and Data::Invalid().

◆ SetInput()

void SetInput ( std::istream &  in)

Redirects input stream to the given stream.

◆ SetOutput()

void SetOutput ( std::ostream &  out,
bool  deleteonchange = false 
)

Redirects the output stream to the given stream.

◆ SetSpecialIdentifierHandler()

void SetSpecialIdentifierHandler ( std::function< Data(char, std::string)>  handler)

Sets the handler for special identifiers.

These are application defined variables and values. Unless they are returned as references, they will be considered as readonly. Characters @, %, !, and $ are treated as special identifiers. They could be functions too.

◆ SetVariable()

◆ Start()

void Start ( InputProvider input)

This method starts the virtual machine with the given input source.

References VirtualMachine::Begin(), and VirtualMachine::Run().

◆ UnsetVariable()

void UnsetVariable ( const std::string &  name)

References ASSERT.

◆ UsingNamespace() [1/2]

void UsingNamespace ( const Namespace name)

Imports symbols of a namespace to the list of global symbols.

References VirtualMachine::CurrentScopeInstance(), Member::GetName(), VirtualMachine::GetScopeInstanceCount(), and Namespace::Members.

◆ UsingNamespace() [2/2]

void UsingNamespace ( const std::string &  name)

Member Data Documentation

◆ Libraries

const Containers::Collection<const Library>& Libraries

Allows read-only access to libraries.

◆ References

ReferenceCounter References

This system allows objects of automatic lifetime.


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