From 9de6443ab7f3cb5ccf79d33d071cb57c9919b113 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 28 Nov 2020 16:14:26 +0100 Subject: [PATCH] LibJS: Add basic support for "with" statements with statements evaluate an expression and put the result of it at the "front" of the scope chain. This is implemented by creating a WithScope object and placing it in front of the VM's current call frame's scope. --- Libraries/LibJS/AST.cpp | 19 +++++++- Libraries/LibJS/CMakeLists.txt | 1 + Libraries/LibJS/Runtime/WithScope.cpp | 67 +++++++++++++++++++++++++++ Libraries/LibJS/Runtime/WithScope.h | 50 ++++++++++++++++++++ 4 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 Libraries/LibJS/Runtime/WithScope.cpp create mode 100644 Libraries/LibJS/Runtime/WithScope.h diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index 4b4e646af4..a78ce9192f 100644 --- a/Libraries/LibJS/AST.cpp +++ b/Libraries/LibJS/AST.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -46,6 +47,7 @@ #include #include #include +#include #include namespace JS { @@ -255,9 +257,22 @@ Value IfStatement::execute(Interpreter& interpreter, GlobalObject& global_object return js_undefined(); } -Value WithStatement::execute(Interpreter&, GlobalObject&) const +Value WithStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const { - ASSERT_NOT_REACHED(); + auto object_value = m_object->execute(interpreter, global_object); + if (interpreter.exception()) + return {}; + + auto* object = object_value.to_object(global_object); + if (interpreter.exception()) + return {}; + + ASSERT(object); + + auto* with_scope = interpreter.heap().allocate(global_object, *object, interpreter.vm().call_frame().scope); + TemporaryChange scope_change(interpreter.vm().call_frame().scope, with_scope); + interpreter.execute_statement(global_object, m_body); + return {}; } Value WhileStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const diff --git a/Libraries/LibJS/CMakeLists.txt b/Libraries/LibJS/CMakeLists.txt index 39efe3281d..90b336df9d 100644 --- a/Libraries/LibJS/CMakeLists.txt +++ b/Libraries/LibJS/CMakeLists.txt @@ -76,6 +76,7 @@ set(SOURCES Runtime/Uint8ClampedArray.cpp Runtime/VM.cpp Runtime/Value.cpp + Runtime/WithScope.cpp Token.cpp ) diff --git a/Libraries/LibJS/Runtime/WithScope.cpp b/Libraries/LibJS/Runtime/WithScope.cpp new file mode 100644 index 0000000000..8afb608bf3 --- /dev/null +++ b/Libraries/LibJS/Runtime/WithScope.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2020, Andreas Kling + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include + +namespace JS { + +WithScope::WithScope(Object& object, ScopeObject* parent_scope) + : ScopeObject(parent_scope) + , m_object(object) +{ +} + +void WithScope::visit_edges(Cell::Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(&m_object); +} + +Optional WithScope::get_from_scope(const FlyString& name) const +{ + auto value = m_object.get(name); + if (value.is_empty()) + return {}; + return Variable { value, DeclarationKind::Var }; +} + +void WithScope::put_to_scope(const FlyString& name, Variable variable) +{ + m_object.put(name, variable.value); +} + +bool WithScope::has_this_binding() const +{ + return parent()->has_this_binding(); +} + +Value WithScope::get_this_binding(GlobalObject& global_object) const +{ + return parent()->get_this_binding(global_object); +} + +} diff --git a/Libraries/LibJS/Runtime/WithScope.h b/Libraries/LibJS/Runtime/WithScope.h new file mode 100644 index 0000000000..ef6072994d --- /dev/null +++ b/Libraries/LibJS/Runtime/WithScope.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2020, Andreas Kling + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include + +namespace JS { + +class WithScope : public ScopeObject { + JS_OBJECT(WithScope, ScopeObject); + +public: + WithScope(Object&, ScopeObject* parent_scope); + + virtual Optional get_from_scope(const FlyString&) const override; + virtual void put_to_scope(const FlyString&, Variable) override; + virtual bool has_this_binding() const override; + virtual Value get_this_binding(GlobalObject&) const override; + +private: + virtual void visit_edges(Visitor&) override; + + Object& m_object; +}; + +}