LibJS: Add [[GlobalThisValue]] internal slot to GlobalEnvironment

Instead of hardcoding the environment's global object as the return
value of GlobalEnvironment::global_this_value(), it now stores an Object
reference which is passed to the constructor for this purpose.

From the spec (https://tc39.es/ecma262/#sec-global-environment-records):

[[GlobalThisValue]] | Object | The value returned by this in global
scope. Hosts may provide any ECMAScript Object value.
This commit is contained in:
Linus Groh 2021-09-11 19:58:27 +01:00
parent 2b8d5696ab
commit 1e79934acf
Notes: sideshowbarker 2024-07-18 04:11:21 +09:00
4 changed files with 13 additions and 18 deletions

View file

@ -13,8 +13,10 @@
namespace JS {
GlobalEnvironment::GlobalEnvironment(GlobalObject& global_object)
// 9.1.2.5 NewGlobalEnvironment ( G, thisValue ), https://tc39.es/ecma262/#sec-newglobalenvironment
GlobalEnvironment::GlobalEnvironment(GlobalObject& global_object, Object& this_value)
: Environment(nullptr)
, m_global_this_value(&this_value)
{
m_object_record = global_object.heap().allocate<ObjectEnvironment>(global_object, global_object, ObjectEnvironment::IsWithEnvironment::No, nullptr);
m_declarative_record = global_object.heap().allocate<DeclarativeEnvironment>(global_object);
@ -24,6 +26,7 @@ void GlobalEnvironment::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_object_record);
visitor.visit(m_global_this_value);
visitor.visit(m_declarative_record);
}
@ -50,11 +53,6 @@ Value GlobalEnvironment::get_this_binding(GlobalObject&) const
return &global_object();
}
Value GlobalEnvironment::global_this_value() const
{
return &global_object();
}
// 9.1.1.4.1 HasBinding ( N ), https://tc39.es/ecma262/#sec-global-environment-records-hasbinding-n
bool GlobalEnvironment::has_binding(FlyString const& name) const
{

View file

@ -14,7 +14,7 @@ class GlobalEnvironment final : public Environment {
JS_ENVIRONMENT(GlobalEnvironment, Environment);
public:
explicit GlobalEnvironment(GlobalObject&);
GlobalEnvironment(GlobalObject&, Object& this_value);
virtual Optional<Variable> get_from_environment(FlyString const&) const override;
virtual bool put_into_environment(FlyString const&, Variable) override;
@ -30,12 +30,8 @@ public:
virtual Value get_binding_value(GlobalObject&, FlyString const& name, bool strict) override;
virtual bool delete_binding(GlobalObject&, FlyString const& name) override;
Value global_this_value() const;
// [[ObjectRecord]]
ObjectEnvironment& object_record() { return *m_object_record; }
// [[DeclarativeRecord]]
Object& global_this_value() { return *m_global_this_value; }
DeclarativeEnvironment& declarative_record() { return *m_declarative_record; }
bool has_var_declaration(FlyString const& name) const;
@ -50,12 +46,13 @@ private:
virtual bool is_global_environment() const override { return true; }
virtual void visit_edges(Visitor&) override;
ObjectEnvironment* m_object_record { nullptr };
DeclarativeEnvironment* m_declarative_record { nullptr };
Vector<FlyString> m_var_names;
ObjectEnvironment* m_object_record { nullptr }; // [[ObjectRecord]]
Object* m_global_this_value { nullptr }; // [[GlobalThisValue]]
DeclarativeEnvironment* m_declarative_record { nullptr }; // [[DeclarativeRecord]]
Vector<FlyString> m_var_names; // [[VarNames]]
};
template<>
inline bool Environment::fast_is<GlobalEnvironment>() const { return is_global_environment(); }
}

View file

@ -129,7 +129,7 @@ void GlobalObject::initialize_global_object()
m_object_prototype = heap().allocate_without_global_object<ObjectPrototype>(*this);
m_function_prototype = heap().allocate_without_global_object<FunctionPrototype>(*this);
m_environment = heap().allocate<GlobalEnvironment>(*this, *this);
m_environment = heap().allocate<GlobalEnvironment>(*this, *this, *this);
m_new_object_shape = vm.heap().allocate_without_global_object<Shape>(*this);
m_new_object_shape->set_prototype_without_transition(m_object_prototype);

View file

@ -647,7 +647,7 @@ void VM::ordinary_call_bind_this(FunctionObject& function, ExecutionContext& cal
this_value = this_argument;
} else if (this_argument.is_nullish()) {
auto& global_environment = callee_realm->environment();
this_value = global_environment.global_this_value();
this_value = &global_environment.global_this_value();
} else {
this_value = this_argument.to_object(function.global_object());
}