move the VM stack off the real stack + std cleanups
parent
3d37ea22c3
commit
4e8c3ee40f
|
@ -15,19 +15,16 @@ namespace cubescript {
|
||||||
|
|
||||||
/* run func, call the second one after finishing */
|
/* 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>
|
template<typename F1, typename F2>
|
||||||
inline void call_with_cleanup(F1 &&dof, F2 &&clf) {
|
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();
|
dof();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,10 +69,19 @@ struct valbuf {
|
||||||
void reserve(std::size_t s) { buf.reserve(s); }
|
void reserve(std::size_t s) { buf.reserve(s); }
|
||||||
void resize(std::size_t s) { buf.resize(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) {
|
void append(T const *beg, T const *end) {
|
||||||
buf.insert(buf.end(), beg, 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 push_back(T const &v) { buf.push_back(v); }
|
||||||
void pop_back() { buf.pop_back(); }
|
void pop_back() { buf.pop_back(); }
|
||||||
|
|
||||||
|
|
|
@ -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 */
|
|
@ -17,6 +17,8 @@ struct thread_state {
|
||||||
ident_link *callstack{};
|
ident_link *callstack{};
|
||||||
/* current codegen state for diagnostics */
|
/* current codegen state for diagnostics */
|
||||||
codegen_state *cstate{};
|
codegen_state *cstate{};
|
||||||
|
/* value stack for VM */
|
||||||
|
valbuf<any_value> vmstack;
|
||||||
/* per-thread storage buffer for error messages */
|
/* per-thread storage buffer for error messages */
|
||||||
charbuf errbuf;
|
charbuf errbuf;
|
||||||
/* we can attach a hook to vm events */
|
/* we can attach a hook to vm events */
|
||||||
|
@ -24,15 +26,9 @@ struct thread_state {
|
||||||
/* loop nesting level */
|
/* loop nesting level */
|
||||||
int loop_level = 0;
|
int loop_level = 0;
|
||||||
|
|
||||||
thread_state(internal_state *cs):
|
thread_state(internal_state *cs);
|
||||||
errbuf{cs}
|
|
||||||
{}
|
|
||||||
|
|
||||||
hook_func set_hook(hook_func f) {
|
hook_func set_hook(hook_func f);
|
||||||
auto hk = std::move(call_hook);
|
|
||||||
call_hook = std::move(f);
|
|
||||||
return hk;
|
|
||||||
}
|
|
||||||
|
|
||||||
hook_func &get_hook() { return call_hook; }
|
hook_func &get_hook() { return call_hook; }
|
||||||
hook_func const &get_hook() const { return call_hook; }
|
hook_func const &get_hook() const { return call_hook; }
|
||||||
|
|
505
src/cs_vm.cc
505
src/cs_vm.cc
File diff suppressed because it is too large
Load Diff
|
@ -11,6 +11,7 @@ libcubescript_src = [
|
||||||
'cs_parser.cc',
|
'cs_parser.cc',
|
||||||
'cs_state.cc',
|
'cs_state.cc',
|
||||||
'cs_strman.cc',
|
'cs_strman.cc',
|
||||||
|
'cs_thread.cc',
|
||||||
'cs_val.cc',
|
'cs_val.cc',
|
||||||
'cs_vm.cc',
|
'cs_vm.cc',
|
||||||
'lib_base.cc',
|
'lib_base.cc',
|
||||||
|
|
Loading…
Reference in New Issue