Reference.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. 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(global_object));
  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>(global_object, 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())
  56. return static_cast<DeclarativeEnvironment*>(m_base_environment)->set_mutable_binding_direct(global_object, m_environment_coordinate->index, value, m_strict);
  57. else
  58. return m_base_environment->set_mutable_binding(global_object, m_name.as_string(), value, m_strict);
  59. }
  60. Completion Reference::throw_reference_error(GlobalObject& global_object) const
  61. {
  62. auto& vm = global_object.vm();
  63. if (!m_name.is_valid())
  64. return vm.throw_completion<ReferenceError>(global_object, ErrorType::ReferenceUnresolvable);
  65. else
  66. return vm.throw_completion<ReferenceError>(global_object, ErrorType::UnknownIdentifier, m_name.to_string_or_symbol().to_display_string());
  67. }
  68. // 6.2.4.5 GetValue ( V ), https://tc39.es/ecma262/#sec-getvalue
  69. ThrowCompletionOr<Value> Reference::get_value(GlobalObject& global_object) const
  70. {
  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(global_object);
  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(global_object));
  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(global_object, m_name);
  94. if (string_value.has_value())
  95. return *string_value;
  96. base_obj = global_object.string_prototype();
  97. } else if (m_base_value.is_number())
  98. base_obj = global_object.number_prototype();
  99. else if (m_base_value.is_boolean())
  100. base_obj = global_object.boolean_prototype();
  101. else
  102. base_obj = TRY(m_base_value.to_object(global_object));
  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())
  113. return static_cast<DeclarativeEnvironment*>(m_base_environment)->get_binding_value_direct(global_object, m_environment_coordinate->index, m_strict);
  114. return m_base_environment->get_binding_value(global_object, 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_(GlobalObject& global_object)
  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. auto& vm = global_object.vm();
  133. // 5. If IsPropertyReference(ref) is true, then
  134. if (is_property_reference()) {
  135. // a. Assert: IsPrivateReference(ref) is false.
  136. VERIFY(!is_private_reference());
  137. // b. If IsSuperReference(ref) is true, throw a ReferenceError exception.
  138. if (is_super_reference())
  139. return vm.throw_completion<ReferenceError>(global_object, ErrorType::UnsupportedDeleteSuperProperty);
  140. // c. Let baseObj be ! ToObject(ref.[[Base]]).
  141. auto* base_obj = MUST(m_base_value.to_object(global_object));
  142. // d. Let deleteStatus be ? baseObj.[[Delete]](ref.[[ReferencedName]]).
  143. bool delete_status = TRY(base_obj->internal_delete(m_name));
  144. // e. If deleteStatus is false and ref.[[Strict]] is true, throw a TypeError exception.
  145. if (!delete_status && m_strict)
  146. return vm.throw_completion<TypeError>(global_object, ErrorType::ReferenceNullishDeleteProperty, m_name, m_base_value.to_string_without_side_effects());
  147. // f. Return deleteStatus.
  148. return delete_status;
  149. }
  150. // 6. Else,
  151. // a. Let base be ref.[[Base]].
  152. // b. Assert: base is an Environment Record.
  153. VERIFY(m_base_type == BaseType::Environment);
  154. // c. Return ? base.DeleteBinding(ref.[[ReferencedName]]).
  155. return m_base_environment->delete_binding(global_object, m_name.as_string());
  156. }
  157. String Reference::to_string() const
  158. {
  159. StringBuilder builder;
  160. builder.append("Reference { Base=");
  161. switch (m_base_type) {
  162. case BaseType::Unresolvable:
  163. builder.append("Unresolvable");
  164. break;
  165. case BaseType::Environment:
  166. builder.appendff("{}", base_environment().class_name());
  167. break;
  168. case BaseType::Value:
  169. if (m_base_value.is_empty())
  170. builder.append("<empty>");
  171. else
  172. builder.appendff("{}", m_base_value.to_string_without_side_effects());
  173. break;
  174. }
  175. builder.append(", ReferencedName=");
  176. if (!m_name.is_valid())
  177. builder.append("<invalid>");
  178. else if (m_name.is_symbol())
  179. builder.appendff("{}", m_name.as_symbol()->to_string());
  180. else
  181. builder.appendff("{}", m_name.to_string());
  182. builder.appendff(", Strict={}", m_strict);
  183. builder.appendff(", ThisValue=");
  184. if (m_this_value.is_empty())
  185. builder.append("<empty>");
  186. else
  187. builder.appendff("{}", m_this_value.to_string_without_side_effects());
  188. builder.append(" }");
  189. return builder.to_string();
  190. }
  191. // 6.2.4.9 MakePrivateReference ( baseValue, privateIdentifier ), https://tc39.es/ecma262/#sec-makeprivatereference
  192. Reference make_private_reference(VM& vm, Value base_value, FlyString const& private_identifier)
  193. {
  194. // 1. Let privEnv be the running execution context's PrivateEnvironment.
  195. auto* private_environment = vm.running_execution_context().private_environment;
  196. // 2. Assert: privEnv is not null.
  197. VERIFY(private_environment);
  198. // 3. Let privateName be ResolvePrivateIdentifier(privEnv, privateIdentifier).
  199. auto private_name = private_environment->resolve_private_identifier(private_identifier);
  200. // 4. Return the Reference Record { [[Base]]: baseValue, [[ReferencedName]]: privateName, [[Strict]]: true, [[ThisValue]]: empty }.
  201. return Reference { base_value, private_name };
  202. }
  203. }