diff --git a/include/cubescript/cubescript/ident.hh b/include/cubescript/cubescript/ident.hh index 7a28b8e..370ee93 100644 --- a/include/cubescript/cubescript/ident.hh +++ b/include/cubescript/cubescript/ident.hh @@ -115,6 +115,9 @@ protected: struct LIBCUBESCRIPT_EXPORT alias: ident { bool is_arg() const; + any_value get_value(state &cs) const; + void set_value(state &cs, any_value v); + protected: alias() = default; }; diff --git a/include/cubescript/cubescript/state.hh b/include/cubescript/cubescript/state.hh index d5295c2..3e1edd5 100644 --- a/include/cubescript/cubescript/state.hh +++ b/include/cubescript/cubescript/state.hh @@ -4,7 +4,6 @@ #include #include #include -#include #include #include "callable.hh" @@ -127,8 +126,6 @@ struct LIBCUBESCRIPT_EXPORT state { void set_alias(std::string_view name, any_value v); - std::optional get_alias_val(std::string_view name); - private: friend struct state_p; diff --git a/src/cs_ident.cc b/src/cs_ident.cc index bbc0e12..9d90889 100644 --- a/src/cs_ident.cc +++ b/src/cs_ident.cc @@ -440,6 +440,19 @@ LIBCUBESCRIPT_EXPORT void string_var::set_raw_value(string_ref val) { static_cast(this)->p_storage = val; } +LIBCUBESCRIPT_EXPORT any_value alias::get_value(state &cs) const { + return state_p{cs}.ts().get_astack(this).node->val_s; +} + +LIBCUBESCRIPT_EXPORT void alias::set_value(state &cs, any_value v) { + auto &ts = state_p{cs}.ts(); + if (is_arg()) { + ts.get_astack(this).set_arg(this, ts, v); + } else { + ts.get_astack(this).set_alias(this, ts, v); + } +} + LIBCUBESCRIPT_EXPORT bool alias::is_arg() const { return (static_cast(this)->p_flags & IDENT_FLAG_ARG); } diff --git a/src/cs_state.cc b/src/cs_state.cc index 0b69cb9..c36f3f7 100644 --- a/src/cs_state.cc +++ b/src/cs_state.cc @@ -496,13 +496,7 @@ LIBCUBESCRIPT_EXPORT void state::set_alias( if (id) { switch (id->get_type()) { case ident_type::ALIAS: { - alias *a = static_cast(id); - auto &ast = p_tstate->get_astack(a); - if (a->is_arg()) { - ast.set_arg(a, *p_tstate, v); - } else { - ast.set_alias(a, *p_tstate, v); - } + static_cast(id)->set_value(*this, std::move(v)); return; } case ident_type::IVAR: @@ -627,18 +621,6 @@ do_add: return cmd; } -LIBCUBESCRIPT_EXPORT std::optional -state::get_alias_val(std::string_view name) { - alias *a = get_alias(name); - if (!a) { - return std::nullopt; - } - if (a->is_arg() && !ident_is_used_arg(a, *p_tstate)) { - return std::nullopt; - } - return p_tstate->get_astack(a).node->val_s.get_str(); -} - LIBCUBESCRIPT_EXPORT void state::init_libs(int libs) { if (libs & LIB_MATH) { init_lib_math(*this); diff --git a/src/lib_base.cc b/src/lib_base.cc index 74a0bd3..7bf2432 100644 --- a/src/lib_base.cc +++ b/src/lib_base.cc @@ -325,11 +325,9 @@ end: }); gcs.new_command("getalias", "s", [](auto &cs, auto args, auto &res) { - auto s0 = cs.get_alias_val(args[0].get_str()); - if (s0) { - res.set_str(*s0); - } else { - res.set_str(""); + auto *id = cs.get_alias(args[0].get_str()); + if (id) { + id->get_value(cs).get_val(res); } }); }