clean up the call stack in a scoped way properly always
parent
e3310ce74f
commit
5674375614
|
@ -2,6 +2,8 @@
|
|||
#define LIBCUBESCRIPT_CS_UTIL_HH
|
||||
|
||||
#include <ostd/string.hh>
|
||||
#include <ostd/utility.hh>
|
||||
#include <ostd/type_traits.hh>
|
||||
|
||||
namespace cscript {
|
||||
|
||||
|
@ -13,6 +15,22 @@ CsFloat cs_parse_float(
|
|||
ostd::ConstCharRange input, ostd::ConstCharRange *end = nullptr
|
||||
);
|
||||
|
||||
template<typename F>
|
||||
struct CsScopeExit {
|
||||
template<typename FF>
|
||||
CsScopeExit(FF &&f): func(ostd::forward<FF>(f)) {}
|
||||
~CsScopeExit() {
|
||||
func();
|
||||
}
|
||||
ostd::Decay<F> func;
|
||||
};
|
||||
|
||||
template<typename F1, typename F2>
|
||||
inline void cs_do_and_cleanup(F1 &&dof, F2 &&clf) {
|
||||
CsScopeExit<F2> cleanup(ostd::forward<F2>(clf));
|
||||
dof();
|
||||
}
|
||||
|
||||
} /* namespace cscript */
|
||||
|
||||
#endif /* LIBCUBESCRIPT_CS_UTIL_HH */
|
||||
|
|
|
@ -443,7 +443,9 @@ static inline void cs_call_alias(
|
|||
CsAliasInternal::compile_code(a, cs)
|
||||
);
|
||||
bcode_incr(codep);
|
||||
cs_do_and_cleanup([&]() {
|
||||
runcode(cs, codep+1, result);
|
||||
}, [&]() {
|
||||
bcode_decr(codep);
|
||||
cs.p_callstack = aliaslink.next;
|
||||
cs.identflags = oldflags;
|
||||
|
@ -462,6 +464,7 @@ static inline void cs_call_alias(
|
|||
force_arg(result, op & CsCodeRetMask);
|
||||
anargs->set_value(oldargs);
|
||||
nargs = offset - skip;
|
||||
});
|
||||
}
|
||||
|
||||
static constexpr int MaxRunDepth = 255;
|
||||
|
@ -631,10 +634,13 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) {
|
|||
for (int i = 0; i < numlocals; ++i) {
|
||||
cs_push_alias(args[offset + i].get_ident(), locals[i]);
|
||||
}
|
||||
cs_do_and_cleanup([&]() {
|
||||
code = runcode(cs, code, result);
|
||||
}, [&]() {
|
||||
for (int i = offset; i < numargs; i++) {
|
||||
cs_pop_alias(args[i].get_ident());
|
||||
}
|
||||
});
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
@ -1448,10 +1454,13 @@ noid:
|
|||
args[offset + j]
|
||||
), locals[j]);
|
||||
}
|
||||
cs_do_and_cleanup([&]() {
|
||||
code = runcode(cs, code, result);
|
||||
}, [&]() {
|
||||
for (ostd::Size j = 0; j < ostd::Size(callargs); ++j) {
|
||||
cs_pop_alias(args[offset + j].get_ident());
|
||||
}
|
||||
});
|
||||
goto exit;
|
||||
}
|
||||
case CsIdIvar:
|
||||
|
|
|
@ -8,6 +8,8 @@
|
|||
#include <ostd/array.hh>
|
||||
#include <ostd/vector.hh>
|
||||
|
||||
#include "cs_util.hh"
|
||||
|
||||
namespace cscript {
|
||||
|
||||
static constexpr int MaxArguments = 25;
|
||||
|
@ -328,7 +330,7 @@ static void cs_do_args(CsState &cs, F body) {
|
|||
cs.p_callstack->id, cs.p_callstack, prevstack->usedargs, prevstack->argstack
|
||||
};
|
||||
cs.p_callstack = &aliaslink;
|
||||
body();
|
||||
cs_do_and_cleanup(ostd::move(body), [&]() {
|
||||
prevstack->usedargs = aliaslink.usedargs;
|
||||
cs.p_callstack = aliaslink.next;
|
||||
int argmask2 = cs.p_callstack->usedargs;
|
||||
|
@ -339,6 +341,7 @@ static void cs_do_args(CsState &cs, F body) {
|
|||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
CsBytecode *cs_copy_code(CsBytecode *c);
|
||||
|
|
Loading…
Reference in New Issue