more internal changes

master
Daniel Kolesa 2016-09-02 21:49:05 +01:00
parent 25894b469e
commit fd8a07f47a
3 changed files with 33 additions and 29 deletions

View File

@ -6,13 +6,19 @@
namespace cscript { namespace cscript {
static inline bool cs_has_cmd_cb(CsIdent *id) { struct CsCommandInternal {
if (!id->is_command() && !id->is_special()) { static void call(CsCommand *c, CsValueRange args, CsValue &ret) {
return false; c->p_cb_cftv(args, ret);
} }
CsCommand *cb = static_cast<CsCommand *>(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<CsCommand *>(id);
return !!cb->p_cb_cftv;
}
};
static inline void cs_push_alias(CsIdent *id, CsIdentStack &st) { static inline void cs_push_alias(CsIdent *id, CsIdentStack &st) {
if (id->is_alias() && (id->get_index() >= MaxArguments)) { 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), " "); cscript::util::tvals_concat(buf, ostd::iter(args, i), " ");
CsValue tv; CsValue tv;
tv.set_mstr(buf.get().iter()); tv.set_mstr(buf.get().iter());
id->get_raw_cb()(CsValueRange(&tv, 1), res); CsCommandInternal::call(id, CsValueRange(&tv, 1), res);
goto cleanup; goto cleanup;
} }
case 'V': case 'V':
i = ostd::max(i + 1, numargs); 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; goto cleanup;
case '1': case '1':
case '2': case '2':
@ -438,7 +444,7 @@ static inline void callcommand(
} }
} }
++i; ++i;
id->get_raw_cb()(CsValueRange(args, i), res); CsCommandInternal::call(id, CsValueRange(args, i), res);
cleanup: cleanup:
for (ostd::Size k = 0; k < ostd::Size(i); ++k) { for (ostd::Size k = 0; k < ostd::Size(i); ++k) {
args[k].cleanup(); args[k].cleanup();
@ -1327,8 +1333,8 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) {
CsCommand *id = static_cast<CsCommand *>(cs.identmap[op >> 8]); CsCommand *id = static_cast<CsCommand *>(cs.identmap[op >> 8]);
int offset = numargs - id->get_num_args(); int offset = numargs - id->get_num_args();
result.force_null(); result.force_null();
id->get_raw_cb()( CsCommandInternal::call(
CsValueRange(args + offset, id->get_num_args()), result id, CsValueRange(args + offset, id->get_num_args()), result
); );
force_arg(result, op & CODE_RET_MASK); force_arg(result, op & CODE_RET_MASK);
free_args(args, numargs, offset); free_args(args, numargs, offset);
@ -1342,8 +1348,8 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) {
CsCommand *id = static_cast<CsCommand *>(cs.identmap[op >> 13]); CsCommand *id = static_cast<CsCommand *>(cs.identmap[op >> 13]);
int callargs = (op >> 8) & 0x1F, offset = numargs - callargs; int callargs = (op >> 8) & 0x1F, offset = numargs - callargs;
result.force_null(); result.force_null();
id->get_raw_cb()( CsCommandInternal::call(
ostd::iter(&args[offset], callargs), result id, ostd::iter(&args[offset], callargs), result
); );
force_arg(result, op & CODE_RET_MASK); force_arg(result, op & CODE_RET_MASK);
free_args(args, numargs, offset); free_args(args, numargs, offset);
@ -1363,9 +1369,7 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) {
); );
CsValue tv; CsValue tv;
tv.set_mstr(buf.get().iter()); tv.set_mstr(buf.get().iter());
id->get_raw_cb()( CsCommandInternal::call(id, CsValueRange(&tv, 1), result);
CsValueRange(&tv, 1), result
);
} }
force_arg(result, op & CODE_RET_MASK); force_arg(result, op & CODE_RET_MASK);
free_args(args, numargs, offset); free_args(args, numargs, offset);
@ -1501,7 +1505,7 @@ noid:
result.force_null(); result.force_null();
switch (id->get_type_raw()) { switch (id->get_type_raw()) {
default: default:
if (!cs_has_cmd_cb(id)) { if (!CsCommandInternal::has_cb(id)) {
free_args(args, numargs, offset - 1); free_args(args, numargs, offset - 1);
force_arg(result, op & CODE_RET_MASK); force_arg(result, op & CODE_RET_MASK);
continue; continue;
@ -1619,7 +1623,7 @@ void CsState::run_ret(CsIdent *id, CsValueRange args, CsValue &ret) {
} else if (id) { } else if (id) {
switch (id->get_type()) { switch (id->get_type()) {
default: default:
if (!cs_has_cmd_cb(id)) { if (!CsCommandInternal::has_cb(id)) {
break; break;
} }
/* fallthrough */ /* fallthrough */

View File

@ -243,6 +243,14 @@ void CsSvar::set_value(CsString val) {
p_storage = ostd::move(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); void cs_init_lib_base(CsState &cs);
CsState::CsState(): p_out(&ostd::out), p_err(&ostd::err) { CsState::CsState(): p_out(&ostd::out), p_err(&ostd::err) {

View File

@ -285,18 +285,10 @@ using CsCommandCb = ostd::Function<void(CsValueRange, CsValue &)>;
struct CsCommand: CsIdent { struct CsCommand: CsIdent {
friend struct CsState; friend struct CsState;
friend struct CsCommandInternal;
ostd::ConstCharRange get_args() const { ostd::ConstCharRange get_args() const;
return p_cargs; int get_num_args() const;
}
int get_num_args() const {
return p_numargs;
}
CsCommandCb &get_raw_cb() {
return p_cb_cftv;
}
private: private:
CsCommand( CsCommand(