ArkScript
A small, fast, functional and scripting language for video games
|
The ArkScript virtual machine, executing ArkScript bytecode. More...
#include <VM.hpp>
Public Member Functions | |
VM (State &state) noexcept | |
Construct a new vm t object. | |
VM (State *state) noexcept | |
int | run () noexcept |
Run the bytecode held in the state. | |
Value & | operator[] (const std::string &name) noexcept |
Retrieve a value from the virtual machine, given its symbol name. | |
template<typename... Args> | |
Value | call (const std::string &name, Args &&... args) |
Call a function from ArkScript, by giving it arguments. | |
template<typename... Args> | |
Value | resolve (const Value *val, Args &&... args) |
Resolving a function call (called by plugins and builtins) | |
Value | resolve (internal::ExecutionContext *context, std::vector< Value > &n) |
Resolves a function call (called by plugins and builtins) | |
void | exit (int code) noexcept |
Ask the VM to exit with a given exit code. | |
void | setUserPointer (void *ptr) noexcept |
Set the User Pointer object. | |
void * | getUserPointer () noexcept |
Retrieves the stored pointer. | |
internal::ExecutionContext * | createAndGetContext () |
Create an execution context and returns it. | |
void | deleteContext (internal::ExecutionContext *ec) |
Free a given execution context. | |
internal::Future * | createFuture (std::vector< Value > &args) |
Create a Future object from a function and its arguments and return a managed pointer to it. | |
void | deleteFuture (internal::Future *f) |
Free a given future. | |
Private Member Functions | |
int | safeRun (internal::ExecutionContext &context, std::size_t untilFrameCount=0) |
Run ArkScript bytecode inside a try catch to retrieve all the exceptions and display a stack trace if needed. | |
void | init () noexcept |
Initialize the VM according to the parameters. | |
uint16_t | readNumber (internal::ExecutionContext &context) |
Read a 2 bytes number from the current bytecode page, starting at the current instruction. | |
Value * | pop (internal::ExecutionContext &context) |
Pop a value from the stack. | |
void | push (const Value &val, internal::ExecutionContext &context) |
Push a value on the stack. | |
void | push (Value &&val, internal::ExecutionContext &context) |
Push a value on the stack. | |
void | push (Value *valptr, internal::ExecutionContext &context) |
Push a value on the stack as a reference. | |
Value * | popAndResolveAsPtr (internal::ExecutionContext &context) |
Pop a value from the stack and resolve it if possible, then return it. | |
void | swapStackForFunCall (uint16_t argc, internal::ExecutionContext &context) |
Move stack values around and invert them. | |
void | createNewScope (internal::ExecutionContext &context) noexcept |
Value * | findNearestVariable (uint16_t id, internal::ExecutionContext &context) noexcept |
Find the nearest variable of a given id. | |
void | returnFromFuncCall (internal::ExecutionContext &context) |
Destroy the current frame and get back to the previous one, resuming execution. | |
void | loadPlugin (uint16_t id, internal::ExecutionContext &context) |
Load a plugin from a constant id. | |
uint16_t | findNearestVariableIdWithValue (const Value &value, internal::ExecutionContext &context) const noexcept |
Find the nearest variable id with a given value. | |
void | throwVMError (internal::ErrorKind kind, const std::string &message) |
Throw a VM error message. | |
void | backtrace (internal::ExecutionContext &context) noexcept |
Display a backtrace when the VM encounter an exception. | |
void | call (internal::ExecutionContext &context, int16_t argc_=-1) |
Function called when the CALL instruction is met in the bytecode. | |
Private Attributes | |
State & | m_state |
std::vector< std::unique_ptr< internal::ExecutionContext > > | m_execution_contexts |
int | m_exit_code |
VM exit code, defaults to 0. Can be changed through sys:exit | |
bool | m_running |
std::mutex | m_mutex |
std::vector< std::shared_ptr< internal::SharedLibrary > > | m_shared_lib_objects |
std::vector< std::unique_ptr< internal::Future > > | m_futures |
Storing the promises while we are resolving them. | |
Value | m_no_value = internal::Builtins::nil |
Value | m_undefined_value |
void * | m_user_pointer |
needed to pass data around when binding ArkScript in a program | |
Friends | |
class | Value |
class | internal::Closure |
class | Repl |
|
explicitnoexcept |
|
privatenoexcept |
Display a backtrace when the VM encounter an exception.
context |
Definition at line 1227 of file VM.cpp.
References Ark::InstPtr, Ark::internal::Scope::m_data, Ark::Value::pageAddr(), Ark::internal::Scope::size(), and Ark::Value::valueType().
Referenced by safeRun().
Value Ark::VM::call | ( | const std::string & | name, |
Args &&... | args | ||
) |
|
inlineprivate |
Function called when the CALL instruction is met in the bytecode.
context | |
argc_ | number of arguments already sent, default to -1 if it needs to search for them by itself |
ExecutionContext * Ark::VM::createAndGetContext | ( | ) |
Create an execution context and returns it.
This method is thread-safe VM wise.
Definition at line 185 of file VM.cpp.
References Ark::internal::ExecutionContext::locals, m_execution_contexts, m_mutex, and Ark::internal::ExecutionContext::scope_count_to_delete.
Referenced by createFuture().
Create a Future object from a function and its arguments and return a managed pointer to it.
This method is thread-safe VM wise.
args |
Definition at line 216 of file VM.cpp.
References createAndGetContext(), m_futures, and m_mutex.
|
inlineprivatenoexcept |
Referenced by init().
void Ark::VM::deleteContext | ( | internal::ExecutionContext * | ec | ) |
Free a given execution context.
This method is thread-safe VM wise.
ec |
Definition at line 203 of file VM.cpp.
References m_execution_contexts, and m_mutex.
Referenced by Ark::internal::Future::resolve().
void Ark::VM::deleteFuture | ( | internal::Future * | f | ) |
|
noexcept |
|
inlineprivatenoexcept |
Find the nearest variable of a given id.
id | the id to find |
context |
Referenced by safeRun().
|
privatenoexcept |
|
noexcept |
Retrieves the stored pointer.
Definition at line 180 of file VM.cpp.
References m_user_pointer.
|
privatenoexcept |
Initialize the VM according to the parameters.
Definition at line 33 of file VM.cpp.
References createNewScope(), Ark::internal::ExecutionContext::fc, Ark::internal::ExecutionContext::locals, Ark::State::m_binded, m_execution_contexts, m_exit_code, m_shared_lib_objects, m_state, Ark::State::m_symbols, Ark::internal::ExecutionContext::saved_scope, Ark::internal::ExecutionContext::scope_count_to_delete, and Ark::internal::ExecutionContext::sp.
Referenced by Ark::Repl::run(), and run().
|
private |
Load a plugin from a constant id.
id | Id of the constant |
context |
Definition at line 88 of file VM.cpp.
References ARK_NO_NAME_FILE, Ark::Utils::fileExists(), Ark::internal::ExecutionContext::locals, Ark::State::m_constants, Ark::State::m_filename, Ark::State::m_libenv, m_shared_lib_objects, m_state, Ark::State::m_symbols, Ark::mapping::name, throwVMError(), and Value.
Referenced by safeRun().
|
noexcept |
Retrieve a value from the virtual machine, given its symbol name.
name | the name of the variable to retrieve |
Definition at line 66 of file VM.cpp.
References Ark::internal::Builtins::nil.
|
inlineprivate |
|
inlineprivate |
Pop a value from the stack and resolve it if possible, then return it.
context |
Referenced by safeRun().
|
inlineprivate |
|
inlineprivate |
Push a value on the stack.
val | |
context |
|
inlineprivate |
Push a value on the stack as a reference.
valptr | |
context |
|
inlineprivate |
Read a 2 bytes number from the current bytecode page, starting at the current instruction.
Modify the instruction pointer to point on the instruction right after the number.
context |
Referenced by safeRun().
Resolving a function call (called by plugins and builtins)
Args |
val | the ArkScript function object |
args | C++ argument list |
Referenced by Ark::internal::Future::Future().
|
inline |
Resolves a function call (called by plugins and builtins)
context | the execution context to use |
n | the function and its arguments |
|
inlineprivate |
Destroy the current frame and get back to the previous one, resuming execution.
Doing the job nobody wants to do: cleaning after everyone has finished to play. This is a sort of primitive garbage collector
context |
Referenced by safeRun().
|
noexcept |
Run the bytecode held in the state.
Definition at line 245 of file VM.cpp.
References init(), m_execution_contexts, m_exit_code, and safeRun().
Referenced by main().
|
private |
Run ArkScript bytecode inside a try catch to retrieve all the exceptions and display a stack trace if needed.
context | |
untilFrameCount | the frame count we need to reach before stopping the VM |
Definition at line 260 of file VM.cpp.
References Ark::Any, backtrace(), Ark::internal::Builtins::builtins, call(), Ark::Closure, Ark::Value::constList(), Ark::internal::Builtins::falseSym, Ark::internal::ExecutionContext::fc, findNearestVariable(), Ark::types::generateError(), Ark::InstPtr, internal::Closure, Ark::internal::ExecutionContext::ip, Ark::Value::isConst(), Ark::Utils::isDouble(), Ark::internal::ExecutionContext::last_symbol, Ark::List, Ark::Value::list(), loadPlugin(), Ark::internal::ExecutionContext::locals, Ark::State::m_constants, Ark::State::m_debug_level, m_exit_code, Ark::State::m_pages, m_running, m_state, Ark::State::m_symbols, Ark::internal::Builtins::nil, Ark::Number, Ark::Value::number(), Ark::PageAddr, Ark::Value::pageAddr(), pop(), popAndResolveAsPtr(), Ark::internal::ExecutionContext::pp, push(), Ark::Value::push_back(), readNumber(), Ark::Value::refClosure(), Ark::Reference, Ark::Value::reference(), returnFromFuncCall(), Ark::internal::ExecutionContext::saved_scope, Ark::internal::Closure::scope(), Ark::internal::ExecutionContext::scope_count_to_delete, Ark::Value::setConst(), Ark::internal::ExecutionContext::sp, Ark::String, Ark::Value::string(), Ark::Value::stringRef(), throwVMError(), Ark::Value::toString(), Ark::internal::Builtins::trueSym, Ark::types_to_str, Ark::User, Value, Ark::Value::valueType(), and Ark::VMStackSize.
Referenced by Ark::Repl::run(), and run().
|
noexcept |
|
inlineprivate |
Move stack values around and invert them.
values: 1, 2, 3, _, _ wanted: pp, ip, 3, 2, 1 positions: 0, 1, 2, 3, 4
argc | number of arguments to swap around |
context |
|
private |
Throw a VM error message.
kind | type of VM error |
message |
Definition at line 1222 of file VM.cpp.
References Ark::internal::errorKinds.
Referenced by loadPlugin(), and safeRun().
|
friend |
|
friend |
Definition at line 165 of file VM.hpp.
Referenced by loadPlugin(), and safeRun().
|
private |
Definition at line 171 of file VM.hpp.
Referenced by createAndGetContext(), deleteContext(), init(), Ark::Repl::run(), and run().
|
private |
|
private |
Storing the promises while we are resolving them.
Definition at line 176 of file VM.hpp.
Referenced by createFuture(), and deleteFuture().
|
private |
Definition at line 174 of file VM.hpp.
Referenced by createAndGetContext(), createFuture(), deleteContext(), and deleteFuture().
|
private |
|
private |
Definition at line 175 of file VM.hpp.
Referenced by init(), and loadPlugin().
|
private |
Definition at line 170 of file VM.hpp.
Referenced by init(), loadPlugin(), and safeRun().
|
private |
needed to pass data around when binding ArkScript in a program
Definition at line 182 of file VM.hpp.
Referenced by getUserPointer().