![]() |
ArkScript
A small, lisp-inspired, functional scripting language
|
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. | |
Value | resolve (internal::ExecutionContext *context, const 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 * | getDefaultContext () |
Return a pointer to the first execution context, for the main thread of the app. | |
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. | |
Static Public Member Functions | |
static void | throwVMError (internal::ErrorKind kind, const std::string &message) |
Throw a VM error message. | |
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) |
Load a symbol by its id in the current context. Performs a lookup in the scope stack, in reverse order. | |
Value * | loadSymbolFromIndex (uint16_t index, internal::ExecutionContext &context) |
Load a symbol by its (reversed) index in the current scope. | |
Value * | loadConstAsPtr (uint16_t id) const |
Load a constant from the constant table by its id. | |
void | store (uint16_t id, const Value *val, internal::ExecutionContext &context) |
Create a new symbol with an associated value in the current scope. | |
void | setVal (uint16_t id, const Value *val, internal::ExecutionContext &context) |
Change the value of a symbol given its identifier. | |
Value | getField (Value *closure, uint16_t id, internal::ExecutionContext &context) |
Value | createList (std::size_t count, internal::ExecutionContext &context) |
void | listAppendInPlace (Value *list, std::size_t count, internal::ExecutionContext &context) |
Value * | pop (internal::ExecutionContext &context) |
Pop a value from the stack. | |
Value * | peekAndResolveAsPtr (internal::ExecutionContext &context) |
Return a pointer to the top of the stack without consuming it, and resolve it if possible. | |
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. | |
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 | throwArityError (std::size_t passed_arg_count, std::size_t expected_arg_count, internal::ExecutionContext &context) |
void | showBacktraceWithException (const std::exception &e, internal::ExecutionContext &context) |
std::optional< internal::InstLoc > | findSourceLocation (std::size_t ip, std::size_t pp) |
Find the nearest source location information given instruction and page pointers. | |
std::string | debugShowSource () |
void | backtrace (internal::ExecutionContext &context, std::ostream &os=std::cout, bool colorize=true) |
Display a backtrace when the VM encounter an exception. | |
void | call (internal::ExecutionContext &context, uint16_t argc, Value *function_ptr=nullptr, internal::PageAddr_t or_address=0) |
Function called when the CALL instruction is met in the bytecode. | |
void | callBuiltin (internal::ExecutionContext &context, const Value &builtin, uint16_t argc, bool remove_return_address=true) |
Builtin called when the CALL_BUILTIN 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 |
Friends | |
class | Value |
class | internal::Closure |
class | Repl |
|
explicitnoexcept |
|
private |
Display a backtrace when the VM encounter an exception.
context | |
os | |
colorize |
Definition at line 2022 of file VM.cpp.
References Ark::internal::ScopeView::atPos(), Ark::internal::ExecutionContext::fc, Ark::Utils::fileExists(), findNearestVariableIdWithValue(), findSourceLocation(), Ark::InstPtr, Ark::internal::ExecutionContext::ip, Ark::internal::ExecutionContext::locals, Ark::State::m_filenames, m_state, Ark::State::m_symbols, Ark::Diagnostics::makeContext(), Ark::Value::pageAddr(), pop(), popAndResolveAsPtr(), Ark::internal::ExecutionContext::pp, returnFromFuncCall(), Ark::internal::ScopeView::size(), Ark::internal::ExecutionContext::sp, Value, and Ark::Value::valueType().
Referenced by safeRun(), and showBacktraceWithException().
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 |
function_ptr | optional pointer to the function to call. If not provided, obtain it from the stack (unless or_address is not 0) |
or_address | optional page address, used if non-zero and function_ptr is nullptr |
|
inlineprivate |
ExecutionContext * Ark::VM::createAndGetContext | ( | ) |
Create an execution context and returns it.
This method is thread-safe VM wise.
Definition at line 343 of file VM.cpp.
References Ark::internal::ExecutionContext::locals, m_execution_contexts, m_mutex, Ark::internal::ExecutionContext::scopes_storage, 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 378 of file VM.cpp.
References createAndGetContext(), Ark::internal::ExecutionContext::last_symbol, m_execution_contexts, m_futures, and m_mutex.
|
private |
Definition at line 205 of file VM.cpp.
References Ark::List, Ark::Value::list(), popAndResolveAsPtr(), and Ark::Value::push_back().
Referenced by safeRun().
|
private |
Definition at line 2010 of file VM.cpp.
References findSourceLocation(), m_execution_contexts, Ark::State::m_filenames, and m_state.
void Ark::VM::deleteContext | ( | internal::ExecutionContext * | ec | ) |
Free a given execution context.
This method is thread-safe VM wise.
ec |
Definition at line 364 of file VM.cpp.
References m_execution_contexts, and m_mutex.
void Ark::VM::deleteFuture | ( | internal::Future * | f | ) |
|
noexcept |
|
inlineprivatenoexcept |
|
privatenoexcept |
Find the nearest variable id with a given value.
Only used to display the call stack traceback
value | the value to search for |
context |
Definition at line 1905 of file VM.cpp.
Referenced by backtrace().
|
private |
Find the nearest source location information given instruction and page pointers.
ip | |
pp |
Definition at line 1987 of file VM.cpp.
References Ark::State::m_inst_locations, and m_state.
Referenced by backtrace(), and debugShowSource().
|
nodiscard |
Used by the REPL to force reload all the plugins and their bound methods.
Definition at line 407 of file VM.cpp.
References m_execution_contexts, m_shared_lib_objects, m_state, Ark::State::m_symbols, and Ark::mapping::name.
Referenced by Ark::Repl::run().
|
inline |
Return a pointer to the first execution context, for the main thread of the app.
|
private |
Definition at line 158 of file VM.cpp.
References Ark::internal::CALL, Ark::Closure, Ark::internal::Closure::hasFieldEndingWith(), Ark::State::inst(), internal::Closure, Ark::internal::ExecutionContext::ip, Ark::internal::ExecutionContext::last_symbol, m_state, Ark::State::m_symbols, Ark::internal::ExecutionContext::pp, Ark::Value::refClosure(), Ark::internal::Closure::refScope(), Ark::internal::Closure::scopePtr(), throwVMError(), Ark::internal::Closure::toString(), Ark::types_to_str, Value, and Ark::Value::valueType().
Referenced by safeRun().
|
privatenoexcept |
Initialize the VM according to the parameters.
Definition at line 124 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::scopes_storage, Ark::internal::ExecutionContext::sp, and Ark::internal::ExecutionContext::stacked_closure_scopes.
Referenced by Ark::Repl::run(), and run().
|
private |
Definition at line 217 of file VM.cpp.
References Ark::Any, Ark::List, popAndResolveAsPtr(), Ark::Value::push_back(), and Ark::Value::valueType().
Referenced by safeRun().
|
inlinenodiscardprivate |
|
private |
Load a plugin from a constant id.
id | Id of the constant |
context |
Definition at line 259 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().
|
inlinenodiscardprivate |
|
inlinenodiscardprivate |
|
noexcept |
Retrieve a value from the virtual machine, given its symbol name.
name | the name of the variable to retrieve |
Definition at line 234 of file VM.cpp.
References Ark::internal::Builtins::nil.
|
inlineprivate |
|
inlineprivate |
Pop a value from the stack.
context |
Definition at line 161 of file VM.hpp.
Referenced by backtrace(), and safeRun().
|
inlineprivate |
Pop a value from the stack and resolve it if possible, then return it.
context |
Definition at line 202 of file VM.hpp.
Referenced by backtrace(), createList(), listAppendInPlace(), and safeRun().
|
inlineprivate |
|
inlineprivate |
|
inlineprivate |
|
inline |
|
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 |
Definition at line 222 of file VM.hpp.
Referenced by backtrace(), safeRun(), and throwArityError().
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 443 of file VM.cpp.
References init(), m_execution_contexts, m_exit_code, and safeRun().
Referenced by __attribute__(), and 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 450 of file VM.cpp.
References Ark::internal::ADD, Ark::Any, Ark::internal::APPEND, Ark::internal::APPEND_IN_PLACE, Ark::internal::APPEND_IN_PLACE_SYM, Ark::internal::APPEND_IN_PLACE_SYM_INDEX, Ark::internal::ASSERT, Ark::internal::AT, Ark::helper::at(), Ark::internal::AT_AT, Ark::internal::AT_SYM_INDEX_SYM_INDEX, Ark::internal::AT_SYM_SYM, backtrace(), Ark::internal::BUILTIN, Ark::internal::Builtins::builtins, Ark::internal::CALL, call(), Ark::internal::CALL_BUILTIN, Ark::internal::CALL_BUILTIN_WITHOUT_RETURN_ADDRESS, Ark::internal::CALL_CURRENT_PAGE, Ark::internal::CALL_SYMBOL, callBuiltin(), Ark::internal::CAPTURE, Ark::internal::CHECK_TYPE_OF, Ark::internal::CHECK_TYPE_OF_BY_INDEX, Ark::Closure, Ark::internal::CONCAT, Ark::internal::CONCAT_IN_PLACE, Ark::Value::constList(), Ark::internal::CREATE_SCOPE, createList(), Ark::internal::DECREMENT, Ark::internal::DECREMENT_BY_INDEX, Ark::internal::DECREMENT_STORE, Ark::internal::DEL, Ark::Error::details(), DISPATCH, Ark::internal::DIV, Ark::internal::EMPTY, Ark::internal::EQ, Ark::internal::EQ_CONST_JUMP_IF_TRUE, Ark::internal::EQ_SYM_INDEX_JUMP_IF_TRUE, Ark::internal::Builtins::falseSym, Ark::internal::ExecutionContext::fc, findNearestVariable(), Ark::internal::GE, Ark::internal::GET_CURRENT_PAGE_ADDR, Ark::internal::GET_FIELD, Ark::internal::GET_FIELD_FROM_SYMBOL, Ark::internal::GET_FIELD_FROM_SYMBOL_INDEX, getField(), GOTO_HALT, Ark::internal::GT, Ark::internal::GT_CONST_JUMP_IF_FALSE, Ark::internal::GT_CONST_JUMP_IF_TRUE, Ark::internal::GT_SYM_JUMP_IF_FALSE, Ark::internal::HALT, Ark::internal::HASFIELD, Ark::internal::HEAD, Ark::helper::head(), Ark::internal::INCREMENT, Ark::internal::INCREMENT_BY_INDEX, Ark::internal::INCREMENT_STORE, 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(), listAppendInPlace(), 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, Ark::internal::LOAD_SYMBOL_BY_INDEX, loadConstAsPtr(), loadPlugin(), loadSymbol(), loadSymbolFromIndex(), Ark::internal::ExecutionContext::locals, Ark::internal::LT, Ark::internal::LT_CONST_JUMP_IF_FALSE, Ark::internal::LT_CONST_JUMP_IF_TRUE, Ark::internal::LT_SYM_JUMP_IF_FALSE, Ark::State::m_constants, m_exit_code, m_running, m_state, Ark::State::m_symbols, Ark::internal::MAKE_CLOSURE, Ark::internal::MOD, Ark::internal::MUL, Ark::internal::NEQ, Ark::internal::NEQ_CONST_JUMP_IF_TRUE, Ark::internal::NEQ_SYM_JUMP_IF_FALSE, Ark::internal::Builtins::nil, Ark::internal::NOP, Ark::internal::NOT, Ark::Number, Ark::Value::number(), Ark::Value::pageAddr(), peekAndResolveAsPtr(), 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::internal::PUSH_RETURN_ADDRESS, Ark::Value::refClosure(), Ark::Reference, Ark::Value::reference(), Ark::internal::Closure::refScope(), Ark::internal::RESET_SCOPE_JUMP, Ark::internal::RET, returnFromFuncCall(), Ark::internal::ExecutionContext::saved_scope, Ark::internal::ExecutionContext::scopes_storage, Ark::internal::SET_AT_2_INDEX, Ark::internal::SET_AT_INDEX, Ark::internal::SET_VAL, Ark::internal::SET_VAL_FROM, Ark::internal::SET_VAL_FROM_INDEX, Ark::internal::SET_VAL_HEAD, Ark::internal::SET_VAL_HEAD_BY_INDEX, Ark::internal::SET_VAL_TAIL, Ark::internal::SET_VAL_TAIL_BY_INDEX, setVal(), Ark::internal::SHORTCIRCUIT_AND, Ark::internal::SHORTCIRCUIT_OR, showBacktraceWithException(), Ark::internal::STORE, store(), Ark::internal::STORE_FROM, Ark::internal::STORE_FROM_INDEX, Ark::internal::STORE_HEAD, Ark::internal::STORE_HEAD_BY_INDEX, Ark::internal::STORE_LIST, Ark::internal::STORE_TAIL, Ark::internal::STORE_TAIL_BY_INDEX, 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::Value::toString(), Ark::internal::Builtins::trueSym, Ark::internal::TYPE, Ark::types_to_str, UNPACK_ARGS, Ark::User, Value, and Ark::Value::valueType().
Referenced by Ark::Repl::run(), and run().
|
inlineprivate |
|
private |
Definition at line 1972 of file VM.cpp.
References backtrace(), and m_exit_code.
Referenced by safeRun().
|
inlineprivate |
|
private |
Definition at line 1915 of file VM.cpp.
References Ark::internal::CALL_BUILTIN_WITHOUT_RETURN_ADDRESS, Ark::State::inst(), Ark::internal::ExecutionContext::ip, Ark::internal::ExecutionContext::last_symbol, m_state, Ark::State::m_symbols, Ark::internal::ExecutionContext::pp, returnFromFuncCall(), Ark::internal::ExecutionContext::sp, Ark::internal::ExecutionContext::stack, Ark::internal::STORE, throwVMError(), and Value.
|
static |
Throw a VM error message.
kind | type of VM error |
message |
Definition at line 438 of file VM.cpp.
References Ark::internal::errorKinds.
Referenced by Ark::helper::at(), getField(), loadPlugin(), safeRun(), and throwArityError().
|
friend |
Definition at line 161 of file VM.hpp.
Referenced by getField(), and safeRun().
|
friend |
Definition at line 160 of file VM.hpp.
Referenced by backtrace(), getField(), safeRun(), and throwArityError().
|
private |
Definition at line 166 of file VM.hpp.
Referenced by createAndGetContext(), createFuture(), debugShowSource(), deleteContext(), forceReloadPlugins(), init(), Ark::Repl::run(), and run().
|
private |
|
private |
Storing the promises while we are resolving them.
Definition at line 171 of file VM.hpp.
Referenced by createFuture(), and deleteFuture().
|
private |
Definition at line 169 of file VM.hpp.
Referenced by createAndGetContext(), createFuture(), deleteContext(), and deleteFuture().
|
private |
|
private |
Definition at line 170 of file VM.hpp.
Referenced by forceReloadPlugins(), init(), and loadPlugin().
|
private |
Definition at line 165 of file VM.hpp.
Referenced by backtrace(), debugShowSource(), findSourceLocation(), forceReloadPlugins(), getField(), init(), loadPlugin(), safeRun(), and throwArityError().