1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 08:57:47 +00:00

LibJS: Bring function environment records closer to the spec

This patch adds FunctionEnvironmentRecord as a subclass of the existing
DeclarativeEnvironmentRecord. Things that are specific to function
environment records move into there, simplifying the base.

Most of the abstract operations related to function environment records
are rewritten to match the spec exactly. I also had to implement
GetThisEnvironment() and GetSuperConstructor() to keep tests working
after the changes, so that's nice as well. :^)
This commit is contained in:
Andreas Kling 2021-06-22 13:30:48 +02:00
parent 6ed6434bab
commit aabd82d508
28 changed files with 228 additions and 159 deletions

View file

@ -13,16 +13,10 @@
namespace JS {
class DeclarativeEnvironmentRecord final : public EnvironmentRecord {
class DeclarativeEnvironmentRecord : public EnvironmentRecord {
JS_OBJECT(DeclarativeEnvironmentRecord, EnvironmentRecord);
public:
enum class ThisBindingStatus {
Lexical,
Initialized,
Uninitialized,
};
enum class EnvironmentRecordType {
Declarative,
Function,
@ -41,38 +35,19 @@ public:
virtual Optional<Variable> get_from_environment_record(FlyString const&) const override;
virtual void put_into_environment_record(FlyString const&, Variable) override;
virtual bool delete_from_environment_record(FlyString const&) override;
virtual bool has_this_binding() const override;
virtual Value get_this_binding(GlobalObject&) const override;
HashMap<FlyString, Variable> const& variables() const { return m_variables; }
Value get_super_base();
ThisBindingStatus this_binding_status() const { return m_this_binding_status; }
void bind_this_value(GlobalObject&, Value this_value);
// Not a standard operation.
void replace_this_binding(Value this_value) { m_this_value = this_value; }
Value new_target() const { return m_new_target; };
void set_new_target(Value new_target) { m_new_target = new_target; }
Function* current_function() const { return m_current_function; }
void set_current_function(Function& function) { m_current_function = &function; }
EnvironmentRecordType type() const { return m_environment_record_type; }
protected:
virtual void visit_edges(Visitor&) override;
private:
virtual bool is_declarative_environment_record() const override { return true; }
virtual void visit_edges(Visitor&) override;
EnvironmentRecordType m_environment_record_type : 8 { EnvironmentRecordType::Declarative };
ThisBindingStatus m_this_binding_status : 8 { ThisBindingStatus::Uninitialized };
HashMap<FlyString, Variable> m_variables;
Value m_this_value;
Value m_new_target;
// Corresponds to [[FunctionObject]]
Function* m_current_function { nullptr };
};
template<>