simple interface to error/pcall on cubescript side

master
Daniel Kolesa 2016-09-09 01:18:06 +02:00
parent bb72c27d2d
commit 9b292188c8
2 changed files with 31 additions and 1 deletions

View File

@ -1082,6 +1082,36 @@ static inline void cs_loop_conc(
} }
void cs_init_lib_base(CsState &cs) { void cs_init_lib_base(CsState &cs) {
cs.new_command("error", "s", [&cs](CsValueRange args, CsValue &) {
cs.error(args[0].get_strr());
});
cs.new_command("pcall", "erre", [&cs](CsValueRange args, CsValue &ret) {
CsStackedValue cst, cret;
if (
!cst.set_alias(args[1].get_ident()) ||
!cret.set_alias(args[2].get_ident())
) {
ret.set_int(0);
return;
}
CsString errmsg;
CsValue result;
bool rc = cs.pcall([&cs, &args, &result]() {
cs.run(args[0].get_code(), result);
}, &errmsg);
ret.set_int(rc);
cst.set_int(rc);
cst.push();
if (!rc) {
cret.set_str(ostd::move(errmsg));
} else {
cret = ostd::move(result);
}
cret.push();
cs.run(args[3].get_code());
});
cs.new_command("?", "tTT", [](CsValueRange args, CsValue &res) { cs.new_command("?", "tTT", [](CsValueRange args, CsValue &res) {
if (args[0].get_bool()) { if (args[0].get_bool()) {
res = ostd::move(args[1]); res = ostd::move(args[1]);

View File

@ -207,7 +207,7 @@ static void do_call(CsState &cs, ostd::ConstCharRange line, bool file = false) {
} }
}, &err)) { }, &err)) {
signal(SIGINT, SIG_DFL); signal(SIGINT, SIG_DFL);
ostd::writeln(err); ostd::writeln("error: ", err);
return; return;
} }
signal(SIGINT, SIG_DFL); signal(SIGINT, SIG_DFL);