diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2021-06-11 03:38:05 +0430 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2021-06-11 19:42:58 +0430 |
commit | 8b3f8879c1f45d701033b31337000fca4330b3f9 (patch) | |
tree | 9dd3bd6f6453a7dc4a770539fcd99cd42f78630d | |
parent | 5ffe23e4f3fa973af7ae24d27aa40acf24a552c2 (diff) | |
download | serenity-8b3f8879c1f45d701033b31337000fca4330b3f9.zip |
LibJS: Use an enum class instead of 'bool is_generator'
This avoid confusion in the order of the multiple boolean parameters
that exist.
-rw-r--r-- | Userland/Libraries/LibJS/AST.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/AST.h | 21 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Bytecode/Op.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Interpreter.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Parser.cpp | 22 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/ScriptFunction.cpp | 14 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/ScriptFunction.h | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/GlobalEventHandlers.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/WebSocket.cpp | 2 |
9 files changed, 46 insertions, 29 deletions
diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp index 6aa1714a35..53cc8da0f6 100644 --- a/Userland/Libraries/LibJS/AST.cpp +++ b/Userland/Libraries/LibJS/AST.cpp @@ -100,7 +100,7 @@ Value FunctionDeclaration::execute(Interpreter& interpreter, GlobalObject&) cons Value FunctionExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const { InterpreterNodeScope node_scope { interpreter, *this }; - return ScriptFunction::create(global_object, name(), body(), parameters(), function_length(), interpreter.current_scope(), is_generator(), is_strict_mode() || interpreter.vm().in_strict_mode(), is_arrow_function()); + return ScriptFunction::create(global_object, name(), body(), parameters(), function_length(), interpreter.current_scope(), kind(), is_strict_mode() || interpreter.vm().in_strict_mode(), is_arrow_function()); } Value ExpressionStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const @@ -1157,7 +1157,7 @@ void BindingPattern::dump(int indent) const void FunctionNode::dump(int indent, String const& class_name) const { print_indent(indent); - outln("{}{} '{}'", class_name, m_is_generator ? "*" : "", name()); + outln("{}{} '{}'", class_name, m_kind == FunctionKind::Generator ? "*" : "", name()); if (!m_parameters.is_empty()) { print_indent(indent + 1); outln("(Parameters)"); diff --git a/Userland/Libraries/LibJS/AST.h b/Userland/Libraries/LibJS/AST.h index a1dfab7f39..5f459fd987 100644 --- a/Userland/Libraries/LibJS/AST.h +++ b/Userland/Libraries/LibJS/AST.h @@ -26,6 +26,11 @@ class VariableDeclaration; class FunctionDeclaration; class Identifier; +enum class FunctionKind { + Generator, + Regular, +}; + template<class T, class... Args> static inline NonnullRefPtr<T> create_ast_node(SourceRange range, Args&&... args) @@ -228,16 +233,16 @@ public: i32 function_length() const { return m_function_length; } bool is_strict_mode() const { return m_is_strict_mode; } bool is_arrow_function() const { return m_is_arrow_function; } - bool is_generator() const { return m_is_generator; } + FunctionKind kind() const { return m_kind; } protected: - FunctionNode(FlyString const& name, NonnullRefPtr<Statement> body, Vector<Parameter> parameters, i32 function_length, NonnullRefPtrVector<VariableDeclaration> variables, bool is_generator, bool is_strict_mode, bool is_arrow_function) + FunctionNode(FlyString const& name, NonnullRefPtr<Statement> body, Vector<Parameter> parameters, i32 function_length, NonnullRefPtrVector<VariableDeclaration> variables, FunctionKind kind, bool is_strict_mode, bool is_arrow_function) : m_name(name) , m_body(move(body)) , m_parameters(move(parameters)) , m_variables(move(variables)) , m_function_length(function_length) - , m_is_generator(is_generator) + , m_kind(kind) , m_is_strict_mode(is_strict_mode) , m_is_arrow_function(is_arrow_function) { @@ -260,7 +265,7 @@ private: Vector<Parameter> const m_parameters; NonnullRefPtrVector<VariableDeclaration> m_variables; const i32 m_function_length; - bool m_is_generator; + FunctionKind m_kind; bool m_is_strict_mode; bool m_is_arrow_function { false }; }; @@ -271,9 +276,9 @@ class FunctionDeclaration final public: static bool must_have_name() { return true; } - FunctionDeclaration(SourceRange source_range, FlyString const& name, NonnullRefPtr<Statement> body, Vector<Parameter> parameters, i32 function_length, NonnullRefPtrVector<VariableDeclaration> variables, bool is_generator, bool is_strict_mode = false) + FunctionDeclaration(SourceRange source_range, FlyString const& name, NonnullRefPtr<Statement> body, Vector<Parameter> parameters, i32 function_length, NonnullRefPtrVector<VariableDeclaration> variables, FunctionKind kind, bool is_strict_mode = false) : Declaration(move(source_range)) - , FunctionNode(name, move(body), move(parameters), function_length, move(variables), is_generator, is_strict_mode, false) + , FunctionNode(name, move(body), move(parameters), function_length, move(variables), kind, is_strict_mode, false) { } @@ -288,9 +293,9 @@ class FunctionExpression final public: static bool must_have_name() { return false; } - FunctionExpression(SourceRange source_range, FlyString const& name, NonnullRefPtr<Statement> body, Vector<Parameter> parameters, i32 function_length, NonnullRefPtrVector<VariableDeclaration> variables, bool is_generator, bool is_strict_mode, bool is_arrow_function = false) + FunctionExpression(SourceRange source_range, FlyString const& name, NonnullRefPtr<Statement> body, Vector<Parameter> parameters, i32 function_length, NonnullRefPtrVector<VariableDeclaration> variables, FunctionKind kind, bool is_strict_mode, bool is_arrow_function = false) : Expression(source_range) - , FunctionNode(name, move(body), move(parameters), function_length, move(variables), is_generator, is_strict_mode, is_arrow_function) + , FunctionNode(name, move(body), move(parameters), function_length, move(variables), kind, is_strict_mode, is_arrow_function) { } diff --git a/Userland/Libraries/LibJS/Bytecode/Op.cpp b/Userland/Libraries/LibJS/Bytecode/Op.cpp index 3776197f58..4009f539f8 100644 --- a/Userland/Libraries/LibJS/Bytecode/Op.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Op.cpp @@ -218,7 +218,7 @@ void Call::execute(Bytecode::Interpreter& interpreter) const void NewFunction::execute(Bytecode::Interpreter& interpreter) const { auto& vm = interpreter.vm(); - interpreter.accumulator() = ScriptFunction::create(interpreter.global_object(), m_function_node.name(), m_function_node.body(), m_function_node.parameters(), m_function_node.function_length(), vm.current_scope(), m_function_node.is_generator(), m_function_node.is_strict_mode(), m_function_node.is_arrow_function()); + interpreter.accumulator() = ScriptFunction::create(interpreter.global_object(), m_function_node.name(), m_function_node.body(), m_function_node.parameters(), m_function_node.function_length(), vm.current_scope(), m_function_node.kind(), m_function_node.is_strict_mode(), m_function_node.is_arrow_function()); } void Return::execute(Bytecode::Interpreter& interpreter) const diff --git a/Userland/Libraries/LibJS/Interpreter.cpp b/Userland/Libraries/LibJS/Interpreter.cpp index 2b383987a3..1c6ef8f73a 100644 --- a/Userland/Libraries/LibJS/Interpreter.cpp +++ b/Userland/Libraries/LibJS/Interpreter.cpp @@ -79,7 +79,7 @@ void Interpreter::enter_scope(const ScopeNode& scope_node, ScopeType scope_type, { ScopeGuard guard([&] { for (auto& declaration : scope_node.functions()) { - auto* function = ScriptFunction::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), current_scope(), declaration.is_generator(), declaration.is_strict_mode()); + auto* function = ScriptFunction::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), current_scope(), declaration.kind(), declaration.is_strict_mode()); vm().set_variable(declaration.name(), function, global_object); } }); diff --git a/Userland/Libraries/LibJS/Parser.cpp b/Userland/Libraries/LibJS/Parser.cpp index cd208fae19..9e6e6358b8 100644 --- a/Userland/Libraries/LibJS/Parser.cpp +++ b/Userland/Libraries/LibJS/Parser.cpp @@ -415,7 +415,9 @@ RefPtr<FunctionExpression> Parser::try_parse_arrow_function_expression(bool expe state_rollback_guard.disarm(); discard_saved_state(); auto body = function_body_result.release_nonnull(); - return create_ast_node<FunctionExpression>({ m_parser_state.m_current_token.filename(), rule_start.position(), position() }, "", move(body), move(parameters), function_length, m_parser_state.m_var_scopes.take_last(), false, is_strict, true); + return create_ast_node<FunctionExpression>( + { m_parser_state.m_current_token.filename(), rule_start.position(), position() }, "", move(body), + move(parameters), function_length, m_parser_state.m_var_scopes.take_last(), FunctionKind::Regular, is_strict, true); } return nullptr; @@ -586,13 +588,20 @@ NonnullRefPtr<ClassExpression> Parser::parse_class_expression(bool expect_class_ if (!super_class.is_null()) { // Set constructor to the result of parsing the source text // constructor(... args){ super (...args);} - auto super_call = create_ast_node<CallExpression>({ m_parser_state.m_current_token.filename(), rule_start.position(), position() }, create_ast_node<SuperExpression>({ m_parser_state.m_current_token.filename(), rule_start.position(), position() }), Vector { CallExpression::Argument { create_ast_node<Identifier>({ m_parser_state.m_current_token.filename(), rule_start.position(), position() }, "args"), true } }); + auto super_call = create_ast_node<CallExpression>( + { m_parser_state.m_current_token.filename(), rule_start.position(), position() }, + create_ast_node<SuperExpression>({ m_parser_state.m_current_token.filename(), rule_start.position(), position() }), + Vector { CallExpression::Argument { create_ast_node<Identifier>({ m_parser_state.m_current_token.filename(), rule_start.position(), position() }, "args"), true } }); constructor_body->append(create_ast_node<ExpressionStatement>({ m_parser_state.m_current_token.filename(), rule_start.position(), position() }, move(super_call))); constructor_body->add_variables(m_parser_state.m_var_scopes.last()); - constructor = create_ast_node<FunctionExpression>({ m_parser_state.m_current_token.filename(), rule_start.position(), position() }, class_name, move(constructor_body), Vector { FunctionNode::Parameter { FlyString { "args" }, nullptr, true } }, 0, NonnullRefPtrVector<VariableDeclaration>(), false, true); + constructor = create_ast_node<FunctionExpression>( + { m_parser_state.m_current_token.filename(), rule_start.position(), position() }, class_name, move(constructor_body), + Vector { FunctionNode::Parameter { FlyString { "args" }, nullptr, true } }, 0, NonnullRefPtrVector<VariableDeclaration>(), FunctionKind::Regular, true); } else { - constructor = create_ast_node<FunctionExpression>({ m_parser_state.m_current_token.filename(), rule_start.position(), position() }, class_name, move(constructor_body), Vector<FunctionNode::Parameter> {}, 0, NonnullRefPtrVector<VariableDeclaration>(), false, true); + constructor = create_ast_node<FunctionExpression>( + { m_parser_state.m_current_token.filename(), rule_start.position(), position() }, class_name, move(constructor_body), + Vector<FunctionNode::Parameter> {}, 0, NonnullRefPtrVector<VariableDeclaration>(), FunctionKind::Regular, true); } } @@ -1381,7 +1390,10 @@ NonnullRefPtr<FunctionNodeType> Parser::parse_function_node(u8 parse_options) auto body = parse_block_statement(is_strict); body->add_variables(m_parser_state.m_var_scopes.last()); body->add_functions(m_parser_state.m_function_scopes.last()); - return create_ast_node<FunctionNodeType>({ m_parser_state.m_current_token.filename(), rule_start.position(), position() }, name, move(body), move(parameters), function_length, NonnullRefPtrVector<VariableDeclaration>(), is_generator, is_strict); + return create_ast_node<FunctionNodeType>( + { m_parser_state.m_current_token.filename(), rule_start.position(), position() }, + name, move(body), move(parameters), function_length, NonnullRefPtrVector<VariableDeclaration>(), + is_generator ? FunctionKind::Generator : FunctionKind::Regular, is_strict); } Vector<FunctionNode::Parameter> Parser::parse_formal_parameters(int& function_length, u8 parse_options) diff --git a/Userland/Libraries/LibJS/Runtime/ScriptFunction.cpp b/Userland/Libraries/LibJS/Runtime/ScriptFunction.cpp index d91f65bc3a..d69cf52578 100644 --- a/Userland/Libraries/LibJS/Runtime/ScriptFunction.cpp +++ b/Userland/Libraries/LibJS/Runtime/ScriptFunction.cpp @@ -33,19 +33,19 @@ static ScriptFunction* typed_this(VM& vm, GlobalObject& global_object) return static_cast<ScriptFunction*>(this_object); } -ScriptFunction* ScriptFunction::create(GlobalObject& global_object, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, ScopeObject* parent_scope, bool is_generator, bool is_strict, bool is_arrow_function) +ScriptFunction* ScriptFunction::create(GlobalObject& global_object, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, ScopeObject* parent_scope, FunctionKind kind, bool is_strict, bool is_arrow_function) { - return global_object.heap().allocate<ScriptFunction>(global_object, global_object, name, body, move(parameters), m_function_length, parent_scope, *global_object.function_prototype(), is_generator, is_strict, is_arrow_function); + return global_object.heap().allocate<ScriptFunction>(global_object, global_object, name, body, move(parameters), m_function_length, parent_scope, *global_object.function_prototype(), kind, is_strict, is_arrow_function); } -ScriptFunction::ScriptFunction(GlobalObject& global_object, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, ScopeObject* parent_scope, Object& prototype, bool is_generator, bool is_strict, bool is_arrow_function) +ScriptFunction::ScriptFunction(GlobalObject& global_object, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, ScopeObject* parent_scope, Object& prototype, FunctionKind kind, bool is_strict, bool is_arrow_function) : Function(prototype, is_arrow_function ? vm().this_value(global_object) : Value(), {}) , m_name(name) , m_body(body) , m_parameters(move(parameters)) , m_parent_scope(parent_scope) , m_function_length(m_function_length) - , m_is_generator(is_generator) + , m_kind(kind) , m_is_strict(is_strict) , m_is_arrow_function(is_arrow_function) { @@ -155,7 +155,7 @@ Value ScriptFunction::execute_function_body() if (bytecode_interpreter) { prepare_arguments(); if (!m_bytecode_executable.has_value()) { - m_bytecode_executable = Bytecode::Generator::generate(m_body, m_is_generator); + m_bytecode_executable = Bytecode::Generator::generate(m_body, m_kind == FunctionKind::Generator); if constexpr (JS_BYTECODE_DEBUG) { dbgln("Compiled Bytecode::Block for function '{}':", m_name); for (auto& block : m_bytecode_executable->basic_blocks) @@ -163,12 +163,12 @@ Value ScriptFunction::execute_function_body() } } auto result = bytecode_interpreter->run(*m_bytecode_executable); - if (!m_is_generator) + if (m_kind != FunctionKind::Generator) return result; return GeneratorObject::create(global_object(), result, this, vm.call_frame().scope, bytecode_interpreter->snapshot_frame()); } else { - VERIFY(!m_is_generator); + VERIFY(m_kind != FunctionKind::Generator); OwnPtr<Interpreter> local_interpreter; ast_interpreter = vm.interpreter_if_exists(); diff --git a/Userland/Libraries/LibJS/Runtime/ScriptFunction.h b/Userland/Libraries/LibJS/Runtime/ScriptFunction.h index fa93085e5d..f4d1f892f1 100644 --- a/Userland/Libraries/LibJS/Runtime/ScriptFunction.h +++ b/Userland/Libraries/LibJS/Runtime/ScriptFunction.h @@ -16,9 +16,9 @@ class ScriptFunction final : public Function { JS_OBJECT(ScriptFunction, Function); public: - static ScriptFunction* create(GlobalObject&, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, ScopeObject* parent_scope, bool is_generator, bool is_strict, bool is_arrow_function = false); + static ScriptFunction* create(GlobalObject&, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, ScopeObject* parent_scope, FunctionKind, bool is_strict, bool is_arrow_function = false); - ScriptFunction(GlobalObject&, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, ScopeObject* parent_scope, Object& prototype, bool is_generator, bool is_strict, bool is_arrow_function = false); + ScriptFunction(GlobalObject&, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, ScopeObject* parent_scope, Object& prototype, FunctionKind, bool is_strict, bool is_arrow_function = false); virtual void initialize(GlobalObject&) override; virtual ~ScriptFunction(); @@ -53,7 +53,7 @@ private: Optional<Bytecode::Executable> m_bytecode_executable; ScopeObject* m_parent_scope { nullptr }; i32 m_function_length { 0 }; - bool m_is_generator { false }; + FunctionKind m_kind { FunctionKind::Regular }; bool m_is_strict { false }; bool m_is_arrow_function { false }; bool m_is_class_constructor { false }; diff --git a/Userland/Libraries/LibWeb/HTML/GlobalEventHandlers.cpp b/Userland/Libraries/LibWeb/HTML/GlobalEventHandlers.cpp index 320090d4e2..8a6006c028 100644 --- a/Userland/Libraries/LibWeb/HTML/GlobalEventHandlers.cpp +++ b/Userland/Libraries/LibWeb/HTML/GlobalEventHandlers.cpp @@ -49,7 +49,7 @@ void GlobalEventHandlers::set_event_handler_attribute(const FlyString& name, HTM dbgln("Failed to parse script in event handler attribute '{}'", name); return; } - auto* function = JS::ScriptFunction::create(self.script_execution_context()->interpreter().global_object(), name, program->body(), program->parameters(), program->function_length(), nullptr, false, false, false); + auto* function = JS::ScriptFunction::create(self.script_execution_context()->interpreter().global_object(), name, program->body(), program->parameters(), program->function_length(), nullptr, JS::FunctionKind::Regular, false, false); VERIFY(function); listener = adopt_ref(*new DOM::EventListener(JS::make_handle(static_cast<JS::Function*>(function)))); } diff --git a/Userland/Libraries/LibWeb/HTML/WebSocket.cpp b/Userland/Libraries/LibWeb/HTML/WebSocket.cpp index 3c5b524367..ae3eff091a 100644 --- a/Userland/Libraries/LibWeb/HTML/WebSocket.cpp +++ b/Userland/Libraries/LibWeb/HTML/WebSocket.cpp @@ -246,7 +246,7 @@ void WebSocket::set_event_handler_attribute(const FlyString& name, HTML::EventHa dbgln("Failed to parse script in event handler attribute '{}'", name); return; } - auto* function = JS::ScriptFunction::create(script_execution_context()->interpreter().global_object(), name, program->body(), program->parameters(), program->function_length(), nullptr, false, false, false); + auto* function = JS::ScriptFunction::create(script_execution_context()->interpreter().global_object(), name, program->body(), program->parameters(), program->function_length(), nullptr, JS::FunctionKind::Regular, false, false); VERIFY(function); listener = adopt_ref(*new DOM::EventListener(JS::make_handle(static_cast<JS::Function*>(function)))); } |