ModuleEnvironment.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2022, David Tuin <davidot@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/AST.h>
  7. #include <LibJS/Runtime/ModuleEnvironment.h>
  8. namespace JS {
  9. // 9.1.2.6 NewModuleEnvironment ( E ), https://tc39.es/ecma262/#sec-newmoduleenvironment
  10. ModuleEnvironment::ModuleEnvironment(Environment* outer_environment)
  11. : DeclarativeEnvironment(outer_environment)
  12. {
  13. }
  14. // 9.1.1.5.1 GetBindingValue ( N, S ), https://tc39.es/ecma262/#sec-module-environment-records-getbindingvalue-n-s
  15. ThrowCompletionOr<Value> ModuleEnvironment::get_binding_value(GlobalObject& global_object, FlyString const& name, bool strict)
  16. {
  17. // 1. Assert: S is true.
  18. VERIFY(strict);
  19. // 2. Assert: envRec has a binding for N.
  20. auto* indirect_binding = get_indirect_binding(name);
  21. VERIFY(indirect_binding || !DeclarativeEnvironment::has_binding(name).is_error());
  22. // 3. If the binding for N is an indirect binding, then
  23. if (indirect_binding) {
  24. // a. Let M and N2 be the indirection values provided when this binding for N was created.
  25. // b. Let targetEnv be M.[[Environment]].
  26. auto* target_env = indirect_binding->module->environment();
  27. // c. If targetEnv is empty, throw a ReferenceError exception.
  28. if (!target_env)
  29. return vm().throw_completion<ReferenceError>(global_object, ErrorType::ModuleNoEnvironment);
  30. // d. Return ? targetEnv.GetBindingValue(N2, true).
  31. return target_env->get_binding_value(global_object, indirect_binding->binding_name, true);
  32. }
  33. // 4. If the binding for N in envRec is an uninitialized binding, throw a ReferenceError exception.
  34. // 5. Return the value currently bound to N in envRec.
  35. // Note: Step 4 & 5 are the steps performed by declarative environment GetBindingValue
  36. return DeclarativeEnvironment::get_binding_value(global_object, name, strict);
  37. }
  38. // Not defined in the spec, see comment in the header.
  39. ThrowCompletionOr<bool> ModuleEnvironment::has_binding(FlyString const& name, Optional<size_t>* out_index) const
  40. {
  41. // 1. If envRec has a binding for the name that is the value of N, return true.
  42. if (get_indirect_binding(name))
  43. return true;
  44. return DeclarativeEnvironment::has_binding(name, out_index);
  45. // 2. Return false.
  46. }
  47. // 9.1.1.5.2 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-module-environment-records-deletebinding-n
  48. ThrowCompletionOr<bool> ModuleEnvironment::delete_binding(GlobalObject&, FlyString const&)
  49. {
  50. // The DeleteBinding concrete method of a module Environment Record is never used within this specification.
  51. VERIFY_NOT_REACHED();
  52. }
  53. // 9.1.1.5.4 GetThisBinding ( ), https://tc39.es/ecma262/#sec-module-environment-records-getthisbinding
  54. ThrowCompletionOr<Value> ModuleEnvironment::get_this_binding(GlobalObject&) const
  55. {
  56. // 1. Return undefined.
  57. return js_undefined();
  58. }
  59. // 9.1.1.5.5 CreateImportBinding ( N, M, N2 ), https://tc39.es/ecma262/#sec-createimportbinding
  60. ThrowCompletionOr<void> ModuleEnvironment::create_import_binding(FlyString name, Module* module, FlyString binding_name)
  61. {
  62. // 1. Assert: envRec does not already have a binding for N.
  63. VERIFY(!get_indirect_binding(name));
  64. // 2. Assert: When M.[[Environment]] is instantiated it will have a direct binding for N2.
  65. // FIXME: I don't know what this means or how to check it.
  66. // 3. Create an immutable indirect binding in envRec for N that references M and N2 as its target binding and record that the binding is initialized.
  67. // Note: We use the fact that the binding is in this list as it being initialized.
  68. m_indirect_bindings.append({ move(name),
  69. module,
  70. move(binding_name) });
  71. // 4. Return NormalCompletion(empty).
  72. return {};
  73. }
  74. ModuleEnvironment::IndirectBinding const* ModuleEnvironment::get_indirect_binding(FlyString const& name) const
  75. {
  76. auto binding_or_end = m_indirect_bindings.find_if([&](IndirectBinding const& binding) {
  77. return binding.name == name;
  78. });
  79. if (binding_or_end.is_end())
  80. return nullptr;
  81. return &(*binding_or_end);
  82. }
  83. }