Skip to content

Commit

Permalink
simplify json a bit more
Browse files Browse the repository at this point in the history
  • Loading branch information
apache-hb committed Apr 4, 2024
1 parent eed7ef6 commit 507e5da
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/cthulhu/util/src/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ static escape_t consume_escape(logger_t *reports, const node_t *node, const char
case 'r': return escape_new(2, '\r');
case '0': return escape_new(2, '\0');
case '\\': return escape_new(2, '\\');
case '"': return escape_new(2, '"');
case '\'': return escape_new(2, '\'');

default:
msg_notify(reports, &kEvent_InvalidStringEscape, node, "unknown escape sequence '\\%c'", *text);
Expand Down
8 changes: 7 additions & 1 deletion src/frontend/gui/editor/include/editor/panels/arena.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct AllocInfo
size_t id; ///< the id of the allocation
Memory size; ///< the size of the allocation
std::chrono::steady_clock::time_point timestamp; ///< the time of the allocation
std::stacktrace trace; ///< the stack trace of the allocation
size_t trace; ///< hash of the stack trace

std::string name; ///< the name of the allocation
const void *parent; ///< the parent of the allocation
Expand Down Expand Up @@ -46,6 +46,12 @@ class TraceArena final : public IArena
// all currently live allocations
std::unordered_set<void *> live_allocs;

// TODO: this is a little silly, but its an easy way to avoid having to
// deal with iterator invalidation
std::unordered_map<size_t, std::stacktrace> stacktraces;

size_t add_stacktrace(const std::stacktrace& trace);

int collect;

// all allocations
Expand Down
23 changes: 13 additions & 10 deletions src/frontend/gui/editor/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,9 @@ struct JsonFile
| ImGuiTreeNodeFlags_Bullet
| ImGuiTreeNodeFlags_NoTreePushOnOpen;

static void draw_json_array(const char *key, const ctu::json::Json& value)
static void draw_json_array(const std::string& key, const ctu::json::Json& value)
{
bool is_open = ImGui::TreeNodeEx(key, kGroupNodeFlags, "%s", key);
bool is_open = ImGui::TreeNodeEx(key.c_str(), kGroupNodeFlags, "%s", key.c_str());

ImGui::TableNextColumn();
ImGui::TextUnformatted("Array");
Expand All @@ -624,33 +624,36 @@ struct JsonFile
{
for (size_t i = 0; i < value.length(); i++)
{
draw_json_item(std::to_string(i).c_str(), value.get(i));
draw_json_item(std::format("[{}]", i).c_str(), value.get(i));
}
ImGui::TreePop();
}
}

static void draw_json_object(const char *key, const ctu::json::Json& object)
static void draw_json_object(const std::string& key, const ctu::json::Json& object)
{
bool is_open = ImGui::TreeNodeEx(key, kGroupNodeFlags, "%s", key);
bool is_open = ImGui::TreeNodeEx(key.c_str(), kGroupNodeFlags, "%s", key.c_str());

ImGui::TableNextColumn();
ImGui::TextUnformatted("Object");

ImGui::TableNextColumn();
if (is_open)
{
for (auto [entry, value] : object.as_object())
// TODO: make iterators work
auto iter = object.as_object().iter();
while (iter.has_next())
{
draw_json_item(entry, value);
auto [entry, value] = iter.next();
draw_json_item(std::string{entry}, value);
}
ImGui::TreePop();
}
}

static void draw_json_value(const char *key, const ctu::json::Json& value)
static void draw_json_value(const std::string& key, const ctu::json::Json& value)
{
ImGui::TreeNodeEx(key, kValueNodeFlags, "%s", key);
ImGui::TreeNodeEx(key.c_str(), kValueNodeFlags, "%s", key.c_str());

ImGui::TableNextColumn();
ImGui::TextUnformatted(get_kind_name(value.get_kind()));
Expand Down Expand Up @@ -681,7 +684,7 @@ struct JsonFile
}
}

static void draw_json_item(const char *key, const ctu::json::Json& value)
static void draw_json_item(const std::string& key, const ctu::json::Json& value)
{
ImGui::TableNextRow();
ImGui::TableNextColumn();
Expand Down
15 changes: 12 additions & 3 deletions src/frontend/gui/editor/src/panels/arena.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ void TraceArena::reset()
allocs.clear();
}

size_t TraceArena::add_stacktrace(const std::stacktrace& trace)
{
// TODO: lets hope these dont collide
size_t hash = std::hash<std::stacktrace>{}(trace);
stacktraces[hash] = trace;
return hash;
}

void TraceArena::create_alloc(void *ptr, size_t size)
{
peak_memory_usage += size;
Expand All @@ -123,7 +131,7 @@ void TraceArena::create_alloc(void *ptr, size_t size)
allocs[ptr].timestamp = std::chrono::high_resolution_clock::now();

if (collect & eCollectStackTrace)
allocs[ptr].trace = std::stacktrace::current();
allocs[ptr].trace = add_stacktrace(std::stacktrace::current());

live_allocs.insert(ptr);
}
Expand Down Expand Up @@ -269,9 +277,10 @@ void TraceArenaWidget::draw_name(const AllocInfo& alloc) const

if (ImGui::BeginPopupContextItem("TracePopup"))
{
if (!alloc.trace.empty())
const auto& trace = arena.stacktraces[alloc.trace];
if (!trace.empty())
{
draw_backtrace(alloc.trace);
draw_backtrace(trace);
}
else
{
Expand Down
7 changes: 6 additions & 1 deletion src/support/json/include/json/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ namespace ctu::json {
///
/// @return the end of the iteration
ObjectIterator end() const;

ObjectIterator iter() const;
};

/// @brief a json array value
Expand Down Expand Up @@ -295,7 +297,7 @@ namespace ctu::json {
/// @brief a member of a json object
struct CT_JSON_API member_t {
/// @brief the key of the member
const char *key;
std::string_view key;

/// @brief the value of the member
Json value;
Expand All @@ -320,6 +322,9 @@ namespace ctu::json {
bool operator!=(const ObjectIterator& other) const;
ObjectIterator &operator++();
member_t operator*() const;

bool has_next() const;
member_t next();
};

/// @brief an iterator over the values of a json array
Expand Down
35 changes: 32 additions & 3 deletions src/support/json/src/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,38 @@ Json Object::operator[](const char *key) const { return get(key); }
ObjectIterator Object::begin() const { return map_iter(m_object); }
ObjectIterator Object::end() const { return {}; }

bool ObjectIterator::operator!=(const ObjectIterator&) const { return map_has_next(&m_iter); }
ObjectIterator &ObjectIterator::operator++() { m_entry = map_next(&m_iter); return *this; }
member_t ObjectIterator::operator*() const { return { (const char*)m_entry.key, (json_t*)m_entry.value }; }
ObjectIterator Object::iter() const { return begin(); }

bool ObjectIterator::operator!=(const ObjectIterator&) const
{
return map_has_next(&m_iter);
}

ObjectIterator &ObjectIterator::operator++()
{
m_entry = map_next(&m_iter);
return *this;
}

member_t ObjectIterator::operator*() const
{
const text_view_t *key = (const text_view_t*)m_entry.key;
std::string_view view { key->text, key->length };
return { view, (json_t*)m_entry.value };
}

bool ObjectIterator::has_next() const
{
return map_has_next(&m_iter);
}

member_t ObjectIterator::next()
{
m_entry = map_next(&m_iter);
const text_view_t *key = (const text_view_t*)m_entry.key;
std::string_view view { key->text, key->length };
return { view, (json_t*)m_entry.value };
}

Json Array::get(size_t index) const { return (json_t*)typevec_offset(&m_array, index); }
Json Array::operator[](size_t index) const { return get(index); }
Expand Down

0 comments on commit 507e5da

Please sign in to comment.