![]() |
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. | |
int | run (bool fail_with_exception=false) |
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. | |
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. | |
bool | forceReloadPlugins () const |
Used by the REPL to force reload all the plugins and their bound methods. | |
Private Member Functions | |
int | safeRun (internal::ExecutionContext &context, std::size_t untilFrameCount=0, bool fail_with_exception=false) |
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. | |
Value * | loadSymbol (uint16_t id, internal::ExecutionContext &context) |
Value * | loadConstAsPtr (uint16_t id) const |
void | store (uint16_t id, const Value *val, internal::ExecutionContext &context) |
void | setVal (uint16_t id, const Value *val, internal::ExecutionContext &context) |
Value * | pop (internal::ExecutionContext &context) |
Pop a value from the stack. | |
void | push (const Value &value, internal::ExecutionContext &context) |
Push a value on the stack. | |
void | push (Value &&value, 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. | |
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 | backtrace (internal::ExecutionContext &context) noexcept |
Display a backtrace when the VM encounter an exception. | |
void | call (internal::ExecutionContext &context, uint16_t argc) |
Function called when the CALL instruction is met in the bytecode. | |
void | callBuiltin (internal::ExecutionContext &context, const Value &builtin, uint16_t argc) |
Builtin called when the CALL_BUILTIN instruction is met in the bytecode. | |
Static Private Member Functions | |
static void | throwVMError (internal::ErrorKind kind, const std::string &message) |
Throw a VM error message. | |
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 |
Friends | |
class | Value |
class | internal::Closure |
class | Repl |
|
explicitnoexcept |
|
privatenoexcept |
Display a backtrace when the VM encounter an exception.
context |
Definition at line 1462 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 |
|
inlineprivate |
ExecutionContext * Ark::VM::createAndGetContext | ( | ) |
Create an execution context and returns it.
This method is thread-safe VM wise.
Definition at line 227 of file VM.cpp.
References Ark::internal::ExecutionContext::locals, m_execution_contexts, m_mutex, and Ark::internal::ExecutionContext::stacked_closure_scopes.
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 256 of file VM.cpp.
References createAndGetContext(), m_futures, and m_mutex.
void Ark::VM::deleteContext | ( | internal::ExecutionContext * | ec | ) |
Free a given execution context.
This method is thread-safe VM wise.
ec |
Definition at line 242 of file VM.cpp.
References m_execution_contexts, and m_mutex.
void Ark::VM::deleteFuture | ( | internal::Future * | f | ) |
|
noexcept |
|
inlineprivatenoexcept |
|
privatenoexcept |
|
nodiscard |
Used by the REPL to force reload all the plugins and their bound methods.
Definition at line 282 of file VM.cpp.
References m_execution_contexts, m_shared_lib_objects, m_state, Ark::State::m_symbols, and Ark::mapping::name.
|
privatenoexcept |
Initialize the VM according to the parameters.
Definition at line 85 of file VM.cpp.
References 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::sp, and Ark::internal::ExecutionContext::stacked_closure_scopes.
Referenced by run().
|
inlineprivate |
|
private |
Load a plugin from a constant id.
id | Id of the constant |
context |
Definition at line 143 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, and throwVMError().
Referenced by safeRun().
|
inlineprivate |
|
noexcept |
Retrieve a value from the virtual machine, given its symbol name.
name | the name of the variable to retrieve |
Definition at line 118 of file VM.cpp.
References Ark::internal::Builtins::nil.
|
inlineprivate |
|
inlineprivate |
|
inlineprivate |
|
inlineprivate |
|
inlineprivate |
|
inline |
|
inlineprivate |
int Ark::VM::run | ( | bool | fail_with_exception = false | ) |
Run the bytecode held in the state.
fail_with_exception | throw if true, display a stacktrace if false |
Definition at line 313 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 |
fail_with_exception | throw if true, display a stacktrace if false |
Definition at line 320 of file VM.cpp.
References Ark::internal::ADD, Ark::Any, Ark::internal::APPEND, Ark::internal::APPEND_IN_PLACE, Ark::internal::ASSERT, Ark::internal::AT, Ark::internal::AT_AT, backtrace(), Ark::internal::BUILTIN, Ark::internal::Builtins::builtins, Ark::internal::CALL, call(), Ark::internal::CALL_BUILTIN, callBuiltin(), Ark::internal::CAPTURE, Ark::Closure, Ark::internal::CONCAT, Ark::internal::CONCAT_IN_PLACE, Ark::Value::constList(), Ark::internal::CREATE_SCOPE, Ark::internal::DECREMENT, Ark::internal::DEL, DISPATCH, Ark::internal::DIV, Ark::internal::DUP, Ark::internal::EMPTY, Ark::internal::EQ, Ark::internal::Builtins::falseSym, Ark::internal::ExecutionContext::fc, findNearestVariable(), Ark::internal::GE, Ark::types::generateError(), Ark::internal::GET_FIELD, GOTO_HALT, Ark::internal::GT, Ark::internal::HALT, Ark::internal::HASFIELD, Ark::internal::Closure::hasFieldEndingWith(), Ark::internal::HEAD, Ark::helper::head(), Ark::internal::INCREMENT, Ark::InstPtr, internal::Closure, Ark::internal::ExecutionContext::ip, Ark::Utils::isDouble(), Ark::Value::isIndexable(), Ark::internal::ISNIL, Ark::internal::JUMP, Ark::internal::ExecutionContext::last_symbol, Ark::internal::LE, Ark::internal::LEN, Ark::internal::LIST, Ark::List, Ark::Value::list(), Ark::internal::LOAD_CONST, Ark::internal::LOAD_CONST_LOAD_CONST, Ark::internal::LOAD_CONST_SET_VAL, Ark::internal::LOAD_CONST_STORE, Ark::internal::LOAD_SYMBOL, loadConstAsPtr(), loadPlugin(), loadSymbol(), Ark::internal::ExecutionContext::locals, Ark::internal::LT, Ark::State::m_constants, m_exit_code, Ark::State::m_pages, m_running, m_state, Ark::State::m_symbols, m_undefined_value, Ark::internal::MAKE_CLOSURE, Ark::internal::MOD, Ark::internal::MUL, Ark::internal::NEQ, Ark::internal::Builtins::nil, Ark::internal::NOP, Ark::internal::NOT, Ark::Number, Ark::Value::number(), Ark::Value::pageAddr(), Ark::internal::PLUGIN, Ark::internal::POP, pop(), Ark::internal::POP_JUMP_IF_FALSE, Ark::internal::POP_JUMP_IF_TRUE, Ark::internal::POP_LIST, Ark::internal::POP_LIST_IN_PLACE, Ark::internal::POP_SCOPE, popAndResolveAsPtr(), Ark::internal::ExecutionContext::pp, push(), Ark::Value::push_back(), Ark::Value::refClosure(), Ark::Reference, Ark::Value::reference(), Ark::internal::Closure::refScope(), Ark::internal::RET, returnFromFuncCall(), Ark::internal::ExecutionContext::saved_scope, Ark::internal::Scope, Ark::internal::Closure::scopePtr(), Ark::internal::SET_AT_2_INDEX, Ark::internal::SET_AT_INDEX, Ark::internal::SET_VAL, Ark::internal::SET_VAL_FROM, Ark::internal::SET_VAL_HEAD, Ark::internal::SET_VAL_TAIL, setVal(), Ark::internal::ExecutionContext::sp, Ark::internal::ExecutionContext::stack, Ark::internal::STORE, store(), Ark::internal::STORE_FROM, Ark::internal::STORE_HEAD, Ark::internal::STORE_TAIL, Ark::String, Ark::Value::string(), Ark::Value::stringRef(), Ark::internal::SUB, Ark::internal::TAIL, Ark::helper::tail(), TARGET, throwVMError(), Ark::internal::TO_NUM, Ark::internal::TO_STR, Ark::internal::Closure::toString(), Ark::Value::toString(), Ark::internal::Builtins::trueSym, Ark::internal::TYPE, Ark::types_to_str, UNPACK_ARGS, Ark::User, Value, Ark::Value::valueType(), and Ark::VMStackSize.
Referenced by run().
|
inlineprivate |
|
inlineprivate |
|
inlineprivate |
|
staticprivate |
Throw a VM error message.
kind | type of VM error |
message |
Definition at line 1457 of file VM.cpp.
References Ark::internal::errorKinds.
Referenced by loadPlugin(), and safeRun().
|
friend |
|
private |
Definition at line 158 of file VM.hpp.
Referenced by createAndGetContext(), deleteContext(), forceReloadPlugins(), init(), and run().
|
private |
|
private |
Storing the promises while we are resolving them.
Definition at line 163 of file VM.hpp.
Referenced by createFuture(), and deleteFuture().
|
private |
Definition at line 161 of file VM.hpp.
Referenced by createAndGetContext(), createFuture(), deleteContext(), and deleteFuture().
|
private |
|
private |
Definition at line 162 of file VM.hpp.
Referenced by forceReloadPlugins(), init(), and loadPlugin().
|
private |
Definition at line 157 of file VM.hpp.
Referenced by forceReloadPlugins(), Ark::internal::Closure::hasFieldEndingWith(), init(), loadPlugin(), and safeRun().
|
private |