xo-gc stack: + gc-report-object-types() primitive

This commit is contained in:
Roland Conybeare 2026-03-29 17:19:23 -04:00
commit 057f0acc72
16 changed files with 286 additions and 12 deletions

View file

@ -112,6 +112,8 @@ namespace xo {
/** return value associated with @p key, if key is present **/
std::optional<obj<AGCObject>> lookup(const DString * key) const noexcept;
/** return value associated with @p key, if key is present **/
std::optional<obj<AGCObject>> lookup_cstr(const char * key) const noexcept;
/** return element @p key-value pair at position @p index (0-based) **/
std::pair<const DString *, obj<AGCObject>> at_index(size_type index) const;
@ -139,6 +141,11 @@ namespace xo {
**/
bool try_update(const pair_type & kvpair);
/** update key-value pair for existing @p key to map to @p value.
* false if @p key not already present.
**/
bool try_update_cstr(const char * key, obj<AGCObject> value);
/** convenience method:
* try_upsert pair (k, @p value), after boxing c-style string @p key with @p mm to get k
**/

View file

@ -35,6 +35,8 @@ namespace xo {
operator long() const noexcept { return value_; }
void assign_value(long x) noexcept { this->value_ = x; }
// GCObject facet
std::size_t shallow_size() const noexcept;

View file

@ -101,7 +101,7 @@ namespace xo {
bool
DArray::resize(size_type new_z) noexcept
{
if (new_z >= capacity_) {
if (new_z > capacity_) {
return false;
} else if (new_z > size_) {
// ensure new size is zeroed (we/re not zeroing if/when we shrink)
@ -149,7 +149,25 @@ namespace xo {
pps->write("]");
return true;
} else {
pps->write("[...]");
pps->write("[");
for (size_type i = 0, n = this->size(); i < n; ++i) {
if (i == 0) {
/* indent, but credit initial [ */
pps->indent(std::max(pps->indent_width(), 1u) - 1);
} else {
/* indent after newline */
pps->newline_indent(ppii.ci1());
}
obj<APrintable> elt = this->at(i).to_facet<APrintable>();
assert(elt.data());
pps->pretty(elt);
}
pps->write("]");
return false;
}
}

View file

@ -50,6 +50,21 @@ namespace xo {
return {};
}
std::optional<obj<AGCObject>>
DDictionary::lookup_cstr(const char * key) const noexcept
{
for (DArray::size_type i = 0, z = keys_->size(); i < z; ++i) {
auto i_key = obj<AGCObject,DString>::from(keys_->at(i));
assert(i_key);
if (strcmp(key, i_key->data()) == 0)
return values_->at(i);
}
return {};
}
std::pair<const DString *, obj<AGCObject>>
DDictionary::at_index(size_type ix) const
{
@ -104,6 +119,24 @@ namespace xo {
return false;
}
bool
DDictionary::try_update_cstr(const char * key, obj<AGCObject> value)
{
for (size_type i = 0, n = keys_->size(); i < n; ++i) {
auto key_i = obj<AGCObject,DString>::from((*keys_)[i]);
assert(key_i);
if (strcmp(key, key_i->data()) == 0) {
values_->assign_at(i, value);
return true;
}
}
return false;
}
bool
DDictionary::try_upsert_cstr(obj<AAllocator> mm, const char * key_cstr, obj<AGCObject> value)
{
@ -183,8 +216,7 @@ namespace xo {
pps->write("{");
for (size_type i = 0, n = this->size(); i < n; ++i) {
if (i > 0)
pps->write(" ");
pps->write(" ");
obj<APrintable> key
= FacetRegistry::instance().variant<APrintable,AGCObject>((*keys_)[i]);
@ -203,7 +235,7 @@ namespace xo {
pps->write(";");
}
pps->write("}");
pps->write(" }");
return true;
} else {
pps->write("{");
@ -211,10 +243,10 @@ namespace xo {
for (size_type i = 0, n = this->size(); i < n; ++i) {
if (i == 0) {
/* indent, but credit initial {. using same line for first (key,value) */
ppii.pps()->indent(std::max(ppii.pps()->indent_width(), 1u) - 1);
pps->indent(std::max(pps->indent_width(), 1u) - 1);
} else {
/* indent after newline */
ppii.pps()->newline_indent(ppii.ci1());
pps->newline_indent(ppii.ci1());
}
obj<APrintable> key