pass thread_state in more places
parent
865bac73f3
commit
0b2c4c7038
|
@ -14,7 +14,7 @@ std::string_view codegen_state::get_str() {
|
|||
size_t nl;
|
||||
char const *beg = source;
|
||||
source = parse_string(
|
||||
cs, std::string_view{source, std::size_t(send - source)}, nl
|
||||
*ts.pstate, std::string_view{source, std::size_t(send - source)}, nl
|
||||
);
|
||||
current_line += nl - 1;
|
||||
auto ret = std::string_view{beg, std::size_t(source - beg)};
|
||||
|
@ -22,7 +22,7 @@ std::string_view codegen_state::get_str() {
|
|||
}
|
||||
|
||||
charbuf codegen_state::get_str_dup() {
|
||||
charbuf buf{cs.p_tstate->istate};
|
||||
charbuf buf{ts.istate};
|
||||
unescape_string(std::back_inserter(buf), get_str());
|
||||
return buf;
|
||||
}
|
||||
|
@ -69,7 +69,7 @@ void codegen_state::skip_comments() {
|
|||
if (current() == '\\') {
|
||||
char c = current(1);
|
||||
if ((c != '\r') && (c != '\n')) {
|
||||
throw error(cs, "invalid line break");
|
||||
throw error{*ts.pstate, "invalid line break"};
|
||||
}
|
||||
/* skip backslash */
|
||||
next_char();
|
||||
|
@ -94,7 +94,7 @@ void codegen_state::skip_comments() {
|
|||
std::string_view codegen_state::get_word() {
|
||||
char const *beg = source;
|
||||
source = parse_word(
|
||||
cs, std::string_view{source, std::size_t(send - source)}
|
||||
*ts.pstate, std::string_view{source, std::size_t(send - source)}
|
||||
);
|
||||
return std::string_view{beg, std::size_t(source - beg)};
|
||||
}
|
||||
|
@ -199,7 +199,7 @@ static inline void compileunescapestr(codegen_state &gs) {
|
|||
gs.code.size() + str.size() / sizeof(uint32_t) + 1
|
||||
);
|
||||
size_t bufs = (gs.code.capacity() - gs.code.size()) * sizeof(uint32_t);
|
||||
auto alloc = std_allocator<char>{gs.cs.p_tstate->istate};
|
||||
auto alloc = std_allocator<char>{gs.ts.istate};
|
||||
auto *buf = alloc.allocate(bufs + 1);
|
||||
char *wbuf = unescape_string(&buf[0], str);
|
||||
memset(
|
||||
|
@ -218,7 +218,7 @@ static bool compilearg(
|
|||
);
|
||||
|
||||
static void compilelookup(codegen_state &gs, int ltype, int prevargs = MAX_RESULTS) {
|
||||
charbuf lookup{gs.cs.p_tstate->istate};
|
||||
charbuf lookup{gs.ts.istate};
|
||||
gs.next_char();
|
||||
switch (gs.current()) {
|
||||
case '(':
|
||||
|
@ -239,7 +239,7 @@ static void compilelookup(codegen_state &gs, int ltype, int prevargs = MAX_RESUL
|
|||
if (lookup.empty()) goto invalid;
|
||||
lookup.push_back('\0');
|
||||
lookupid:
|
||||
ident *id = gs.cs.new_ident(lookup.str_term());
|
||||
ident *id = gs.ts.pstate->new_ident(lookup.str_term());
|
||||
if (id) {
|
||||
switch (id->get_type()) {
|
||||
case ident_type::IVAR:
|
||||
|
@ -466,7 +466,7 @@ static bool compileblockstr(codegen_state &gs, char const *str, char const *send
|
|||
int startc = gs.code.size();
|
||||
gs.code.push_back(BC_INST_VAL | BC_RET_STRING);
|
||||
gs.code.reserve(gs.code.size() + (send - str) / sizeof(uint32_t) + 1);
|
||||
auto alloc = std_allocator<char>{gs.cs.p_tstate->istate};
|
||||
auto alloc = std_allocator<char>{gs.ts.istate};
|
||||
auto asz = ((send - str) / sizeof(uint32_t) + 1) * sizeof(uint32_t);
|
||||
char *buf = alloc.allocate(asz);
|
||||
int len = 0;
|
||||
|
@ -486,7 +486,7 @@ static bool compileblockstr(codegen_state &gs, char const *str, char const *send
|
|||
case '\"': {
|
||||
char const *start = str;
|
||||
str = parse_string(
|
||||
gs.cs, std::string_view{str, send}
|
||||
*gs.ts.pstate, std::string_view{str, send}
|
||||
);
|
||||
memcpy(&buf[len], start, std::size_t(str - start));
|
||||
len += (str - start);
|
||||
|
@ -519,7 +519,7 @@ done:
|
|||
}
|
||||
|
||||
static bool compileblocksub(codegen_state &gs, int prevargs) {
|
||||
charbuf lookup{gs.cs.p_tstate->istate};
|
||||
charbuf lookup{gs.ts.istate};
|
||||
switch (gs.current()) {
|
||||
case '(':
|
||||
if (!compilearg(gs, VAL_ANY, prevargs)) {
|
||||
|
@ -543,7 +543,7 @@ static bool compileblocksub(codegen_state &gs, int prevargs) {
|
|||
}
|
||||
lookup.push_back('\0');
|
||||
lookupid:
|
||||
ident *id = gs.cs.new_ident(lookup.str_term());
|
||||
ident *id = gs.ts.pstate->new_ident(lookup.str_term());
|
||||
if (id) {
|
||||
switch (id->get_type()) {
|
||||
case ident_type::IVAR:
|
||||
|
@ -583,7 +583,7 @@ static void compileblockmain(codegen_state &gs, int wordtype, int prevargs) {
|
|||
for (int brak = 1; brak;) {
|
||||
switch (gs.skip_until("@\"/[]")) {
|
||||
case '\0':
|
||||
throw error(gs.cs, "missing \"]\"");
|
||||
throw error{*gs.ts.pstate, "missing \"]\""};
|
||||
return;
|
||||
case '\"':
|
||||
gs.get_str();
|
||||
|
@ -612,7 +612,7 @@ static void compileblockmain(codegen_state &gs, int wordtype, int prevargs) {
|
|||
if (brak > level) {
|
||||
continue;
|
||||
} else if (brak < level) {
|
||||
throw error(gs.cs, "too many @s");
|
||||
throw error{*gs.ts.pstate, "too many @s"};
|
||||
return;
|
||||
}
|
||||
if (!concs && prevargs >= MAX_RESULTS) {
|
||||
|
@ -1214,7 +1214,7 @@ static void compile_and_or(
|
|||
}
|
||||
|
||||
static void compilestatements(codegen_state &gs, int rettype, int brak, int prevargs) {
|
||||
charbuf idname{gs.cs.p_tstate->istate};
|
||||
charbuf idname{gs.ts.istate};
|
||||
for (;;) {
|
||||
gs.skip_comments();
|
||||
idname.clear();
|
||||
|
@ -1240,7 +1240,7 @@ static void compilestatements(codegen_state &gs, int rettype, int brak, int prev
|
|||
gs.next_char();
|
||||
if (!idname.empty()) {
|
||||
idname.push_back('\0');
|
||||
ident *id = gs.cs.new_ident(idname.str_term());
|
||||
ident *id = gs.ts.pstate->new_ident(idname.str_term());
|
||||
if (id) {
|
||||
switch (id->get_type()) {
|
||||
case ident_type::ALIAS:
|
||||
|
@ -1309,7 +1309,7 @@ noid:
|
|||
gs.code.push_back(BC_INST_CALL_U | (numargs << 8));
|
||||
} else {
|
||||
idname.push_back('\0');
|
||||
ident *id = gs.cs.get_ident(idname.str_term());
|
||||
ident *id = gs.ts.pstate->get_ident(idname.str_term());
|
||||
if (!id) {
|
||||
if (is_valid_name(idname.str_term())) {
|
||||
gs.gen_str(idname.str_term());
|
||||
|
@ -1435,7 +1435,7 @@ endstatement:
|
|||
switch (gs.skip_until(")];/\n")) {
|
||||
case '\0':
|
||||
if (gs.current() != brak) {
|
||||
throw error(gs.cs, "missing \"%c\"", char(brak));
|
||||
throw error{*gs.ts.pstate, "missing \"%c\"", char(brak)};
|
||||
return;
|
||||
}
|
||||
return;
|
||||
|
@ -1445,7 +1445,7 @@ endstatement:
|
|||
gs.next_char();
|
||||
return;
|
||||
}
|
||||
throw error(gs.cs, "unexpected \"%c\"", gs.current());
|
||||
throw error{*gs.ts.pstate, "unexpected \"%c\"", gs.current()};
|
||||
return;
|
||||
case '/':
|
||||
gs.next_char();
|
||||
|
|
|
@ -23,7 +23,7 @@ static constexpr int ID_IDX_NUMARGS = MAX_ARGUMENTS + 1;
|
|||
static constexpr int ID_IDX_DBGALIAS = MAX_ARGUMENTS + 2;
|
||||
|
||||
struct codegen_state {
|
||||
state &cs;
|
||||
thread_state &ts;
|
||||
codegen_state *prevps;
|
||||
bool parsing = true;
|
||||
valbuf<uint32_t> code;
|
||||
|
@ -32,11 +32,11 @@ struct codegen_state {
|
|||
std::string_view src_name;
|
||||
|
||||
codegen_state() = delete;
|
||||
codegen_state(state &csr):
|
||||
cs{csr}, prevps{csr.p_tstate->cstate}, code{csr.p_tstate->istate},
|
||||
codegen_state(thread_state &tsr):
|
||||
ts{tsr}, prevps{tsr.cstate}, code{tsr.istate},
|
||||
source{}, send{}, current_line{1}, src_name{}
|
||||
{
|
||||
csr.p_tstate->cstate = this;
|
||||
tsr.cstate = this;
|
||||
}
|
||||
|
||||
~codegen_state() {
|
||||
|
@ -47,7 +47,7 @@ struct codegen_state {
|
|||
if (!parsing) {
|
||||
return;
|
||||
}
|
||||
cs.p_tstate->cstate = prevps;
|
||||
ts.cstate = prevps;
|
||||
parsing = false;
|
||||
}
|
||||
|
||||
|
@ -122,11 +122,11 @@ struct codegen_state {
|
|||
}
|
||||
|
||||
void gen_ident() {
|
||||
gen_ident(cs.p_tstate->istate->identmap[ID_IDX_DUMMY]);
|
||||
gen_ident(ts.istate->identmap[ID_IDX_DUMMY]);
|
||||
}
|
||||
|
||||
void gen_ident(std::string_view word) {
|
||||
gen_ident(cs.new_ident(word));
|
||||
gen_ident(ts.pstate->new_ident(word));
|
||||
}
|
||||
|
||||
void gen_value(
|
||||
|
|
|
@ -151,20 +151,20 @@ void alias_impl::redo_arg(ident_stack &st) {
|
|||
clean_code();
|
||||
}
|
||||
|
||||
void alias_impl::set_arg(state &cs, any_value &v) {
|
||||
if (ident_is_used_arg(this, cs)) {
|
||||
void alias_impl::set_arg(thread_state &ts, any_value &v) {
|
||||
if (ident_is_used_arg(this, ts)) {
|
||||
p_val = std::move(v);
|
||||
clean_code();
|
||||
} else {
|
||||
push_arg(v, cs.p_tstate->callstack->argstack[get_index()], false);
|
||||
cs.p_tstate->callstack->usedargs |= 1 << get_index();
|
||||
push_arg(v, ts.callstack->argstack[get_index()], false);
|
||||
ts.callstack->usedargs |= 1 << get_index();
|
||||
}
|
||||
}
|
||||
|
||||
void alias_impl::set_alias(state &cs, any_value &v) {
|
||||
void alias_impl::set_alias(thread_state &ts, any_value &v) {
|
||||
p_val = std::move(v);
|
||||
clean_code();
|
||||
p_flags = (p_flags & cs.identflags) | cs.identflags;
|
||||
p_flags = (p_flags & ts.pstate->identflags) | ts.pstate->identflags;
|
||||
}
|
||||
|
||||
void alias_impl::clean_code() {
|
||||
|
@ -174,13 +174,13 @@ void alias_impl::clean_code() {
|
|||
}
|
||||
}
|
||||
|
||||
bcode *alias_impl::compile_code(state &cs) {
|
||||
bcode *alias_impl::compile_code(thread_state &ts) {
|
||||
if (!p_acode) {
|
||||
codegen_state gs(cs);
|
||||
codegen_state gs(ts);
|
||||
gs.code.reserve(64);
|
||||
gs.gen_main(get_value().get_str());
|
||||
/* i wish i could steal the memory somehow */
|
||||
uint32_t *code = bcode_alloc(cs.p_tstate->istate, gs.code.size());
|
||||
uint32_t *code = bcode_alloc(ts.istate, gs.code.size());
|
||||
memcpy(code, gs.code.data(), gs.code.size() * sizeof(uint32_t));
|
||||
bcode_incr(code);
|
||||
p_acode = reinterpret_cast<bcode *>(code);
|
||||
|
@ -195,11 +195,11 @@ command_impl::command_impl(
|
|||
p_cargs{args}, p_cb_cftv{std::move(f)}, p_numargs{nargs}
|
||||
{}
|
||||
|
||||
bool ident_is_used_arg(ident *id, state &cs) {
|
||||
if (!cs.p_tstate->callstack) {
|
||||
bool ident_is_used_arg(ident *id, thread_state &ts) {
|
||||
if (!ts.callstack) {
|
||||
return true;
|
||||
}
|
||||
return cs.p_tstate->callstack->usedargs & (1 << id->get_index());
|
||||
return ts.callstack->usedargs & (1 << id->get_index());
|
||||
}
|
||||
|
||||
/* public interface */
|
||||
|
|
|
@ -87,11 +87,11 @@ struct alias_impl: ident_impl, alias {
|
|||
void pop_arg();
|
||||
void undo_arg(ident_stack &st);
|
||||
void redo_arg(ident_stack &st);
|
||||
void set_arg(state &cs, any_value &v);
|
||||
void set_alias(state &cs, any_value &v);
|
||||
void set_arg(thread_state &ts, any_value &v);
|
||||
void set_alias(thread_state &ts, any_value &v);
|
||||
|
||||
void clean_code();
|
||||
bcode *compile_code(state &cs);
|
||||
bcode *compile_code(thread_state &ts);
|
||||
|
||||
bcode *p_acode;
|
||||
ident_stack *p_astack;
|
||||
|
@ -112,7 +112,7 @@ struct command_impl: ident_impl, command {
|
|||
int p_numargs;
|
||||
};
|
||||
|
||||
bool ident_is_used_arg(ident *id, state &cs);
|
||||
bool ident_is_used_arg(ident *id, thread_state &ts);
|
||||
|
||||
} /* namespace cubescript */
|
||||
|
||||
|
|
|
@ -371,9 +371,9 @@ LIBCUBESCRIPT_EXPORT void state::set_alias(
|
|||
case ident_type::ALIAS: {
|
||||
alias_impl *a = static_cast<alias_impl *>(id);
|
||||
if (a->get_index() < MAX_ARGUMENTS) {
|
||||
a->set_arg(*this, v);
|
||||
a->set_arg(*p_tstate, v);
|
||||
} else {
|
||||
a->set_alias(*this, v);
|
||||
a->set_alias(*p_tstate, v);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
@ -654,7 +654,7 @@ state::get_alias_val(std::string_view name) {
|
|||
if (!a) {
|
||||
return std::nullopt;
|
||||
}
|
||||
if ((a->get_index() < MAX_ARGUMENTS) && !ident_is_used_arg(a, *this)) {
|
||||
if ((a->get_index() < MAX_ARGUMENTS) && !ident_is_used_arg(a, *p_tstate)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return a->get_value().get_str();
|
||||
|
|
|
@ -256,7 +256,7 @@ bcode *any_value::force_code(state &cs) {
|
|||
default:
|
||||
break;
|
||||
}
|
||||
codegen_state gs{cs};
|
||||
codegen_state gs{*cs.p_tstate};
|
||||
gs.code.reserve(64);
|
||||
gs.gen_main(get_str());
|
||||
gs.done();
|
||||
|
|
30
src/cs_vm.cc
30
src/cs_vm.cc
|
@ -266,7 +266,7 @@ static inline void call_alias(
|
|||
ts.callstack = &aliaslink;
|
||||
std::uint32_t *codep = static_cast<
|
||||
alias_impl *
|
||||
>(a)->compile_code(*ts.pstate)->get_raw();
|
||||
>(a)->compile_code(ts)->get_raw();
|
||||
bcode_incr(codep);
|
||||
call_with_cleanup([&]() {
|
||||
runcode(ts, codep+1, result);
|
||||
|
@ -320,7 +320,7 @@ static inline alias *get_lookup_id(thread_state &ts, std::uint32_t op) {
|
|||
|
||||
static inline alias *get_lookuparg_id(thread_state &ts, std::uint32_t op) {
|
||||
ident *id = ts.istate->identmap[op >> 8];
|
||||
if (!ident_is_used_arg(id, *ts.pstate)) {
|
||||
if (!ident_is_used_arg(id, ts)) {
|
||||
return nullptr;
|
||||
}
|
||||
return static_cast<alias *>(id);
|
||||
|
@ -358,7 +358,7 @@ static inline int get_lookupu_type(
|
|||
}
|
||||
if (
|
||||
(id->get_index() < MAX_ARGUMENTS) &&
|
||||
!ident_is_used_arg(id, *ts.pstate)
|
||||
!ident_is_used_arg(id, ts)
|
||||
) {
|
||||
return ID_UNKNOWN;
|
||||
}
|
||||
|
@ -707,7 +707,7 @@ static std::uint32_t *runcode(
|
|||
}
|
||||
case BC_INST_COMPILE: {
|
||||
any_value &arg = args.back();
|
||||
codegen_state gs{cs};
|
||||
codegen_state gs{ts};
|
||||
switch (arg.get_type()) {
|
||||
case value_type::INT:
|
||||
gs.code.reserve(8);
|
||||
|
@ -752,7 +752,7 @@ static std::uint32_t *runcode(
|
|||
case value_type::STRING: {
|
||||
std::string_view s = arg.get_str();
|
||||
if (!s.empty()) {
|
||||
codegen_state gs{cs};
|
||||
codegen_state gs{ts};
|
||||
gs.code.reserve(64);
|
||||
gs.gen_main(s);
|
||||
gs.done();
|
||||
|
@ -782,7 +782,7 @@ static std::uint32_t *runcode(
|
|||
alias *a = static_cast<alias *>(
|
||||
ts.istate->identmap[op >> 8]
|
||||
);
|
||||
if (!ident_is_used_arg(a, cs)) {
|
||||
if (!ident_is_used_arg(a, ts)) {
|
||||
any_value nv{cs};
|
||||
static_cast<alias_impl *>(a)->push_arg(
|
||||
nv, ts.callstack->argstack[a->get_index()],
|
||||
|
@ -799,7 +799,10 @@ static std::uint32_t *runcode(
|
|||
if (arg.get_type() == value_type::STRING) {
|
||||
id = cs.new_ident(arg.get_str());
|
||||
}
|
||||
if ((id->get_index() < MAX_ARGUMENTS) && !ident_is_used_arg(id, cs)) {
|
||||
if (
|
||||
(id->get_index() < MAX_ARGUMENTS) &&
|
||||
!ident_is_used_arg(id, ts)
|
||||
) {
|
||||
any_value nv{cs};
|
||||
static_cast<alias_impl *>(id)->push_arg(
|
||||
nv, ts.callstack->argstack[id->get_index()],
|
||||
|
@ -1259,13 +1262,13 @@ static std::uint32_t *runcode(
|
|||
case BC_INST_ALIAS:
|
||||
static_cast<alias_impl *>(
|
||||
ts.istate->identmap[op >> 8]
|
||||
)->set_alias(cs, args.back());
|
||||
)->set_alias(ts, args.back());
|
||||
args.pop_back();
|
||||
continue;
|
||||
case BC_INST_ALIAS_ARG:
|
||||
static_cast<alias_impl *>(
|
||||
ts.istate->identmap[op >> 8]
|
||||
)->set_arg(cs, args.back());
|
||||
)->set_arg(ts, args.back());
|
||||
args.pop_back();
|
||||
continue;
|
||||
case BC_INST_ALIAS_U: {
|
||||
|
@ -1307,7 +1310,7 @@ static std::uint32_t *runcode(
|
|||
std::size_t callargs = (op >> 8) & 0x1F;
|
||||
std::size_t nnargs = args.size();
|
||||
std::size_t offset = nnargs - callargs;
|
||||
if (!ident_is_used_arg(id, cs)) {
|
||||
if (!ident_is_used_arg(id, ts)) {
|
||||
args.resize(offset, any_value{cs});
|
||||
force_arg(result, op & BC_INST_RET_MASK);
|
||||
continue;
|
||||
|
@ -1422,7 +1425,7 @@ noid:
|
|||
alias *a = static_cast<alias *>(id);
|
||||
if (
|
||||
(a->get_index() < MAX_ARGUMENTS) &&
|
||||
!ident_is_used_arg(a, cs)
|
||||
!ident_is_used_arg(a, ts)
|
||||
) {
|
||||
args.resize(offset - 1, any_value{cs});
|
||||
force_arg(result, op & BC_INST_RET_MASK);
|
||||
|
@ -1453,7 +1456,7 @@ static void do_run(
|
|||
state &cs, std::string_view file, std::string_view code,
|
||||
any_value &ret
|
||||
) {
|
||||
codegen_state gs{cs};
|
||||
codegen_state gs{*cs.p_tstate};
|
||||
gs.src_name = file;
|
||||
gs.code.reserve(64);
|
||||
gs.gen_main(code, VAL_ANY);
|
||||
|
@ -1536,7 +1539,8 @@ void state::run(ident *id, std::span<any_value> args, any_value &ret) {
|
|||
case ident_type::ALIAS: {
|
||||
alias *a = static_cast<alias *>(id);
|
||||
if (
|
||||
(a->get_index() < MAX_ARGUMENTS) && !ident_is_used_arg(a, *this)
|
||||
(a->get_index() < MAX_ARGUMENTS) &&
|
||||
!ident_is_used_arg(a, *p_tstate)
|
||||
) {
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -87,8 +87,8 @@ void init_lib_base(state &gcs) {
|
|||
rc = false;
|
||||
}
|
||||
ret.set_int(rc);
|
||||
static_cast<alias_impl *>(cret)->set_alias(cs, result);
|
||||
static_cast<alias_impl *>(css)->set_alias(cs, tback);
|
||||
static_cast<alias_impl *>(cret)->set_alias(*cs.p_tstate, result);
|
||||
static_cast<alias_impl *>(css)->set_alias(*cs.p_tstate, tback);
|
||||
});
|
||||
|
||||
gcs.new_command("?", "ttt", [](auto &, auto args, auto &res) {
|
||||
|
|
Loading…
Reference in New Issue