1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 03:08:13 +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;
};