Skip to content

Commit

Permalink
abstract_context: De-inline local reference accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
lhmouse committed Oct 17, 2023
1 parent 1c10ad2 commit d3f06e1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 35 deletions.
50 changes: 50 additions & 0 deletions asteria/runtime/abstract_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,54 @@ Abstract_Context::
{
}

Reference&
Abstract_Context::
do_mut_named_reference(Reference* hint_opt, phsh_stringR name) const
{
return hint_opt ? *hint_opt : this->m_named_refs.insert(name);
}

void
Abstract_Context::
do_clear_named_references() noexcept
{
this->m_named_refs.clear();
}

const Reference*
Abstract_Context::
get_named_reference_opt(phsh_stringR name) const
{
auto qref = this->m_named_refs.find_opt(name);
if(ROCKET_UNEXPECT(!qref) && (name[0] == '_') && (name[1] == '_')) {
// Create a lazy reference. Built-in references such as `__func`
// are only created when they are mentioned.
qref = this->do_create_lazy_reference_opt(nullptr, name);
}
return qref;
}

Reference&
Abstract_Context::
insert_named_reference(phsh_stringR name)
{
bool newly = false;
auto& ref = this->m_named_refs.insert(name, &newly);
if(ROCKET_UNEXPECT(newly) && (name[0] == '_') && (name[1] == '_')) {
// If a built-in reference has been inserted, it may have a default
// value, so initialize it. DO NOT CALL THIS FUNCTION INSIDE
// `do_create_lazy_reference_opt()`, as it will result in infinite
// recursion; call `do_mut_named_reference()` instead.
this->do_create_lazy_reference_opt(&ref, name);
}
return ref;
}

bool
Abstract_Context::
erase_named_reference(phsh_stringR name, Reference* refp_opt) noexcept
{
return this->m_named_refs.erase(name, refp_opt);
}

} // namespace asteria
40 changes: 5 additions & 35 deletions asteria/runtime/abstract_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,10 @@ class Abstract_Context
// This function is called by `do_create_lazy_reference_opt()` to avoid
// infinite recursion.
Reference&
do_mut_named_reference(Reference* hint_opt, phsh_stringR name) const
{
return hint_opt ? *hint_opt : this->m_named_refs.insert(name);
}
do_mut_named_reference(Reference* hint_opt, phsh_stringR name) const;

void
do_clear_named_references() noexcept
{
this->m_named_refs.clear();
}
do_clear_named_references() noexcept;

public:
ASTERIA_NONCOPYABLE_DESTRUCTOR(Abstract_Context);
Expand All @@ -68,37 +62,13 @@ class Abstract_Context
{ return this->do_get_parent_opt(); }

const Reference*
get_named_reference_opt(phsh_stringR name) const
{
auto qref = this->m_named_refs.find_opt(name);
if(ROCKET_UNEXPECT(!qref) && (name[0] == '_') && (name[1] == '_')) {
// Create a lazy reference. Built-in references such as `__func`
// are only created when they are mentioned.
qref = this->do_create_lazy_reference_opt(nullptr, name);
}
return qref;
}
get_named_reference_opt(phsh_stringR name) const;

Reference&
insert_named_reference(phsh_stringR name)
{
bool newly;
auto& ref = this->m_named_refs.insert(name, &newly);
if(ROCKET_UNEXPECT(newly) && (name[0] == '_') && (name[1] == '_')) {
// If a built-in reference has been inserted, it may have a default
// value, so initialize it. DO NOT CALL THIS FUNCTION INSIDE
// `do_create_lazy_reference_opt()`, as it will result in infinite
// recursion; call `do_mut_named_reference()` instead.
this->do_create_lazy_reference_opt(&ref, name);
}
return ref;
}
insert_named_reference(phsh_stringR name);

bool
erase_named_reference(phsh_stringR name, Reference* refp_opt = nullptr) noexcept
{
return this->m_named_refs.erase(name, refp_opt);
}
erase_named_reference(phsh_stringR name, Reference* refp_opt = nullptr) noexcept;
};

} // namespace asteria
Expand Down

0 comments on commit d3f06e1

Please sign in to comment.