add getter/setter api on alias, drop old broken api

master
Daniel Kolesa 2021-04-05 18:19:32 +02:00
parent a8cfa08bd8
commit f64df83dd8
5 changed files with 20 additions and 27 deletions

View File

@ -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;
};

View File

@ -4,7 +4,6 @@
#include <cstddef>
#include <span>
#include <utility>
#include <optional>
#include <string_view>
#include "callable.hh"
@ -127,8 +126,6 @@ struct LIBCUBESCRIPT_EXPORT state {
void set_alias(std::string_view name, any_value v);
std::optional<string_ref> get_alias_val(std::string_view name);
private:
friend struct state_p;

View File

@ -440,6 +440,19 @@ LIBCUBESCRIPT_EXPORT void string_var::set_raw_value(string_ref val) {
static_cast<svar_impl *>(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<alias_impl const *>(this)->p_flags & IDENT_FLAG_ARG);
}

View File

@ -496,13 +496,7 @@ LIBCUBESCRIPT_EXPORT void state::set_alias(
if (id) {
switch (id->get_type()) {
case ident_type::ALIAS: {
alias *a = static_cast<alias *>(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<alias *>(id)->set_value(*this, std::move(v));
return;
}
case ident_type::IVAR:
@ -627,18 +621,6 @@ do_add:
return cmd;
}
LIBCUBESCRIPT_EXPORT std::optional<string_ref>
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);

View File

@ -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);
}
});
}