1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 04:47:34 +00:00

AK: Stop allowing implicit downcast with RefPtr and NonnullRefPtr

We were allowing this dangerous kind of thing:

RefPtr<Base> base;
RefPtr<Derived> derived = base;

This patch changes the {Nonnull,}RefPtr constructors so this is no
longer possible.

To downcast one of these pointers, there is now static_ptr_cast<T>:

RefPtr<Derived> derived = static_ptr_cast<Derived>(base);

Fixing this exposed a ton of cowboy-downcasts in various places,
which we're now forced to fix. :^)
This commit is contained in:
Andreas Kling 2020-04-05 11:11:07 +02:00
parent 058c614110
commit 1d468ed6d3
11 changed files with 68 additions and 54 deletions

View file

@ -137,11 +137,11 @@ class Declaration : public Statement {
class FunctionNode {
public:
const FlyString& name() const { return m_name; }
const ScopeNode& body() const { return *m_body; }
const Statement& body() const { return *m_body; }
const Vector<FlyString>& parameters() const { return m_parameters; };
protected:
FunctionNode(const FlyString& name, NonnullRefPtr<ScopeNode> body, Vector<FlyString> parameters = {})
FunctionNode(const FlyString& name, NonnullRefPtr<Statement> body, Vector<FlyString> parameters = {})
: m_name(name)
, m_body(move(body))
, m_parameters(move(parameters))
@ -152,7 +152,7 @@ protected:
private:
FlyString m_name;
NonnullRefPtr<ScopeNode> m_body;
NonnullRefPtr<Statement> m_body;
const Vector<FlyString> m_parameters;
};
@ -162,7 +162,7 @@ class FunctionDeclaration final
public:
static bool must_have_name() { return true; }
FunctionDeclaration(String name, NonnullRefPtr<ScopeNode> body, Vector<FlyString> parameters = {})
FunctionDeclaration(String name, NonnullRefPtr<Statement> body, Vector<FlyString> parameters = {})
: FunctionNode(move(name), move(body), move(parameters))
{
}
@ -179,7 +179,7 @@ class FunctionExpression final : public Expression
public:
static bool must_have_name() { return false; }
FunctionExpression(const FlyString& name, NonnullRefPtr<ScopeNode> body, Vector<FlyString> parameters = {})
FunctionExpression(const FlyString& name, NonnullRefPtr<Statement> body, Vector<FlyString> parameters = {})
: FunctionNode(name, move(body), move(parameters))
{
}
@ -241,14 +241,14 @@ private:
class WhileStatement : public Statement {
public:
WhileStatement(NonnullRefPtr<Expression> test, NonnullRefPtr<ScopeNode> body)
WhileStatement(NonnullRefPtr<Expression> test, NonnullRefPtr<Statement> body)
: m_test(move(test))
, m_body(move(body))
{
}
const Expression& test() const { return *m_test; }
const ScopeNode& body() const { return *m_body; }
const Statement& body() const { return *m_body; }
virtual Value execute(Interpreter&) const override;
virtual void dump(int indent) const override;
@ -257,19 +257,19 @@ private:
virtual const char* class_name() const override { return "WhileStatement"; }
NonnullRefPtr<Expression> m_test;
NonnullRefPtr<ScopeNode> m_body;
NonnullRefPtr<Statement> m_body;
};
class DoWhileStatement : public Statement {
public:
DoWhileStatement(NonnullRefPtr<Expression> test, NonnullRefPtr<ScopeNode> body)
DoWhileStatement(NonnullRefPtr<Expression> test, NonnullRefPtr<Statement> body)
: m_test(move(test))
, m_body(move(body))
{
}
const Expression& test() const { return *m_test; }
const ScopeNode& body() const { return *m_body; }
const Statement& body() const { return *m_body; }
virtual Value execute(Interpreter&) const override;
virtual void dump(int indent) const override;
@ -278,12 +278,12 @@ private:
virtual const char* class_name() const override { return "DoWhileStatement"; }
NonnullRefPtr<Expression> m_test;
NonnullRefPtr<ScopeNode> m_body;
NonnullRefPtr<Statement> m_body;
};
class ForStatement : public Statement {
public:
ForStatement(RefPtr<ASTNode> init, RefPtr<Expression> test, RefPtr<Expression> update, NonnullRefPtr<ScopeNode> body)
ForStatement(RefPtr<ASTNode> init, RefPtr<Expression> test, RefPtr<Expression> update, NonnullRefPtr<Statement> body)
: m_init(move(init))
, m_test(move(test))
, m_update(move(update))
@ -294,7 +294,7 @@ public:
const ASTNode* init() const { return m_init; }
const Expression* test() const { return m_test; }
const Expression* update() const { return m_update; }
const ScopeNode& body() const { return *m_body; }
const Statement& body() const { return *m_body; }
virtual Value execute(Interpreter&) const override;
virtual void dump(int indent) const override;
@ -305,7 +305,7 @@ private:
RefPtr<ASTNode> m_init;
RefPtr<Expression> m_test;
RefPtr<Expression> m_update;
NonnullRefPtr<ScopeNode> m_body;
NonnullRefPtr<Statement> m_body;
};
enum class BinaryOp {
@ -569,7 +569,7 @@ private:
virtual const char* class_name() const override { return "UpdateExpression"; }
UpdateOp m_op;
NonnullRefPtr<Identifier> m_argument;
NonnullRefPtr<Expression> m_argument;
bool m_prefixed;
};

View file

@ -33,7 +33,7 @@
namespace JS {
ScriptFunction::ScriptFunction(const ScopeNode& body, Vector<FlyString> parameters)
ScriptFunction::ScriptFunction(const Statement& body, Vector<FlyString> parameters)
: m_body(body)
, m_parameters(move(parameters))
{

View file

@ -32,10 +32,10 @@ namespace JS {
class ScriptFunction final : public Function {
public:
ScriptFunction(const ScopeNode& body, Vector<FlyString> parameters = {});
ScriptFunction(const Statement& body, Vector<FlyString> parameters = {});
virtual ~ScriptFunction();
const ScopeNode& body() const { return m_body; }
const Statement& body() const { return m_body; }
const Vector<FlyString>& parameters() const { return m_parameters; };
virtual Value call(Interpreter&) override;
@ -48,7 +48,7 @@ private:
static Value length_getter(Interpreter&);
static void length_setter(Interpreter&, Value);
NonnullRefPtr<ScopeNode> m_body;
NonnullRefPtr<Statement> m_body;
const Vector<FlyString> m_parameters;
};

View file

@ -221,7 +221,7 @@ void Document::layout()
if (!m_layout_root) {
LayoutTreeBuilder tree_builder;
m_layout_root = tree_builder.build(*this);
m_layout_root = static_ptr_cast<LayoutDocument>(tree_builder.build(*this));
}
m_layout_root->layout();
m_layout_root->set_needs_display();

View file

@ -153,39 +153,40 @@ NonnullRefPtr<StyleValue> parse_css_value(const StringView& string)
return StringStyleValue::create(string);
}
RefPtr<StyleValue> parse_line_width(const StringView& part)
RefPtr<LengthStyleValue> parse_line_width(const StringView& part)
{
NonnullRefPtr<StyleValue> value = parse_css_value(part);
if (value->is_length())
return value;
return static_ptr_cast<LengthStyleValue>(value);
return nullptr;
}
RefPtr<StyleValue> parse_color(const StringView& part)
RefPtr<ColorStyleValue> parse_color(const StringView& part)
{
NonnullRefPtr<StyleValue> value = parse_css_value(part);
if (value->is_color())
return value;
return static_ptr_cast<ColorStyleValue>(value);
return nullptr;
}
RefPtr<StyleValue> parse_line_style(const StringView& part)
RefPtr<StringStyleValue> parse_line_style(const StringView& part)
{
NonnullRefPtr<StyleValue> value = parse_css_value(part);
if (value->is_string()) {
if (value->to_string() == "dotted")
return value;
if (value->to_string() == "dashed")
return value;
if (value->to_string() == "solid")
return value;
if (value->to_string() == "double")
return value;
if (value->to_string() == "groove")
return value;
if (value->to_string() == "ridge")
return value;
}
NonnullRefPtr<StyleValue> parsed_value = parse_css_value(part);
if (!parsed_value->is_string())
return nullptr;
auto value = static_ptr_cast<StringStyleValue>(parsed_value);
if (value->to_string() == "dotted")
return value;
if (value->to_string() == "dashed")
return value;
if (value->to_string() == "solid")
return value;
if (value->to_string() == "double")
return value;
if (value->to_string() == "groove")
return value;
if (value->to_string() == "ridge")
return value;
return nullptr;
}

View file

@ -36,8 +36,8 @@ RefPtr<StyleDeclaration> parse_css_declaration(const StringView&);
NonnullRefPtr<StyleValue> parse_css_value(const StringView&);
Optional<Selector> parse_selector(const StringView&);
RefPtr<StyleValue> parse_line_width(const StringView&);
RefPtr<StyleValue> parse_color(const StringView&);
RefPtr<StyleValue> parse_line_style(const StringView&);
RefPtr<LengthStyleValue> parse_line_width(const StringView&);
RefPtr<ColorStyleValue> parse_color(const StringView&);
RefPtr<StringStyleValue> parse_line_style(const StringView&);
}