From fd8a07f47a7bbc4901d138fdbe7e7511ccc25ecf Mon Sep 17 00:00:00 2001 From: q66 Date: Fri, 2 Sep 2016 21:49:05 +0100 Subject: [PATCH] more internal changes --- cs_vm.cc | 40 ++++++++++++++++++++++------------------ cubescript.cc | 8 ++++++++ cubescript.hh | 14 +++----------- 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/cs_vm.cc b/cs_vm.cc index 35b7fbfa..11252afa 100644 --- a/cs_vm.cc +++ b/cs_vm.cc @@ -6,13 +6,19 @@ namespace cscript { -static inline bool cs_has_cmd_cb(CsIdent *id) { - if (!id->is_command() && !id->is_special()) { - return false; +struct CsCommandInternal { + static void call(CsCommand *c, CsValueRange args, CsValue &ret) { + c->p_cb_cftv(args, ret); } - CsCommand *cb = static_cast(id); - return !!cb->get_raw_cb(); -} + + static bool has_cb(CsIdent *id) { + if (!id->is_command() && !id->is_special()) { + return false; + } + CsCommand *cb = static_cast(id); + return !!cb->p_cb_cftv; + } +}; static inline void cs_push_alias(CsIdent *id, CsIdentStack &st) { if (id->is_alias() && (id->get_index() >= MaxArguments)) { @@ -419,12 +425,12 @@ static inline void callcommand( cscript::util::tvals_concat(buf, ostd::iter(args, i), " "); CsValue tv; tv.set_mstr(buf.get().iter()); - id->get_raw_cb()(CsValueRange(&tv, 1), res); + CsCommandInternal::call(id, CsValueRange(&tv, 1), res); goto cleanup; } case 'V': i = ostd::max(i + 1, numargs); - id->get_raw_cb()(ostd::iter(args, i), res); + CsCommandInternal::call(id, ostd::iter(args, i), res); goto cleanup; case '1': case '2': @@ -438,7 +444,7 @@ static inline void callcommand( } } ++i; - id->get_raw_cb()(CsValueRange(args, i), res); + CsCommandInternal::call(id, CsValueRange(args, i), res); cleanup: for (ostd::Size k = 0; k < ostd::Size(i); ++k) { args[k].cleanup(); @@ -1327,8 +1333,8 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) { CsCommand *id = static_cast(cs.identmap[op >> 8]); int offset = numargs - id->get_num_args(); result.force_null(); - id->get_raw_cb()( - CsValueRange(args + offset, id->get_num_args()), result + CsCommandInternal::call( + id, CsValueRange(args + offset, id->get_num_args()), result ); force_arg(result, op & CODE_RET_MASK); free_args(args, numargs, offset); @@ -1342,8 +1348,8 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) { CsCommand *id = static_cast(cs.identmap[op >> 13]); int callargs = (op >> 8) & 0x1F, offset = numargs - callargs; result.force_null(); - id->get_raw_cb()( - ostd::iter(&args[offset], callargs), result + CsCommandInternal::call( + id, ostd::iter(&args[offset], callargs), result ); force_arg(result, op & CODE_RET_MASK); free_args(args, numargs, offset); @@ -1363,9 +1369,7 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) { ); CsValue tv; tv.set_mstr(buf.get().iter()); - id->get_raw_cb()( - CsValueRange(&tv, 1), result - ); + CsCommandInternal::call(id, CsValueRange(&tv, 1), result); } force_arg(result, op & CODE_RET_MASK); free_args(args, numargs, offset); @@ -1501,7 +1505,7 @@ noid: result.force_null(); switch (id->get_type_raw()) { default: - if (!cs_has_cmd_cb(id)) { + if (!CsCommandInternal::has_cb(id)) { free_args(args, numargs, offset - 1); force_arg(result, op & CODE_RET_MASK); continue; @@ -1619,7 +1623,7 @@ void CsState::run_ret(CsIdent *id, CsValueRange args, CsValue &ret) { } else if (id) { switch (id->get_type()) { default: - if (!cs_has_cmd_cb(id)) { + if (!CsCommandInternal::has_cb(id)) { break; } /* fallthrough */ diff --git a/cubescript.cc b/cubescript.cc index 0c14aed6..d7ca295d 100644 --- a/cubescript.cc +++ b/cubescript.cc @@ -243,6 +243,14 @@ void CsSvar::set_value(CsString val) { p_storage = ostd::move(val); } +ostd::ConstCharRange CsCommand::get_args() const { + return p_cargs; +} + +int CsCommand::get_num_args() const { + return p_numargs; +} + void cs_init_lib_base(CsState &cs); CsState::CsState(): p_out(&ostd::out), p_err(&ostd::err) { diff --git a/cubescript.hh b/cubescript.hh index c30615dc..009b5e08 100644 --- a/cubescript.hh +++ b/cubescript.hh @@ -285,18 +285,10 @@ using CsCommandCb = ostd::Function; struct CsCommand: CsIdent { friend struct CsState; + friend struct CsCommandInternal; - ostd::ConstCharRange get_args() const { - return p_cargs; - } - - int get_num_args() const { - return p_numargs; - } - - CsCommandCb &get_raw_cb() { - return p_cb_cftv; - } + ostd::ConstCharRange get_args() const; + int get_num_args() const; private: CsCommand(