2021-06-22 11:30:48 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2021, Andreas Kling <andreas@ladybird.org>
|
2021-06-22 11:30:48 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-10-09 15:41:18 +00:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2021-07-01 10:24:46 +00:00
|
|
|
#include <LibJS/Runtime/DeclarativeEnvironment.h>
|
2021-06-22 11:30:48 +00:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2021-07-01 10:24:46 +00:00
|
|
|
class FunctionEnvironment final : public DeclarativeEnvironment {
|
|
|
|
JS_ENVIRONMENT(FunctionEnvironment, DeclarativeEnvironment);
|
2023-11-19 08:45:05 +00:00
|
|
|
JS_DECLARE_ALLOCATOR(FunctionEnvironment);
|
2021-06-22 11:30:48 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
enum class ThisBindingStatus : u8 {
|
|
|
|
Lexical,
|
|
|
|
Initialized,
|
|
|
|
Uninitialized,
|
|
|
|
};
|
|
|
|
|
2022-03-14 16:25:06 +00:00
|
|
|
virtual ~FunctionEnvironment() override = default;
|
2021-06-22 11:30:48 +00:00
|
|
|
|
|
|
|
ThisBindingStatus this_binding_status() const { return m_this_binding_status; }
|
|
|
|
void set_this_binding_status(ThisBindingStatus status) { m_this_binding_status = status; }
|
|
|
|
|
2021-09-24 21:49:24 +00:00
|
|
|
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; }
|
2021-06-22 11:30:48 +00:00
|
|
|
|
|
|
|
Value new_target() const { return m_new_target; }
|
2021-12-28 22:50:23 +00:00
|
|
|
void set_new_target(Value new_target)
|
|
|
|
{
|
|
|
|
VERIFY(!new_target.is_empty());
|
|
|
|
m_new_target = new_target;
|
|
|
|
}
|
2021-06-22 11:30:48 +00:00
|
|
|
|
|
|
|
// Abstract operations
|
2021-10-09 15:41:18 +00:00
|
|
|
ThrowCompletionOr<Value> get_super_base() const;
|
2021-06-22 11:30:48 +00:00
|
|
|
bool has_super_binding() const;
|
|
|
|
virtual bool has_this_binding() const override;
|
2022-08-21 14:12:43 +00:00
|
|
|
virtual ThrowCompletionOr<Value> get_this_binding(VM&) const override;
|
|
|
|
ThrowCompletionOr<Value> bind_this_value(VM&, Value);
|
2021-06-22 11:30:48 +00:00
|
|
|
|
|
|
|
private:
|
2022-08-28 21:51:28 +00:00
|
|
|
explicit FunctionEnvironment(Environment* parent_environment);
|
|
|
|
|
2021-07-01 10:24:46 +00:00
|
|
|
virtual bool is_function_environment() const override { return true; }
|
2021-06-22 11:30:48 +00:00
|
|
|
virtual void visit_edges(Visitor&) override;
|
|
|
|
|
2021-12-28 22:50:23 +00:00
|
|
|
Value m_this_value; // [[ThisValue]]
|
|
|
|
ThisBindingStatus m_this_binding_status { ThisBindingStatus::Uninitialized }; // [[ThisBindingStatus]]
|
2023-02-26 23:09:02 +00:00
|
|
|
GCPtr<ECMAScriptFunctionObject> m_function_object; // [[FunctionObject]]
|
2021-12-28 22:50:23 +00:00
|
|
|
Value m_new_target { js_undefined() }; // [[NewTarget]]
|
2021-06-22 11:30:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2021-07-01 10:24:46 +00:00
|
|
|
inline bool Environment::fast_is<FunctionEnvironment>() const { return is_function_environment(); }
|
2021-06-22 11:30:48 +00:00
|
|
|
|
|
|
|
}
|