Reference.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/AST.h>
  7. #include <LibJS/Runtime/DeclarativeEnvironment.h>
  8. #include <LibJS/Runtime/Error.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. #include <LibJS/Runtime/Reference.h>
  11. namespace JS {
  12. // 6.2.4.6 PutValue ( V, W ), https://tc39.es/ecma262/#sec-putvalue
  13. ThrowCompletionOr<void> Reference::put_value(VM& vm, Value value)
  14. {
  15. // 1. ReturnIfAbrupt(V).
  16. // 2. ReturnIfAbrupt(W).
  17. // 3. If V is not a Reference Record, throw a ReferenceError exception.
  18. if (!is_valid_reference())
  19. return vm.throw_completion<ReferenceError>(ErrorType::InvalidLeftHandAssignment);
  20. // 4. If IsUnresolvableReference(V) is true, then
  21. if (is_unresolvable()) {
  22. // a. If V.[[Strict]] is true, throw a ReferenceError exception.
  23. if (m_strict)
  24. return throw_reference_error(vm);
  25. // b. Let globalObj be GetGlobalObject().
  26. auto& global_object = vm.get_global_object();
  27. // c. Perform ? Set(globalObj, V.[[ReferencedName]], W, false).
  28. TRY(global_object.set(m_name, value, Object::ShouldThrowExceptions::No));
  29. // Return unused.
  30. return {};
  31. }
  32. // 5. If IsPropertyReference(V) is true, then
  33. if (is_property_reference()) {
  34. // a. Let baseObj be ? ToObject(V.[[Base]]).
  35. auto* base_obj = TRY(m_base_value.to_object(vm));
  36. // b. If IsPrivateReference(V) is true, then
  37. if (is_private_reference()) {
  38. // i. Return ? PrivateSet(baseObj, V.[[ReferencedName]], W).
  39. return base_obj->private_set(m_private_name, value);
  40. }
  41. // c. Let succeeded be ? baseObj.[[Set]](V.[[ReferencedName]], W, GetThisValue(V)).
  42. auto succeeded = TRY(base_obj->internal_set(m_name, value, get_this_value()));
  43. // d. If succeeded is false and V.[[Strict]] is true, throw a TypeError exception.
  44. if (!succeeded && m_strict)
  45. return vm.throw_completion<TypeError>(ErrorType::ReferenceNullishSetProperty, m_name, m_base_value.to_string_without_side_effects());
  46. // e. Return unused.
  47. return {};
  48. }
  49. // 6. Else,
  50. // a. Let base be V.[[Base]].
  51. // b. Assert: base is an Environment Record.
  52. VERIFY(m_base_type == BaseType::Environment);
  53. VERIFY(m_base_environment);
  54. // c. Return ? base.SetMutableBinding(V.[[ReferencedName]], W, V.[[Strict]]) (see 9.1).
  55. if (m_environment_coordinate.has_value() && m_environment_coordinate->index != EnvironmentCoordinate::global_marker)
  56. return static_cast<DeclarativeEnvironment*>(m_base_environment)->set_mutable_binding_direct(vm, m_environment_coordinate->index, value, m_strict);
  57. else
  58. return m_base_environment->set_mutable_binding(vm, m_name.as_string(), value, m_strict);
  59. }
  60. Completion Reference::throw_reference_error(VM& vm) const
  61. {
  62. if (!m_name.is_valid())
  63. return vm.throw_completion<ReferenceError>(ErrorType::ReferenceUnresolvable);
  64. else
  65. return vm.throw_completion<ReferenceError>(ErrorType::UnknownIdentifier, m_name.to_string_or_symbol().to_display_string());
  66. }
  67. // 6.2.4.5 GetValue ( V ), https://tc39.es/ecma262/#sec-getvalue
  68. ThrowCompletionOr<Value> Reference::get_value(VM& vm) const
  69. {
  70. auto& realm = *vm.current_realm();
  71. // 1. ReturnIfAbrupt(V).
  72. // 2. If V is not a Reference Record, return V.
  73. // 3. If IsUnresolvableReference(V) is true, throw a ReferenceError exception.
  74. if (!is_valid_reference() || is_unresolvable())
  75. return throw_reference_error(vm);
  76. // 4. If IsPropertyReference(V) is true, then
  77. if (is_property_reference()) {
  78. // a. Let baseObj be ? ToObject(V.[[Base]]).
  79. // NOTE: Deferred as an optimization; we might not actually need to create an object.
  80. // b. If IsPrivateReference(V) is true, then
  81. if (is_private_reference()) {
  82. // FIXME: We need to be able to specify the receiver for this
  83. // if we want to use it in error messages in future
  84. // as things currently stand this does the "wrong thing" but
  85. // the error is unobservable
  86. auto* base_obj = TRY(m_base_value.to_object(vm));
  87. // i. Return ? PrivateGet(baseObj, V.[[ReferencedName]]).
  88. return base_obj->private_get(m_private_name);
  89. }
  90. // OPTIMIZATION: For various primitives we can avoid actually creating a new object for them.
  91. Object* base_obj = nullptr;
  92. if (m_base_value.is_string()) {
  93. auto string_value = m_base_value.as_string().get(vm, m_name);
  94. if (string_value.has_value())
  95. return *string_value;
  96. base_obj = realm.intrinsics().string_prototype();
  97. } else if (m_base_value.is_number())
  98. base_obj = realm.intrinsics().number_prototype();
  99. else if (m_base_value.is_boolean())
  100. base_obj = realm.intrinsics().boolean_prototype();
  101. else
  102. base_obj = TRY(m_base_value.to_object(vm));
  103. // c. Return ? baseObj.[[Get]](V.[[ReferencedName]], GetThisValue(V)).
  104. return base_obj->internal_get(m_name, get_this_value());
  105. }
  106. // 5. Else,
  107. // a. Let base be V.[[Base]].
  108. // b. Assert: base is an Environment Record.
  109. VERIFY(m_base_type == BaseType::Environment);
  110. VERIFY(m_base_environment);
  111. // c. Return ? base.GetBindingValue(V.[[ReferencedName]], V.[[Strict]]) (see 9.1).
  112. if (m_environment_coordinate.has_value() && m_environment_coordinate->index != EnvironmentCoordinate::global_marker)
  113. return static_cast<DeclarativeEnvironment*>(m_base_environment)->get_binding_value_direct(vm, m_environment_coordinate->index, m_strict);
  114. return m_base_environment->get_binding_value(vm, m_name.as_string(), m_strict);
  115. }
  116. // 13.5.1.2 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-delete-operator-runtime-semantics-evaluation
  117. ThrowCompletionOr<bool> Reference::delete_(VM& vm)
  118. {
  119. // 13.5.1.2 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-delete-operator-runtime-semantics-evaluation
  120. // UnaryExpression : delete UnaryExpression
  121. // NOTE: The following steps have already been evaluated by the time we get here:
  122. // 1. Let ref be the result of evaluating UnaryExpression.
  123. // 2. ReturnIfAbrupt(ref).
  124. // 3. If ref is not a Reference Record, return true.
  125. // 4. If IsUnresolvableReference(ref) is true, then
  126. if (is_unresolvable()) {
  127. // a. Assert: ref.[[Strict]] is false.
  128. VERIFY(!m_strict);
  129. // b. Return true.
  130. return true;
  131. }
  132. // 5. If IsPropertyReference(ref) is true, then
  133. if (is_property_reference()) {
  134. // a. Assert: IsPrivateReference(ref) is false.
  135. VERIFY(!is_private_reference());
  136. // b. If IsSuperReference(ref) is true, throw a ReferenceError exception.
  137. if (is_super_reference())
  138. return vm.throw_completion<ReferenceError>(ErrorType::UnsupportedDeleteSuperProperty);
  139. // c. Let baseObj be ! ToObject(ref.[[Base]]).
  140. auto* base_obj = MUST(m_base_value.to_object(vm));
  141. // d. Let deleteStatus be ? baseObj.[[Delete]](ref.[[ReferencedName]]).
  142. bool delete_status = TRY(base_obj->internal_delete(m_name));
  143. // e. If deleteStatus is false and ref.[[Strict]] is true, throw a TypeError exception.
  144. if (!delete_status && m_strict)
  145. return vm.throw_completion<TypeError>(ErrorType::ReferenceNullishDeleteProperty, m_name, m_base_value.to_string_without_side_effects());
  146. // f. Return deleteStatus.
  147. return delete_status;
  148. }
  149. // 6. Else,
  150. // a. Let base be ref.[[Base]].
  151. // b. Assert: base is an Environment Record.
  152. VERIFY(m_base_type == BaseType::Environment);
  153. // c. Return ? base.DeleteBinding(ref.[[ReferencedName]]).
  154. return m_base_environment->delete_binding(vm, m_name.as_string());
  155. }
  156. String Reference::to_string() const
  157. {
  158. StringBuilder builder;
  159. builder.append("Reference { Base="sv);
  160. switch (m_base_type) {
  161. case BaseType::Unresolvable:
  162. builder.append("Unresolvable"sv);
  163. break;
  164. case BaseType::Environment:
  165. builder.appendff("{}", base_environment().class_name());
  166. break;
  167. case BaseType::Value:
  168. if (m_base_value.is_empty())
  169. builder.append("<empty>"sv);
  170. else
  171. builder.appendff("{}", m_base_value.to_string_without_side_effects());
  172. break;
  173. }
  174. builder.append(", ReferencedName="sv);
  175. if (!m_name.is_valid())
  176. builder.append("<invalid>"sv);
  177. else if (m_name.is_symbol())
  178. builder.appendff("{}", m_name.as_symbol()->to_string());
  179. else
  180. builder.appendff("{}", m_name.to_string());
  181. builder.appendff(", Strict={}", m_strict);
  182. builder.appendff(", ThisValue=");
  183. if (m_this_value.is_empty())
  184. builder.append("<empty>"sv);
  185. else
  186. builder.appendff("{}", m_this_value.to_string_without_side_effects());
  187. builder.append(" }"sv);
  188. return builder.to_string();
  189. }
  190. // 6.2.4.8 InitializeReferencedBinding ( V, W ), https://tc39.es/ecma262/#sec-object.prototype.hasownproperty
  191. ThrowCompletionOr<void> Reference::initialize_referenced_binding(VM& vm, Value value) const
  192. {
  193. VERIFY(!is_unresolvable());
  194. VERIFY(m_base_type == BaseType::Environment);
  195. return m_base_environment->initialize_binding(vm, m_name.as_string(), value);
  196. }
  197. // 6.2.4.9 MakePrivateReference ( baseValue, privateIdentifier ), https://tc39.es/ecma262/#sec-makeprivatereference
  198. Reference make_private_reference(VM& vm, Value base_value, FlyString const& private_identifier)
  199. {
  200. // 1. Let privEnv be the running execution context's PrivateEnvironment.
  201. auto* private_environment = vm.running_execution_context().private_environment;
  202. // 2. Assert: privEnv is not null.
  203. VERIFY(private_environment);
  204. // 3. Let privateName be ResolvePrivateIdentifier(privEnv, privateIdentifier).
  205. auto private_name = private_environment->resolve_private_identifier(private_identifier);
  206. // 4. Return the Reference Record { [[Base]]: baseValue, [[ReferencedName]]: privateName, [[Strict]]: true, [[ThisValue]]: empty }.
  207. return Reference { base_value, private_name };
  208. }
  209. }