diff --git a/include/cubescript/cubescript.hh b/include/cubescript/cubescript.hh index d43f368..b20e2c5 100644 --- a/include/cubescript/cubescript.hh +++ b/include/cubescript/cubescript.hh @@ -56,22 +56,11 @@ struct ident; struct any_value; struct global_var; -using hook_func = callable; -using var_cb_func = callable; -using command_func = callable< +using hook_func = callable; +using command_func = callable< void, state &, std::span, any_value & >; -enum { - IDENT_FLAG_PERSIST = 1 << 0, - IDENT_FLAG_OVERRIDE = 1 << 1, - IDENT_FLAG_HEX = 1 << 2, - IDENT_FLAG_READONLY = 1 << 3, - IDENT_FLAG_OVERRIDDEN = 1 << 4, - IDENT_FLAG_UNKNOWN = 1 << 5, - IDENT_FLAG_ARG = 1 << 6 -}; - struct internal_state; struct thread_state; struct ident_impl; @@ -314,8 +303,6 @@ enum class loop_state { }; struct LIBCUBESCRIPT_EXPORT state { - int identflags = 0; - state(); state(alloc_func func, void *data); virtual ~state(); @@ -336,7 +323,6 @@ struct LIBCUBESCRIPT_EXPORT state { void swap(state &s) { std::swap(p_tstate, s.p_tstate); - std::swap(identflags, s.identflags); } state new_thread(); @@ -352,55 +338,17 @@ struct LIBCUBESCRIPT_EXPORT state { void init_libs(int libs = LIB_ALL); - void clear_override(ident &id); - void clear_overrides(); - - ident *new_ident(std::string_view name, int flags = IDENT_FLAG_UNKNOWN); - - template integer_var *new_ivar( - std::string_view name, integer_type m, integer_type x, integer_type v, - F &&f, int flags = 0 - ) { - return new_ivar( - name, m, x, v, - var_cb_func{std::forward(f), callable_alloc, this}, flags - ); - } - integer_var *new_ivar( - std::string_view name, integer_type m, integer_type x, integer_type v - ) { - return new_ivar(name, m, x, v, var_cb_func{}, 0); - } - - template + std::string_view n, integer_type v, bool read_only = false + ); float_var *new_fvar( - std::string_view name, float_type m, float_type x, float_type v, - F &&f, int flags = 0 - ) { - return new_fvar( - name, m, x, v, - var_cb_func{std::forward(f), callable_alloc, this}, flags - ); - } - float_var *new_fvar( - std::string_view name, float_type m, float_type x, float_type v - ) { - return new_fvar(name, m, x, v, var_cb_func{}, 0); - } - - template + std::string_view n, float_type v, bool read_only = false + ); string_var *new_svar( - std::string_view name, std::string_view v, F &&f, int flags = 0 - ) { - return new_svar( - name, v, - var_cb_func{std::forward(f), callable_alloc, this}, flags - ); - } - string_var *new_svar(std::string_view name, std::string_view v) { - return new_svar(name, v, var_cb_func{}, 0); - } + std::string_view n, std::string_view v, bool read_only = false + ); + + ident *new_ident(std::string_view name, int flags); template command *new_command( @@ -419,9 +367,6 @@ struct LIBCUBESCRIPT_EXPORT state { std::span get_idents(); std::span get_idents() const; - void reset_var(std::string_view name); - void touch_var(std::string_view name); - void run(bcode_ref const &code, any_value &ret); void run(std::string_view code, any_value &ret); void run(std::string_view code, any_value &ret, std::string_view source); @@ -454,18 +399,6 @@ struct LIBCUBESCRIPT_EXPORT state { private: hook_func set_call_hook(hook_func func); - integer_var *new_ivar( - std::string_view n, integer_type m, integer_type x, integer_type v, - var_cb_func f, int flags - ); - float_var *new_fvar( - std::string_view n, float_type m, float_type x, float_type v, - var_cb_func f, int flags - ); - string_var *new_svar( - std::string_view n, std::string_view v, var_cb_func f, int flags - ); - command *new_command( std::string_view name, std::string_view args, command_func func ); diff --git a/src/cs_error.cc b/src/cs_error.cc index 23e49b6..eb70973 100644 --- a/src/cs_error.cc +++ b/src/cs_error.cc @@ -1,5 +1,7 @@ #include +#include + #include "cs_gen.hh" #include "cs_thread.hh" @@ -85,7 +87,10 @@ LIBCUBESCRIPT_EXPORT stack_state error::save_stack(thread_state &ts) { integer_var *dalias = static_cast( ts.istate->identmap[ID_IDX_DBGALIAS] ); - if (!dalias->get_value()) { + auto dval = std::clamp( + dalias->get_value(), integer_type(0), integer_type(1000) + ); + if (!dval) { return stack_state{ts, nullptr, !!ts.callstack}; } int total = 0, depth = 0; @@ -96,13 +101,13 @@ LIBCUBESCRIPT_EXPORT stack_state error::save_stack(thread_state &ts) { return stack_state{ts, nullptr, false}; } stack_state::node *st = ts.istate->create_array( - std::min(total, dalias->get_value()) + std::min(total, dval) ); stack_state::node *ret = st, *nd = st; ++st; for (ident_link *l = ts.callstack; l; l = l->next) { ++depth; - if (depth < dalias->get_value()) { + if (depth < dval) { nd->id = l->id; nd->index = total - depth + 1; if (!l->next) { @@ -117,7 +122,7 @@ LIBCUBESCRIPT_EXPORT stack_state error::save_stack(thread_state &ts) { nd->next = nullptr; } } - return stack_state{ts, ret, total > dalias->get_value()}; + return stack_state{ts, ret, total > dval}; } } /* namespace cubescript */ diff --git a/src/cs_ident.cc b/src/cs_ident.cc index 7c13b8e..f3c6d5d 100644 --- a/src/cs_ident.cc +++ b/src/cs_ident.cc @@ -18,54 +18,21 @@ bool ident_is_callable(ident const *id) { } var_impl::var_impl( - ident_type tp, string_ref name, var_cb_func f, int fl + ident_type tp, string_ref name, int fl ): - ident_impl{tp, name, fl}, cb_var{std::move(f)} + ident_impl{tp, name, fl} {} -void var_impl::changed(state &cs) { - if (cb_var) { - switch (p_type) { - case ID_IVAR: - cb_var(cs, *static_cast(this)); - break; - case ID_FVAR: - cb_var(cs, *static_cast(this)); - break; - case ID_SVAR: - cb_var(cs, *static_cast(this)); - break; - default: - break; - } - } -} - -ivar_impl::ivar_impl( - string_ref name, integer_type m, integer_type x, integer_type v, var_cb_func f, int fl -): - var_impl{ - ident_type::IVAR, name, std::move(f), - fl | ((m > x) ? IDENT_FLAG_READONLY : 0) - }, - p_storage{v}, p_minval{m}, p_maxval{x}, p_overrideval{0} +ivar_impl::ivar_impl(string_ref name, integer_type v, int fl): + var_impl{ident_type::IVAR, name, fl}, p_storage{v} {} -fvar_impl::fvar_impl( - string_ref name, float_type m, float_type x, float_type v, var_cb_func f, int fl -): - var_impl{ - ident_type::FVAR, name, std::move(f), - fl | ((m > x) ? IDENT_FLAG_READONLY : 0) - }, - p_storage{v}, p_minval{m}, p_maxval{x}, p_overrideval{0} +fvar_impl::fvar_impl(string_ref name, float_type v, int fl): + var_impl{ident_type::FVAR, name, fl}, p_storage{v} {} -svar_impl::svar_impl( - string_ref name, string_ref v, string_ref ov, var_cb_func f, int fl -): - var_impl{ident_type::SVAR, name, std::move(f), fl}, - p_storage{v}, p_overrideval{ov} +svar_impl::svar_impl(string_ref name, string_ref v, int fl): + var_impl{ident_type::SVAR, name, fl}, p_storage{v} {} alias_impl::alias_impl( @@ -153,11 +120,9 @@ void alias_stack::set_arg(alias *a, thread_state &ts, any_value &v) { node->val_s = std::move(v); } -void alias_stack::set_alias(alias *a, thread_state &ts, any_value &v) { +void alias_stack::set_alias(alias *, thread_state &, any_value &v) { node->val_s = std::move(v); node->code = bcode_ref{}; - auto *ai = static_cast(a); - ai->p_flags = (ai->p_flags & ts.pstate->identflags) | ts.pstate->identflags; } /* public interface */ diff --git a/src/cs_ident.hh b/src/cs_ident.hh index d2545f7..6f95231 100644 --- a/src/cs_ident.hh +++ b/src/cs_ident.hh @@ -16,6 +16,12 @@ enum { ID_NOT, ID_AND, ID_OR }; +enum { + IDENT_FLAG_UNKNOWN = 1 << 0, + IDENT_FLAG_ARG = 1 << 1, + IDENT_FLAG_READONLY = 1 << 2 +}; + struct ident_stack { any_value val_s; bcode_ref code; @@ -64,38 +70,25 @@ struct ident_impl { bool ident_is_callable(ident const *id); struct var_impl: ident_impl { - var_impl( - ident_type tp, string_ref name, var_cb_func func, int flags = 0 - ); - - var_cb_func cb_var; - - void changed(state &cs); + var_impl(ident_type tp, string_ref name, int flags); }; struct ivar_impl: var_impl, integer_var { - ivar_impl( - string_ref n, integer_type m, integer_type x, integer_type v, var_cb_func f, int flags - ); + ivar_impl(string_ref n, integer_type v, int flags); - integer_type p_storage, p_minval, p_maxval, p_overrideval; + integer_type p_storage; }; struct fvar_impl: var_impl, float_var { - fvar_impl( - string_ref n, float_type m, float_type x, float_type v, - var_cb_func f, int flags - ); + fvar_impl(string_ref n, float_type v, int flags); - float_type p_storage, p_minval, p_maxval, p_overrideval; + float_type p_storage; }; struct svar_impl: var_impl, string_var { - svar_impl( - string_ref n, string_ref v, string_ref ov, var_cb_func f, int flags - ); + svar_impl(string_ref n, string_ref v, int flags); - string_ref p_storage, p_overrideval; + string_ref p_storage; }; struct alias_impl: ident_impl, alias { diff --git a/src/cs_state.cc b/src/cs_state.cc index f8aa60f..a80cb6e 100644 --- a/src/cs_state.cc +++ b/src/cs_state.cc @@ -83,12 +83,12 @@ state::state(alloc_func func, void *data) { throw internal_error{"invalid dummy index"}; } - id = new_ivar("numargs", MAX_ARGUMENTS, 0, 0); + id = new_ivar("numargs", 0, true); if (id->get_index() != ID_IDX_NUMARGS) { throw internal_error{"invalid numargs index"}; } - id = new_ivar("dbgalias", 0, 1000, 4); + id = new_ivar("dbgalias", 4); if (id->get_index() != ID_IDX_DBGALIAS) { throw internal_error{"invalid dbgalias index"}; } @@ -289,45 +289,38 @@ LIBCUBESCRIPT_EXPORT std::span state::get_idents() const { } LIBCUBESCRIPT_EXPORT integer_var *state::new_ivar( - std::string_view n, integer_type m, integer_type x, integer_type v, - var_cb_func f, int flags + std::string_view n, integer_type v, bool read_only ) { auto *iv = p_tstate->istate->create( - string_ref{p_tstate->istate, n}, m, x, v, std::move(f), flags + string_ref{p_tstate->istate, n}, v, + read_only ? IDENT_FLAG_READONLY : 0 ); add_ident(iv, iv); return iv; } LIBCUBESCRIPT_EXPORT float_var *state::new_fvar( - std::string_view n, float_type m, float_type x, float_type v, - var_cb_func f, int flags + std::string_view n, float_type v, bool read_only ) { auto *fv = p_tstate->istate->create( - string_ref{p_tstate->istate, n}, m, x, v, std::move(f), flags + string_ref{p_tstate->istate, n}, v, + read_only ? IDENT_FLAG_READONLY : 0 ); add_ident(fv, fv); return fv; } LIBCUBESCRIPT_EXPORT string_var *state::new_svar( - std::string_view n, std::string_view v, var_cb_func f, int flags + std::string_view n, std::string_view v, bool read_only ) { auto *sv = p_tstate->istate->create( string_ref{p_tstate->istate, n}, string_ref{p_tstate->istate, v}, - string_ref{p_tstate->istate, ""}, std::move(f), flags + read_only ? IDENT_FLAG_READONLY : 0 ); add_ident(sv, sv); return sv; } -LIBCUBESCRIPT_EXPORT void state::touch_var(std::string_view name) { - ident *id = get_ident(name); - if (id && id->is_var()) { - static_cast(id->p_impl)->changed(*this); - } -} - LIBCUBESCRIPT_EXPORT void state::set_alias( std::string_view name, any_value v ) { @@ -361,7 +354,7 @@ LIBCUBESCRIPT_EXPORT void state::set_alias( throw error{*this, "cannot alias invalid name '%s'", name.data()}; } else { auto *a = p_tstate->istate->create( - *this, string_ref{p_tstate->istate, name}, std::move(v), identflags + *this, string_ref{p_tstate->istate, name}, std::move(v), 0 ); add_ident(a, a); } diff --git a/src/cs_vm.cc b/src/cs_vm.cc index 67b58a5..c6a737e 100644 --- a/src/cs_vm.cc +++ b/src/cs_vm.cc @@ -226,8 +226,6 @@ bool exec_alias( } auto oldargs = anargs->get_value(); anargs->set_value(integer_type(callargs)); - int oldflags = ts.pstate->identflags; - ts.pstate->identflags |= a->get_flags()&IDENT_FLAG_OVERRIDDEN; ident_link aliaslink = {a, ts.callstack, uargs}; ts.callstack = &aliaslink; if (!aast.node->code) { @@ -242,7 +240,6 @@ bool exec_alias( bcode_ref coderef = aast.node->code; auto cleanup = [&]() { ts.callstack = aliaslink.next; - ts.pstate->identflags = oldflags; auto amask = aliaslink.usedargs; for (std::size_t i = 0; i < callargs; i++) { ts.get_astack(