move the VM stack off the real stack + std cleanups

master
Daniel Kolesa 2021-03-24 22:37:53 +01:00
parent 3d37ea22c3
commit 4e8c3ee40f
5 changed files with 341 additions and 224 deletions

View File

@ -15,19 +15,16 @@ namespace cubescript {
/* run func, call the second one after finishing */
template<typename F>
struct CsScopeExit {
template<typename FF>
CsScopeExit(FF &&f): func(std::forward<FF>(f)) {}
~CsScopeExit() {
func();
}
std::decay_t<F> func;
};
template<typename F1, typename F2>
inline void call_with_cleanup(F1 &&dof, F2 &&clf) {
CsScopeExit<F2> cleanup(std::forward<F2>(clf));
struct scope_exit {
scope_exit(std::decay_t<F2> &f): func(&f) {}
~scope_exit() {
(*func)();
}
std::decay_t<F2> *func;
};
scope_exit cleanup(clf);
dof();
}
@ -72,10 +69,19 @@ struct valbuf {
void reserve(std::size_t s) { buf.reserve(s); }
void resize(std::size_t s) { buf.resize(s); }
void resize(std::size_t s, value_type const &v) {
buf.resize(s, v);
}
void append(T const *beg, T const *end) {
buf.insert(buf.end(), beg, end);
}
template<typename ...A>
reference emplace_back(A &&...args) {
return buf.emplace_back(std::forward<A>(args)...);
}
void push_back(T const &v) { buf.push_back(v); }
void pop_back() { buf.pop_back(); }

19
src/cs_thread.cc 100644
View File

@ -0,0 +1,19 @@
#include "cs_thread.hh"
#include "cs_gen.hh"
namespace cubescript {
thread_state::thread_state(internal_state *cs):
vmstack{cs}, errbuf{cs}
{
vmstack.reserve(MAX_ARGUMENTS + MAX_RESULTS);
}
hook_func thread_state::set_hook(hook_func f) {
auto hk = std::move(call_hook);
call_hook = std::move(f);
return hk;
}
} /* namespace cubescript */

View File

@ -17,6 +17,8 @@ struct thread_state {
ident_link *callstack{};
/* current codegen state for diagnostics */
codegen_state *cstate{};
/* value stack for VM */
valbuf<any_value> vmstack;
/* per-thread storage buffer for error messages */
charbuf errbuf;
/* we can attach a hook to vm events */
@ -24,15 +26,9 @@ struct thread_state {
/* loop nesting level */
int loop_level = 0;
thread_state(internal_state *cs):
errbuf{cs}
{}
thread_state(internal_state *cs);
hook_func set_hook(hook_func f) {
auto hk = std::move(call_hook);
call_hook = std::move(f);
return hk;
}
hook_func set_hook(hook_func f);
hook_func &get_hook() { return call_hook; }
hook_func const &get_hook() const { return call_hook; }

File diff suppressed because it is too large Load Diff

View File

@ -11,6 +11,7 @@ libcubescript_src = [
'cs_parser.cc',
'cs_state.cc',
'cs_strman.cc',
'cs_thread.cc',
'cs_val.cc',
'cs_vm.cc',
'lib_base.cc',