mirror of
https://github.com/RGBCube/serenity
synced 2025-07-23 07:47:34 +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 {
|
namespace AST {
|
||||||
|
|
||||||
template<typename T, typename... Args>
|
template<typename T, typename... Args>
|
||||||
static inline RefPtr<T> create(Args... args)
|
static inline NonnullRefPtr<T> create(Args... args)
|
||||||
{
|
{
|
||||||
return adopt(*new T(args...));
|
return adopt(*new T(args...));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
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));
|
return adopt(*new T(arg));
|
||||||
}
|
}
|
||||||
|
@ -226,7 +226,7 @@ RefPtr<Value> ListConcatenate::run(RefPtr<Shell> shell)
|
||||||
|
|
||||||
for (auto& element : m_list) {
|
for (auto& element : m_list) {
|
||||||
if (!result) {
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
auto element_value = element->run(shell)->resolve_without_cast(shell);
|
auto element_value = element->run(shell)->resolve_without_cast(shell);
|
||||||
|
@ -239,7 +239,7 @@ RefPtr<Value> ListConcatenate::run(RefPtr<Shell> shell)
|
||||||
else
|
else
|
||||||
result = create<CommandSequenceValue>(move(joined_commands));
|
result = create<CommandSequenceValue>(move(joined_commands));
|
||||||
} else {
|
} else {
|
||||||
Vector<RefPtr<Value>> values;
|
NonnullRefPtrVector<Value> values;
|
||||||
|
|
||||||
if (result->is_list_without_resolution()) {
|
if (result->is_list_without_resolution()) {
|
||||||
values.append(static_cast<ListValue*>(result.ptr())->values());
|
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(create<StringValue>(result));
|
||||||
}
|
}
|
||||||
|
|
||||||
values.append(move(element_value));
|
values.append(element_value.release_nonnull());
|
||||||
|
|
||||||
result = create<ListValue>(move(values));
|
result = create<ListValue>(move(values));
|
||||||
}
|
}
|
||||||
|
@ -496,7 +496,7 @@ RefPtr<Value> CastToList::run(RefPtr<Shell> shell)
|
||||||
return inner_value;
|
return inner_value;
|
||||||
|
|
||||||
auto values = inner_value->resolve_as_list(shell);
|
auto values = inner_value->resolve_as_list(shell);
|
||||||
Vector<RefPtr<Value>> cast_values;
|
NonnullRefPtrVector<Value> cast_values;
|
||||||
for (auto& value : values)
|
for (auto& value : values)
|
||||||
cast_values.append(create<StringValue>(value));
|
cast_values.append(create<StringValue>(value));
|
||||||
|
|
||||||
|
@ -754,7 +754,7 @@ RefPtr<Value> ForLoop::run(RefPtr<Shell> shell)
|
||||||
if (!m_block)
|
if (!m_block)
|
||||||
return create<ListValue>({});
|
return create<ListValue>({});
|
||||||
|
|
||||||
Vector<RefPtr<Value>> values;
|
NonnullRefPtrVector<Value> values;
|
||||||
auto resolved = m_iterated_expression->run(shell)->resolve_without_cast(shell);
|
auto resolved = m_iterated_expression->run(shell)->resolve_without_cast(shell);
|
||||||
if (resolved->is_list_without_resolution())
|
if (resolved->is_list_without_resolution())
|
||||||
values = static_cast<ListValue*>(resolved.ptr())->values();
|
values = static_cast<ListValue*>(resolved.ptr())->values();
|
||||||
|
@ -1899,16 +1899,16 @@ Vector<String> ListValue::resolve_as_list(RefPtr<Shell> shell)
|
||||||
{
|
{
|
||||||
Vector<String> values;
|
Vector<String> values;
|
||||||
for (auto& value : m_contained_values)
|
for (auto& value : m_contained_values)
|
||||||
values.append(value->resolve_as_list(shell));
|
values.append(value.resolve_as_list(shell));
|
||||||
|
|
||||||
return values;
|
return values;
|
||||||
}
|
}
|
||||||
|
|
||||||
RefPtr<Value> ListValue::resolve_without_cast(RefPtr<Shell> shell)
|
RefPtr<Value> ListValue::resolve_without_cast(RefPtr<Shell> shell)
|
||||||
{
|
{
|
||||||
Vector<RefPtr<Value>> values;
|
NonnullRefPtrVector<Value> values;
|
||||||
for (auto& value : m_contained_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));
|
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() const override { return true; }
|
||||||
virtual bool is_list_without_resolution() const override { return true; }
|
virtual bool is_list_without_resolution() const override { return true; }
|
||||||
ListValue(Vector<String> values);
|
ListValue(Vector<String> values);
|
||||||
ListValue(Vector<RefPtr<Value>> values)
|
ListValue(Vector<NonnullRefPtr<Value>> values)
|
||||||
: m_contained_values(move(values))
|
: m_contained_values(move(static_cast<NonnullRefPtrVector<Value>&>(values)))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
const Vector<RefPtr<Value>>& values() const { return m_contained_values; }
|
const NonnullRefPtrVector<Value>& values() const { return m_contained_values; }
|
||||||
Vector<RefPtr<Value>>& values() { return m_contained_values; }
|
NonnullRefPtrVector<Value>& values() { return m_contained_values; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Vector<RefPtr<Value>> m_contained_values;
|
NonnullRefPtrVector<Value> m_contained_values;
|
||||||
};
|
};
|
||||||
|
|
||||||
class StringValue final : public Value {
|
class StringValue final : public Value {
|
||||||
|
|
|
@ -664,7 +664,7 @@ int Shell::builtin_shift(int argc, const char** argv)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!argv_->is_list())
|
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();
|
auto& values = static_cast<AST::ListValue*>(argv_.ptr())->values();
|
||||||
if ((size_t)count > values.size()) {
|
if ((size_t)count > values.size()) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue