1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 07:58:11 +00:00

LibJS: Rename scope to environment

This is an editorial change in the ECMA-262 spec.

See: 3246553
This commit is contained in:
Linus Groh 2022-05-01 01:10:05 +02:00
parent cb66474fb5
commit acda12597a
8 changed files with 43 additions and 43 deletions

View file

@ -292,22 +292,22 @@ Value FunctionExpression::instantiate_ordinary_function_expression(Interpreter&
auto has_own_name = !name().is_empty(); auto has_own_name = !name().is_empty();
auto const& used_name = has_own_name ? name() : given_name; auto const& used_name = has_own_name ? name() : given_name;
auto* scope = interpreter.lexical_environment(); auto* environment = interpreter.lexical_environment();
if (has_own_name) { if (has_own_name) {
VERIFY(scope); VERIFY(environment);
scope = new_declarative_environment(*scope); environment = new_declarative_environment(*environment);
MUST(scope->create_immutable_binding(global_object, name(), false)); MUST(environment->create_immutable_binding(global_object, name(), false));
} }
auto* private_scope = interpreter.vm().running_execution_context().private_environment; auto* private_environment = interpreter.vm().running_execution_context().private_environment;
auto closure = ECMAScriptFunctionObject::create(global_object, used_name, source_text(), body(), parameters(), function_length(), scope, private_scope, kind(), is_strict_mode(), might_need_arguments_object(), contains_direct_call_to_eval(), is_arrow_function()); auto closure = ECMAScriptFunctionObject::create(global_object, used_name, source_text(), body(), parameters(), function_length(), environment, private_environment, kind(), is_strict_mode(), might_need_arguments_object(), contains_direct_call_to_eval(), is_arrow_function());
// FIXME: 6. Perform SetFunctionName(closure, name). // FIXME: 6. Perform SetFunctionName(closure, name).
// FIXME: 7. Perform MakeConstructor(closure). // FIXME: 7. Perform MakeConstructor(closure).
if (has_own_name) if (has_own_name)
MUST(scope->initialize_binding(global_object, name(), closure)); MUST(environment->initialize_binding(global_object, name(), closure));
return closure; return closure;
} }
@ -1667,14 +1667,14 @@ ThrowCompletionOr<ClassElement::ClassValue> StaticInitializer::class_element_eva
// 1. Let lex be the running execution context's LexicalEnvironment. // 1. Let lex be the running execution context's LexicalEnvironment.
auto* lexical_environment = interpreter.vm().running_execution_context().lexical_environment; auto* lexical_environment = interpreter.vm().running_execution_context().lexical_environment;
// 2. Let privateScope be the running execution context's PrivateEnvironment. // 2. Let privateEnv be the running execution context's PrivateEnvironment.
auto* private_scope = interpreter.vm().running_execution_context().private_environment; auto* private_environment = interpreter.vm().running_execution_context().private_environment;
// 3. Let sourceText be the empty sequence of Unicode code points. // 3. Let sourceText be the empty sequence of Unicode code points.
// 4. Let formalParameters be an instance of the production FormalParameters : [empty] . // 4. Let formalParameters be an instance of the production FormalParameters : [empty] .
// 5. Let bodyFunction be OrdinaryFunctionCreate(%Function.prototype%, sourceText, formalParameters, ClassStaticBlockBody, non-lexical-this, lex, privateScope). // 5. Let bodyFunction be OrdinaryFunctionCreate(%Function.prototype%, sourceText, formalParameters, ClassStaticBlockBody, non-lexical-this, lex, privateEnv).
// Note: The function bodyFunction is never directly accessible to ECMAScript code. // Note: The function bodyFunction is never directly accessible to ECMAScript code.
auto* body_function = ECMAScriptFunctionObject::create(global_object, String::empty(), String::empty(), *m_function_body, {}, 0, lexical_environment, private_scope, FunctionKind::Normal, true, false, m_contains_direct_call_to_eval, false); auto* body_function = ECMAScriptFunctionObject::create(global_object, String::empty(), String::empty(), *m_function_body, {}, 0, lexical_environment, private_environment, FunctionKind::Normal, true, false, m_contains_direct_call_to_eval, false);
// 6. Perform MakeMethod(bodyFunction, homeObject). // 6. Perform MakeMethod(bodyFunction, homeObject).
body_function->make_method(home_object); body_function->make_method(home_object);
@ -1756,7 +1756,7 @@ ThrowCompletionOr<ECMAScriptFunctionObject*> ClassExpression::class_definition_e
auto& vm = interpreter.vm(); auto& vm = interpreter.vm();
auto* environment = vm.lexical_environment(); auto* environment = vm.lexical_environment();
VERIFY(environment); VERIFY(environment);
auto* class_scope = new_declarative_environment(*environment); auto* class_environment = new_declarative_environment(*environment);
// We might not set the lexical environment but we always want to restore it eventually. // We might not set the lexical environment but we always want to restore it eventually.
ArmedScopeGuard restore_environment = [&] { ArmedScopeGuard restore_environment = [&] {
@ -1764,7 +1764,7 @@ ThrowCompletionOr<ECMAScriptFunctionObject*> ClassExpression::class_definition_e
}; };
if (!binding_name.is_null()) if (!binding_name.is_null())
MUST(class_scope->create_immutable_binding(global_object, binding_name, true)); MUST(class_environment->create_immutable_binding(global_object, binding_name, true));
auto* outer_private_environment = vm.running_execution_context().private_environment; auto* outer_private_environment = vm.running_execution_context().private_environment;
auto* class_private_environment = new_private_environment(vm, outer_private_environment); auto* class_private_environment = new_private_environment(vm, outer_private_environment);
@ -1780,7 +1780,7 @@ ThrowCompletionOr<ECMAScriptFunctionObject*> ClassExpression::class_definition_e
auto* constructor_parent = vm.current_realm()->global_object().function_prototype(); auto* constructor_parent = vm.current_realm()->global_object().function_prototype();
if (!m_super_class.is_null()) { if (!m_super_class.is_null()) {
vm.running_execution_context().lexical_environment = class_scope; vm.running_execution_context().lexical_environment = class_environment;
// Note: Since our execute does evaluation and GetValue in once we must check for a valid reference first // Note: Since our execute does evaluation and GetValue in once we must check for a valid reference first
@ -1815,7 +1815,7 @@ ThrowCompletionOr<ECMAScriptFunctionObject*> ClassExpression::class_definition_e
auto* prototype = Object::create(global_object, proto_parent); auto* prototype = Object::create(global_object, proto_parent);
VERIFY(prototype); VERIFY(prototype);
vm.running_execution_context().lexical_environment = class_scope; vm.running_execution_context().lexical_environment = class_environment;
vm.running_execution_context().private_environment = class_private_environment; vm.running_execution_context().private_environment = class_private_environment;
ScopeGuard restore_private_environment = [&] { ScopeGuard restore_private_environment = [&] {
vm.running_execution_context().private_environment = outer_private_environment; vm.running_execution_context().private_environment = outer_private_environment;
@ -1889,7 +1889,7 @@ ThrowCompletionOr<ECMAScriptFunctionObject*> ClassExpression::class_definition_e
restore_environment.disarm(); restore_environment.disarm();
if (!binding_name.is_null()) if (!binding_name.is_null())
MUST(class_scope->initialize_binding(global_object, binding_name, class_constructor)); MUST(class_environment->initialize_binding(global_object, binding_name, class_constructor));
for (auto& field : instance_fields) for (auto& field : instance_fields)
class_constructor->add_field(field); class_constructor->add_field(field);

View file

@ -16,9 +16,9 @@ namespace JS {
DeclarativeEnvironment* DeclarativeEnvironment::create_for_per_iteration_bindings(Badge<ForStatement>, DeclarativeEnvironment& other, size_t bindings_size) DeclarativeEnvironment* DeclarativeEnvironment::create_for_per_iteration_bindings(Badge<ForStatement>, DeclarativeEnvironment& other, size_t bindings_size)
{ {
auto bindings = other.m_bindings.span().slice(0, bindings_size); auto bindings = other.m_bindings.span().slice(0, bindings_size);
auto* parent_scope = other.outer_environment(); auto* parent_environment = other.outer_environment();
return parent_scope->heap().allocate_without_global_object<DeclarativeEnvironment>(parent_scope, bindings); return parent_environment->heap().allocate_without_global_object<DeclarativeEnvironment>(parent_environment, bindings);
} }
DeclarativeEnvironment::DeclarativeEnvironment() DeclarativeEnvironment::DeclarativeEnvironment()
@ -26,13 +26,13 @@ DeclarativeEnvironment::DeclarativeEnvironment()
{ {
} }
DeclarativeEnvironment::DeclarativeEnvironment(Environment* parent_scope) DeclarativeEnvironment::DeclarativeEnvironment(Environment* parent_environment)
: Environment(parent_scope) : Environment(parent_environment)
{ {
} }
DeclarativeEnvironment::DeclarativeEnvironment(Environment* parent_scope, Span<Binding const> bindings) DeclarativeEnvironment::DeclarativeEnvironment(Environment* parent_environment, Span<Binding const> bindings)
: Environment(parent_scope) : Environment(parent_environment)
, m_bindings(bindings) , m_bindings(bindings)
{ {
} }

View file

@ -30,8 +30,8 @@ public:
static DeclarativeEnvironment* create_for_per_iteration_bindings(Badge<ForStatement>, DeclarativeEnvironment& other, size_t bindings_size); static DeclarativeEnvironment* create_for_per_iteration_bindings(Badge<ForStatement>, DeclarativeEnvironment& other, size_t bindings_size);
DeclarativeEnvironment(); DeclarativeEnvironment();
explicit DeclarativeEnvironment(Environment* parent_scope); explicit DeclarativeEnvironment(Environment* parent_environment);
explicit DeclarativeEnvironment(Environment* parent_scope, Span<Binding const> bindings); explicit DeclarativeEnvironment(Environment* parent_environment, Span<Binding const> bindings);
virtual ~DeclarativeEnvironment() override = default; virtual ~DeclarativeEnvironment() override = default;
virtual ThrowCompletionOr<bool> has_binding(FlyString const& name, Optional<size_t>* = nullptr) const override; virtual ThrowCompletionOr<bool> has_binding(FlyString const& name, Optional<size_t>* = nullptr) const override;

View file

@ -29,7 +29,7 @@
namespace JS { namespace JS {
ECMAScriptFunctionObject* ECMAScriptFunctionObject::create(GlobalObject& global_object, FlyString name, String source_text, Statement const& ecmascript_code, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, Environment* parent_scope, PrivateEnvironment* private_scope, FunctionKind kind, bool is_strict, bool might_need_arguments_object, bool contains_direct_call_to_eval, bool is_arrow_function, Variant<PropertyKey, PrivateName, Empty> class_field_initializer_name) ECMAScriptFunctionObject* ECMAScriptFunctionObject::create(GlobalObject& global_object, FlyString name, String source_text, Statement const& ecmascript_code, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, Environment* parent_environment, PrivateEnvironment* private_environment, FunctionKind kind, bool is_strict, bool might_need_arguments_object, bool contains_direct_call_to_eval, bool is_arrow_function, Variant<PropertyKey, PrivateName, Empty> class_field_initializer_name)
{ {
Object* prototype = nullptr; Object* prototype = nullptr;
switch (kind) { switch (kind) {
@ -46,20 +46,20 @@ ECMAScriptFunctionObject* ECMAScriptFunctionObject::create(GlobalObject& global_
prototype = global_object.async_generator_function_prototype(); prototype = global_object.async_generator_function_prototype();
break; break;
} }
return global_object.heap().allocate<ECMAScriptFunctionObject>(global_object, move(name), move(source_text), ecmascript_code, move(parameters), m_function_length, parent_scope, private_scope, *prototype, kind, is_strict, might_need_arguments_object, contains_direct_call_to_eval, is_arrow_function, move(class_field_initializer_name)); return global_object.heap().allocate<ECMAScriptFunctionObject>(global_object, move(name), move(source_text), ecmascript_code, move(parameters), m_function_length, parent_environment, private_environment, *prototype, kind, is_strict, might_need_arguments_object, contains_direct_call_to_eval, is_arrow_function, move(class_field_initializer_name));
} }
ECMAScriptFunctionObject* ECMAScriptFunctionObject::create(GlobalObject& global_object, FlyString name, Object& prototype, String source_text, Statement const& ecmascript_code, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, Environment* parent_scope, PrivateEnvironment* private_scope, FunctionKind kind, bool is_strict, bool might_need_arguments_object, bool contains_direct_call_to_eval, bool is_arrow_function, Variant<PropertyKey, PrivateName, Empty> class_field_initializer_name) ECMAScriptFunctionObject* ECMAScriptFunctionObject::create(GlobalObject& global_object, FlyString name, Object& prototype, String source_text, Statement const& ecmascript_code, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, Environment* parent_environment, PrivateEnvironment* private_environment, FunctionKind kind, bool is_strict, bool might_need_arguments_object, bool contains_direct_call_to_eval, bool is_arrow_function, Variant<PropertyKey, PrivateName, Empty> class_field_initializer_name)
{ {
return global_object.heap().allocate<ECMAScriptFunctionObject>(global_object, move(name), move(source_text), ecmascript_code, move(parameters), m_function_length, parent_scope, private_scope, prototype, kind, is_strict, might_need_arguments_object, contains_direct_call_to_eval, is_arrow_function, move(class_field_initializer_name)); return global_object.heap().allocate<ECMAScriptFunctionObject>(global_object, move(name), move(source_text), ecmascript_code, move(parameters), m_function_length, parent_environment, private_environment, prototype, kind, is_strict, might_need_arguments_object, contains_direct_call_to_eval, is_arrow_function, move(class_field_initializer_name));
} }
ECMAScriptFunctionObject::ECMAScriptFunctionObject(FlyString name, String source_text, Statement const& ecmascript_code, Vector<FunctionNode::Parameter> formal_parameters, i32 function_length, Environment* parent_scope, PrivateEnvironment* private_scope, Object& prototype, FunctionKind kind, bool strict, bool might_need_arguments_object, bool contains_direct_call_to_eval, bool is_arrow_function, Variant<PropertyKey, PrivateName, Empty> class_field_initializer_name) ECMAScriptFunctionObject::ECMAScriptFunctionObject(FlyString name, String source_text, Statement const& ecmascript_code, Vector<FunctionNode::Parameter> formal_parameters, i32 function_length, Environment* parent_environment, PrivateEnvironment* private_environment, Object& prototype, FunctionKind kind, bool strict, bool might_need_arguments_object, bool contains_direct_call_to_eval, bool is_arrow_function, Variant<PropertyKey, PrivateName, Empty> class_field_initializer_name)
: FunctionObject(prototype) : FunctionObject(prototype)
, m_name(move(name)) , m_name(move(name))
, m_function_length(function_length) , m_function_length(function_length)
, m_environment(parent_scope) , m_environment(parent_environment)
, m_private_environment(private_scope) , m_private_environment(private_environment)
, m_formal_parameters(move(formal_parameters)) , m_formal_parameters(move(formal_parameters))
, m_ecmascript_code(ecmascript_code) , m_ecmascript_code(ecmascript_code)
, m_realm(global_object().associated_realm()) , m_realm(global_object().associated_realm())

View file

@ -32,10 +32,10 @@ public:
Global, Global,
}; };
static ECMAScriptFunctionObject* create(GlobalObject&, FlyString name, String source_text, Statement const& ecmascript_code, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, Environment* parent_scope, PrivateEnvironment* private_scope, FunctionKind, bool is_strict, bool might_need_arguments_object = true, bool contains_direct_call_to_eval = true, bool is_arrow_function = false, Variant<PropertyKey, PrivateName, Empty> class_field_initializer_name = {}); static ECMAScriptFunctionObject* create(GlobalObject&, FlyString name, String source_text, Statement const& ecmascript_code, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, Environment* parent_environment, PrivateEnvironment* private_environment, FunctionKind, bool is_strict, bool might_need_arguments_object = true, bool contains_direct_call_to_eval = true, bool is_arrow_function = false, Variant<PropertyKey, PrivateName, Empty> class_field_initializer_name = {});
static ECMAScriptFunctionObject* create(GlobalObject&, FlyString name, Object& prototype, String source_text, Statement const& ecmascript_code, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, Environment* parent_scope, PrivateEnvironment* private_scope, FunctionKind, bool is_strict, bool might_need_arguments_object = true, bool contains_direct_call_to_eval = true, bool is_arrow_function = false, Variant<PropertyKey, PrivateName, Empty> class_field_initializer_name = {}); static ECMAScriptFunctionObject* create(GlobalObject&, FlyString name, Object& prototype, String source_text, Statement const& ecmascript_code, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, Environment* parent_environment, PrivateEnvironment* private_environment, FunctionKind, bool is_strict, bool might_need_arguments_object = true, bool contains_direct_call_to_eval = true, bool is_arrow_function = false, Variant<PropertyKey, PrivateName, Empty> class_field_initializer_name = {});
ECMAScriptFunctionObject(FlyString name, String source_text, Statement const& ecmascript_code, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, Environment* parent_scope, PrivateEnvironment* private_scope, Object& prototype, FunctionKind, bool is_strict, bool might_need_arguments_object, bool contains_direct_call_to_eval, bool is_arrow_function, Variant<PropertyKey, PrivateName, Empty> class_field_initializer_name); ECMAScriptFunctionObject(FlyString name, String source_text, Statement const& ecmascript_code, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, Environment* parent_environment, PrivateEnvironment* private_environment, Object& prototype, FunctionKind, bool is_strict, bool might_need_arguments_object, bool contains_direct_call_to_eval, bool is_arrow_function, Variant<PropertyKey, PrivateName, Empty> class_field_initializer_name);
virtual void initialize(GlobalObject&) override; virtual void initialize(GlobalObject&) override;
virtual ~ECMAScriptFunctionObject() override = default; virtual ~ECMAScriptFunctionObject() override = default;

View file

@ -228,14 +228,14 @@ ThrowCompletionOr<ECMAScriptFunctionObject*> FunctionConstructor::create_dynamic
// 28. Let realmF be the current Realm Record. // 28. Let realmF be the current Realm Record.
auto* realm = vm.current_realm(); auto* realm = vm.current_realm();
// 29. Let scope be realmF.[[GlobalEnv]]. // 29. Let env be realmF.[[GlobalEnv]].
auto* scope = &realm->global_environment(); auto* environment = &realm->global_environment();
// 30. Let privateScope be null. // 30. Let privateEnv be null.
PrivateEnvironment* private_scope = nullptr; PrivateEnvironment* private_environment = nullptr;
// 31. Let F be ! OrdinaryFunctionCreate(proto, sourceText, parameters, body, non-lexical-this, scope, privateScope). // 31. Let F be ! OrdinaryFunctionCreate(proto, sourceText, parameters, body, non-lexical-this, env, privateEnv).
auto* function = ECMAScriptFunctionObject::create(global_object, "anonymous", *prototype, move(source_text), expr->body(), expr->parameters(), expr->function_length(), scope, private_scope, expr->kind(), expr->is_strict_mode(), expr->might_need_arguments_object(), contains_direct_call_to_eval); auto* function = ECMAScriptFunctionObject::create(global_object, "anonymous", *prototype, move(source_text), expr->body(), expr->parameters(), expr->function_length(), environment, private_environment, expr->kind(), expr->is_strict_mode(), expr->might_need_arguments_object(), contains_direct_call_to_eval);
// FIXME: Remove the name argument from create() and do this instead. // FIXME: Remove the name argument from create() and do this instead.
// 32. Perform SetFunctionName(F, "anonymous"). // 32. Perform SetFunctionName(F, "anonymous").

View file

@ -11,8 +11,8 @@
namespace JS { namespace JS {
FunctionEnvironment::FunctionEnvironment(Environment* parent_scope) FunctionEnvironment::FunctionEnvironment(Environment* parent_environment)
: DeclarativeEnvironment(parent_scope) : DeclarativeEnvironment(parent_environment)
{ {
} }

View file

@ -22,7 +22,7 @@ public:
Uninitialized, Uninitialized,
}; };
explicit FunctionEnvironment(Environment* parent_scope); explicit FunctionEnvironment(Environment* parent_environment);
virtual ~FunctionEnvironment() override = default; virtual ~FunctionEnvironment() override = default;
ThisBindingStatus this_binding_status() const { return m_this_binding_status; } ThisBindingStatus this_binding_status() const { return m_this_binding_status; }