From c12125fa817bdbf423cb01e171addf9765eca417 Mon Sep 17 00:00:00 2001 From: Jack Karamanian Date: Sat, 30 May 2020 00:10:42 -0500 Subject: LibJS: Track whether ScriptFunctions and FunctionExpressions are arrow functions --- Libraries/LibJS/AST.cpp | 2 +- Libraries/LibJS/AST.h | 5 ++++- Libraries/LibJS/Parser.cpp | 2 +- Libraries/LibJS/Runtime/ScriptFunction.cpp | 7 ++++--- Libraries/LibJS/Runtime/ScriptFunction.h | 5 +++-- 5 files changed, 13 insertions(+), 8 deletions(-) (limited to 'Libraries') diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index f3cad3a386..2414bfbf90 100644 --- a/Libraries/LibJS/AST.cpp +++ b/Libraries/LibJS/AST.cpp @@ -76,7 +76,7 @@ Value FunctionDeclaration::execute(Interpreter& interpreter) const Value FunctionExpression::execute(Interpreter& interpreter) const { - return ScriptFunction::create(interpreter.global_object(), name(), body(), parameters(), function_length(), interpreter.current_environment()); + return ScriptFunction::create(interpreter.global_object(), name(), body(), parameters(), function_length(), interpreter.current_environment(), m_is_arrow_function); } Value ExpressionStatement::execute(Interpreter& interpreter) const diff --git a/Libraries/LibJS/AST.h b/Libraries/LibJS/AST.h index 297f41884b..115a193604 100644 --- a/Libraries/LibJS/AST.h +++ b/Libraries/LibJS/AST.h @@ -223,8 +223,9 @@ class FunctionExpression final public: static bool must_have_name() { return false; } - FunctionExpression(const FlyString& name, NonnullRefPtr body, Vector parameters, i32 function_length, NonnullRefPtrVector variables) + FunctionExpression(const FlyString& name, NonnullRefPtr body, Vector parameters, i32 function_length, NonnullRefPtrVector variables, bool is_arrow_function = false) : FunctionNode(name, move(body), move(parameters), function_length, move(variables)) + , m_is_arrow_function(is_arrow_function) { } @@ -233,6 +234,8 @@ public: private: virtual const char* class_name() const override { return "FunctionExpression"; } + + bool m_is_arrow_function; }; class ErrorExpression final : public Expression { diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp index 575ad25cfa..616c353430 100644 --- a/Libraries/LibJS/Parser.cpp +++ b/Libraries/LibJS/Parser.cpp @@ -382,7 +382,7 @@ RefPtr Parser::try_parse_arrow_function_expression(bool expe if (!function_body_result.is_null()) { state_rollback_guard.disarm(); auto body = function_body_result.release_nonnull(); - return create_ast_node("", move(body), move(parameters), function_length, m_parser_state.m_var_scopes.take_last()); + return create_ast_node("", move(body), move(parameters), function_length, m_parser_state.m_var_scopes.take_last(), true); } return nullptr; diff --git a/Libraries/LibJS/Runtime/ScriptFunction.cpp b/Libraries/LibJS/Runtime/ScriptFunction.cpp index 1b6aa3ef3b..b5b857f620 100644 --- a/Libraries/LibJS/Runtime/ScriptFunction.cpp +++ b/Libraries/LibJS/Runtime/ScriptFunction.cpp @@ -47,18 +47,19 @@ static ScriptFunction* script_function_from(Interpreter& interpreter) return static_cast(this_object); } -ScriptFunction* ScriptFunction::create(GlobalObject& global_object, const FlyString& name, const Statement& body, Vector parameters, i32 m_function_length, LexicalEnvironment* parent_environment) +ScriptFunction* ScriptFunction::create(GlobalObject& global_object, const FlyString& name, const Statement& body, Vector parameters, i32 m_function_length, LexicalEnvironment* parent_environment, bool is_arrow_function) { - return global_object.heap().allocate(name, body, move(parameters), m_function_length, parent_environment, *global_object.function_prototype()); + return global_object.heap().allocate(name, body, move(parameters), m_function_length, parent_environment, *global_object.function_prototype(), is_arrow_function); } -ScriptFunction::ScriptFunction(const FlyString& name, const Statement& body, Vector parameters, i32 m_function_length, LexicalEnvironment* parent_environment, Object& prototype) +ScriptFunction::ScriptFunction(const FlyString& name, const Statement& body, Vector parameters, i32 m_function_length, LexicalEnvironment* parent_environment, Object& prototype, bool is_arrow_function) : Function(prototype) , m_name(name) , m_body(body) , m_parameters(move(parameters)) , m_parent_environment(parent_environment) , m_function_length(m_function_length) + , m_is_arrow_function(is_arrow_function) { define_property("prototype", Object::create_empty(interpreter(), interpreter().global_object()), 0); define_native_property("length", length_getter, nullptr, Attribute::Configurable); diff --git a/Libraries/LibJS/Runtime/ScriptFunction.h b/Libraries/LibJS/Runtime/ScriptFunction.h index cfce3a8688..04db16d601 100644 --- a/Libraries/LibJS/Runtime/ScriptFunction.h +++ b/Libraries/LibJS/Runtime/ScriptFunction.h @@ -33,9 +33,9 @@ namespace JS { class ScriptFunction final : public Function { public: - static ScriptFunction* create(GlobalObject&, const FlyString& name, const Statement& body, Vector parameters, i32 m_function_length, LexicalEnvironment* parent_environment); + static ScriptFunction* create(GlobalObject&, const FlyString& name, const Statement& body, Vector parameters, i32 m_function_length, LexicalEnvironment* parent_environment, bool is_arrow_function = false); - ScriptFunction(const FlyString& name, const Statement& body, Vector parameters, i32 m_function_length, LexicalEnvironment* parent_environment, Object& prototype); + ScriptFunction(const FlyString& name, const Statement& body, Vector parameters, i32 m_function_length, LexicalEnvironment* parent_environment, Object& prototype, bool is_arrow_function = false); virtual ~ScriptFunction(); const Statement& body() const { return m_body; } @@ -61,6 +61,7 @@ private: const Vector m_parameters; LexicalEnvironment* m_parent_environment { nullptr }; i32 m_function_length; + bool m_is_arrow_function; }; } -- cgit v1.2.3