ladybird/Userland/Libraries/LibJS/Runtime/Environment.h
davidot 830ea0414c LibJS: Make scoping follow the spec
Before this we used an ad-hoc combination of references and 'variables'
stored in a hashmap. This worked in most cases but is not spec like.
Additionally hoisting, dynamically naming functions and scope analysis
was not done properly.

This patch fixes all of that by:
  - Implement BindingInitialization for destructuring assignment.
  - Implementing a new ScopePusher which tracks the lexical and var
    scoped declarations. This hoists functions to the top level if no
    lexical declaration name overlaps. Furthermore we do checking of
    redeclarations in the ScopePusher now requiring less checks all over
    the place.
  - Add methods for parsing the directives and statement lists instead
    of having that code duplicated in multiple places. This allows
    declarations to pushed to the appropriate scope more easily.
  - Remove the non spec way of storing 'variables' in
    DeclarativeEnvironment and make Reference follow the spec instead of
    checking both the bindings and 'variables'.
  - Remove all scoping related things from the Interpreter. And instead
    use environments as specified by the spec. This also includes fixing
    that NativeFunctions did not produce a valid FunctionEnvironment
    which could cause issues with callbacks and eval. All
    FunctionObjects now have a valid NewFunctionEnvironment
    implementation.
  - Remove execute_statements from Interpreter and instead use
    ASTNode::execute everywhere this simplifies AST.cpp as you no longer
    need to worry about which method to call.
  - Make ScopeNodes setup their own environment. This uses four
    different methods specified by the spec
    {Block, Function, Eval, Global}DeclarationInstantiation with the
    annexB extensions.
  - Implement and use NamedEvaluation where specified.

Additionally there are fixes to things exposed by these changes to eval,
{for, for-in, for-of} loops and assignment.

Finally it also fixes some tests in test-js which where passing before
but not now that we have correct behavior :^).
2021-09-30 08:16:32 +01:00

68 lines
2.5 KiB
C++

/*
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/Object.h>
namespace JS {
struct Variable {
Value value;
DeclarationKind declaration_kind;
};
#define JS_ENVIRONMENT(class_, base_class) \
public: \
using Base = base_class; \
virtual char const* class_name() const override { return #class_; }
class Environment : public Cell {
public:
GlobalObject& global_object() { return *m_global_object; }
GlobalObject const& global_object() const { return *m_global_object; }
virtual void initialize(GlobalObject&) override;
virtual bool has_this_binding() const { return false; }
virtual Value get_this_binding(GlobalObject&) const { return {}; }
virtual Object* with_base_object() const { return nullptr; }
virtual bool has_binding([[maybe_unused]] FlyString const& name) const { return false; }
virtual void create_mutable_binding(GlobalObject&, [[maybe_unused]] FlyString const& name, [[maybe_unused]] bool can_be_deleted) { }
virtual void create_immutable_binding(GlobalObject&, [[maybe_unused]] FlyString const& name, [[maybe_unused]] bool strict) { }
virtual void initialize_binding(GlobalObject&, [[maybe_unused]] FlyString const& name, Value) { }
virtual void set_mutable_binding(GlobalObject&, [[maybe_unused]] FlyString const& name, Value, [[maybe_unused]] bool strict) { }
virtual Value get_binding_value(GlobalObject&, [[maybe_unused]] FlyString const& name, [[maybe_unused]] bool strict) { return {}; }
virtual bool delete_binding(GlobalObject&, [[maybe_unused]] FlyString const& name) { return false; }
// [[OuterEnv]]
Environment* outer_environment() { return m_outer_environment; }
Environment const* outer_environment() const { return m_outer_environment; }
virtual bool is_global_environment() const { return false; }
virtual bool is_declarative_environment() const { return false; }
virtual bool is_function_environment() const { return false; }
template<typename T>
bool fast_is() const = delete;
virtual char const* class_name() const override { return "Environment"; }
protected:
explicit Environment(Environment* parent);
virtual void visit_edges(Visitor&) override;
private:
virtual bool is_environment() const final { return true; }
GlobalObject* m_global_object { nullptr };
Environment* m_outer_environment { nullptr };
};
}