add proper var value setting helpers

master
Daniel Kolesa 2021-04-04 02:58:04 +02:00
parent e9fc023daa
commit bab8633a05
5 changed files with 64 additions and 32 deletions

View File

@ -261,7 +261,8 @@ protected:
struct LIBCUBESCRIPT_EXPORT integer_var: global_var { struct LIBCUBESCRIPT_EXPORT integer_var: global_var {
integer_type get_value() const; integer_type get_value() const;
void set_value(integer_type val); void set_value(state &cs, integer_type val, bool do_write = true);
void set_raw_value(integer_type val);
protected: protected:
integer_var() = default; integer_var() = default;
@ -269,7 +270,8 @@ protected:
struct LIBCUBESCRIPT_EXPORT float_var: global_var { struct LIBCUBESCRIPT_EXPORT float_var: global_var {
float_type get_value() const; float_type get_value() const;
void set_value(float_type val); void set_value(state &cs, float_type val, bool do_write = true);
void set_raw_value(float_type val);
protected: protected:
float_var() = default; float_var() = default;
@ -277,7 +279,8 @@ protected:
struct LIBCUBESCRIPT_EXPORT string_var: global_var { struct LIBCUBESCRIPT_EXPORT string_var: global_var {
string_ref get_value() const; string_ref get_value() const;
void set_value(string_ref val); void set_value(state &cs, string_ref val, bool do_write = true);
void set_raw_value(string_ref val);
protected: protected:
string_var() = default; string_var() = default;

View File

@ -350,7 +350,22 @@ LIBCUBESCRIPT_EXPORT integer_type integer_var::get_value() const {
return static_cast<ivar_impl const *>(this)->p_storage; return static_cast<ivar_impl const *>(this)->p_storage;
} }
LIBCUBESCRIPT_EXPORT void integer_var::set_value(integer_type val) { LIBCUBESCRIPT_EXPORT void integer_var::set_value(
state &cs, integer_type val, bool do_write
) {
if (is_read_only()) {
throw error{
cs, "variable '%s' is read only", get_name().data()
};
}
if (!do_write) {
return;
}
save(cs);
set_raw_value(val);
}
LIBCUBESCRIPT_EXPORT void integer_var::set_raw_value(integer_type val) {
static_cast<ivar_impl *>(this)->p_storage = val; static_cast<ivar_impl *>(this)->p_storage = val;
} }
@ -358,7 +373,22 @@ LIBCUBESCRIPT_EXPORT float_type float_var::get_value() const {
return static_cast<fvar_impl const *>(this)->p_storage; return static_cast<fvar_impl const *>(this)->p_storage;
} }
LIBCUBESCRIPT_EXPORT void float_var::set_value(float_type val) { LIBCUBESCRIPT_EXPORT void float_var::set_value(
state &cs, float_type val, bool do_write
) {
if (is_read_only()) {
throw error{
cs, "variable '%s' is read only", get_name().data()
};
}
if (!do_write) {
return;
}
save(cs);
set_raw_value(val);
}
LIBCUBESCRIPT_EXPORT void float_var::set_raw_value(float_type val) {
static_cast<fvar_impl *>(this)->p_storage = val; static_cast<fvar_impl *>(this)->p_storage = val;
} }
@ -366,7 +396,22 @@ LIBCUBESCRIPT_EXPORT string_ref string_var::get_value() const {
return static_cast<svar_impl const *>(this)->p_storage; return static_cast<svar_impl const *>(this)->p_storage;
} }
LIBCUBESCRIPT_EXPORT void string_var::set_value(string_ref val) { LIBCUBESCRIPT_EXPORT void string_var::set_value(
state &cs, string_ref val, bool do_write
) {
if (is_read_only()) {
throw error{
cs, "variable '%s' is read only", get_name().data()
};
}
if (!do_write) {
return;
}
save(cs);
set_raw_value(std::move(val));
}
LIBCUBESCRIPT_EXPORT void string_var::set_raw_value(string_ref val) {
static_cast<svar_impl *>(this)->p_storage = val; static_cast<svar_impl *>(this)->p_storage = val;
} }

View File

@ -302,7 +302,7 @@ LIBCUBESCRIPT_EXPORT void state::clear_override(ident &id) {
} }
case ident_type::IVAR: { case ident_type::IVAR: {
ivar_impl &iv = static_cast<ivar_impl &>(id); ivar_impl &iv = static_cast<ivar_impl &>(id);
iv.set_value(iv.p_override); iv.set_raw_value(iv.p_override);
//iv.changed(*this); //iv.changed(*this);
static_cast<ivar_impl *>( static_cast<ivar_impl *>(
static_cast<integer_var *>(&iv) static_cast<integer_var *>(&iv)
@ -311,7 +311,7 @@ LIBCUBESCRIPT_EXPORT void state::clear_override(ident &id) {
} }
case ident_type::FVAR: { case ident_type::FVAR: {
fvar_impl &fv = static_cast<fvar_impl &>(id); fvar_impl &fv = static_cast<fvar_impl &>(id);
fv.set_value(fv.p_override); fv.set_raw_value(fv.p_override);
//fv.changed(*this); //fv.changed(*this);
static_cast<fvar_impl *>( static_cast<fvar_impl *>(
static_cast<float_var *>(&fv) static_cast<float_var *>(&fv)
@ -320,7 +320,7 @@ LIBCUBESCRIPT_EXPORT void state::clear_override(ident &id) {
} }
case ident_type::SVAR: { case ident_type::SVAR: {
svar_impl &sv = static_cast<svar_impl &>(id); svar_impl &sv = static_cast<svar_impl &>(id);
sv.set_value(sv.p_override); sv.set_raw_value(sv.p_override);
//sv.changed(*this); //sv.changed(*this);
static_cast<svar_impl *>( static_cast<svar_impl *>(
static_cast<string_var *>(&sv) static_cast<string_var *>(&sv)

View File

@ -234,7 +234,7 @@ bool exec_alias(
auto oldargs = anargs->get_value(); auto oldargs = anargs->get_value();
auto oldflags = ts.ident_flags; auto oldflags = ts.ident_flags;
ts.ident_flags = aast.flags; ts.ident_flags = aast.flags;
anargs->set_value(integer_type(callargs)); anargs->set_raw_value(integer_type(callargs));
ident_link aliaslink = {a, ts.callstack, uargs}; ident_link aliaslink = {a, ts.callstack, uargs};
ts.callstack = &aliaslink; ts.callstack = &aliaslink;
if (!aast.node->code) { if (!aast.node->code) {
@ -267,7 +267,7 @@ bool exec_alias(
} }
ts.idstack.resize(noff, ident_stack{*ts.pstate}); ts.idstack.resize(noff, ident_stack{*ts.pstate});
force_arg(result, op & BC_INST_RET_MASK); force_arg(result, op & BC_INST_RET_MASK);
anargs->set_value(integer_type(oldargs)); anargs->set_raw_value(integer_type(oldargs));
nargs = offset - skip; nargs = offset - skip;
}; };
try { try {

View File

@ -336,21 +336,15 @@ int main(int argc, char **argv) {
} }
return; return;
} }
if (iv->is_read_only()) {
throw cs::error{
css, "variable '%s' is read only", iv->get_name().data()
};
}
iv->save(css);
if (nargs == 2) { if (nargs == 2) {
iv->set_value(args[1].get_int()); iv->set_value(css, args[1].get_int());
} else if (nargs == 3) { } else if (nargs == 3) {
iv->set_value( iv->set_value(
(args[1].get_int() << 8) | (args[2].get_int() << 16) css, (args[1].get_int() << 8) | (args[2].get_int() << 16)
); );
} else { } else {
iv->set_value( iv->set_value(
args[1].get_int() | (args[2].get_int() << 8) | css, args[1].get_int() | (args[2].get_int() << 8) |
(args[3].get_int() << 16) (args[3].get_int() << 16)
); );
} }
@ -366,13 +360,8 @@ int main(int argc, char **argv) {
} else { } else {
std::printf("%s = %.7g\n", fv->get_name().data(), val); std::printf("%s = %.7g\n", fv->get_name().data(), val);
} }
} else if (fv->is_read_only()) {
throw cs::error{
css, "variable '%s' is read only", fv->get_name().data()
};
} else { } else {
fv->save(css); fv->set_value(css, args[1].get_float());
fv->set_value(args[1].get_float());
} }
}); });
@ -386,13 +375,8 @@ int main(int argc, char **argv) {
} else { } else {
std::printf("%s = [%s]\n", sv->get_name().data(), val.data()); std::printf("%s = [%s]\n", sv->get_name().data(), val.data());
} }
} else if (sv->is_read_only()) {
throw cs::error{
css, "variable '%s' is read only", sv->get_name().data()
};
} else { } else {
sv->save(css); sv->set_value(css, args[1].get_str());
sv->set_value(args[1].get_str());
} }
}); });