remove the 255 run-depth limitation
also add optional per-thread API that sets the maximum run depth (0 means no limit, default) if you want it; since we no longer store stuff on the stack it should not be necessary thoughmaster
parent
c177013ea2
commit
04441f206d
|
@ -412,6 +412,9 @@ struct LIBCUBESCRIPT_EXPORT state {
|
||||||
bool get_persist_mode() const;
|
bool get_persist_mode() const;
|
||||||
bool set_persist_mode(bool v);
|
bool set_persist_mode(bool v);
|
||||||
|
|
||||||
|
std::size_t get_max_run_depth() const;
|
||||||
|
std::size_t set_max_run_depth(std::size_t v);
|
||||||
|
|
||||||
void set_alias(std::string_view name, any_value v);
|
void set_alias(std::string_view name, any_value v);
|
||||||
|
|
||||||
std::optional<string_ref> get_alias_val(std::string_view name);
|
std::optional<string_ref> get_alias_val(std::string_view name);
|
||||||
|
|
|
@ -859,4 +859,14 @@ LIBCUBESCRIPT_EXPORT bool state::set_persist_mode(bool v) {
|
||||||
return was;
|
return was;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LIBCUBESCRIPT_EXPORT std::size_t state::get_max_run_depth() const {
|
||||||
|
return p_tstate->max_run_depth;
|
||||||
|
}
|
||||||
|
|
||||||
|
LIBCUBESCRIPT_EXPORT std::size_t state::set_max_run_depth(std::size_t v) {
|
||||||
|
auto old = p_tstate->max_run_depth;
|
||||||
|
p_tstate->max_run_depth = v;
|
||||||
|
return old;
|
||||||
|
}
|
||||||
|
|
||||||
} /* namespace cubescript */
|
} /* namespace cubescript */
|
||||||
|
|
|
@ -35,12 +35,16 @@ struct thread_state {
|
||||||
charbuf errbuf;
|
charbuf errbuf;
|
||||||
/* we can attach a hook to vm events */
|
/* we can attach a hook to vm events */
|
||||||
hook_func call_hook{};
|
hook_func call_hook{};
|
||||||
/* loop nesting level */
|
|
||||||
int loop_level = 0;
|
|
||||||
/* thread ident flags */
|
|
||||||
int ident_flags = 0;
|
|
||||||
/* whether we own the internal state (i.e. not a side thread */
|
/* whether we own the internal state (i.e. not a side thread */
|
||||||
bool owner = false;
|
bool owner = false;
|
||||||
|
/* thread ident flags */
|
||||||
|
int ident_flags = 0;
|
||||||
|
/* run depth limit */
|
||||||
|
std::size_t max_run_depth = 0;
|
||||||
|
/* current run depth */
|
||||||
|
std::size_t run_depth = 0;
|
||||||
|
/* loop nesting level */
|
||||||
|
std::size_t loop_level = 0;
|
||||||
|
|
||||||
thread_state(internal_state *cs);
|
thread_state(internal_state *cs);
|
||||||
|
|
||||||
|
|
11
src/cs_vm.cc
11
src/cs_vm.cc
|
@ -279,17 +279,14 @@ bool exec_alias(
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static constexpr int MaxRunDepth = 255;
|
run_depth_guard::run_depth_guard(thread_state &ts): tsp(&ts) {
|
||||||
static thread_local int rundepth = 0;
|
if (ts.max_run_depth && (ts.run_depth >= ts.max_run_depth)) {
|
||||||
|
|
||||||
run_depth_guard::run_depth_guard(thread_state &ts) {
|
|
||||||
if (rundepth >= MaxRunDepth) {
|
|
||||||
throw error{ts, "exceeded recursion limit"};
|
throw error{ts, "exceeded recursion limit"};
|
||||||
}
|
}
|
||||||
++rundepth;
|
++ts.run_depth;
|
||||||
}
|
}
|
||||||
|
|
||||||
run_depth_guard::~run_depth_guard() { --rundepth; }
|
run_depth_guard::~run_depth_guard() { --tsp->run_depth; }
|
||||||
|
|
||||||
static inline alias *get_lookup_id(
|
static inline alias *get_lookup_id(
|
||||||
thread_state &ts, std::uint32_t op, alias_stack *&ast
|
thread_state &ts, std::uint32_t op, alias_stack *&ast
|
||||||
|
|
|
@ -22,6 +22,8 @@ struct run_depth_guard {
|
||||||
run_depth_guard(run_depth_guard const &) = delete;
|
run_depth_guard(run_depth_guard const &) = delete;
|
||||||
run_depth_guard(run_depth_guard &&) = delete;
|
run_depth_guard(run_depth_guard &&) = delete;
|
||||||
~run_depth_guard();
|
~run_depth_guard();
|
||||||
|
|
||||||
|
thread_state *tsp;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct stack_guard {
|
struct stack_guard {
|
||||||
|
|
Loading…
Reference in New Issue