Reference.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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/Error.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. #include <LibJS/Runtime/Reference.h>
  10. namespace JS {
  11. // 6.2.4.6 PutValue ( V, W ), https://tc39.es/ecma262/#sec-putvalue
  12. void Reference::put_value(GlobalObject& global_object, Value value)
  13. {
  14. auto& vm = global_object.vm();
  15. if (is_unresolvable()) {
  16. if (m_strict) {
  17. throw_reference_error(global_object);
  18. return;
  19. }
  20. global_object.set(m_name, value, Object::ShouldThrowExceptions::No);
  21. return;
  22. }
  23. if (is_property_reference()) {
  24. // FIXME: This is an ad-hoc hack until we support proper variable bindings.
  25. if (!m_base_value.is_object() && vm.in_strict_mode()) {
  26. if (m_base_value.is_nullish())
  27. vm.throw_exception<TypeError>(global_object, ErrorType::ReferenceNullishSetProperty, m_name, m_base_value.to_string_without_side_effects());
  28. else
  29. vm.throw_exception<TypeError>(global_object, ErrorType::ReferencePrimitiveSetProperty, m_name, m_base_value.typeof(), m_base_value.to_string_without_side_effects());
  30. return;
  31. }
  32. auto* base_obj = m_base_value.to_object(global_object);
  33. if (!base_obj)
  34. return;
  35. bool succeeded = base_obj->internal_set(m_name, value, get_this_value());
  36. if (vm.exception())
  37. return;
  38. if (!succeeded && m_strict) {
  39. vm.throw_exception<TypeError>(global_object, ErrorType::ReferenceNullishSetProperty, m_name, m_base_value.to_string_without_side_effects());
  40. return;
  41. }
  42. return;
  43. }
  44. VERIFY(m_base_type == BaseType::Environment);
  45. auto existing_variable = m_base_environment->get_from_environment(m_name.as_string());
  46. Variable variable {
  47. .value = value,
  48. .declaration_kind = existing_variable.has_value() ? existing_variable->declaration_kind : DeclarationKind::Var
  49. };
  50. // FIXME: This is a hack until we support proper variable bindings.
  51. if (variable.declaration_kind == DeclarationKind::Const) {
  52. vm.throw_exception<TypeError>(global_object, ErrorType::InvalidAssignToConst);
  53. return;
  54. }
  55. bool succeeded = m_base_environment->put_into_environment(m_name.as_string(), variable);
  56. if (vm.exception())
  57. return;
  58. if (!succeeded && m_strict) {
  59. // FIXME: This is a hack and will disappear when we support proper variable bindings.
  60. vm.throw_exception<TypeError>(global_object, ErrorType::DescWriteNonWritable, m_name);
  61. return;
  62. }
  63. }
  64. void Reference::throw_reference_error(GlobalObject& global_object) const
  65. {
  66. auto& vm = global_object.vm();
  67. if (!m_name.is_valid())
  68. vm.throw_exception<ReferenceError>(global_object, ErrorType::ReferenceUnresolvable);
  69. else
  70. vm.throw_exception<ReferenceError>(global_object, ErrorType::UnknownIdentifier, m_name.to_string_or_symbol().to_display_string());
  71. }
  72. // 6.2.4.5 GetValue ( V ), https://tc39.es/ecma262/#sec-getvalue
  73. Value Reference::get_value(GlobalObject& global_object, bool throw_if_undefined) const
  74. {
  75. if (is_unresolvable()) {
  76. throw_reference_error(global_object);
  77. return {};
  78. }
  79. if (is_property_reference()) {
  80. auto* base_obj = m_base_value.to_object(global_object);
  81. if (!base_obj)
  82. return {};
  83. return base_obj->get(m_name);
  84. }
  85. VERIFY(m_base_type == BaseType::Environment);
  86. auto value = m_base_environment->get_from_environment(m_name.as_string());
  87. if (!value.has_value()) {
  88. if (!throw_if_undefined) {
  89. // FIXME: This is an ad-hoc hack for the `typeof` operator until we support proper variable bindings.
  90. return js_undefined();
  91. }
  92. throw_reference_error(global_object);
  93. return {};
  94. }
  95. return value->value;
  96. }
  97. // 13.5.1.2 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-delete-operator-runtime-semantics-evaluation
  98. bool Reference::delete_(GlobalObject& global_object)
  99. {
  100. // 13.5.1.2 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-delete-operator-runtime-semantics-evaluation
  101. // UnaryExpression : delete UnaryExpression
  102. // NOTE: The following steps have already been evaluated by the time we get here:
  103. // 1. Let ref be the result of evaluating UnaryExpression.
  104. // 2. ReturnIfAbrupt(ref).
  105. // 3. If ref is not a Reference Record, return true.
  106. // 4. If IsUnresolvableReference(ref) is true, then
  107. if (is_unresolvable()) {
  108. // a. Assert: ref.[[Strict]] is false.
  109. VERIFY(!m_strict);
  110. // b. Return true.
  111. return true;
  112. }
  113. auto& vm = global_object.vm();
  114. // 5. If IsPropertyReference(ref) is true, then
  115. if (is_property_reference()) {
  116. // a. Assert: ! IsPrivateReference(ref) is false.
  117. // FIXME: We don't have private references yet.
  118. // b. If IsSuperReference(ref) is true, throw a ReferenceError exception.
  119. if (is_super_reference()) {
  120. vm.throw_exception<ReferenceError>(global_object, ErrorType::UnsupportedDeleteSuperProperty);
  121. return {};
  122. }
  123. // c. Let baseObj be ! ToObject(ref.[[Base]]).
  124. auto* base_obj = m_base_value.to_object(global_object);
  125. VERIFY(base_obj);
  126. // d. Let deleteStatus be ? baseObj.[[Delete]](ref.[[ReferencedName]]).
  127. bool delete_status = base_obj->internal_delete(m_name);
  128. if (vm.exception())
  129. return {};
  130. // e. If deleteStatus is false and ref.[[Strict]] is true, throw a TypeError exception.
  131. if (!delete_status && m_strict) {
  132. vm.throw_exception<TypeError>(global_object, ErrorType::ReferenceNullishDeleteProperty, m_name, m_base_value.to_string_without_side_effects());
  133. return {};
  134. }
  135. // f. Return deleteStatus.
  136. return delete_status;
  137. }
  138. // 6. Else,
  139. // a. Let base be ref.[[Base]].
  140. // b. Assert: base is an Environment Record.
  141. VERIFY(m_base_type == BaseType::Environment);
  142. // c. Return ? base.DeleteBinding(ref.[[ReferencedName]]).
  143. // FIXME: This is ad-hoc, we should be calling DeleteBinding.
  144. return m_base_environment->delete_from_environment(m_name.as_string());
  145. }
  146. String Reference::to_string() const
  147. {
  148. StringBuilder builder;
  149. builder.append("Reference { Base=");
  150. switch (m_base_type) {
  151. case BaseType::Unresolvable:
  152. builder.append("Unresolvable");
  153. break;
  154. case BaseType::Environment:
  155. builder.appendff("{}", base_environment().class_name());
  156. break;
  157. case BaseType::Value:
  158. if (m_base_value.is_empty())
  159. builder.append("<empty>");
  160. else
  161. builder.appendff("{}", m_base_value.to_string_without_side_effects());
  162. break;
  163. }
  164. builder.append(", ReferencedName=");
  165. if (!m_name.is_valid())
  166. builder.append("<invalid>");
  167. else if (m_name.is_symbol())
  168. builder.appendff("{}", m_name.as_symbol()->to_string());
  169. else
  170. builder.appendff("{}", m_name.to_string());
  171. builder.appendff(", Strict={}", m_strict);
  172. builder.appendff(", ThisValue=");
  173. if (m_this_value.is_empty())
  174. builder.append("<empty>");
  175. else
  176. builder.appendff("{}", m_this_value.to_string_without_side_effects());
  177. builder.append(" }");
  178. return builder.to_string();
  179. }
  180. }