1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:17:44 +00:00

LibJS: Make indirect bindings of module behave like normal bindings

Before this we attempted to hack around this by only overriding
has_binding. However this did not cover all cases, for example when
assigning to variables before their declaration it didn't throw.
By using the new find_binding_and_index virtual method we can just
pretend the indirect bindings are real.

Since indirect binding do come from a normal environment we need to
ensure you cannot modify the binding and that properties like mutable
are false as expected by the spec for such an indirect binding.
This commit is contained in:
davidot 2022-09-01 23:48:57 +02:00 committed by Linus Groh
parent 2484bbc4e0
commit 0fc67ffd62
5 changed files with 85 additions and 17 deletions

View file

@ -48,17 +48,6 @@ ThrowCompletionOr<Value> ModuleEnvironment::get_binding_value(VM& vm, FlyString
return DeclarativeEnvironment::get_binding_value(vm, name, strict);
}
// Not defined in the spec, see comment in the header.
ThrowCompletionOr<bool> ModuleEnvironment::has_binding(FlyString const& name, Optional<size_t>* out_index) const
{
// 1. If envRec has a binding for the name that is the value of N, return true.
if (get_indirect_binding(name))
return true;
return DeclarativeEnvironment::has_binding(name, out_index);
// 2. Return false.
}
// 9.1.1.5.2 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-module-environment-records-deletebinding-n
ThrowCompletionOr<bool> ModuleEnvironment::delete_binding(VM&, FlyString const&)
{
@ -102,4 +91,39 @@ ModuleEnvironment::IndirectBinding const* ModuleEnvironment::get_indirect_bindin
return &(*binding_or_end);
}
Optional<ModuleEnvironment::BindingAndIndex> ModuleEnvironment::find_binding_and_index(FlyString const& name) const
{
auto* indirect_binding = get_indirect_binding(name);
if (indirect_binding != nullptr) {
auto* target_env = indirect_binding->module->environment();
if (!target_env)
return {};
VERIFY(is<ModuleEnvironment>(target_env));
auto& target_module_environment = static_cast<ModuleEnvironment&>(*target_env);
auto result = target_module_environment.find_binding_and_index(indirect_binding->binding_name);
if (!result.has_value())
return {};
// NOTE: We must pretend this binding is actually from this environment
// so as specified by
// 9.1.1.5.5 CreateImportBinding ( N, M, N2 ), https://tc39.es/ecma262/#sec-createimportbinding
// It creates a new initialized immutable indirect binding for the
// name N. A binding must not already exist in this Environment
// Record for N. N2 is the name of a binding that exists in M's
// Module Environment Record. Accesses to the value of the new
// binding will indirectly access the bound value of the target
// binding.
// We don't alter the name of the binding as the name is only used
// for lookup.
Binding copy_binding = result->binding();
copy_binding.mutable_ = false;
copy_binding.can_be_deleted = false;
copy_binding.initialized = true;
return BindingAndIndex { copy_binding };
}
return DeclarativeEnvironment::find_binding_and_index(name);
}
}