remove file exec api
this is something each thing will want to do on its own... but add a new set of calls that take code source for debug infomaster
parent
55d5397f4f
commit
d626fba537
|
@ -481,11 +481,13 @@ struct OSTD_EXPORT cs_state {
|
||||||
|
|
||||||
void run(cs_bcode *code, cs_value &ret);
|
void run(cs_bcode *code, cs_value &ret);
|
||||||
void run(std::string_view code, cs_value &ret);
|
void run(std::string_view code, cs_value &ret);
|
||||||
|
void run(std::string_view code, cs_value &ret, std::string_view source);
|
||||||
void run(cs_ident *id, cs_value_r args, cs_value &ret);
|
void run(cs_ident *id, cs_value_r args, cs_value &ret);
|
||||||
|
|
||||||
void run(cs_bcode *code);
|
cs_value run(cs_bcode *code);
|
||||||
void run(std::string_view code);
|
cs_value run(std::string_view code);
|
||||||
void run(cs_ident *id, cs_value_r args);
|
cs_value run(std::string_view code, std::string_view source);
|
||||||
|
cs_value run(cs_ident *id, cs_value_r args);
|
||||||
|
|
||||||
cs_loop_state run_loop(cs_bcode *code, cs_value &ret);
|
cs_loop_state run_loop(cs_bcode *code, cs_value &ret);
|
||||||
cs_loop_state run_loop(cs_bcode *code);
|
cs_loop_state run_loop(cs_bcode *code);
|
||||||
|
@ -494,13 +496,6 @@ struct OSTD_EXPORT cs_state {
|
||||||
return p_inloop;
|
return p_inloop;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<cs_strref> run_file_str(std::string_view fname);
|
|
||||||
std::optional<cs_int> run_file_int(std::string_view fname);
|
|
||||||
std::optional<cs_float> run_file_float(std::string_view fname);
|
|
||||||
std::optional<bool> run_file_bool(std::string_view fname);
|
|
||||||
bool run_file(std::string_view fname, cs_value &ret);
|
|
||||||
bool run_file(std::string_view fname);
|
|
||||||
|
|
||||||
void set_alias(std::string_view name, cs_value v);
|
void set_alias(std::string_view name, cs_value v);
|
||||||
|
|
||||||
void set_var_int(
|
void set_var_int(
|
||||||
|
|
92
src/cs_vm.cc
92
src/cs_vm.cc
|
@ -1582,6 +1582,12 @@ void cs_state::run(std::string_view code, cs_value &ret) {
|
||||||
cs_run(*this, std::string_view{}, code, ret);
|
cs_run(*this, std::string_view{}, code, ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void cs_state::run(
|
||||||
|
std::string_view code, cs_value &ret, std::string_view source
|
||||||
|
) {
|
||||||
|
cs_run(*this, source, code, ret);
|
||||||
|
}
|
||||||
|
|
||||||
void cs_state::run(cs_ident *id, cs_value_r args, cs_value &ret) {
|
void cs_state::run(cs_ident *id, cs_value_r args, cs_value &ret) {
|
||||||
int nargs = int(args.size());
|
int nargs = int(args.size());
|
||||||
ret.set_none();
|
ret.set_none();
|
||||||
|
@ -1727,19 +1733,28 @@ bool cs_state::run_bool(cs_ident *id, cs_value_r args) {
|
||||||
return ret.get_bool();
|
return ret.get_bool();
|
||||||
}
|
}
|
||||||
|
|
||||||
void cs_state::run(cs_bcode *code) {
|
cs_value cs_state::run(cs_bcode *code) {
|
||||||
cs_value ret{*this};
|
cs_value ret{*this};
|
||||||
run(code, ret);
|
run(code, ret);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cs_state::run(std::string_view code) {
|
cs_value cs_state::run(std::string_view code) {
|
||||||
cs_value ret{*this};
|
cs_value ret{*this};
|
||||||
run(code, ret);
|
run(code, ret);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cs_state::run(cs_ident *id, cs_value_r args) {
|
cs_value cs_state::run(std::string_view code, std::string_view source) {
|
||||||
|
cs_value ret{*this};
|
||||||
|
run(code, ret, source);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
cs_value cs_state::run(cs_ident *id, cs_value_r args) {
|
||||||
cs_value ret{*this};
|
cs_value ret{*this};
|
||||||
run(id, args, ret);
|
run(id, args, ret);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
cs_loop_state cs_state::run_loop(cs_bcode *code, cs_value &ret) {
|
cs_loop_state cs_state::run_loop(cs_bcode *code, cs_value &ret) {
|
||||||
|
@ -1764,75 +1779,4 @@ cs_loop_state cs_state::run_loop(cs_bcode *code) {
|
||||||
return run_loop(code, ret);
|
return run_loop(code, ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool cs_run_file(
|
|
||||||
cs_state &cs, std::string_view fname, cs_value &ret
|
|
||||||
) {
|
|
||||||
std::unique_ptr<char[]> buf;
|
|
||||||
size_t len;
|
|
||||||
|
|
||||||
ostd::file_stream f(fname, ostd::stream_mode::READ);
|
|
||||||
if (!f.is_open()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
len = f.size();
|
|
||||||
buf = std::make_unique<char[]>(len + 1);
|
|
||||||
if (!buf) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
f.get(buf.get(), len);
|
|
||||||
} catch (...) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
buf[len] = '\0';
|
|
||||||
|
|
||||||
cs_run(cs, fname, std::string_view{buf.get(), len}, ret);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<cs_strref> cs_state::run_file_str(std::string_view fname) {
|
|
||||||
cs_value ret{*this};
|
|
||||||
if (!cs_run_file(*this, fname, ret)) {
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
return ret.get_str();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<cs_int> cs_state::run_file_int(std::string_view fname) {
|
|
||||||
cs_value ret{*this};
|
|
||||||
if (!cs_run_file(*this, fname, ret)) {
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
return ret.get_int();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<cs_float> cs_state::run_file_float(std::string_view fname) {
|
|
||||||
cs_value ret{*this};
|
|
||||||
if (!cs_run_file(*this, fname, ret)) {
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
return ret.get_float();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<bool> cs_state::run_file_bool(std::string_view fname) {
|
|
||||||
cs_value ret{*this};
|
|
||||||
if (!cs_run_file(*this, fname, ret)) {
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
return ret.get_bool();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool cs_state::run_file(std::string_view fname, cs_value &ret) {
|
|
||||||
return cs_run_file(*this, fname, ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool cs_state::run_file(std::string_view fname) {
|
|
||||||
cs_value ret{*this};
|
|
||||||
if (!cs_run_file(*this, fname, ret)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
} /* namespace cscript */
|
} /* namespace cscript */
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include <ostd/platform.hh>
|
#include <ostd/platform.hh>
|
||||||
#include <ostd/io.hh>
|
#include <ostd/io.hh>
|
||||||
|
@ -233,13 +234,39 @@ static void repl_print_var(cs_state const &cs, cs_var const &var) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool do_run_file(cs_state &cs, std::string_view fname, cs_value &ret) {
|
||||||
|
std::unique_ptr<char[]> buf;
|
||||||
|
std::size_t len;
|
||||||
|
|
||||||
|
ostd::file_stream f{fname, ostd::stream_mode::READ};
|
||||||
|
if (!f.is_open()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
len = f.size();
|
||||||
|
buf = std::make_unique<char[]>(len + 1);
|
||||||
|
if (!buf) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
f.get(buf.get(), len);
|
||||||
|
} catch (...) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
buf[len] = '\0';
|
||||||
|
|
||||||
|
cs.run(std::string_view{buf.get(), len}, ret, fname);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
static bool do_call(cs_state &cs, std::string_view line, bool file = false) {
|
static bool do_call(cs_state &cs, std::string_view line, bool file = false) {
|
||||||
cs_value ret{cs};
|
cs_value ret{cs};
|
||||||
scs = &cs;
|
scs = &cs;
|
||||||
signal(SIGINT, do_sigint);
|
signal(SIGINT, do_sigint);
|
||||||
try {
|
try {
|
||||||
if (file) {
|
if (file) {
|
||||||
if (!cs.run_file(line, ret)) {
|
if (!do_run_file(cs, line, ret)) {
|
||||||
ostd::cerr.writeln("cannot read file: ", line);
|
ostd::cerr.writeln("cannot read file: ", line);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -325,7 +352,8 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
gcs.new_command("exec", "s", [](auto &cs, auto args, auto &) {
|
gcs.new_command("exec", "s", [](auto &cs, auto args, auto &) {
|
||||||
auto file = args[0].get_str();
|
auto file = args[0].get_str();
|
||||||
bool ret = cs.run_file(file);
|
cs_value val{cs};
|
||||||
|
bool ret = do_run_file(cs, file, val);
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
throw cscript::cs_error(
|
throw cscript::cs_error(
|
||||||
cs, "could not run file \"%s\"", file
|
cs, "could not run file \"%s\"", file
|
||||||
|
|
Loading…
Reference in New Issue