libcubescript/src/cs_error.cc

80 lines
2.0 KiB
C++
Raw Normal View History

#include <cubescript/cubescript.hh>
2021-05-03 00:39:00 +02:00
#include <cstdlib>
2021-04-03 03:37:58 +02:00
#include <algorithm>
#include "cs_thread.hh"
#include "cs_error.hh"
2021-03-23 23:32:25 +01:00
namespace cubescript {
2021-05-09 20:21:35 +02:00
static typename error::stack_node *save_stack(state &cs) {
auto &ts = state_p{cs}.ts();
builtin_var *dalias = ts.istate->ivar_dbgalias;
auto dval = std::size_t(std::clamp(
dalias->value().get_integer(), integer_type(0), integer_type(1000)
));
2021-04-03 03:37:58 +02:00
if (!dval) {
2021-05-09 20:21:35 +02:00
return nullptr;
}
std::size_t depth = 0;
std::size_t total = ts.callstack.size();
if (!total) {
2021-05-09 20:21:35 +02:00
return nullptr;
}
2021-05-09 20:21:35 +02:00
auto *st = ts.istate->create_array<typename error::stack_node>(
2021-04-03 03:37:58 +02:00
std::min(total, dval)
);
2021-05-09 20:21:35 +02:00
typename error::stack_node *ret = st, *nd = st;
++st;
for (std::size_t i = total - 1;; --i) {
auto &lev = ts.callstack[i];
++depth;
2021-04-03 03:37:58 +02:00
if (depth < dval) {
nd->id = &lev.id;
nd->index = total - depth + 1;
if (i == 0) {
nd->next = nullptr;
nd = st++;
break;
}
nd->next = st;
nd = st++;
} else if (i == 0) {
nd->id = &lev.id;
nd->index = 1;
nd->next = nullptr;
break;
}
}
2021-05-09 20:21:35 +02:00
return ret;
}
LIBCUBESCRIPT_EXPORT error::~error() {
std::size_t slen = 0;
for (stack_node const *nd = p_stack; nd; nd = nd->next) {
++slen;
}
state_p{*p_state}.ts().istate->destroy_array(p_stack, slen);
}
LIBCUBESCRIPT_EXPORT error::error(state &cs, std::string_view msg):
2021-05-09 20:21:35 +02:00
p_errbeg{}, p_errend{}, p_state{&cs}
{
char *sp;
char *buf = state_p{cs}.ts().request_errbuf(msg.size(), sp);
std::memcpy(buf, msg.data(), msg.size());
buf[msg.size()] = '\0';
p_errbeg = sp;
p_errend = buf + msg.size();
p_stack = save_stack(cs);
}
LIBCUBESCRIPT_EXPORT error::error(
state &cs, char const *errbeg, char const *errend
2021-05-09 20:21:35 +02:00
): p_errbeg{errbeg}, p_errend{errend}, p_state{&cs} {
p_stack = save_stack(cs);
}
2021-03-23 23:32:25 +01:00
} /* namespace cubescript */