2021-03-22 22:33:01 +01:00
|
|
|
#include <cubescript/cubescript.hh>
|
|
|
|
|
2021-05-03 00:39:00 +02:00
|
|
|
#include <cstdlib>
|
2021-04-03 03:37:58 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2021-03-24 20:33:20 +01:00
|
|
|
#include "cs_thread.hh"
|
2021-05-08 17:20:56 +02:00
|
|
|
#include "cs_error.hh"
|
2021-03-22 22:33:01 +01:00
|
|
|
|
2021-03-23 23:32:25 +01:00
|
|
|
namespace cubescript {
|
2021-03-22 22:33:01 +01:00
|
|
|
|
2021-05-09 20:21:35 +02:00
|
|
|
static typename error::stack_node *save_stack(state &cs) {
|
2021-04-05 04:10:39 +02:00
|
|
|
auto &ts = state_p{cs}.ts();
|
2021-05-06 23:13:48 +02:00
|
|
|
builtin_var *dalias = ts.istate->ivar_dbgalias;
|
2021-05-14 23:14:48 +02:00
|
|
|
auto dval = std::size_t(std::clamp(
|
2021-05-06 03:34:25 +02:00
|
|
|
dalias->value().get_integer(), integer_type(0), integer_type(1000)
|
2021-05-14 23:14:48 +02:00
|
|
|
));
|
2021-04-03 03:37:58 +02:00
|
|
|
if (!dval) {
|
2021-05-09 20:21:35 +02:00
|
|
|
return nullptr;
|
2021-03-22 22:33:01 +01:00
|
|
|
}
|
2021-05-14 23:14:48 +02:00
|
|
|
std::size_t depth = 0;
|
|
|
|
std::size_t total = ts.callstack.size();
|
2021-03-22 22:33:01 +01:00
|
|
|
if (!total) {
|
2021-05-09 20:21:35 +02:00
|
|
|
return nullptr;
|
2021-03-22 22:33:01 +01:00
|
|
|
}
|
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-03-22 22:33:01 +01:00
|
|
|
);
|
2021-05-09 20:21:35 +02:00
|
|
|
typename error::stack_node *ret = st, *nd = st;
|
2021-03-22 22:33:01 +01:00
|
|
|
++st;
|
2021-05-14 23:14:48 +02:00
|
|
|
for (std::size_t i = total - 1;; --i) {
|
|
|
|
auto &lev = ts.callstack[i];
|
2021-03-22 22:33:01 +01:00
|
|
|
++depth;
|
2021-04-03 03:37:58 +02:00
|
|
|
if (depth < dval) {
|
2021-05-14 23:14:48 +02:00
|
|
|
nd->id = &lev.id;
|
2021-03-22 22:33:01 +01:00
|
|
|
nd->index = total - depth + 1;
|
2021-05-14 23:14:48 +02:00
|
|
|
if (i == 0) {
|
2021-03-22 22:33:01 +01:00
|
|
|
nd->next = nullptr;
|
2021-05-14 23:14:48 +02:00
|
|
|
nd = st++;
|
|
|
|
break;
|
2021-03-22 22:33:01 +01:00
|
|
|
}
|
2021-05-14 23:14:48 +02:00
|
|
|
nd->next = st;
|
2021-03-22 22:33:01 +01:00
|
|
|
nd = st++;
|
2021-05-14 23:14:48 +02:00
|
|
|
} else if (i == 0) {
|
|
|
|
nd->id = &lev.id;
|
2021-03-22 22:33:01 +01:00
|
|
|
nd->index = 1;
|
|
|
|
nd->next = nullptr;
|
2021-05-14 23:14:48 +02:00
|
|
|
break;
|
2021-03-22 22:33:01 +01:00
|
|
|
}
|
|
|
|
}
|
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);
|
2021-03-22 22:33:01 +01:00
|
|
|
}
|
|
|
|
|
2021-05-08 17:20:56 +02:00
|
|
|
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}
|
2021-05-08 17:20:56 +02:00
|
|
|
{
|
|
|
|
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} {
|
2021-05-08 17:20:56 +02:00
|
|
|
p_stack = save_stack(cs);
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:32:25 +01:00
|
|
|
} /* namespace cubescript */
|