Reference.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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())
  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. auto& global_object = realm.global_object();
  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(vm);
  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(vm, m_environment_coordinate->index, m_strict);
  115. return m_base_environment->get_binding_value(vm, 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_(VM& vm)
  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. // 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>(ErrorType::UnsupportedDeleteSuperProperty);
  140. // c. Let baseObj be ! ToObject(ref.[[Base]]).
  141. auto* base_obj = MUST(m_base_value.to_object(vm));
  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>(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(vm, m_name.as_string());
  156. }
  157. String Reference::to_string() const
  158. {
  159. StringBuilder builder;
  160. builder.append("Reference { Base="sv);
  161. switch (m_base_type) {
  162. case BaseType::Unresolvable:
  163. builder.append("Unresolvable"sv);
  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>"sv);
  171. else
  172. builder.appendff("{}", m_base_value.to_string_without_side_effects());
  173. break;
  174. }
  175. builder.append(", ReferencedName="sv);
  176. if (!m_name.is_valid())
  177. builder.append("<invalid>"sv);
  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>"sv);
  186. else
  187. builder.appendff("{}", m_this_value.to_string_without_side_effects());
  188. builder.append(" }"sv);
  189. return builder.to_string();
  190. }
  191. // 6.2.4.8 InitializeReferencedBinding ( V, W ), https://tc39.es/ecma262/#sec-object.prototype.hasownproperty
  192. ThrowCompletionOr<void> Reference::initialize_referenced_binding(VM& vm, Value value) const
  193. {
  194. VERIFY(!is_unresolvable());
  195. VERIFY(m_base_type == BaseType::Environment);
  196. return m_base_environment->initialize_binding(vm, m_name.as_string(), value);
  197. }
  198. // 6.2.4.9 MakePrivateReference ( baseValue, privateIdentifier ), https://tc39.es/ecma262/#sec-makeprivatereference
  199. Reference make_private_reference(VM& vm, Value base_value, FlyString const& private_identifier)
  200. {
  201. // 1. Let privEnv be the running execution context's PrivateEnvironment.
  202. auto* private_environment = vm.running_execution_context().private_environment;
  203. // 2. Assert: privEnv is not null.
  204. VERIFY(private_environment);
  205. // 3. Let privateName be ResolvePrivateIdentifier(privEnv, privateIdentifier).
  206. auto private_name = private_environment->resolve_private_identifier(private_identifier);
  207. // 4. Return the Reference Record { [[Base]]: baseValue, [[ReferencedName]]: privateName, [[Strict]]: true, [[ThisValue]]: empty }.
  208. return Reference { base_value, private_name };
  209. }
  210. }