Fix subObjectAt

This commit is contained in:
r0qs 2023-08-28 13:46:25 +02:00
parent 4d8d6fe447
commit 61162deea7
No known key found for this signature in database
GPG Key ID: 61503DBA6667276C
3 changed files with 27 additions and 15 deletions

View File

@ -164,13 +164,15 @@ void Object::visitPath(YulString _qualifiedName, std::function<bool(Object const
if (subObjectIt != object->subObjects.end()) if (subObjectIt != object->subObjects.end())
{ {
object = dynamic_cast<Object const*>(subObjectIt->get()); object = dynamic_cast<Object const*>(subObjectIt->get());
if (object)
{
yulAssert(object, "Assembly object <" + object->name.str() + "> not found or does not contain code."); yulAssert(object, "Assembly object <" + object->name.str() + "> not found or does not contain code.");
yulAssert(object->subId != std::numeric_limits<size_t>::max(), "");
if (_visitor(object)) if (_visitor(object))
break; break;
} }
} }
} }
}
std::vector<size_t> Object::pathToSubObject(YulString _qualifiedName) const std::vector<size_t> Object::pathToSubObject(YulString _qualifiedName) const
{ {
@ -183,17 +185,30 @@ std::vector<size_t> Object::pathToSubObject(YulString _qualifiedName) const
yulAssert(!_qualifiedName.empty(), ""); yulAssert(!_qualifiedName.empty(), "");
this->visitPath(_qualifiedName, [&](Object const* _object) -> bool { this->visitPath(_qualifiedName, [&](Object const* _object) -> bool {
yulAssert(_object->subId != std::numeric_limits<size_t>::max(), "");
path.push_back({_object->subId}); path.push_back({_object->subId});
return false; return false;
}); });
return path; return path;
} }
std::shared_ptr<Object> Object::objectAt(std::shared_ptr<Object> const& _object, std::string const& _qualifiedName) std::shared_ptr<Object> Object::subObjectAt(std::string const& _qualifiedName)
{ {
if (_qualifiedName.empty())
return nullptr;
// If there is no `.` in the given `_qualifiedName`, the target
// object name is considered to be the `_qualifiedName`, otherwise,
// the target object name is the last element in the path given by `_qualifiedName`.
std::string targetObjectName = _qualifiedName;
size_t targetObjectPos = _qualifiedName.find_last_of(".");
if (targetObjectPos != std::string::npos)
targetObjectName = _qualifiedName.substr(targetObjectPos + 1);
std::shared_ptr<Object> foundObject = nullptr; std::shared_ptr<Object> foundObject = nullptr;
_object->visitPath(YulString(_qualifiedName), [&](Object const* _subObject) -> bool { this->visitPath(YulString(_qualifiedName), [&](Object const* _subObject) -> bool {
if (_qualifiedName.empty() || _qualifiedName == _subObject->name.str()) if (targetObjectName == _subObject->name.str())
{ {
foundObject = std::make_shared<Object>(*_subObject); foundObject = std::make_shared<Object>(*_subObject);
return true; return true;

View File

@ -117,6 +117,10 @@ public:
/// The path must not lead to a @a Data object (will throw in that case). /// The path must not lead to a @a Data object (will throw in that case).
std::vector<size_t> pathToSubObject(YulString _qualifiedName) const; std::vector<size_t> pathToSubObject(YulString _qualifiedName) const;
/// Searches for a subobject at @param _qualifiedName within the current object.
/// @returns a shared_ptr to the subobject or a nullptr if it was not found.
std::shared_ptr<Object> subObjectAt(std::string const& _qualifiedName);
/// Visits all subobjects in the path given by the @a _qualifiedName /// Visits all subobjects in the path given by the @a _qualifiedName
/// of the current object applying the function @a _visitor. /// of the current object applying the function @a _visitor.
/// If @a _visitor returns `true` the visiting stops, otherwise, /// If @a _visitor returns `true` the visiting stops, otherwise,
@ -138,13 +142,6 @@ public:
/// @returns the name of the special metadata data object. /// @returns the name of the special metadata data object.
static std::string metadataName() { return ".metadata"; } static std::string metadataName() { return ".metadata"; }
/// Searches for an Object at @param _qualifiedName within @param _object.
/// @returns a shared_ptr to the Object or a nullptr if it was not found.
static std::shared_ptr<Object> objectAt(
std::shared_ptr<Object> const& _object,
std::string const& _qualifiedName
);
}; };
} }

View File

@ -253,7 +253,7 @@ public:
{ {
if (!m_inputIsCodeBlock) if (!m_inputIsCodeBlock)
{ {
shared_ptr<Object> subObject = Object::objectAt(m_object, _objectPath); shared_ptr<Object> subObject = m_object->subObjectAt(_objectPath);
if (subObject == nullptr) if (subObject == nullptr)
solThrow(Exception, "Assembly object not found."); solThrow(Exception, "Assembly object not found.");