Reference.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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>(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(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())
  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>(ErrorType::ReferenceUnresolvable);
  65. else
  66. return vm.throw_completion<ReferenceError>(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. auto& vm = global_object.vm();
  72. // 1. ReturnIfAbrupt(V).
  73. // 2. If V is not a Reference Record, return V.
  74. // 3. If IsUnresolvableReference(V) is true, throw a ReferenceError exception.
  75. if (!is_valid_reference() || is_unresolvable())
  76. return throw_reference_error(global_object);
  77. // 4. If IsPropertyReference(V) is true, then
  78. if (is_property_reference()) {
  79. // a. Let baseObj be ? ToObject(V.[[Base]]).
  80. // NOTE: Deferred as an optimization; we might not actually need to create an object.
  81. // b. If IsPrivateReference(V) is true, then
  82. if (is_private_reference()) {
  83. // FIXME: We need to be able to specify the receiver for this
  84. // if we want to use it in error messages in future
  85. // as things currently stand this does the "wrong thing" but
  86. // the error is unobservable
  87. auto* base_obj = TRY(m_base_value.to_object(vm));
  88. // i. Return ? PrivateGet(baseObj, V.[[ReferencedName]]).
  89. return base_obj->private_get(m_private_name);
  90. }
  91. // OPTIMIZATION: For various primitives we can avoid actually creating a new object for them.
  92. Object* base_obj = nullptr;
  93. if (m_base_value.is_string()) {
  94. auto string_value = m_base_value.as_string().get(global_object, m_name);
  95. if (string_value.has_value())
  96. return *string_value;
  97. base_obj = global_object.string_prototype();
  98. } else if (m_base_value.is_number())
  99. base_obj = global_object.number_prototype();
  100. else if (m_base_value.is_boolean())
  101. base_obj = global_object.boolean_prototype();
  102. else
  103. base_obj = TRY(m_base_value.to_object(vm));
  104. // c. Return ? baseObj.[[Get]](V.[[ReferencedName]], GetThisValue(V)).
  105. return base_obj->internal_get(m_name, get_this_value());
  106. }
  107. // 5. Else,
  108. // a. Let base be V.[[Base]].
  109. // b. Assert: base is an Environment Record.
  110. VERIFY(m_base_type == BaseType::Environment);
  111. VERIFY(m_base_environment);
  112. // c. Return ? base.GetBindingValue(V.[[ReferencedName]], V.[[Strict]]) (see 9.1).
  113. if (m_environment_coordinate.has_value())
  114. return static_cast<DeclarativeEnvironment*>(m_base_environment)->get_binding_value_direct(global_object, m_environment_coordinate->index, m_strict);
  115. return m_base_environment->get_binding_value(global_object, m_name.as_string(), m_strict);
  116. }
  117. // 13.5.1.2 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-delete-operator-runtime-semantics-evaluation
  118. ThrowCompletionOr<bool> Reference::delete_(GlobalObject& global_object)
  119. {
  120. // 13.5.1.2 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-delete-operator-runtime-semantics-evaluation
  121. // UnaryExpression : delete UnaryExpression
  122. // NOTE: The following steps have already been evaluated by the time we get here:
  123. // 1. Let ref be the result of evaluating UnaryExpression.
  124. // 2. ReturnIfAbrupt(ref).
  125. // 3. If ref is not a Reference Record, return true.
  126. // 4. If IsUnresolvableReference(ref) is true, then
  127. if (is_unresolvable()) {
  128. // a. Assert: ref.[[Strict]] is false.
  129. VERIFY(!m_strict);
  130. // b. Return true.
  131. return true;
  132. }
  133. auto& vm = global_object.vm();
  134. // 5. If IsPropertyReference(ref) is true, then
  135. if (is_property_reference()) {
  136. // a. Assert: IsPrivateReference(ref) is false.
  137. VERIFY(!is_private_reference());
  138. // b. If IsSuperReference(ref) is true, throw a ReferenceError exception.
  139. if (is_super_reference())
  140. return vm.throw_completion<ReferenceError>(ErrorType::UnsupportedDeleteSuperProperty);
  141. // c. Let baseObj be ! ToObject(ref.[[Base]]).
  142. auto* base_obj = MUST(m_base_value.to_object(vm));
  143. // d. Let deleteStatus be ? baseObj.[[Delete]](ref.[[ReferencedName]]).
  144. bool delete_status = TRY(base_obj->internal_delete(m_name));
  145. // e. If deleteStatus is false and ref.[[Strict]] is true, throw a TypeError exception.
  146. if (!delete_status && m_strict)
  147. return vm.throw_completion<TypeError>(ErrorType::ReferenceNullishDeleteProperty, m_name, m_base_value.to_string_without_side_effects());
  148. // f. Return deleteStatus.
  149. return delete_status;
  150. }
  151. // 6. Else,
  152. // a. Let base be ref.[[Base]].
  153. // b. Assert: base is an Environment Record.
  154. VERIFY(m_base_type == BaseType::Environment);
  155. // c. Return ? base.DeleteBinding(ref.[[ReferencedName]]).
  156. return m_base_environment->delete_binding(global_object, m_name.as_string());
  157. }
  158. String Reference::to_string() const
  159. {
  160. StringBuilder builder;
  161. builder.append("Reference { Base="sv);
  162. switch (m_base_type) {
  163. case BaseType::Unresolvable:
  164. builder.append("Unresolvable"sv);
  165. break;
  166. case BaseType::Environment:
  167. builder.appendff("{}", base_environment().class_name());
  168. break;
  169. case BaseType::Value:
  170. if (m_base_value.is_empty())
  171. builder.append("<empty>"sv);
  172. else
  173. builder.appendff("{}", m_base_value.to_string_without_side_effects());
  174. break;
  175. }
  176. builder.append(", ReferencedName="sv);
  177. if (!m_name.is_valid())
  178. builder.append("<invalid>"sv);
  179. else if (m_name.is_symbol())
  180. builder.appendff("{}", m_name.as_symbol()->to_string());
  181. else
  182. builder.appendff("{}", m_name.to_string());
  183. builder.appendff(", Strict={}", m_strict);
  184. builder.appendff(", ThisValue=");
  185. if (m_this_value.is_empty())
  186. builder.append("<empty>"sv);
  187. else
  188. builder.appendff("{}", m_this_value.to_string_without_side_effects());
  189. builder.append(" }"sv);
  190. return builder.to_string();
  191. }
  192. // 6.2.4.9 MakePrivateReference ( baseValue, privateIdentifier ), https://tc39.es/ecma262/#sec-makeprivatereference
  193. Reference make_private_reference(VM& vm, Value base_value, FlyString const& private_identifier)
  194. {
  195. // 1. Let privEnv be the running execution context's PrivateEnvironment.
  196. auto* private_environment = vm.running_execution_context().private_environment;
  197. // 2. Assert: privEnv is not null.
  198. VERIFY(private_environment);
  199. // 3. Let privateName be ResolvePrivateIdentifier(privEnv, privateIdentifier).
  200. auto private_name = private_environment->resolve_private_identifier(private_identifier);
  201. // 4. Return the Reference Record { [[Base]]: baseValue, [[ReferencedName]]: privateName, [[Strict]]: true, [[ThisValue]]: empty }.
  202. return Reference { base_value, private_name };
  203. }
  204. }