From 194d5f960ba71fd01dcc46564416af7d13543832 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Mon, 5 Apr 2021 18:32:45 +0200 Subject: [PATCH] drop ret-by-reference run APIs --- include/cubescript/cubescript/state.hh | 5 -- src/cs_state.cc | 72 ++++++++------------------ src/cs_vm.cc | 8 +-- src/lib_base.cc | 16 +++--- tools/repl.cc | 4 +- 5 files changed, 37 insertions(+), 68 deletions(-) diff --git a/include/cubescript/cubescript/state.hh b/include/cubescript/cubescript/state.hh index 3e1edd5..1a9c04f 100644 --- a/include/cubescript/cubescript/state.hh +++ b/include/cubescript/cubescript/state.hh @@ -102,11 +102,6 @@ struct LIBCUBESCRIPT_EXPORT state { std::span get_idents(); std::span get_idents() const; - void run(bcode_ref const &code, any_value &ret); - void run(std::string_view code, any_value &ret); - void run(std::string_view code, any_value &ret, std::string_view source); - void run(ident *id, std::span args, any_value &ret); - any_value run(bcode_ref const &code); any_value run(std::string_view code); any_value run(std::string_view code, std::string_view source); diff --git a/src/cs_state.cc b/src/cs_state.cc index c36f3f7..5dcdc7f 100644 --- a/src/cs_state.cc +++ b/src/cs_state.cc @@ -175,19 +175,19 @@ state::state(alloc_func func, void *data) { /* builtins */ p = new_command("do", "e", [](auto &cs, auto args, auto &res) { - cs.run(args[0].get_code(), res); + res = cs.run(args[0].get_code()); }); static_cast(p)->p_type = ID_DO; p = new_command("doargs", "e", [](auto &cs, auto args, auto &res) { call_with_args(*cs.p_tstate, [&cs, &res, &args]() { - cs.run(args[0].get_code(), res); + res = cs.run(args[0].get_code()); }); }); static_cast(p)->p_type = ID_DOARGS; p = new_command("if", "tee", [](auto &cs, auto args, auto &res) { - cs.run((args[0].get_bool() ? args[1] : args[2]).get_code(), res); + res = cs.run((args[0].get_bool() ? args[1] : args[2]).get_code()); }); static_cast(p)->p_type = ID_IF; @@ -208,7 +208,7 @@ state::state(alloc_func func, void *data) { for (size_t i = 0; i < args.size(); ++i) { auto code = args[i].get_code(); if (code) { - cs.run(code, res); + res = cs.run(code); } else { res = std::move(args[i]); } @@ -227,7 +227,7 @@ state::state(alloc_func func, void *data) { for (size_t i = 0; i < args.size(); ++i) { auto code = args[i].get_code(); if (code) { - cs.run(code, res); + res = cs.run(code); } else { res = std::move(args[i]); } @@ -501,11 +501,9 @@ LIBCUBESCRIPT_EXPORT void state::set_alias( } case ident_type::IVAR: case ident_type::FVAR: - case ident_type::SVAR: { - any_value ret{*this}; - run(id, std::span{&v, 1}, ret); + case ident_type::SVAR: + run(id, std::span{&v, 1}); break; - } default: throw error{ *this, "cannot redefine builtin %s with an alias", @@ -633,15 +631,17 @@ LIBCUBESCRIPT_EXPORT void state::init_libs(int libs) { } } -LIBCUBESCRIPT_EXPORT void state::run(bcode_ref const &code, any_value &ret) { +LIBCUBESCRIPT_EXPORT any_value state::run(bcode_ref const &code) { + any_value ret{*this}; bcode *p = code; vm_exec(*p_tstate, reinterpret_cast(p), ret); + return ret; } -static void do_run( - thread_state &ts, std::string_view file, std::string_view code, - any_value &ret +static any_value do_run( + thread_state &ts, std::string_view file, std::string_view code ) { + any_value ret{*ts.pstate}; codegen_state gs{ts}; gs.src_name = file; gs.code.reserve(64); @@ -652,23 +652,24 @@ static void do_run( bcode_ref cref{reinterpret_cast(cbuf + 1)}; bcode *p = cref; vm_exec(ts, p->get_raw(), ret); + return ret; } -LIBCUBESCRIPT_EXPORT void state::run(std::string_view code, any_value &ret) { - do_run(*p_tstate, std::string_view{}, code, ret); +LIBCUBESCRIPT_EXPORT any_value state::run(std::string_view code) { + return do_run(*p_tstate, std::string_view{}, code); } -LIBCUBESCRIPT_EXPORT void state::run( - std::string_view code, any_value &ret, std::string_view source +LIBCUBESCRIPT_EXPORT any_value state::run( + std::string_view code, std::string_view source ) { - do_run(*p_tstate, source, code, ret); + return do_run(*p_tstate, source, code); } -LIBCUBESCRIPT_EXPORT void state::run( - ident *id, std::span args, any_value &ret +LIBCUBESCRIPT_EXPORT any_value state::run( + ident *id, std::span args ) { + any_value ret{*this}; std::size_t nargs = args.size(); - ret.set_none(); run_depth_guard level{*p_tstate}; /* incr and decr on scope exit */ if (id) { switch (id->get_type()) { @@ -762,33 +763,6 @@ LIBCUBESCRIPT_EXPORT void state::run( } } } -} - -LIBCUBESCRIPT_EXPORT any_value state::run(bcode_ref const &code) { - any_value ret{*this}; - run(code, ret); - return ret; -} - -LIBCUBESCRIPT_EXPORT any_value state::run(std::string_view code) { - any_value ret{*this}; - run(code, ret); - return ret; -} - -LIBCUBESCRIPT_EXPORT any_value state::run( - std::string_view code, std::string_view source -) { - any_value ret{*this}; - run(code, ret, source); - return ret; -} - -LIBCUBESCRIPT_EXPORT any_value state::run( - ident *id, std::span args -) { - any_value ret{*this}; - run(id, args, ret); return ret; } @@ -797,7 +771,7 @@ LIBCUBESCRIPT_EXPORT loop_state state::run_loop( ) { ++p_tstate->loop_level; try { - run(code, ret); + ret = run(code); } catch (break_exception) { --p_tstate->loop_level; return loop_state::BREAK; diff --git a/src/cs_vm.cc b/src/cs_vm.cc index 48f380e..01c12c5 100644 --- a/src/cs_vm.cc +++ b/src/cs_vm.cc @@ -572,7 +572,7 @@ std::uint32_t *vm_exec( call_with_args(ts, [&]() { auto v = std::move(args.back()); args.pop_back(); - cs.run(v.get_code(), result); + result = cs.run(v.get_code()); force_arg(result, op & BC_INST_RET_MASK); }); continue; @@ -583,7 +583,7 @@ std::uint32_t *vm_exec( case BC_INST_DO | BC_RET_FLOAT: { auto v = std::move(args.back()); args.pop_back(); - cs.run(v.get_code(), result); + result = cs.run(v.get_code()); force_arg(result, op & BC_INST_RET_MASK); continue; } @@ -614,7 +614,7 @@ std::uint32_t *vm_exec( auto v = std::move(args.back()); args.pop_back(); if (v.get_type() == value_type::CODE) { - cs.run(v.get_code(), result); + result = cs.run(v.get_code()); } else { result = std::move(v); } @@ -628,7 +628,7 @@ std::uint32_t *vm_exec( auto v = std::move(args.back()); args.pop_back(); if (v.get_type() == value_type::CODE) { - cs.run(v.get_code(), result); + result = cs.run(v.get_code()); } else { result = std::move(v); } diff --git a/src/lib_base.cc b/src/lib_base.cc index 7bf2432..b11a966 100644 --- a/src/lib_base.cc +++ b/src/lib_base.cc @@ -80,7 +80,7 @@ void init_lib_base(state &gcs) { any_value result{cs}, tback{cs}; bool rc = true; try { - cs.run(args[0].get_code(), result); + result = cs.run(args[0].get_code()); } catch (error const &e) { result.set_str(e.what()); if (e.get_stack().get()) { @@ -108,11 +108,11 @@ void init_lib_base(state &gcs) { for (size_t i = 0; i < args.size(); i += 2) { if ((i + 1) < args.size()) { if (cs.run(args[i].get_code()).get_bool()) { - cs.run(args[i + 1].get_code(), res); + res = cs.run(args[i + 1].get_code()); break; } } else { - cs.run(args[i].get_code(), res); + res = cs.run(args[i].get_code()); break; } } @@ -125,7 +125,7 @@ void init_lib_base(state &gcs) { (args[i].get_type() == value_type::NONE) || (args[i].get_int() == val) ) { - cs.run(args[i + 1].get_code(), res); + res = cs.run(args[i + 1].get_code()); return; } } @@ -138,7 +138,7 @@ void init_lib_base(state &gcs) { (args[i].get_type() == value_type::NONE) || (args[i].get_float() == val) ) { - cs.run(args[i + 1].get_code(), res); + res = cs.run(args[i + 1].get_code()); return; } } @@ -151,7 +151,7 @@ void init_lib_base(state &gcs) { (args[i].get_type() == value_type::NONE) || (args[i].get_str() == val) ) { - cs.run(args[i + 1].get_code(), res); + res = cs.run(args[i + 1].get_code()); return; } } @@ -164,7 +164,7 @@ void init_lib_base(state &gcs) { } if (args[1].get_bool()) { st.set(args[1]); - cs.run(args[2].get_code(), res); + res = cs.run(args[2].get_code()); } } }); @@ -308,7 +308,7 @@ end: return; } st.set(args[1]); - cs.run(args[2].get_code(), res); + res = cs.run(args[2].get_code()); } }); diff --git a/tools/repl.cc b/tools/repl.cc index e2f2058..2c41361 100644 --- a/tools/repl.cc +++ b/tools/repl.cc @@ -224,7 +224,7 @@ static bool do_run_file( buf[len] = '\0'; - cs.run(std::string_view{buf.get(), std::size_t(len)}, ret, fname); + ret = cs.run(std::string_view{buf.get(), std::size_t(len)}, fname); return true; } @@ -238,7 +238,7 @@ static bool do_call(cs::state &cs, std::string_view line, bool file = false) { std::fprintf(stderr, "cannot read file: %s\n", line.data()); } } else { - cs.run(line, ret); + ret = cs.run(line); } } catch (cs::error const &e) { signal(SIGINT, SIG_DFL);