From dc5294bac3c963f174d764dbf5b60189c7ea20c1 Mon Sep 17 00:00:00 2001 From: q66 Date: Sat, 10 Sep 2016 20:43:58 +0200 Subject: [PATCH] re-done pcall without blocks --- include/cubescript/cubescript.hh | 5 ++++- src/cs_vm.cc | 2 +- src/cubescript.cc | 32 +++++++++++++++++--------------- tools/repl.cc | 1 + 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/include/cubescript/cubescript.hh b/include/cubescript/cubescript.hh index 4ccce190..fe82b7b3 100644 --- a/include/cubescript/cubescript.hh +++ b/include/cubescript/cubescript.hh @@ -693,7 +693,7 @@ namespace util { auto rt = ostd::format( writer, ((nd->index == 1) && st.gap()) - ? " ..%d) %s\n" : " %d) %s\n", + ? " ..%d) %s" : " %d) %s", nd->index, nd->id->get_name() ); if (rt > 0) { @@ -702,6 +702,9 @@ namespace util { return ret; } nd = nd->next; + if (nd) { + ret += writer.put('\n'); + } } return ret; } diff --git a/src/cs_vm.cc b/src/cs_vm.cc index d47e03d2..aa0dee5e 100644 --- a/src/cs_vm.cc +++ b/src/cs_vm.cc @@ -96,7 +96,7 @@ CsStackState cs_save_stack(CsState &cs) { nd->next = nullptr; } } - return CsStackState(ret, total != dalias->get_value()); + return CsStackState(ret, total > dalias->get_value()); } static void bcode_ref(ostd::Uint32 *code) { diff --git a/src/cubescript.cc b/src/cubescript.cc index 41db5188..70125270 100644 --- a/src/cubescript.cc +++ b/src/cubescript.cc @@ -1080,30 +1080,32 @@ void cs_init_lib_base(CsState &cs) { 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()) - ) { + cs.new_command("pcall", "err", [&cs](CsValueRange args, CsValue &ret) { + CsAlias *cret = args[1].get_ident()->get_alias(), + *css = args[2].get_ident()->get_alias(); + if (!cret || !css) { ret.set_int(0); return; } CsString errmsg; - CsValue result; + CsValue result, tback; + CsStackState stack; bool rc = cs.pcall([&cs, &args, &result]() { cs.run(args[0].get_code(), result); - }, &errmsg); + }, &errmsg, &stack); ret.set_int(rc); - cst.set_int(rc); - cst.push(); if (!rc) { - cret.set_str(ostd::move(errmsg)); - } else { - cret = ostd::move(result); + result.set_str(ostd::move(errmsg)); + if (stack.get()) { + auto app = ostd::appender(); + cscript::util::print_stack(app, stack); + tback.set_str(ostd::move(app.get())); + } + } + CsAliasInternal::set_alias(cret, cs, result); + if (css->get_index() != DummyIdx) { + CsAliasInternal::set_alias(css, cs, tback); } - cret.push(); - cs.run(args[3].get_code()); }); cs.new_command("?", "tTT", [](CsValueRange args, CsValue &res) { diff --git a/tools/repl.cc b/tools/repl.cc index 756d7212..32213093 100644 --- a/tools/repl.cc +++ b/tools/repl.cc @@ -218,6 +218,7 @@ static bool do_call(CsState &cs, ostd::ConstCharRange line, bool file = false) { } cs.get_out().writeln(col.empty() ? "stdin: " : "stdin:", err); cscript::util::print_stack(cs.get_out().iter(), st); + cs.get_out().write('\n'); return false; } signal(SIGINT, SIG_DFL);