mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 01:47:34 +00:00
LibJS: Bring Reference records a bit closer to the ECMAScript spec
Our Reference class now has the same fields as the spec: - Base (a non-nullish value, an environment record, or `unresolvable`) - Referenced Name (the name of the binding) - Strict (whether the reference originated in strict mode code) - ThisValue (if non-empty, the reference represents a `super` keyword) The main difference from before is that we now resolve the environment record that a reference interacts with. Previously we simply resolved to either "local variable" or "global variable". The associated abstract operations are still largely non-conforming, since we don't yet implement proper variable bindings. But this patch should at least fix a handful of test262 cases. :^) There's one minor regression: some TypeError message strings get a little worse due to doing a RequireObjectCoercible earlier in the evaluation of MemberExpression.
This commit is contained in:
parent
6e1932e8b2
commit
bce7fdba81
6 changed files with 193 additions and 123 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -7,6 +7,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <LibJS/Runtime/EnvironmentRecord.h>
|
||||
#include <LibJS/Runtime/PropertyName.h>
|
||||
#include <LibJS/Runtime/Value.h>
|
||||
|
||||
|
@ -14,69 +15,105 @@ namespace JS {
|
|||
|
||||
class Reference {
|
||||
public:
|
||||
enum class BaseType : u8 {
|
||||
Unresolvable,
|
||||
Value,
|
||||
EnvironmentRecord,
|
||||
};
|
||||
|
||||
Reference() { }
|
||||
Reference(Value base, const PropertyName& name, bool strict = false)
|
||||
: m_base(base)
|
||||
Reference(BaseType type, PropertyName const& name, bool strict)
|
||||
: m_base_type(type)
|
||||
, m_name(name)
|
||||
, m_strict(strict)
|
||||
{
|
||||
}
|
||||
|
||||
enum LocalVariableTag { LocalVariable };
|
||||
Reference(LocalVariableTag, const FlyString& name, bool strict = false)
|
||||
: m_base(js_null())
|
||||
Reference(Value base, PropertyName const& name, Value this_value, bool strict = false)
|
||||
: m_base_type(BaseType::Value)
|
||||
, m_base_value(base)
|
||||
, m_name(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(EnvironmentRecord& base, FlyString const& referenced_name, bool strict = false)
|
||||
: m_base_type(BaseType::EnvironmentRecord)
|
||||
, m_base_environment_record(&base)
|
||||
, m_name(referenced_name)
|
||||
, m_strict(strict)
|
||||
, m_local_variable(true)
|
||||
{
|
||||
}
|
||||
|
||||
enum GlobalVariableTag { GlobalVariable };
|
||||
Reference(GlobalVariableTag, const FlyString& name, bool strict = false)
|
||||
: m_base(js_null())
|
||||
, m_name(name)
|
||||
, m_strict(strict)
|
||||
, m_global_variable(true)
|
||||
Value base() const
|
||||
{
|
||||
VERIFY(m_base_type == BaseType::Value);
|
||||
return m_base_value;
|
||||
}
|
||||
|
||||
Value base() const { return m_base; }
|
||||
const PropertyName& name() const { return m_name; }
|
||||
EnvironmentRecord& base_environment() const
|
||||
{
|
||||
VERIFY(m_base_type == BaseType::EnvironmentRecord);
|
||||
return *m_base_environment_record;
|
||||
}
|
||||
|
||||
PropertyName const& name() const { return m_name; }
|
||||
bool is_strict() const { return m_strict; }
|
||||
|
||||
bool is_unresolvable() const { return m_base.is_empty(); }
|
||||
bool is_property() const
|
||||
// 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
|
||||
{
|
||||
return m_base.is_object() || has_primitive_base();
|
||||
if (is_unresolvable())
|
||||
return false;
|
||||
if (m_base_type == BaseType::EnvironmentRecord)
|
||||
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;
|
||||
}
|
||||
|
||||
bool has_primitive_base() const
|
||||
// 6.2.4.7 GetThisValue ( V ), https://tc39.es/ecma262/#sec-getthisvalue
|
||||
Value get_this_value() const
|
||||
{
|
||||
return m_base.is_boolean() || m_base.is_string() || m_base.is_number();
|
||||
VERIFY(is_property_reference());
|
||||
if (is_super_reference())
|
||||
return m_this_value;
|
||||
return m_base_value;
|
||||
}
|
||||
|
||||
bool is_local_variable() const
|
||||
// 6.2.4.3 IsSuperReference ( V ), https://tc39.es/ecma262/#sec-issuperreference
|
||||
bool is_super_reference() const
|
||||
{
|
||||
return m_local_variable;
|
||||
}
|
||||
|
||||
bool is_global_variable() const
|
||||
{
|
||||
return m_global_variable;
|
||||
return !m_this_value.is_empty();
|
||||
}
|
||||
|
||||
void put(GlobalObject&, Value);
|
||||
Value get(GlobalObject&);
|
||||
Value get(GlobalObject&, bool throw_if_undefined = true);
|
||||
bool delete_(GlobalObject&);
|
||||
|
||||
String to_string() const;
|
||||
|
||||
private:
|
||||
void throw_reference_error(GlobalObject&);
|
||||
|
||||
Value m_base;
|
||||
BaseType m_base_type { BaseType::Unresolvable };
|
||||
union {
|
||||
Value m_base_value;
|
||||
EnvironmentRecord* m_base_environment_record;
|
||||
};
|
||||
PropertyName m_name;
|
||||
Value m_this_value;
|
||||
bool m_strict { false };
|
||||
bool m_local_variable { false };
|
||||
bool m_global_variable { false };
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue