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-03-27 00:26:59 +01:00
|
|
|
LIBCUBESCRIPT_EXPORT stack_state::stack_state(
|
2021-05-09 20:01:47 +02:00
|
|
|
state &cs, node *nd
|
2021-03-27 00:26:59 +01:00
|
|
|
):
|
2021-05-09 20:01:47 +02:00
|
|
|
p_state{cs}, p_node{nd}
|
2021-03-27 00:26:59 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
LIBCUBESCRIPT_EXPORT stack_state::stack_state(stack_state &&st):
|
2021-05-09 20:01:47 +02:00
|
|
|
p_state{st.p_state}, p_node{st.p_node}
|
2021-03-27 00:26:59 +01:00
|
|
|
{
|
|
|
|
st.p_node = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
LIBCUBESCRIPT_EXPORT stack_state::~stack_state() {
|
|
|
|
size_t len = 0;
|
|
|
|
for (node const *nd = p_node; nd; nd = nd->next) {
|
|
|
|
++len;
|
|
|
|
}
|
2021-04-05 04:10:39 +02:00
|
|
|
state_p{p_state}.ts().istate->destroy_array(p_node, len);
|
2021-03-27 00:26:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
LIBCUBESCRIPT_EXPORT stack_state &stack_state::operator=(stack_state &&st) {
|
|
|
|
p_node = st.p_node;
|
|
|
|
st.p_node = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
LIBCUBESCRIPT_EXPORT stack_state::node const *stack_state::get() const {
|
|
|
|
return p_node;
|
|
|
|
}
|
|
|
|
|
2021-05-08 17:20:56 +02:00
|
|
|
static stack_state 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-04-03 03:37:58 +02:00
|
|
|
auto dval = std::clamp(
|
2021-05-06 03:34:25 +02:00
|
|
|
dalias->value().get_integer(), integer_type(0), integer_type(1000)
|
2021-04-03 03:37:58 +02:00
|
|
|
);
|
|
|
|
if (!dval) {
|
2021-05-09 20:01:47 +02:00
|
|
|
return stack_state{cs, nullptr};
|
2021-03-22 22:33:01 +01:00
|
|
|
}
|
|
|
|
int total = 0, depth = 0;
|
2021-03-26 02:59:42 +01:00
|
|
|
for (ident_link *l = ts.callstack; l; l = l->next) {
|
2021-03-22 22:33:01 +01:00
|
|
|
total++;
|
|
|
|
}
|
|
|
|
if (!total) {
|
2021-05-09 20:01:47 +02:00
|
|
|
return stack_state{cs, nullptr};
|
2021-03-22 22:33:01 +01:00
|
|
|
}
|
2021-03-26 02:59:42 +01:00
|
|
|
stack_state::node *st = ts.istate->create_array<stack_state::node>(
|
2021-04-03 03:37:58 +02:00
|
|
|
std::min(total, dval)
|
2021-03-22 22:33:01 +01:00
|
|
|
);
|
2021-03-26 02:59:42 +01:00
|
|
|
stack_state::node *ret = st, *nd = st;
|
2021-03-22 22:33:01 +01:00
|
|
|
++st;
|
2021-03-26 02:59:42 +01:00
|
|
|
for (ident_link *l = ts.callstack; l; l = l->next) {
|
2021-03-22 22:33:01 +01:00
|
|
|
++depth;
|
2021-04-03 03:37:58 +02:00
|
|
|
if (depth < dval) {
|
2021-03-22 22:33:01 +01:00
|
|
|
nd->id = l->id;
|
|
|
|
nd->index = total - depth + 1;
|
|
|
|
if (!l->next) {
|
|
|
|
nd->next = nullptr;
|
|
|
|
} else {
|
|
|
|
nd->next = st;
|
|
|
|
}
|
|
|
|
nd = st++;
|
|
|
|
} else if (!l->next) {
|
|
|
|
nd->id = l->id;
|
|
|
|
nd->index = 1;
|
|
|
|
nd->next = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2021-05-09 20:01:47 +02:00
|
|
|
return stack_state{cs, ret};
|
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):
|
|
|
|
p_errbeg{}, p_errend{}, p_stack{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
|
|
|
|
): p_errbeg{errbeg}, p_errend{errend}, p_stack{cs} {
|
|
|
|
p_stack = save_stack(cs);
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:32:25 +01:00
|
|
|
} /* namespace cubescript */
|