forked from OctaForge/libcubescript
cleanups and add alloc method that will be used to request memory
parent
df5e2bb507
commit
aa367f4a0c
|
@ -5,13 +5,13 @@
|
|||
namespace cscript {
|
||||
|
||||
template<typename T, typename U>
|
||||
inline T &csv_get(U &stor) {
|
||||
static inline T &csv_get(U &stor) {
|
||||
/* ugly, but internal and unlikely to cause bugs */
|
||||
return const_cast<T &>(reinterpret_cast<T const &>(stor));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void csv_cleanup(CsValueType tv, T &stor) {
|
||||
static inline void csv_cleanup(CsValueType tv, T &stor) {
|
||||
switch (tv) {
|
||||
case CsValueType::string:
|
||||
delete[] csv_get<char *>(stor);
|
||||
|
|
48
cs_vm.cc
48
cs_vm.cc
|
@ -644,7 +644,7 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) {
|
|||
case CODE_DOARGS | RET_FLOAT:
|
||||
if (cs.p_stack != &cs.noalias) {
|
||||
cs_do_args(cs, [&]() {
|
||||
cs.run_ret(args[--numargs].get_code(), result);
|
||||
cs.run(args[--numargs].get_code(), result);
|
||||
force_arg(result, op & CODE_RET_MASK);
|
||||
});
|
||||
continue;
|
||||
|
@ -654,7 +654,7 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) {
|
|||
case CODE_DO | RET_STR:
|
||||
case CODE_DO | RET_INT:
|
||||
case CODE_DO | RET_FLOAT:
|
||||
cs.run_ret(args[--numargs].get_code(), result);
|
||||
cs.run(args[--numargs].get_code(), result);
|
||||
force_arg(result, op & CODE_RET_MASK);
|
||||
continue;
|
||||
|
||||
|
@ -681,7 +681,7 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) {
|
|||
ostd::Uint32 len = op >> 8;
|
||||
--numargs;
|
||||
if (args[numargs].get_type() == CsValueType::code) {
|
||||
cs.run_ret(args[numargs].get_code(), result);
|
||||
cs.run(args[numargs].get_code(), result);
|
||||
} else {
|
||||
result = ostd::move(args[numargs]);
|
||||
}
|
||||
|
@ -694,7 +694,7 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) {
|
|||
ostd::Uint32 len = op >> 8;
|
||||
--numargs;
|
||||
if (args[numargs].get_type() == CsValueType::code) {
|
||||
cs.run_ret(args[numargs].get_code(), result);
|
||||
cs.run(args[numargs].get_code(), result);
|
||||
} else {
|
||||
result = ostd::move(args[numargs]);
|
||||
}
|
||||
|
@ -1518,11 +1518,11 @@ exit:
|
|||
return code;
|
||||
}
|
||||
|
||||
void CsState::run_ret(CsBytecode *code, CsValue &ret) {
|
||||
void CsState::run(CsBytecode *code, CsValue &ret) {
|
||||
runcode(*this, reinterpret_cast<ostd::Uint32 *>(code), ret);
|
||||
}
|
||||
|
||||
void CsState::run_ret(ostd::ConstCharRange code, CsValue &ret) {
|
||||
void CsState::run(ostd::ConstCharRange code, CsValue &ret) {
|
||||
GenState gs(*this);
|
||||
gs.code.reserve(64);
|
||||
/* FIXME range */
|
||||
|
@ -1533,7 +1533,7 @@ void CsState::run_ret(ostd::ConstCharRange code, CsValue &ret) {
|
|||
}
|
||||
}
|
||||
|
||||
void CsState::run_ret(CsIdent *id, CsValueRange args, CsValue &ret) {
|
||||
void CsState::run(CsIdent *id, CsValueRange args, CsValue &ret) {
|
||||
int nargs = int(args.size());
|
||||
ret.set_null();
|
||||
++rundepth;
|
||||
|
@ -1609,89 +1609,89 @@ void CsState::run_ret(CsIdent *id, CsValueRange args, CsValue &ret) {
|
|||
|
||||
CsString CsState::run_str(CsBytecode *code) {
|
||||
CsValue ret;
|
||||
run_ret(code, ret);
|
||||
run(code, ret);
|
||||
return ret.get_str();
|
||||
}
|
||||
|
||||
CsString CsState::run_str(ostd::ConstCharRange code) {
|
||||
CsValue ret;
|
||||
run_ret(code, ret);
|
||||
run(code, ret);
|
||||
return ret.get_str();
|
||||
}
|
||||
|
||||
CsString CsState::run_str(CsIdent *id, CsValueRange args) {
|
||||
CsValue ret;
|
||||
run_ret(id, args, ret);
|
||||
run(id, args, ret);
|
||||
return ret.get_str();
|
||||
}
|
||||
|
||||
CsInt CsState::run_int(CsBytecode *code) {
|
||||
CsValue ret;
|
||||
run_ret(code, ret);
|
||||
run(code, ret);
|
||||
return ret.get_int();
|
||||
}
|
||||
|
||||
CsInt CsState::run_int(ostd::ConstCharRange code) {
|
||||
CsValue ret;
|
||||
run_ret(code, ret);
|
||||
run(code, ret);
|
||||
return ret.get_int();
|
||||
}
|
||||
|
||||
CsInt CsState::run_int(CsIdent *id, CsValueRange args) {
|
||||
CsValue ret;
|
||||
run_ret(id, args, ret);
|
||||
run(id, args, ret);
|
||||
return ret.get_int();
|
||||
}
|
||||
|
||||
CsFloat CsState::run_float(CsBytecode *code) {
|
||||
CsValue ret;
|
||||
run_ret(code, ret);
|
||||
run(code, ret);
|
||||
return ret.get_float();
|
||||
}
|
||||
|
||||
CsFloat CsState::run_float(ostd::ConstCharRange code) {
|
||||
CsValue ret;
|
||||
run_ret(code, ret);
|
||||
run(code, ret);
|
||||
return ret.get_float();
|
||||
}
|
||||
|
||||
CsFloat CsState::run_float(CsIdent *id, CsValueRange args) {
|
||||
CsValue ret;
|
||||
run_ret(id, args, ret);
|
||||
run(id, args, ret);
|
||||
return ret.get_float();
|
||||
}
|
||||
|
||||
bool CsState::run_bool(CsBytecode *code) {
|
||||
CsValue ret;
|
||||
run_ret(code, ret);
|
||||
run(code, ret);
|
||||
return ret.get_bool();
|
||||
}
|
||||
|
||||
bool CsState::run_bool(ostd::ConstCharRange code) {
|
||||
CsValue ret;
|
||||
run_ret(code, ret);
|
||||
run(code, ret);
|
||||
return ret.get_bool();
|
||||
}
|
||||
|
||||
bool CsState::run_bool(CsIdent *id, CsValueRange args) {
|
||||
CsValue ret;
|
||||
run_ret(id, args, ret);
|
||||
run(id, args, ret);
|
||||
return ret.get_bool();
|
||||
}
|
||||
|
||||
void CsState::run(CsBytecode *code) {
|
||||
CsValue ret;
|
||||
run_ret(code, ret);
|
||||
run(code, ret);
|
||||
}
|
||||
|
||||
void CsState::run(ostd::ConstCharRange code) {
|
||||
CsValue ret;
|
||||
run_ret(code, ret);
|
||||
run(code, ret);
|
||||
}
|
||||
|
||||
void CsState::run(CsIdent *id, CsValueRange args) {
|
||||
CsValue ret;
|
||||
run_ret(id, args, ret);
|
||||
run(id, args, ret);
|
||||
}
|
||||
|
||||
static bool cs_run_file(
|
||||
|
@ -1716,7 +1716,7 @@ static bool cs_run_file(
|
|||
ostd::ConstCharRange src_str = ostd::ConstCharRange(buf.get(), len);
|
||||
cs_src_file = fname;
|
||||
cs_src_str = src_str;
|
||||
cs.run_ret(src_str, ret);
|
||||
cs.run(src_str, ret);
|
||||
cs_src_file = old_src_file;
|
||||
cs_src_str = old_src_str;
|
||||
return true;
|
||||
|
@ -1754,7 +1754,7 @@ ostd::Maybe<bool> CsState::run_file_bool(ostd::ConstCharRange fname) {
|
|||
return ret.get_bool();
|
||||
}
|
||||
|
||||
bool CsState::run_file_ret(ostd::ConstCharRange fname, CsValue &ret) {
|
||||
bool CsState::run_file(ostd::ConstCharRange fname, CsValue &ret) {
|
||||
return cs_run_file(*this, fname, ret);
|
||||
}
|
||||
|
||||
|
|
1
cs_vm.hh
1
cs_vm.hh
|
@ -37,6 +37,7 @@ static inline int cs_vtype_to_int(CsValueType v) {
|
|||
return cs_valtypet[int(v)];
|
||||
}
|
||||
|
||||
/* instruction: uint32 [length 24][retflag 2][opcode 6] */
|
||||
enum {
|
||||
CODE_START = 0,
|
||||
CODE_OFFSET,
|
||||
|
|
|
@ -272,19 +272,19 @@ CsState::CsState(): p_out(&ostd::out), p_err(&ostd::err) {
|
|||
assert(id->get_index() == DbgaliasIdx);
|
||||
|
||||
new_command("do", "e", [this](CsValueRange args, CsValue &res) {
|
||||
run_ret(args[0].get_code(), res);
|
||||
run(args[0].get_code(), res);
|
||||
})->p_type = ID_DO;
|
||||
|
||||
new_command("doargs", "e", [this](CsValueRange args, CsValue &res) {
|
||||
if (p_stack != &noalias) {
|
||||
cs_do_args(*this, [&]() { run_ret(args[0].get_code(), res); });
|
||||
cs_do_args(*this, [&]() { run(args[0].get_code(), res); });
|
||||
} else {
|
||||
run_ret(args[0].get_code(), res);
|
||||
run(args[0].get_code(), res);
|
||||
}
|
||||
})->p_type = ID_DOARGS;
|
||||
|
||||
new_command("if", "tee", [this](CsValueRange args, CsValue &res) {
|
||||
run_ret((args[0].get_bool() ? args[1] : args[2]).get_code(), res);
|
||||
run((args[0].get_bool() ? args[1] : args[2]).get_code(), res);
|
||||
})->p_type = ID_IF;
|
||||
|
||||
new_command("result", "T", [](CsValueRange args, CsValue &res) {
|
||||
|
@ -302,7 +302,7 @@ CsState::CsState(): p_out(&ostd::out), p_err(&ostd::err) {
|
|||
for (ostd::Size i = 0; i < args.size(); ++i) {
|
||||
CsBytecode *code = args[i].get_code();
|
||||
if (code) {
|
||||
run_ret(code, res);
|
||||
run(code, res);
|
||||
} else {
|
||||
res = ostd::move(args[i]);
|
||||
}
|
||||
|
@ -320,7 +320,7 @@ CsState::CsState(): p_out(&ostd::out), p_err(&ostd::err) {
|
|||
for (ostd::Size i = 0; i < args.size(); ++i) {
|
||||
CsBytecode *code = args[i].get_code();
|
||||
if (code) {
|
||||
run_ret(code, res);
|
||||
run(code, res);
|
||||
} else {
|
||||
res = ostd::move(args[i]);
|
||||
}
|
||||
|
@ -386,6 +386,14 @@ CsHookCb &CsState::get_call_hook() {
|
|||
return p_callhook;
|
||||
}
|
||||
|
||||
void *CsState::alloc(void *ptr, ostd::Size, ostd::Size ns) {
|
||||
delete[] static_cast<ostd::byte *>(ptr);
|
||||
if (!ns) {
|
||||
return nullptr;
|
||||
}
|
||||
return new ostd::byte[ns];
|
||||
}
|
||||
|
||||
void CsState::clear_override(CsIdent &id) {
|
||||
if (!(id.get_flags() & IDF_OVERRIDDEN)) {
|
||||
return;
|
||||
|
@ -1008,7 +1016,7 @@ static inline void cs_loop_conc(
|
|||
idv.set_int(offset + i * step);
|
||||
idv.push();
|
||||
CsValue v;
|
||||
cs.run_ret(body, v);
|
||||
cs.run(body, v);
|
||||
CsString vstr = ostd::move(v.get_str());
|
||||
if (space && i) {
|
||||
s.push(' ');
|
||||
|
@ -1033,11 +1041,11 @@ void cs_init_lib_base(CsState &cs) {
|
|||
for (ostd::Size i = 0; i < args.size(); i += 2) {
|
||||
if ((i + 1) < args.size()) {
|
||||
if (cs.run_bool(args[i].get_code())) {
|
||||
cs.run_ret(args[i + 1].get_code(), res);
|
||||
cs.run(args[i + 1].get_code(), res);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
cs.run_ret(args[i].get_code(), res);
|
||||
cs.run(args[i].get_code(), res);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -1050,7 +1058,7 @@ void cs_init_lib_base(CsState &cs) {
|
|||
(args[i].get_type() == CsValueType::null) ||
|
||||
(args[i].get_int() == val)
|
||||
) {
|
||||
cs.run_ret(args[i + 1].get_code(), res);
|
||||
cs.run(args[i + 1].get_code(), res);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -1063,7 +1071,7 @@ void cs_init_lib_base(CsState &cs) {
|
|||
(args[i].get_type() == CsValueType::null) ||
|
||||
(args[i].get_float() == val)
|
||||
) {
|
||||
cs.run_ret(args[i + 1].get_code(), res);
|
||||
cs.run(args[i + 1].get_code(), res);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -1076,7 +1084,7 @@ void cs_init_lib_base(CsState &cs) {
|
|||
(args[i].get_type() == CsValueType::null) ||
|
||||
(args[i].get_str() == val)
|
||||
) {
|
||||
cs.run_ret(args[i + 1].get_code(), res);
|
||||
cs.run(args[i + 1].get_code(), res);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -1090,7 +1098,7 @@ void cs_init_lib_base(CsState &cs) {
|
|||
if (args[1].get_bool()) {
|
||||
idv = ostd::move(args[1]);
|
||||
idv.push();
|
||||
cs.run_ret(args[2].get_code(), res);
|
||||
cs.run(args[2].get_code(), res);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -1215,7 +1223,7 @@ void cs_init_lib_base(CsState &cs) {
|
|||
|
||||
cs.new_command("nodebug", "e", [&cs](CsValueRange args, CsValue &res) {
|
||||
++cs.nodebug;
|
||||
cs.run_ret(args[0].get_code(), res);
|
||||
cs.run(args[0].get_code(), res);
|
||||
--cs.nodebug;
|
||||
});
|
||||
|
||||
|
@ -1226,7 +1234,7 @@ void cs_init_lib_base(CsState &cs) {
|
|||
}
|
||||
idv = ostd::move(args[1]);
|
||||
idv.push();
|
||||
cs.run_ret(args[2].get_code(), res);
|
||||
cs.run(args[2].get_code(), res);
|
||||
});
|
||||
|
||||
cs.new_command("resetvar", "s", [&cs](CsValueRange args, CsValue &res) {
|
||||
|
|
|
@ -328,7 +328,7 @@ struct OSTD_EXPORT CsState {
|
|||
int nodebug = 0;
|
||||
|
||||
CsState();
|
||||
~CsState();
|
||||
virtual ~CsState();
|
||||
|
||||
CsStream const &get_out() const;
|
||||
CsStream &get_out();
|
||||
|
@ -342,6 +342,8 @@ struct OSTD_EXPORT CsState {
|
|||
CsHookCb const &get_call_hook() const;
|
||||
CsHookCb &get_call_hook();
|
||||
|
||||
virtual void *alloc(void *ptr, ostd::Size olds, ostd::Size news);
|
||||
|
||||
void init_libs(int libs = CS_LIB_ALL);
|
||||
|
||||
void clear_override(CsIdent &id);
|
||||
|
@ -406,9 +408,9 @@ struct OSTD_EXPORT CsState {
|
|||
bool run_bool(ostd::ConstCharRange code);
|
||||
bool run_bool(CsIdent *id, CsValueRange args);
|
||||
|
||||
void run_ret(CsBytecode *code, CsValue &ret);
|
||||
void run_ret(ostd::ConstCharRange code, CsValue &ret);
|
||||
void run_ret(CsIdent *id, CsValueRange args, CsValue &ret);
|
||||
void run(CsBytecode *code, CsValue &ret);
|
||||
void run(ostd::ConstCharRange code, CsValue &ret);
|
||||
void run(CsIdent *id, CsValueRange args, CsValue &ret);
|
||||
|
||||
void run(CsBytecode *code);
|
||||
void run(ostd::ConstCharRange code);
|
||||
|
@ -418,7 +420,7 @@ struct OSTD_EXPORT CsState {
|
|||
ostd::Maybe<CsInt> run_file_int(ostd::ConstCharRange fname);
|
||||
ostd::Maybe<CsFloat> run_file_float(ostd::ConstCharRange fname);
|
||||
ostd::Maybe<bool> run_file_bool(ostd::ConstCharRange fname);
|
||||
bool run_file_ret(ostd::ConstCharRange fname, CsValue &ret);
|
||||
bool run_file(ostd::ConstCharRange fname, CsValue &ret);
|
||||
bool run_file(ostd::ConstCharRange fname);
|
||||
|
||||
void set_alias(ostd::ConstCharRange name, CsValue v);
|
||||
|
|
|
@ -86,7 +86,7 @@ static void cs_loop_list_conc(
|
|||
r.push(' ');
|
||||
}
|
||||
CsValue v;
|
||||
cs.run_ret(body, v);
|
||||
cs.run(body, v);
|
||||
CsString vstr = ostd::move(v.get_str());
|
||||
r.push_n(vstr.data(), vstr.size());
|
||||
}
|
||||
|
|
|
@ -182,7 +182,7 @@ static void do_call(CsState &cs, ostd::ConstCharRange line) {
|
|||
CsValue ret;
|
||||
signal(SIGINT, do_sigint);
|
||||
try {
|
||||
cs.run_ret(line, ret);
|
||||
cs.run(line, ret);
|
||||
} catch (InterruptedException) {
|
||||
signal(SIGINT, SIG_DFL);
|
||||
ostd::writeln("<execution interrupted>");
|
||||
|
|
Loading…
Reference in New Issue