mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 05:38:15 +00:00
Shell: Store ListValue's values in a NonnullRefPtrVector<Value>
A ListValue never stores null Values, so it makes sense to restrict it. This also propagates use of NonnullRefPtr to the create() helpers. There's a small bit of awkwardness around the use of initializer_list, since we cannot directly construct a NonnullRefPtrVector from one.
This commit is contained in:
parent
08e5371f44
commit
420e809fee
3 changed files with 16 additions and 16 deletions
|
@ -36,13 +36,13 @@
|
|||
namespace AST {
|
||||
|
||||
template<typename T, typename... Args>
|
||||
static inline RefPtr<T> create(Args... args)
|
||||
static inline NonnullRefPtr<T> create(Args... args)
|
||||
{
|
||||
return adopt(*new T(args...));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static inline RefPtr<T> create(std::initializer_list<RefPtr<Value>> arg)
|
||||
static inline NonnullRefPtr<T> create(std::initializer_list<NonnullRefPtr<Value>> arg)
|
||||
{
|
||||
return adopt(*new T(arg));
|
||||
}
|
||||
|
@ -226,7 +226,7 @@ RefPtr<Value> ListConcatenate::run(RefPtr<Shell> shell)
|
|||
|
||||
for (auto& element : m_list) {
|
||||
if (!result) {
|
||||
result = create<ListValue>({ element->run(shell)->resolve_without_cast(shell) });
|
||||
result = create<ListValue>({ element->run(shell)->resolve_without_cast(shell).release_nonnull() });
|
||||
continue;
|
||||
}
|
||||
auto element_value = element->run(shell)->resolve_without_cast(shell);
|
||||
|
@ -239,7 +239,7 @@ RefPtr<Value> ListConcatenate::run(RefPtr<Shell> shell)
|
|||
else
|
||||
result = create<CommandSequenceValue>(move(joined_commands));
|
||||
} else {
|
||||
Vector<RefPtr<Value>> values;
|
||||
NonnullRefPtrVector<Value> values;
|
||||
|
||||
if (result->is_list_without_resolution()) {
|
||||
values.append(static_cast<ListValue*>(result.ptr())->values());
|
||||
|
@ -248,7 +248,7 @@ RefPtr<Value> ListConcatenate::run(RefPtr<Shell> shell)
|
|||
values.append(create<StringValue>(result));
|
||||
}
|
||||
|
||||
values.append(move(element_value));
|
||||
values.append(element_value.release_nonnull());
|
||||
|
||||
result = create<ListValue>(move(values));
|
||||
}
|
||||
|
@ -496,7 +496,7 @@ RefPtr<Value> CastToList::run(RefPtr<Shell> shell)
|
|||
return inner_value;
|
||||
|
||||
auto values = inner_value->resolve_as_list(shell);
|
||||
Vector<RefPtr<Value>> cast_values;
|
||||
NonnullRefPtrVector<Value> cast_values;
|
||||
for (auto& value : values)
|
||||
cast_values.append(create<StringValue>(value));
|
||||
|
||||
|
@ -754,7 +754,7 @@ RefPtr<Value> ForLoop::run(RefPtr<Shell> shell)
|
|||
if (!m_block)
|
||||
return create<ListValue>({});
|
||||
|
||||
Vector<RefPtr<Value>> values;
|
||||
NonnullRefPtrVector<Value> values;
|
||||
auto resolved = m_iterated_expression->run(shell)->resolve_without_cast(shell);
|
||||
if (resolved->is_list_without_resolution())
|
||||
values = static_cast<ListValue*>(resolved.ptr())->values();
|
||||
|
@ -1899,16 +1899,16 @@ Vector<String> ListValue::resolve_as_list(RefPtr<Shell> shell)
|
|||
{
|
||||
Vector<String> values;
|
||||
for (auto& value : m_contained_values)
|
||||
values.append(value->resolve_as_list(shell));
|
||||
values.append(value.resolve_as_list(shell));
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
RefPtr<Value> ListValue::resolve_without_cast(RefPtr<Shell> shell)
|
||||
{
|
||||
Vector<RefPtr<Value>> values;
|
||||
NonnullRefPtrVector<Value> values;
|
||||
for (auto& value : m_contained_values)
|
||||
values.append(value->resolve_without_cast(shell));
|
||||
values.append(value.resolve_without_cast(shell).release_nonnull());
|
||||
|
||||
return create<ListValue>(move(values));
|
||||
}
|
||||
|
|
10
Shell/AST.h
10
Shell/AST.h
|
@ -238,16 +238,16 @@ public:
|
|||
virtual bool is_list() const override { return true; }
|
||||
virtual bool is_list_without_resolution() const override { return true; }
|
||||
ListValue(Vector<String> values);
|
||||
ListValue(Vector<RefPtr<Value>> values)
|
||||
: m_contained_values(move(values))
|
||||
ListValue(Vector<NonnullRefPtr<Value>> values)
|
||||
: m_contained_values(move(static_cast<NonnullRefPtrVector<Value>&>(values)))
|
||||
{
|
||||
}
|
||||
|
||||
const Vector<RefPtr<Value>>& values() const { return m_contained_values; }
|
||||
Vector<RefPtr<Value>>& values() { return m_contained_values; }
|
||||
const NonnullRefPtrVector<Value>& values() const { return m_contained_values; }
|
||||
NonnullRefPtrVector<Value>& values() { return m_contained_values; }
|
||||
|
||||
private:
|
||||
Vector<RefPtr<Value>> m_contained_values;
|
||||
NonnullRefPtrVector<Value> m_contained_values;
|
||||
};
|
||||
|
||||
class StringValue final : public Value {
|
||||
|
|
|
@ -664,7 +664,7 @@ int Shell::builtin_shift(int argc, const char** argv)
|
|||
}
|
||||
|
||||
if (!argv_->is_list())
|
||||
argv_ = adopt(*new AST::ListValue({ argv_ }));
|
||||
argv_ = adopt(*new AST::ListValue({ argv_.release_nonnull() }));
|
||||
|
||||
auto& values = static_cast<AST::ListValue*>(argv_.ptr())->values();
|
||||
if ((size_t)count > values.size()) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue