move shared state into cs_util

master
Daniel Kolesa 2021-03-19 02:27:36 +01:00
parent 126d6ab2b6
commit 08212df80f
3 changed files with 54 additions and 54 deletions

View File

@ -3,6 +3,7 @@
#include <type_traits>
#include <unordered_map>
#include <vector>
#include <ostd/string.hh>
@ -40,7 +41,56 @@ inline void cs_do_and_cleanup(F1 &&dof, F2 &&clf) {
dof();
}
struct cs_shared_state;
struct cs_strman;
struct cs_shared_state {
cs_map<ostd::string_range, cs_ident *> idents;
cs_vector<cs_ident *> identmap;
cs_alloc_cb allocf;
cs_vprint_cb varprintf;
cs_strman *strman;
void *aptr;
void *alloc(void *ptr, size_t os, size_t ns) {
return allocf(aptr, ptr, os, ns);
}
template<typename T, typename ...A>
T *create(A &&...args) {
T *ret = static_cast<T *>(alloc(nullptr, 0, sizeof(T)));
new (ret) T(std::forward<A>(args)...);
return ret;
}
template<typename T>
T *create_array(size_t len) {
T *ret = static_cast<T *>(alloc(nullptr, 0, len * sizeof(T)));
for (size_t i = 0; i < len; ++i) {
new (&ret[i]) T();
}
return ret;
}
template<typename T>
void destroy(T *v) noexcept {
v->~T();
alloc(v, sizeof(T), 0);
}
template<typename T>
void destroy_array(T *v, size_t len) noexcept {
v->~T();
alloc(v, len * sizeof(T), 0);
}
};
inline cs_shared_state *cs_get_sstate(cs_state &cs) {
return cs.p_state;
}
inline cs_strref cs_make_strref(char const *p, cs_shared_state &cs) {
return cs_strref{p, cs};
}
/* string manager
*

View File

@ -114,55 +114,6 @@ enum {
CS_CODE_FLAG_FALSE = 0 << CS_CODE_RET
};
struct cs_shared_state {
cs_map<ostd::string_range, cs_ident *> idents;
cs_vector<cs_ident *> identmap;
cs_alloc_cb allocf;
cs_vprint_cb varprintf;
cs_strman *strman;
void *aptr;
void *alloc(void *ptr, size_t os, size_t ns) {
return allocf(aptr, ptr, os, ns);
}
template<typename T, typename ...A>
T *create(A &&...args) {
T *ret = static_cast<T *>(alloc(nullptr, 0, sizeof(T)));
new (ret) T(std::forward<A>(args)...);
return ret;
}
template<typename T>
T *create_array(size_t len) {
T *ret = static_cast<T *>(alloc(nullptr, 0, len * sizeof(T)));
for (size_t i = 0; i < len; ++i) {
new (&ret[i]) T();
}
return ret;
}
template<typename T>
void destroy(T *v) noexcept {
v->~T();
alloc(v, sizeof(T), 0);
}
template<typename T>
void destroy_array(T *v, size_t len) noexcept {
v->~T();
alloc(v, len * sizeof(T), 0);
}
};
inline cs_shared_state *cs_get_sstate(cs_state &cs) {
return cs.p_state;
}
inline cs_strref cs_make_strref(char const *p, cs_shared_state &cs) {
return cs_strref{p, cs};
}
struct CsBreakException {
};

View File

@ -3,7 +3,6 @@
#include <cubescript/cubescript.hh>
#include "cs_util.hh"
#include "cs_vm.hh"
namespace cscript {
@ -66,11 +65,11 @@ void cs_init_lib_string(cs_state &cs) {
}
auto const *cbuf = ics->strman->steal(buf);
auto sr = cs_make_strref(cbuf, *ics);
sman->unref(cbuf);
ics->strman->unref(cbuf);
res.set_str(sr);
});
cs.new_command("strupper", "s", [](auto &, auto args, auto &res) {
cs.new_command("strupper", "s", [](auto &ccs, auto args, auto &res) {
auto inps = ostd::string_range{args[0].get_str()};
auto *ics = cs_get_sstate(ccs);
auto *buf = ics->strman->alloc_buf(inps.size());
@ -79,7 +78,7 @@ void cs_init_lib_string(cs_state &cs) {
}
auto const *cbuf = ics->strman->steal(buf);
auto sr = cs_make_strref(cbuf, *ics);
sman->unref(cbuf);
ics->strman->unref(cbuf);
res.set_str(sr);
});