![]() |
Gorgon Game Engine
|
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... | |
ScopeInstance & | CurrentScopeInstance () 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 > ¶ms, 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) |
Variable * | getvarref (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 VirtualMachine & | Get () |
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... | |
This class defines a virtual environment for scripts to run.
|
explicit |
~VirtualMachine | ( | ) |
void Activate | ( | ) |
Activate this VM for this thread.
This VM will automatically activate when Start is issued, therefore, this is mostly used for debugging
void AddLibrary | ( | const Library & | library | ) |
Includes a new library to be used in this virtual machine.
References Member::GetName(), and VirtualMachine::GetScopeInstanceCount().
void AttachCommandConsole | ( | ) |
Creates a new InputSource using a console input provider.
Also creates an activates a new execution scope using this input source.
void Begin | ( | InputProvider & | input | ) |
This method begins a new execution scope without starting execution.
References VirtualMachine::Activate(), and InputProvider::GetName().
void CompileCurrent | ( | ) |
Commands virtual machine to compile current execution scope.
Might cause issues with interactive input sources.
ScopeInstance& CurrentScopeInstance | ( | ) | const |
Returns the current exection scope.
bool DetachCommandConsole | ( | ) |
If there is an attached command console, this function detaches that console, stops execution and returns true.
Otherwise, it returns false.
Executes a function in the current scope.
References Value::SetLiteral().
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().
|
static |
Returns the current VM for this thread.
Data FindSymbol | ( | const std::string & | original, |
bool | reference = false , |
||
bool | allownull = false |
||
) |
Finds the given symbol and resolves its value.
References ASSERT, Type::CanMorphTo(), VirtualMachine::CurrentScopeInstance(), Gorgon::String::Extract(), Hashmap< K_, T_, KeyFn, M_, C_ >::Find(), ScopeInstance::FindSymbol(), Data::GetData(), Data::GetType(), Type::InheritedSymbols, Data::IsReference(), Data::IsValid(), Namespace::Members, Type::MorphTo(), Gorgon::Scripting::Types::Namespace(), Any::Pointer(), and Data::ReferenceValue().
|
static |
Returns the current VM for this thread.
std::istream& GetInput | ( | ) | const |
Returns the input stream.
SourceMarker GetMarkerForNext | ( | ) | const |
Returns the code marker for the next line.
std::ostream& GetOutput | ( | ) | const |
Returns the output stream.
Data GetReturnValue | ( | ) | const |
Returns the data returned from the last executed script.
unsigned GetScopeInstanceCount | ( | ) | const |
Returns the number of active execution scopes.
If this number is 0, VM cannot be started without providing additional code source.
Variable GetVariable | ( | const std::string & | name | ) |
References ASSERT, and Data::IsValid().
Variable * getvarref | ( | const std::string & | var | ) |
Internal, returns pointer to the variable. Can return nullptr. Only searches in VM variables.
References ASSERT.
bool IsVariableSet | ( | const std::string & | name | ) |
References ASSERT.
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().
void Jump | ( | unsigned long | line | ) |
Changes the current executing line.
void LongJump | ( | SourceMarker | marker | ) |
Changes current executing line.
This function can jump to a previous execution point. Useful for keywords like try/catch
void RemoveLibrary | ( | const Library & | library | ) |
Removes a library.
Update symbol table
void Reset | ( | ) |
Resets any runtime information that this VM has.
This includes all scopes and global variables
void ResetInput | ( | ) |
Resets the input stream to default stream that is given in the constructor.
void ResetOutput | ( | ) |
Resets the output stream to default stream that is given in the constructor.
void Return | ( | Data | value = Data::Invalid() | ) |
Returns from the currently running script and sets return data to the given value.
void Run | ( | ) |
This method starts the virtual machine.
void Run | ( | std::shared_ptr< ScopeInstance > | scope | ) |
This method starts the virtual machine with the given scopeinstance.
References VirtualMachine::Run().
void Run | ( | unsigned | executiontarget | ) |
This method starts the virtual machine.
executiontarget | depth of aimed execution. This value should be less than the current. |
References Gorgon::Graphics::Color::Black, and Data::Invalid().
void SetInput | ( | std::istream & | in | ) |
Redirects input stream to the given stream.
void SetOutput | ( | std::ostream & | out, |
bool | deleteonchange = false |
||
) |
Redirects the output stream to the given stream.
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.
void SetVariable | ( | const std::string & | name, |
Data | data, | ||
bool | ref = false |
||
) |
References ASSERT, Type::Assign(), Gorgon::Scripting::fixparameter(), VirtualMachine::Get(), Data::GetType(), ReferenceCounter::Increase(), Data::IsConstant(), Data::IsReference(), Type::IsReferenceType(), Data::IsValid(), VirtualMachine::References, Variable::Set(), and Variable::SetReferenceable().
void Start | ( | InputProvider & | input | ) |
This method starts the virtual machine with the given input source.
References VirtualMachine::Begin(), and VirtualMachine::Run().
void UnsetVariable | ( | const std::string & | name | ) |
References ASSERT.
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.
void UsingNamespace | ( | const std::string & | name | ) |
Imports symbols of a namespace to the list of global symbols.
References Type::CanMorphTo(), VirtualMachine::FindSymbol(), VirtualMachine::GetScopeInstanceCount(), Namespace::GetType(), Type::MorphTo(), Gorgon::Scripting::Types::Namespace(), Data::ReferenceValue(), and VirtualMachine::UsingNamespace().
const Containers::Collection<const Library>& Libraries |
Allows read-only access to libraries.
ReferenceCounter References |
This system allows objects of automatic lifetime.