From 6e0e8a824296558b2d2eb9a18084820c8178eab8 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 14 Jun 2021 10:52:15 +0200 Subject: [PATCH] LibJS: Teach Reference to access call frame arguments directly --- Userland/Libraries/LibJS/AST.cpp | 2 ++ Userland/Libraries/LibJS/Runtime/Reference.cpp | 8 ++++++++ Userland/Libraries/LibJS/Runtime/Reference.h | 10 ++++++++++ 3 files changed, 20 insertions(+) diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp index 669968a1ad..8a8908ed2c 100644 --- a/Userland/Libraries/LibJS/AST.cpp +++ b/Userland/Libraries/LibJS/AST.cpp @@ -668,6 +668,8 @@ Reference Expression::to_reference(Interpreter&, GlobalObject&) const Reference Identifier::to_reference(Interpreter& interpreter, GlobalObject&) const { + if (m_argument_index.has_value()) + return Reference(Reference::CallFrameArgument, m_argument_index.value(), string()); return interpreter.vm().get_reference(string()); } diff --git a/Userland/Libraries/LibJS/Runtime/Reference.cpp b/Userland/Libraries/LibJS/Runtime/Reference.cpp index 2bc6b466f8..76ccbb042e 100644 --- a/Userland/Libraries/LibJS/Runtime/Reference.cpp +++ b/Userland/Libraries/LibJS/Runtime/Reference.cpp @@ -19,6 +19,11 @@ void Reference::put(GlobalObject& global_object, Value value) return; } + if (m_call_frame_argument_index.has_value()) { + global_object.vm().call_frame().arguments[m_call_frame_argument_index.value()] = value; + return; + } + if (is_local_variable() || is_global_variable()) { if (is_local_variable()) vm.set_variable(m_name.to_string(), value, global_object); @@ -68,6 +73,9 @@ Value Reference::get(GlobalObject& global_object) return {}; } + if (m_call_frame_argument_index.has_value()) + return global_object.vm().argument(m_call_frame_argument_index.value()); + if (is_local_variable() || is_global_variable()) { Value value; if (is_local_variable()) diff --git a/Userland/Libraries/LibJS/Runtime/Reference.h b/Userland/Libraries/LibJS/Runtime/Reference.h index 864d7855c4..337d852623 100644 --- a/Userland/Libraries/LibJS/Runtime/Reference.h +++ b/Userland/Libraries/LibJS/Runtime/Reference.h @@ -40,6 +40,15 @@ public: { } + enum CallFrameArgumentTag { CallFrameArgument }; + Reference(CallFrameArgumentTag, size_t index, FlyString const& name) + : m_base(js_null()) + , m_name(name) + , m_call_frame_argument_index(index) + , m_local_variable(true) + { + } + Value base() const { return m_base; } const PropertyName& name() const { return m_name; } bool is_strict() const { return m_strict; } @@ -74,6 +83,7 @@ private: Value m_base; PropertyName m_name; + Optional m_call_frame_argument_index; bool m_strict { false }; bool m_local_variable { false }; bool m_global_variable { false };