libcubescript/src/cs_vm.hh

115 lines
2.8 KiB
C++
Raw Normal View History

2021-03-23 23:29:32 +01:00
#ifndef LIBCUBESCRIPT_VM_HH
#define LIBCUBESCRIPT_VM_HH
2016-08-12 18:38:43 +02:00
#include <cubescript/cubescript.hh>
2016-08-12 18:38:43 +02:00
2021-03-23 01:11:21 +01:00
#include "cs_std.hh"
#include "cs_ident.hh"
2021-03-24 02:21:27 +01:00
#include "cs_gen.hh"
#include "cs_thread.hh"
2021-03-23 23:32:25 +01:00
namespace cubescript {
2016-08-12 18:38:43 +02:00
2021-03-24 02:21:27 +01:00
struct break_exception {
2016-09-14 23:24:13 +02:00
};
2021-03-24 02:21:27 +01:00
struct continue_exception {
2016-08-12 18:38:43 +02:00
};
struct run_depth_guard {
run_depth_guard() = delete;
run_depth_guard(thread_state &ts);
run_depth_guard(run_depth_guard const &) = delete;
run_depth_guard(run_depth_guard &&) = delete;
~run_depth_guard();
};
struct stack_guard {
thread_state *tsp;
std::size_t oldtop;
stack_guard() = delete;
stack_guard(thread_state &ts):
tsp{&ts}, oldtop{ts.vmstack.size()}
{}
~stack_guard() {
tsp->vmstack.resize(oldtop, any_value{*tsp->pstate});
}
stack_guard(stack_guard const &) = delete;
stack_guard(stack_guard &&) = delete;
};
template<typename F>
static void call_with_args(thread_state &ts, F body) {
if (!ts.callstack) {
2016-09-10 19:54:55 +02:00
body();
return;
}
auto mask = ts.callstack->usedargs;
std::size_t noff = ts.idstack.size();
for (std::size_t i = 0; mask.any(); ++i) {
if (mask[0]) {
auto &ast = ts.get_astack(
static_cast<alias *>(ts.istate->identmap[i])
);
auto &st = ts.idstack.emplace_back(*ts.pstate);
st.next = ast.node;
ast.node = ast.node->next;
}
mask >>= 1;
}
ident_link *prevstack = ts.callstack->next;
2021-03-23 23:29:32 +01:00
ident_link aliaslink = {
ts.callstack->id, ts.callstack,
prevstack ? prevstack->usedargs : argset{}
};
if (!prevstack) {
aliaslink.usedargs.set();
}
ts.callstack = &aliaslink;
2021-03-30 23:49:50 +02:00
auto cleanup = [&]() {
2016-09-15 04:30:37 +02:00
if (prevstack) {
prevstack->usedargs = aliaslink.usedargs;
}
ts.callstack = aliaslink.next;
auto mask2 = ts.callstack->usedargs;
for (std::size_t i = 0, nredo = 0; mask2.any(); ++i) {
if (mask2[0]) {
ts.get_astack(
static_cast<alias *>(ts.istate->identmap[i])
).node = ts.idstack[noff + nredo++].next;
}
mask2 >>= 1;
}
ts.idstack.resize(noff, ident_stack{*ts.pstate});
2021-03-30 23:49:50 +02:00
};
try {
body();
} catch (...) {
cleanup();
throw;
}
cleanup();
}
void exec_command(
thread_state &ts, command_impl *id, any_value *args, any_value &res,
std::size_t nargs, bool lookup = false
);
bool exec_alias(
thread_state &ts, alias *a, any_value *args, any_value &result,
std::size_t callargs, std::size_t &nargs, std::size_t offset,
std::size_t skip, std::uint32_t op, bool ncheck = false
);
std::uint32_t *vm_exec(
thread_state &ts, std::uint32_t *code, any_value &result
);
2021-03-23 23:32:25 +01:00
} /* namespace cubescript */
2016-08-12 18:38:43 +02:00
2021-03-23 23:29:32 +01:00
#endif /* LIBCUBESCRIPT_VM_HH */