ladybird/Userland/Libraries/LibJS/Runtime/FunctionEnvironment.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

67 lines
2.2 KiB
C++

/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/DeclarativeEnvironment.h>
#include <LibJS/Runtime/ECMAScriptFunctionObject.h>
namespace JS {
class FunctionEnvironment final : public DeclarativeEnvironment {
JS_ENVIRONMENT(FunctionEnvironment, DeclarativeEnvironment);
public:
enum class ThisBindingStatus : u8 {
Lexical,
Initialized,
Uninitialized,
};
explicit FunctionEnvironment(Environment* parent_scope);
virtual ~FunctionEnvironment() override;
// [[ThisValue]]
Value this_value() const { return m_this_value; }
void set_this_value(Value value) { m_this_value = value; }
// Not a standard operation.
void replace_this_binding(Value this_value) { m_this_value = this_value; }
// [[ThisBindingStatus]]
ThisBindingStatus this_binding_status() const { return m_this_binding_status; }
void set_this_binding_status(ThisBindingStatus status) { m_this_binding_status = status; }
// [[FunctionObject]]
ECMAScriptFunctionObject& function_object() { return *m_function_object; }
ECMAScriptFunctionObject const& function_object() const { return *m_function_object; }
void set_function_object(ECMAScriptFunctionObject& function) { m_function_object = &function; }
// [[NewTarget]]
Value new_target() const { return m_new_target; }
void set_new_target(Value new_target) { m_new_target = new_target; }
// Abstract operations
Value get_super_base() const;
bool has_super_binding() const;
virtual bool has_this_binding() const override;
virtual Value get_this_binding(GlobalObject&) const override;
Value bind_this_value(GlobalObject&, Value);
private:
virtual bool is_function_environment() const override { return true; }
virtual void visit_edges(Visitor&) override;
Value m_this_value;
ThisBindingStatus m_this_binding_status { ThisBindingStatus::Uninitialized };
ECMAScriptFunctionObject* m_function_object { nullptr };
Value m_new_target;
};
template<>
inline bool Environment::fast_is<FunctionEnvironment>() const { return is_function_environment(); }
}