Skip to content

Commit

Permalink
Cown: Reifing cown objects
Browse files Browse the repository at this point in the history
  • Loading branch information
xFrednet committed Oct 19, 2024
1 parent 074457b commit 450dda4
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
49 changes: 49 additions & 0 deletions src/rt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,54 @@ namespace rt::core
}
};

// The prototype object for cown
inline PrototypeObject* cownPrototypeObject()
{
static PrototypeObject* proto = new PrototypeObject("Cown");
return proto;
}

class CownObject : public objects::DynObject
{
// For now always false, but might be needed later if we want to simulate
// concurrency.
bool aquired = false;

public:
CownObject(objects::DynObject* region)
: objects::DynObject(cownPrototypeObject()),
{
// FIXME: Add once regions are reified
// assert(
// region->get_prototype() == regionPrototype() &&
// "Cowns can only store regions");
//
// FIXME: Also check that the region has a LRC == 1, with 1
// being the reference passed into this constructor
this->set("region", region);
}

bool is_aquired()
{
return aquired;
}

std::string get_name()
{
return "<cown>";
}

objects::DynObject* is_primitive()
{
return this;
}

bool is_cown() override
{
return false;
}
};

inline std::set<objects::DynObject*>* globals()
{
static std::set<objects::DynObject*>* globals =
Expand All @@ -185,6 +233,7 @@ namespace rt::core
keyIterPrototypeObject(),
trueObject(),
falseObject(),
cownPrototypeObject(),
};
return globals;
}
Expand Down
18 changes: 14 additions & 4 deletions src/rt/objects/dyn_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ namespace rt::objects
{
friend class Reference;
friend objects::DynObject* rt::make_iter(objects::DynObject* obj);
friend void
rt::ui::mermaid(std::vector<objects::DynObject*>& roots, std::ostream& out, std::vector<objects::DynObject*>* taint);
friend void rt::ui::mermaid(
std::vector<objects::DynObject*>& roots,
std::ostream& out,
std::vector<objects::DynObject*>* taint);
friend void destruct(DynObject* obj);
friend void dealloc(DynObject* obj);
template<typename Pre, typename Post>
Expand Down Expand Up @@ -241,7 +243,8 @@ namespace rt::objects
get_region(obj)->objects.erase(obj);
}
obj->region.set_tag(ImmutableTag);
return true;

return !obj->is_cown();
});
}

Expand All @@ -250,6 +253,11 @@ namespace rt::objects
return region.get_tag() == ImmutableTag;
}

virtual bool is_cown()
{
return false;
}

[[nodiscard]] DynObject* get(std::string name)
{
auto result = fields.find(name);
Expand All @@ -270,7 +278,7 @@ namespace rt::objects

[[nodiscard]] DynObject* set(std::string name, DynObject* value)
{
if (is_immutable())
if (is_immutable() && this->is_cown())
{
ui::error("Cannot mutate immutable object");
}
Expand All @@ -282,6 +290,7 @@ namespace rt::objects
// The caller must provide an rc for value.
[[nodiscard]] DynObject* set_prototype(DynObject* value)
{
// No need to check for a cown, since cowns already have a set prototype
if (is_immutable())
{
ui::error("Cannot mutate immutable object");
Expand Down Expand Up @@ -331,6 +340,7 @@ namespace rt::objects
static void
move_reference(DynObject* src, DynObject* dst, DynObject* target)
{
// An immutable cown can't be moved to another region
if (target == nullptr || target->is_immutable())
return;

Expand Down
27 changes: 27 additions & 0 deletions src/rt/rt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,18 @@ namespace rt
return new core::FrameObject(parent);
}

objects::DynObject* make_cown(objects::DynObject* region)
{
return new core::CownObject(region);
}

thread_local objects::RegionPointer objects::DynObject::local_region =
new Region();

void freeze(objects::DynObject* obj)
{
// Cown specific handling of the freeze operation is handled by the
// `freeze()` implementation of the object
obj->freeze();
}

Expand All @@ -49,6 +56,15 @@ namespace rt

objects::DynObject* get(objects::DynObject* obj, std::string key)
{
if (obj->get_prototype() == core::cownPrototypeObject())
{
core::CownObject* cown = reinterpret_cast<core::CownObject*>(obj);
if (!cown->is_aquired())
{
ui::error(
"the cown needs to be aquired, before its data can be accessed");
}
}
return obj->get(key);
}

Expand All @@ -72,6 +88,17 @@ namespace rt
objects::DynObject*
set(objects::DynObject* obj, std::string key, objects::DynObject* value)
{
if (obj->get_prototype() == core::cownPrototypeObject())
{
core::CownObject* cown = reinterpret_cast<core::CownObject*>(obj);
if (!cown->is_aquired())
{
// Overwriting data can change the RC and then call destructors of the
// type this action therefore requires the cown to be aquired
ui::error("the cown needs to be aquired, before its data can modified");
}
}

return obj->set(key, value);
}

Expand Down
1 change: 1 addition & 0 deletions src/rt/rt.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace rt
objects::DynObject* make_str(std::string str_value);
objects::DynObject* make_object();
objects::DynObject* make_frame(objects::DynObject* parent);
objects::DynObject* make_cown(objects::DynObject* region);

void freeze(objects::DynObject* obj);
void create_region(objects::DynObject* objects);
Expand Down

0 comments on commit 450dda4

Please sign in to comment.