eliminate more future private accesses

master
Daniel Kolesa 2021-03-26 02:59:42 +01:00
parent 0b2c4c7038
commit bd9a6cbf7c
8 changed files with 64 additions and 60 deletions

View File

@ -529,8 +529,6 @@ enum class loop_state {
}; };
struct LIBCUBESCRIPT_EXPORT state { struct LIBCUBESCRIPT_EXPORT state {
thread_state *p_tstate = nullptr;
int identflags = 0; int identflags = 0;
state(); state();
@ -695,6 +693,8 @@ struct LIBCUBESCRIPT_EXPORT state {
void print_var(global_var const &v) const; void print_var(global_var const &v) const;
thread_state *p_tstate = nullptr;
private: private:
hook_func set_call_hook(hook_func func); hook_func set_call_hook(hook_func func);
var_print_func set_var_printer(var_print_func func); var_print_func set_var_printer(var_print_func func);
@ -728,15 +728,15 @@ private:
void *alloc(void *ptr, size_t olds, size_t news); void *alloc(void *ptr, size_t olds, size_t news);
}; };
struct stack_state_node {
stack_state_node const *next;
ident const *id;
int index;
};
struct stack_state { struct stack_state {
struct node {
node const *next;
ident const *id;
int index;
};
stack_state() = delete; stack_state() = delete;
stack_state(state &cs, stack_state_node *nd = nullptr, bool gap = false); stack_state(thread_state &ts, node *nd = nullptr, bool gap = false);
stack_state(stack_state const &) = delete; stack_state(stack_state const &) = delete;
stack_state(stack_state &&st); stack_state(stack_state &&st);
~stack_state(); ~stack_state();
@ -744,12 +744,12 @@ struct stack_state {
stack_state &operator=(stack_state const &) = delete; stack_state &operator=(stack_state const &) = delete;
stack_state &operator=(stack_state &&); stack_state &operator=(stack_state &&);
stack_state_node const *get() const; node const *get() const;
bool gap() const; bool gap() const;
private: private:
state &p_state; thread_state &p_state;
stack_state_node *p_node; node *p_node;
bool p_gap; bool p_gap;
}; };
@ -774,25 +774,30 @@ struct LIBCUBESCRIPT_EXPORT error {
return p_stack; return p_stack;
} }
error(state &cs, std::string_view msg): template<typename ...A>
p_errmsg(), p_stack(cs) error(state &cs, std::string_view msg, A const &...args):
error{*cs.p_tstate, msg, args...}
{}
error(thread_state &ts, std::string_view msg):
p_errmsg{}, p_stack{ts}
{ {
char *sp; char *sp;
char *buf = request_buf(cs, msg.size(), sp); char *buf = request_buf(ts, msg.size(), sp);
std::memcpy(buf, msg.data(), msg.size()); std::memcpy(buf, msg.data(), msg.size());
buf[msg.size()] = '\0'; buf[msg.size()] = '\0';
p_errmsg = std::string_view{sp, buf + msg.size()}; p_errmsg = std::string_view{sp, buf + msg.size()};
p_stack = save_stack(cs); p_stack = save_stack(ts);
} }
template<typename ...A> template<typename ...A>
error(state &cs, std::string_view msg, A const &...args): error(thread_state &ts, std::string_view msg, A const &...args):
p_errmsg(), p_stack(cs) p_errmsg{}, p_stack{ts}
{ {
std::size_t sz = msg.size() + 64; std::size_t sz = msg.size() + 64;
char *buf, *sp; char *buf, *sp;
for (;;) { for (;;) {
buf = request_buf(cs, sz, sp); buf = request_buf(ts, sz, sp);
int written = std::snprintf(buf, sz, msg.data(), args...); int written = std::snprintf(buf, sz, msg.data(), args...);
if (written <= 0) { if (written <= 0) {
throw internal_error{"format error"}; throw internal_error{"format error"};
@ -802,12 +807,12 @@ struct LIBCUBESCRIPT_EXPORT error {
sz = std::size_t(written); sz = std::size_t(written);
} }
p_errmsg = std::string_view{sp, buf + sz}; p_errmsg = std::string_view{sp, buf + sz};
p_stack = save_stack(cs); p_stack = save_stack(ts);
} }
private: private:
stack_state save_stack(state &cs); stack_state save_stack(thread_state &ts);
char *request_buf(state &cs, std::size_t bufs, char *&sp); char *request_buf(thread_state &ts, std::size_t bufs, char *&sp);
std::string_view p_errmsg; std::string_view p_errmsg;
stack_state p_stack; stack_state p_stack;

View File

@ -6,10 +6,10 @@
namespace cubescript { namespace cubescript {
LIBCUBESCRIPT_EXPORT char *error::request_buf( LIBCUBESCRIPT_EXPORT char *error::request_buf(
state &cs, std::size_t bufs, char *&sp thread_state &ts, std::size_t bufs, char *&sp
) { ) {
charbuf &cb = cs.p_tstate->errbuf; charbuf &cb = ts.errbuf;
codegen_state *gs = cs.p_tstate->cstate; codegen_state *gs = ts.cstate;
cb.clear(); cb.clear();
std::size_t sz = 0; std::size_t sz = 0;
if (gs) { if (gs) {
@ -44,27 +44,26 @@ LIBCUBESCRIPT_EXPORT char *error::request_buf(
return &cb[sz]; return &cb[sz];
} }
LIBCUBESCRIPT_EXPORT stack_state error::save_stack(state &cs) { LIBCUBESCRIPT_EXPORT stack_state error::save_stack(thread_state &ts) {
auto &ts = *cs.p_tstate;
integer_var *dalias = static_cast<integer_var *>( integer_var *dalias = static_cast<integer_var *>(
ts.istate->identmap[ID_IDX_DBGALIAS] ts.istate->identmap[ID_IDX_DBGALIAS]
); );
if (!dalias->get_value()) { if (!dalias->get_value()) {
return stack_state(cs, nullptr, !!cs.p_tstate->callstack); return stack_state{ts, nullptr, !!ts.callstack};
} }
int total = 0, depth = 0; int total = 0, depth = 0;
for (ident_link *l = cs.p_tstate->callstack; l; l = l->next) { for (ident_link *l = ts.callstack; l; l = l->next) {
total++; total++;
} }
if (!total) { if (!total) {
return stack_state(cs, nullptr, false); return stack_state{ts, nullptr, false};
} }
stack_state_node *st = ts.istate->create_array<stack_state_node>( stack_state::node *st = ts.istate->create_array<stack_state::node>(
std::min(total, dalias->get_value()) std::min(total, dalias->get_value())
); );
stack_state_node *ret = st, *nd = st; stack_state::node *ret = st, *nd = st;
++st; ++st;
for (ident_link *l = cs.p_tstate->callstack; l; l = l->next) { for (ident_link *l = ts.callstack; l; l = l->next) {
++depth; ++depth;
if (depth < dalias->get_value()) { if (depth < dalias->get_value()) {
nd->id = l->id; nd->id = l->id;
@ -81,7 +80,7 @@ LIBCUBESCRIPT_EXPORT stack_state error::save_stack(state &cs) {
nd->next = nullptr; nd->next = nullptr;
} }
} }
return stack_state(cs, ret, total > dalias->get_value()); return stack_state{ts, ret, total > dalias->get_value()};
} }
} /* namespace cubescript */ } /* namespace cubescript */

View File

@ -69,7 +69,7 @@ void codegen_state::skip_comments() {
if (current() == '\\') { if (current() == '\\') {
char c = current(1); char c = current(1);
if ((c != '\r') && (c != '\n')) { if ((c != '\r') && (c != '\n')) {
throw error{*ts.pstate, "invalid line break"}; throw error{ts, "invalid line break"};
} }
/* skip backslash */ /* skip backslash */
next_char(); next_char();
@ -583,7 +583,7 @@ static void compileblockmain(codegen_state &gs, int wordtype, int prevargs) {
for (int brak = 1; brak;) { for (int brak = 1; brak;) {
switch (gs.skip_until("@\"/[]")) { switch (gs.skip_until("@\"/[]")) {
case '\0': case '\0':
throw error{*gs.ts.pstate, "missing \"]\""}; throw error{gs.ts, "missing \"]\""};
return; return;
case '\"': case '\"':
gs.get_str(); gs.get_str();
@ -612,7 +612,7 @@ static void compileblockmain(codegen_state &gs, int wordtype, int prevargs) {
if (brak > level) { if (brak > level) {
continue; continue;
} else if (brak < level) { } else if (brak < level) {
throw error{*gs.ts.pstate, "too many @s"}; throw error{gs.ts, "too many @s"};
return; return;
} }
if (!concs && prevargs >= MAX_RESULTS) { if (!concs && prevargs >= MAX_RESULTS) {
@ -1435,7 +1435,7 @@ endstatement:
switch (gs.skip_until(")];/\n")) { switch (gs.skip_until(")];/\n")) {
case '\0': case '\0':
if (gs.current() != brak) { if (gs.current() != brak) {
throw error{*gs.ts.pstate, "missing \"%c\"", char(brak)}; throw error{gs.ts, "missing \"%c\"", char(brak)};
return; return;
} }
return; return;
@ -1445,7 +1445,7 @@ endstatement:
gs.next_char(); gs.next_char();
return; return;
} }
throw error{*gs.ts.pstate, "unexpected \"%c\"", gs.current()}; throw error{gs.ts, "unexpected \"%c\"", gs.current()};
return; return;
case '/': case '/':
gs.next_char(); gs.next_char();

View File

@ -399,7 +399,7 @@ LIBCUBESCRIPT_EXPORT std::size_t list_parser::count() {
LIBCUBESCRIPT_EXPORT string_ref list_parser::get_item() const { LIBCUBESCRIPT_EXPORT string_ref list_parser::get_item() const {
if (!p_quoted_item.empty() && (p_quoted_item.front() == '"')) { if (!p_quoted_item.empty() && (p_quoted_item.front() == '"')) {
charbuf buf{p_state->p_tstate->istate}; charbuf buf{*p_state};
unescape_string(std::back_inserter(buf), p_item); unescape_string(std::back_inserter(buf), p_item);
return string_ref{*p_state, buf.str()}; return string_ref{*p_state, buf.str()};
} }

View File

@ -5,5 +5,6 @@
namespace cubescript { namespace cubescript {
charbuf::charbuf(state &cs): charbuf{cs.p_tstate->istate} {} charbuf::charbuf(state &cs): charbuf{cs.p_tstate->istate} {}
charbuf::charbuf(thread_state &ts): charbuf{ts.istate} {}
} /* namespace cubescript */ } /* namespace cubescript */

View File

@ -13,8 +13,6 @@
namespace cubescript { namespace cubescript {
struct state;
/* run func, call the second one after finishing */ /* run func, call the second one after finishing */
template<typename F1, typename F2> template<typename F1, typename F2>
@ -110,6 +108,7 @@ struct valbuf {
struct charbuf: valbuf<char> { struct charbuf: valbuf<char> {
charbuf(internal_state *cs): valbuf<char>{cs} {} charbuf(internal_state *cs): valbuf<char>{cs} {}
charbuf(state &cs); charbuf(state &cs);
charbuf(thread_state &ts);
void append(char const *beg, char const *end) { void append(char const *beg, char const *end) {
valbuf<char>::append(beg, end); valbuf<char>::append(beg, end);

View File

@ -28,11 +28,11 @@ static inline void pop_alias(ident *id) {
} }
} }
stack_state::stack_state(state &cs, stack_state_node *nd, bool gap): stack_state::stack_state(thread_state &ts, node *nd, bool gap):
p_state(cs), p_node(nd), p_gap(gap) p_state{ts}, p_node{nd}, p_gap{gap}
{} {}
stack_state::stack_state(stack_state &&st): stack_state::stack_state(stack_state &&st):
p_state(st.p_state), p_node(st.p_node), p_gap(st.p_gap) p_state{st.p_state}, p_node{st.p_node}, p_gap{st.p_gap}
{ {
st.p_node = nullptr; st.p_node = nullptr;
st.p_gap = false; st.p_gap = false;
@ -40,10 +40,10 @@ stack_state::stack_state(stack_state &&st):
stack_state::~stack_state() { stack_state::~stack_state() {
size_t len = 0; size_t len = 0;
for (stack_state_node const *nd = p_node; nd; nd = nd->next) { for (node const *nd = p_node; nd; nd = nd->next) {
++len; ++len;
} }
p_state.p_tstate->istate->destroy_array(p_node, len); p_state.istate->destroy_array(p_node, len);
} }
stack_state &stack_state::operator=(stack_state &&st) { stack_state &stack_state::operator=(stack_state &&st) {
@ -54,7 +54,7 @@ stack_state &stack_state::operator=(stack_state &&st) {
return *this; return *this;
} }
stack_state_node const *stack_state::get() const { stack_state::node const *stack_state::get() const {
return p_node; return p_node;
} }
@ -297,9 +297,9 @@ static thread_local int rundepth = 0;
struct run_depth_guard { struct run_depth_guard {
run_depth_guard() = delete; run_depth_guard() = delete;
run_depth_guard(state &cs) { run_depth_guard(thread_state &ts) {
if (rundepth >= MaxRunDepth) { if (rundepth >= MaxRunDepth) {
throw error(cs, "exceeded recursion limit"); throw error{ts, "exceeded recursion limit"};
} }
++rundepth; ++rundepth;
} }
@ -394,7 +394,7 @@ static std::uint32_t *runcode(
) { ) {
result.set_none(); result.set_none();
auto &cs = *ts.pstate; auto &cs = *ts.pstate;
run_depth_guard level{cs}; /* incr and decr on scope exit */ run_depth_guard level{ts}; /* incr and decr on scope exit */
stack_guard guard{ts}; /* resize back to original */ stack_guard guard{ts}; /* resize back to original */
auto &args = ts.vmstack; auto &args = ts.vmstack;
auto &chook = cs.get_call_hook(); auto &chook = cs.get_call_hook();
@ -1453,38 +1453,38 @@ void state::run(bcode *code, any_value &ret) {
} }
static void do_run( static void do_run(
state &cs, std::string_view file, std::string_view code, thread_state &ts, std::string_view file, std::string_view code,
any_value &ret any_value &ret
) { ) {
codegen_state gs{*cs.p_tstate}; codegen_state gs{ts};
gs.src_name = file; gs.src_name = file;
gs.code.reserve(64); gs.code.reserve(64);
gs.gen_main(code, VAL_ANY); gs.gen_main(code, VAL_ANY);
gs.done(); gs.done();
std::uint32_t *cbuf = bcode_alloc(cs.p_tstate->istate, gs.code.size()); std::uint32_t *cbuf = bcode_alloc(ts.istate, gs.code.size());
std::memcpy(cbuf, gs.code.data(), gs.code.size() * sizeof(std::uint32_t)); std::memcpy(cbuf, gs.code.data(), gs.code.size() * sizeof(std::uint32_t));
bcode_incr(cbuf); bcode_incr(cbuf);
call_with_cleanup([&cs, cbuf, &ret]() { call_with_cleanup([&ts, cbuf, &ret]() {
runcode(*cs.p_tstate, cbuf + 1, ret); runcode(ts, cbuf + 1, ret);
}, [cbuf]() { }, [cbuf]() {
bcode_decr(cbuf); bcode_decr(cbuf);
}); });
} }
void state::run(std::string_view code, any_value &ret) { void state::run(std::string_view code, any_value &ret) {
do_run(*this, std::string_view{}, code, ret); do_run(*p_tstate, std::string_view{}, code, ret);
} }
void state::run( void state::run(
std::string_view code, any_value &ret, std::string_view source std::string_view code, any_value &ret, std::string_view source
) { ) {
do_run(*this, source, code, ret); do_run(*p_tstate, source, code, ret);
} }
void state::run(ident *id, std::span<any_value> args, any_value &ret) { void state::run(ident *id, std::span<any_value> args, any_value &ret) {
std::size_t nargs = args.size(); std::size_t nargs = args.size();
ret.set_none(); ret.set_none();
run_depth_guard level{*this}; /* incr and decr on scope exit */ run_depth_guard level{*p_tstate}; /* incr and decr on scope exit */
if (id) { if (id) {
switch (id->get_type()) { switch (id->get_type()) {
default: default:

View File

@ -63,7 +63,7 @@ end:
void init_lib_base(state &gcs) { void init_lib_base(state &gcs) {
gcs.new_command("error", "s", [](auto &cs, auto args, auto &) { gcs.new_command("error", "s", [](auto &cs, auto args, auto &) {
throw error(cs, args[0].get_str()); throw error{cs, args[0].get_str()};
}); });
gcs.new_command("pcall", "err", [](auto &cs, auto args, auto &ret) { gcs.new_command("pcall", "err", [](auto &cs, auto args, auto &ret) {