get rid of call_with_cleanup

master
Daniel Kolesa 2021-03-30 23:49:50 +02:00
parent 19f0ff379a
commit eddbf64c87
4 changed files with 41 additions and 32 deletions

View File

@ -794,11 +794,13 @@ static void do_run(
std::uint32_t *cbuf = bcode_alloc(ts.istate, gs.code.size()); std::uint32_t *cbuf = bcode_alloc(ts.istate, gs.code.size());
std::memcpy(cbuf, gs.code.data(), gs.code.size() * sizeof(std::uint32_t)); std::memcpy(cbuf, gs.code.data(), gs.code.size() * sizeof(std::uint32_t));
bcode_incr(cbuf); bcode_incr(cbuf);
call_with_cleanup([&ts, cbuf, &ret]() { try {
vm_exec(ts, cbuf + 1, ret); vm_exec(ts, cbuf + 1, ret);
}, [cbuf]() { } catch (...) {
bcode_decr(cbuf); bcode_decr(cbuf);
}); throw;
}
bcode_decr(cbuf);
} }
LIBCUBESCRIPT_EXPORT void state::run(std::string_view code, any_value &ret) { LIBCUBESCRIPT_EXPORT void state::run(std::string_view code, any_value &ret) {

View File

@ -13,21 +13,6 @@
namespace cubescript { namespace cubescript {
/* run func, call the second one after finishing */
template<typename F1, typename F2>
inline void call_with_cleanup(F1 &&dof, 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();
}
/* a value buffer */ /* a value buffer */
template<typename T> template<typename T>

View File

@ -224,9 +224,7 @@ void exec_alias(
alias_impl * alias_impl *
>(a)->compile_code(ts)->get_raw(); >(a)->compile_code(ts)->get_raw();
bcode_incr(codep); bcode_incr(codep);
call_with_cleanup([&]() { auto cleanup = [&]() {
vm_exec(ts, codep+1, result);
}, [&]() {
bcode_decr(codep); bcode_decr(codep);
ts.callstack = aliaslink.next; ts.callstack = aliaslink.next;
ts.pstate->identflags = oldflags; ts.pstate->identflags = oldflags;
@ -247,7 +245,14 @@ void exec_alias(
force_arg(result, op & BC_INST_RET_MASK); force_arg(result, op & BC_INST_RET_MASK);
anargs->set_value(integer_type(oldargs)); anargs->set_value(integer_type(oldargs));
nargs = offset - skip; nargs = offset - skip;
}); };
try {
vm_exec(ts, codep + 1, result);
} catch (...) {
cleanup();
throw;
}
cleanup();
} }
static constexpr int MaxRunDepth = 255; static constexpr int MaxRunDepth = 255;
@ -520,14 +525,19 @@ std::uint32_t *vm_exec(
ts.idstack.emplace_back(*ts.pstate) ts.idstack.emplace_back(*ts.pstate)
); );
} }
call_with_cleanup([&]() { auto cleanup = [&]() {
code = vm_exec(ts, code, result);
}, [&]() {
for (std::size_t i = offset; i < args.size(); ++i) { for (std::size_t i = offset; i < args.size(); ++i) {
pop_alias(args[i].get_ident()); pop_alias(args[i].get_ident());
} }
ts.idstack.resize(idstsz, ident_stack{*ts.pstate}); ts.idstack.resize(idstsz, ident_stack{*ts.pstate});
}); };
try {
code = vm_exec(ts, code, result);
} catch (...) {
cleanup();
throw;
}
cleanup();
return code; return code;
} }
@ -1138,14 +1148,19 @@ noid:
ts.idstack.emplace_back(*ts.pstate) ts.idstack.emplace_back(*ts.pstate)
); );
} }
call_with_cleanup([&]() { auto cleanup = [&]() {
code = vm_exec(ts, code, result);
}, [&]() {
for (size_t j = 0; j < size_t(callargs); ++j) { for (size_t j = 0; j < size_t(callargs); ++j) {
pop_alias(args[offset + j].get_ident()); pop_alias(args[offset + j].get_ident());
} }
ts.idstack.resize(idstsz, ident_stack{*ts.pstate}); ts.idstack.resize(idstsz, ident_stack{*ts.pstate});
}); };
try {
code = vm_exec(ts, code, result);
} catch (...) {
cleanup();
throw;
}
cleanup();
return code; return code;
} }
case ID_IVAR: case ID_IVAR:

View File

@ -66,7 +66,7 @@ static void call_with_args(thread_state &ts, F body) {
aliaslink.usedargs.set(); aliaslink.usedargs.set();
} }
ts.callstack = &aliaslink; ts.callstack = &aliaslink;
call_with_cleanup(std::move(body), [&]() { auto cleanup = [&]() {
if (prevstack) { if (prevstack) {
prevstack->usedargs = aliaslink.usedargs; prevstack->usedargs = aliaslink.usedargs;
} }
@ -81,7 +81,14 @@ static void call_with_args(thread_state &ts, F body) {
mask2 >>= 1; mask2 >>= 1;
} }
ts.idstack.resize(noff, ident_stack{*ts.pstate}); ts.idstack.resize(noff, ident_stack{*ts.pstate});
}); };
try {
body();
} catch (...) {
cleanup();
throw;
}
cleanup();
} }
void exec_command( void exec_command(