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

134 lines
3.6 KiB
C++

/*
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/String.h>
#include <LibJS/Runtime/Environment.h>
#include <LibJS/Runtime/PropertyName.h>
#include <LibJS/Runtime/Value.h>
namespace JS {
class Reference {
public:
enum class BaseType : u8 {
Unresolvable,
Value,
Environment,
};
Reference() { }
Reference(BaseType type, PropertyName name, bool strict)
: m_base_type(type)
, m_name(move(name))
, m_strict(strict)
{
}
Reference(Value base, PropertyName name, Value this_value, bool strict = false)
: m_base_type(BaseType::Value)
, m_base_value(base)
, m_name(move(name))
, m_this_value(this_value)
, m_strict(strict)
{
if (base.is_nullish()) {
m_base_type = BaseType::Unresolvable;
m_base_value = {};
m_this_value = {};
m_name = {};
}
}
Reference(Environment& base, FlyString referenced_name, bool strict = false)
: m_base_type(BaseType::Environment)
, m_base_environment(&base)
, m_name(move(referenced_name))
, m_strict(strict)
{
}
Value base() const
{
VERIFY(m_base_type == BaseType::Value);
return m_base_value;
}
Environment& base_environment() const
{
VERIFY(m_base_type == BaseType::Environment);
return *m_base_environment;
}
PropertyName const& name() const { return m_name; }
bool is_strict() const { return m_strict; }
// 6.2.4.2 IsUnresolvableReference ( V ), https://tc39.es/ecma262/#sec-isunresolvablereference
bool is_unresolvable() const { return m_base_type == BaseType::Unresolvable; }
// 6.2.4.1 IsPropertyReference ( V ), https://tc39.es/ecma262/#sec-ispropertyreference
bool is_property_reference() const
{
if (is_unresolvable())
return false;
if (m_base_type == BaseType::Environment)
return false;
if (m_base_value.is_boolean() || m_base_value.is_string() || m_base_value.is_symbol() || m_base_value.is_bigint() || m_base_value.is_number() || m_base_value.is_object())
return true;
return false;
}
// 6.2.4.7 GetThisValue ( V ), https://tc39.es/ecma262/#sec-getthisvalue
Value get_this_value() const
{
VERIFY(is_property_reference());
if (is_super_reference())
return m_this_value;
return m_base_value;
}
// 6.2.4.3 IsSuperReference ( V ), https://tc39.es/ecma262/#sec-issuperreference
bool is_super_reference() const
{
return !m_this_value.is_empty();
}
// Note: Non-standard helper.
bool is_environment_reference() const
{
return m_base_type == BaseType::Environment;
}
void initialize_referenced_binding(GlobalObject& global_object, Value value) const
{
VERIFY(!is_unresolvable());
VERIFY(m_base_type == BaseType::Environment);
m_base_environment->initialize_binding(global_object, m_name.as_string(), value);
}
void put_value(GlobalObject&, Value);
Value get_value(GlobalObject&) const;
bool delete_(GlobalObject&);
String to_string() const;
bool is_valid_reference() const { return m_name.is_valid(); }
private:
void throw_reference_error(GlobalObject&) const;
BaseType m_base_type { BaseType::Unresolvable };
union {
Value m_base_value {};
Environment* m_base_environment;
};
PropertyName m_name;
Value m_this_value;
bool m_strict { false };
};
}