From d80e922913e4cc99403156dd13884583fd55ed02 Mon Sep 17 00:00:00 2001 From: q66 Date: Sun, 21 Aug 2016 00:51:45 +0100 Subject: [PATCH] more cleanups --- cs_vm.cc | 7 +------ cubescript.cc | 23 +++++++++++++++-------- cubescript.hh | 3 ++- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/cs_vm.cc b/cs_vm.cc index 4a6699b4..11ea3960 100644 --- a/cs_vm.cc +++ b/cs_vm.cc @@ -463,12 +463,7 @@ static inline void cs_call_alias( a, cs.p_stack, (1<code) { - a->code = reinterpret_cast( - compilecode(cs, a->val_v.get_str()) - ); - } - ostd::Uint32 *codep = reinterpret_cast(a->code); + ostd::Uint32 *codep = reinterpret_cast(a->compile_code(cs)); bcode_incr(codep); runcode(cs, codep+1, (result)); bcode_decr(codep); diff --git a/cubescript.cc b/cubescript.cc index 59dc5731..7577122f 100644 --- a/cubescript.cc +++ b/cubescript.cc @@ -67,31 +67,31 @@ Svar::Svar(ostd::ConstCharRange name, char **s, VarCb f, int fl): Alias::Alias(ostd::ConstCharRange name, char *a, int fl): Ident(IdentType::alias, name, fl), - code(nullptr), p_astack(nullptr) + p_acode(nullptr), p_astack(nullptr) { val_v.set_mstr(a); } Alias::Alias(ostd::ConstCharRange name, CsInt a, int fl): Ident(IdentType::alias, name, fl), - code(nullptr), p_astack(nullptr) + p_acode(nullptr), p_astack(nullptr) { val_v.set_int(a); } Alias::Alias(ostd::ConstCharRange name, CsFloat a, int fl): Ident(IdentType::alias, name, fl), - code(nullptr), p_astack(nullptr) + p_acode(nullptr), p_astack(nullptr) { val_v.set_float(a); } Alias::Alias(ostd::ConstCharRange name, int fl): Ident(IdentType::alias, name, fl), - code(nullptr), p_astack(nullptr) + p_acode(nullptr), p_astack(nullptr) { val_v.set_null(); } Alias::Alias(ostd::ConstCharRange name, CsValue const &v, int fl): Ident(IdentType::alias, name, fl), - code(nullptr), val_v(v), p_astack(nullptr) + val_v(v), p_acode(nullptr), p_astack(nullptr) {} Command::Command( @@ -254,7 +254,7 @@ CsState::~CsState() { Alias *a = i->get_alias(); if (a) { a->force_null(); - delete[] reinterpret_cast(a->code); + a->clean_code(); } else if (i->is_command() || i->is_special()) { delete[] static_cast(i)->cargs; } @@ -723,13 +723,20 @@ void Alias::get_cval(CsValue &v) const { } void Alias::clean_code() { - ostd::Uint32 *bcode = reinterpret_cast(code); + ostd::Uint32 *bcode = reinterpret_cast(p_acode); if (bcode) { bcode_decr(bcode); - code = nullptr; + p_acode = nullptr; } } +Bytecode *Alias::compile_code(CsState &cs) { + if (!p_acode) { + p_acode = reinterpret_cast(compilecode(cs, val_v.get_str())); + } + return p_acode; +} + void Alias::push_arg(CsValue const &v, IdentStack &st, bool um) { if (p_astack == &st) { /* prevent cycles and unnecessary code elsewhere */ diff --git a/cubescript.hh b/cubescript.hh index a025ca71..f26ceb2c 100644 --- a/cubescript.hh +++ b/cubescript.hh @@ -295,7 +295,6 @@ private: }; struct OSTD_EXPORT Alias: Ident { - Bytecode *code; CsValue val_v; Alias(ostd::ConstCharRange n, char *a, int flags); @@ -340,6 +339,7 @@ struct OSTD_EXPORT Alias: Ident { void set_alias(CsState &cs, CsValue &v); void clean_code(); + Bytecode *compile_code(CsState &cs); void force_null() { cleanup_value(); @@ -347,6 +347,7 @@ struct OSTD_EXPORT Alias: Ident { } private: + Bytecode *p_acode; IdentStack *p_astack; };