Reference.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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(GlobalObject& global_object, Value value)
  14. {
  15. auto& vm = global_object.vm();
  16. // 1. ReturnIfAbrupt(V).
  17. // 2. ReturnIfAbrupt(W).
  18. // 3. If V is not a Reference Record, throw a ReferenceError exception.
  19. if (!is_valid_reference())
  20. return vm.throw_completion<ReferenceError>(global_object, ErrorType::InvalidLeftHandAssignment);
  21. // 4. If IsUnresolvableReference(V) is true, then
  22. if (is_unresolvable()) {
  23. // a. If V.[[Strict]] is true, throw a ReferenceError exception.
  24. if (m_strict)
  25. return throw_reference_error(global_object);
  26. // b. Let globalObj be GetGlobalObject().
  27. // c. Return ? Set(globalObj, V.[[ReferencedName]], W, false).
  28. TRY(global_object.set(m_name, value, Object::ShouldThrowExceptions::No));
  29. return {};
  30. }
  31. // 5. If IsPropertyReference(V) is true, then
  32. if (is_property_reference()) {
  33. // a. Let baseObj be ? ToObject(V.[[Base]]).
  34. auto* base_obj = TRY(m_base_value.to_object(global_object));
  35. // b. If IsPrivateReference(V) is true, then
  36. if (is_private_reference()) {
  37. // i. Return ? PrivateSet(baseObj, V.[[ReferencedName]], W).
  38. return base_obj->private_set(m_private_name, value);
  39. }
  40. // c. Let succeeded be ? baseObj.[[Set]](V.[[ReferencedName]], W, GetThisValue(V)).
  41. auto succeeded = TRY(base_obj->internal_set(m_name, value, get_this_value()));
  42. // d. If succeeded is false and V.[[Strict]] is true, throw a TypeError exception.
  43. if (!succeeded && m_strict)
  44. return vm.throw_completion<TypeError>(global_object, ErrorType::ReferenceNullishSetProperty, m_name, m_base_value.to_string_without_side_effects());
  45. // e. Return.
  46. return {};
  47. }
  48. // 6. Else,
  49. // a. Let base be V.[[Base]].
  50. // b. Assert: base is an Environment Record.
  51. VERIFY(m_base_type == BaseType::Environment);
  52. VERIFY(m_base_environment);
  53. // c. Return ? base.SetMutableBinding(V.[[ReferencedName]], W, V.[[Strict]]) (see 9.1).
  54. if (m_environment_coordinate.has_value())
  55. return static_cast<DeclarativeEnvironment*>(m_base_environment)->set_mutable_binding_direct(global_object, m_environment_coordinate->index, value, m_strict);
  56. else
  57. return m_base_environment->set_mutable_binding(global_object, m_name.as_string(), value, m_strict);
  58. }
  59. Completion Reference::throw_reference_error(GlobalObject& global_object) const
  60. {
  61. auto& vm = global_object.vm();
  62. if (!m_name.is_valid())
  63. return vm.throw_completion<ReferenceError>(global_object, ErrorType::ReferenceUnresolvable);
  64. else
  65. return vm.throw_completion<ReferenceError>(global_object, 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(GlobalObject& global_object) const
  69. {
  70. // 1. ReturnIfAbrupt(V).
  71. // 2. If V is not a Reference Record, return V.
  72. // 3. If IsUnresolvableReference(V) is true, throw a ReferenceError exception.
  73. if (!is_valid_reference() || is_unresolvable())
  74. return throw_reference_error(global_object);
  75. // 4. If IsPropertyReference(V) is true, then
  76. if (is_property_reference()) {
  77. // a. Let baseObj be ? ToObject(V.[[Base]]).
  78. // NOTE: Deferred as an optimization; we might not actually need to create an object.
  79. // b. If IsPrivateReference(V) is true, then
  80. if (is_private_reference()) {
  81. // FIXME: We need to be able to specify the receiver for this
  82. // if we want to use it in error messages in future
  83. // as things currently stand this does the "wrong thing" but
  84. // the error is unobservable
  85. auto* base_obj = TRY(m_base_value.to_object(global_object));
  86. // i. Return ? PrivateGet(baseObj, V.[[ReferencedName]]).
  87. return base_obj->private_get(m_private_name);
  88. }
  89. // OPTIMIZATION: For various primitives we can avoid actually creating a new object for them.
  90. Object* base_obj = nullptr;
  91. if (m_base_value.is_string()) {
  92. auto string_value = m_base_value.as_string().get(global_object, m_name);
  93. if (string_value.has_value())
  94. return *string_value;
  95. base_obj = global_object.string_prototype();
  96. } else if (m_base_value.is_number())
  97. base_obj = global_object.number_prototype();
  98. else if (m_base_value.is_boolean())
  99. base_obj = global_object.boolean_prototype();
  100. else
  101. base_obj = TRY(m_base_value.to_object(global_object));
  102. // c. Return ? baseObj.[[Get]](V.[[ReferencedName]], GetThisValue(V)).
  103. return base_obj->internal_get(m_name, get_this_value());
  104. }
  105. // 5. Else,
  106. // a. Let base be V.[[Base]].
  107. // b. Assert: base is an Environment Record.
  108. VERIFY(m_base_type == BaseType::Environment);
  109. VERIFY(m_base_environment);
  110. // c. Return ? base.GetBindingValue(V.[[ReferencedName]], V.[[Strict]]) (see 9.1).
  111. if (m_environment_coordinate.has_value())
  112. return static_cast<DeclarativeEnvironment*>(m_base_environment)->get_binding_value_direct(global_object, m_environment_coordinate->index, m_strict);
  113. return m_base_environment->get_binding_value(global_object, m_name.as_string(), m_strict);
  114. }
  115. // 13.5.1.2 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-delete-operator-runtime-semantics-evaluation
  116. ThrowCompletionOr<bool> Reference::delete_(GlobalObject& global_object)
  117. {
  118. // 13.5.1.2 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-delete-operator-runtime-semantics-evaluation
  119. // UnaryExpression : delete UnaryExpression
  120. // NOTE: The following steps have already been evaluated by the time we get here:
  121. // 1. Let ref be the result of evaluating UnaryExpression.
  122. // 2. ReturnIfAbrupt(ref).
  123. // 3. If ref is not a Reference Record, return true.
  124. // 4. If IsUnresolvableReference(ref) is true, then
  125. if (is_unresolvable()) {
  126. // a. Assert: ref.[[Strict]] is false.
  127. VERIFY(!m_strict);
  128. // b. Return true.
  129. return true;
  130. }
  131. auto& vm = global_object.vm();
  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>(global_object, ErrorType::UnsupportedDeleteSuperProperty);
  139. // c. Let baseObj be ! ToObject(ref.[[Base]]).
  140. auto* base_obj = MUST(m_base_value.to_object(global_object));
  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>(global_object, 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(global_object, m_name.as_string());
  155. }
  156. String Reference::to_string() const
  157. {
  158. StringBuilder builder;
  159. builder.append("Reference { Base=");
  160. switch (m_base_type) {
  161. case BaseType::Unresolvable:
  162. builder.append("Unresolvable");
  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>");
  170. else
  171. builder.appendff("{}", m_base_value.to_string_without_side_effects());
  172. break;
  173. }
  174. builder.append(", ReferencedName=");
  175. if (!m_name.is_valid())
  176. builder.append("<invalid>");
  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>");
  185. else
  186. builder.appendff("{}", m_this_value.to_string_without_side_effects());
  187. builder.append(" }");
  188. return builder.to_string();
  189. }
  190. // 6.2.4.9 MakePrivateReference ( baseValue, privateIdentifier ), https://tc39.es/ecma262/#sec-makeprivatereference
  191. Reference make_private_reference(VM& vm, Value base_value, FlyString const& private_identifier)
  192. {
  193. // 1. Let privEnv be the running execution context's PrivateEnvironment.
  194. auto* private_environment = vm.running_execution_context().private_environment;
  195. // 2. Assert: privEnv is not null.
  196. VERIFY(private_environment);
  197. // 3. Let privateName be ! ResolvePrivateIdentifier(privEnv, privateIdentifier).
  198. auto private_name = private_environment->resolve_private_identifier(private_identifier);
  199. // 4. Return the Reference Record { [[Base]]: baseValue, [[ReferencedName]]: privateName, [[Strict]]: true, [[ThisValue]]: empty }.
  200. return Reference { base_value, private_name };
  201. }
  202. }