ProxyObject.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/AbstractOperations.h>
  8. #include <LibJS/Runtime/Accessor.h>
  9. #include <LibJS/Runtime/Array.h>
  10. #include <LibJS/Runtime/Error.h>
  11. #include <LibJS/Runtime/GlobalObject.h>
  12. #include <LibJS/Runtime/ProxyObject.h>
  13. namespace JS {
  14. bool static is_compatible_property_descriptor(bool is_extensible, PropertyDescriptor new_descriptor, Optional<PropertyDescriptor> current_descriptor_optional)
  15. {
  16. if (!current_descriptor_optional.has_value())
  17. return is_extensible;
  18. auto current_descriptor = current_descriptor_optional.value();
  19. if (new_descriptor.attributes.is_empty() && new_descriptor.value.is_empty() && !new_descriptor.getter && !new_descriptor.setter)
  20. return true;
  21. if (!current_descriptor.attributes.is_configurable()) {
  22. if (new_descriptor.attributes.is_configurable())
  23. return false;
  24. if (new_descriptor.attributes.has_enumerable() && new_descriptor.attributes.is_enumerable() != current_descriptor.attributes.is_enumerable())
  25. return false;
  26. }
  27. if (new_descriptor.is_generic_descriptor())
  28. return true;
  29. if (current_descriptor.is_data_descriptor() != new_descriptor.is_data_descriptor() && !current_descriptor.attributes.is_configurable())
  30. return false;
  31. if (current_descriptor.is_data_descriptor() && new_descriptor.is_data_descriptor() && !current_descriptor.attributes.is_configurable() && !current_descriptor.attributes.is_writable()) {
  32. if (new_descriptor.attributes.is_writable())
  33. return false;
  34. return new_descriptor.value.is_empty() && same_value(new_descriptor.value, current_descriptor.value);
  35. }
  36. return true;
  37. }
  38. ProxyObject* ProxyObject::create(GlobalObject& global_object, Object& target, Object& handler)
  39. {
  40. return global_object.heap().allocate<ProxyObject>(global_object, target, handler, *global_object.object_prototype());
  41. }
  42. ProxyObject::ProxyObject(Object& target, Object& handler, Object& prototype)
  43. : Function(prototype)
  44. , m_target(target)
  45. , m_handler(handler)
  46. {
  47. }
  48. ProxyObject::~ProxyObject()
  49. {
  50. }
  51. Object* ProxyObject::prototype()
  52. {
  53. auto& vm = this->vm();
  54. if (m_is_revoked) {
  55. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked);
  56. return nullptr;
  57. }
  58. auto trap = Value(&m_handler).get_method(global_object(), vm.names.getPrototypeOf);
  59. if (vm.exception())
  60. return nullptr;
  61. if (!trap)
  62. return m_target.prototype();
  63. auto trap_result = vm.call(*trap, Value(&m_handler), Value(&m_target));
  64. if (vm.exception())
  65. return nullptr;
  66. if (!trap_result.is_object() && !trap_result.is_null()) {
  67. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyGetPrototypeOfReturn);
  68. return nullptr;
  69. }
  70. if (m_target.is_extensible()) {
  71. if (vm.exception())
  72. return nullptr;
  73. if (trap_result.is_null())
  74. return nullptr;
  75. return &trap_result.as_object();
  76. }
  77. auto target_proto = m_target.prototype();
  78. if (vm.exception())
  79. return nullptr;
  80. if (!same_value(trap_result, Value(target_proto))) {
  81. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyGetPrototypeOfNonExtensible);
  82. return nullptr;
  83. }
  84. return &trap_result.as_object();
  85. }
  86. const Object* ProxyObject::prototype() const
  87. {
  88. auto& vm = this->vm();
  89. if (m_is_revoked) {
  90. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked);
  91. return nullptr;
  92. }
  93. return const_cast<const Object*>(const_cast<ProxyObject*>(this)->prototype());
  94. }
  95. bool ProxyObject::set_prototype(Object* object)
  96. {
  97. auto& vm = this->vm();
  98. if (m_is_revoked) {
  99. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked);
  100. return false;
  101. }
  102. auto trap = Value(&m_handler).get_method(global_object(), vm.names.setPrototypeOf);
  103. if (vm.exception())
  104. return false;
  105. if (!trap)
  106. return m_target.set_prototype(object);
  107. auto trap_result = vm.call(*trap, Value(&m_handler), Value(&m_target), Value(object));
  108. if (vm.exception() || !trap_result.to_boolean())
  109. return false;
  110. if (m_target.is_extensible())
  111. return true;
  112. auto* target_proto = m_target.prototype();
  113. if (vm.exception())
  114. return false;
  115. if (!same_value(Value(object), Value(target_proto))) {
  116. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxySetPrototypeOfNonExtensible);
  117. return false;
  118. }
  119. return true;
  120. }
  121. bool ProxyObject::is_extensible() const
  122. {
  123. auto& vm = this->vm();
  124. if (m_is_revoked) {
  125. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked);
  126. return false;
  127. }
  128. auto trap = Value(&m_handler).get_method(global_object(), vm.names.isExtensible);
  129. if (vm.exception())
  130. return false;
  131. if (!trap)
  132. return m_target.is_extensible();
  133. auto trap_result = vm.call(*trap, Value(&m_handler), Value(&m_target));
  134. if (vm.exception())
  135. return false;
  136. if (trap_result.to_boolean() != m_target.is_extensible()) {
  137. if (!vm.exception())
  138. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyIsExtensibleReturn);
  139. return false;
  140. }
  141. return trap_result.to_boolean();
  142. }
  143. bool ProxyObject::prevent_extensions()
  144. {
  145. auto& vm = this->vm();
  146. if (m_is_revoked) {
  147. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked);
  148. return false;
  149. }
  150. auto trap = Value(&m_handler).get_method(global_object(), vm.names.preventExtensions);
  151. if (vm.exception())
  152. return false;
  153. if (!trap)
  154. return m_target.prevent_extensions();
  155. auto trap_result = vm.call(*trap, Value(&m_handler), Value(&m_target));
  156. if (vm.exception())
  157. return false;
  158. if (trap_result.to_boolean() && m_target.is_extensible()) {
  159. if (!vm.exception())
  160. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyPreventExtensionsReturn);
  161. return false;
  162. }
  163. return trap_result.to_boolean();
  164. }
  165. Optional<PropertyDescriptor> ProxyObject::get_own_property_descriptor(const PropertyName& name) const
  166. {
  167. auto& vm = this->vm();
  168. if (m_is_revoked) {
  169. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked);
  170. return {};
  171. }
  172. auto trap = Value(&m_handler).get_method(global_object(), vm.names.getOwnPropertyDescriptor);
  173. if (vm.exception())
  174. return {};
  175. if (!trap)
  176. return m_target.get_own_property_descriptor(name);
  177. auto trap_result = vm.call(*trap, Value(&m_handler), Value(&m_target), name.to_value(vm));
  178. if (vm.exception())
  179. return {};
  180. if (!trap_result.is_object() && !trap_result.is_undefined()) {
  181. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyGetOwnDescriptorReturn);
  182. return {};
  183. }
  184. auto target_desc = m_target.get_own_property_descriptor(name);
  185. if (vm.exception())
  186. return {};
  187. if (trap_result.is_undefined()) {
  188. if (!target_desc.has_value())
  189. return {};
  190. if (!target_desc.value().attributes.is_configurable()) {
  191. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyGetOwnDescriptorNonConfigurable);
  192. return {};
  193. }
  194. if (!m_target.is_extensible()) {
  195. if (!vm.exception())
  196. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyGetOwnDescriptorUndefinedReturn);
  197. return {};
  198. }
  199. return {};
  200. }
  201. auto result_desc = PropertyDescriptor::from_dictionary(vm, trap_result.as_object());
  202. if (vm.exception())
  203. return {};
  204. if (!is_compatible_property_descriptor(m_target.is_extensible(), result_desc, target_desc)) {
  205. if (!vm.exception())
  206. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyGetOwnDescriptorInvalidDescriptor);
  207. return {};
  208. }
  209. if (!result_desc.attributes.is_configurable() && (!target_desc.has_value() || target_desc.value().attributes.is_configurable())) {
  210. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyGetOwnDescriptorInvalidNonConfig);
  211. return {};
  212. }
  213. return result_desc;
  214. }
  215. bool ProxyObject::define_property(const StringOrSymbol& property_name, const Object& descriptor, bool throw_exceptions)
  216. {
  217. auto& vm = this->vm();
  218. if (m_is_revoked) {
  219. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked);
  220. return false;
  221. }
  222. auto trap = Value(&m_handler).get_method(global_object(), vm.names.defineProperty);
  223. if (vm.exception())
  224. return false;
  225. if (!trap)
  226. return m_target.define_property(property_name, descriptor, throw_exceptions);
  227. auto trap_result = vm.call(*trap, Value(&m_handler), Value(&m_target), property_name.to_value(vm), Value(const_cast<Object*>(&descriptor)));
  228. if (vm.exception() || !trap_result.to_boolean())
  229. return false;
  230. auto target_desc = m_target.get_own_property_descriptor(property_name);
  231. if (vm.exception())
  232. return false;
  233. bool setting_config_false = false;
  234. if (descriptor.has_property(vm.names.configurable) && !descriptor.get(vm.names.configurable).to_boolean())
  235. setting_config_false = true;
  236. if (vm.exception())
  237. return false;
  238. if (!target_desc.has_value()) {
  239. if (!m_target.is_extensible()) {
  240. if (!vm.exception())
  241. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyDefinePropNonExtensible);
  242. return false;
  243. }
  244. if (setting_config_false) {
  245. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyDefinePropNonConfigurableNonExisting);
  246. return false;
  247. }
  248. } else {
  249. if (!is_compatible_property_descriptor(m_target.is_extensible(), PropertyDescriptor::from_dictionary(vm, descriptor), target_desc)) {
  250. if (!vm.exception())
  251. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyDefinePropIncompatibleDescriptor);
  252. return false;
  253. }
  254. if (setting_config_false && target_desc.value().attributes.is_configurable()) {
  255. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyDefinePropExistingConfigurable);
  256. return false;
  257. }
  258. }
  259. return true;
  260. }
  261. bool ProxyObject::has_property(const PropertyName& name) const
  262. {
  263. auto& vm = this->vm();
  264. if (m_is_revoked) {
  265. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked);
  266. return false;
  267. }
  268. auto trap = Value(&m_handler).get_method(global_object(), vm.names.has);
  269. if (vm.exception())
  270. return false;
  271. if (!trap)
  272. return m_target.has_property(name);
  273. auto trap_result = vm.call(*trap, Value(&m_handler), Value(&m_target), name.to_value(vm));
  274. if (vm.exception())
  275. return false;
  276. if (!trap_result.to_boolean()) {
  277. auto target_desc = m_target.get_own_property_descriptor(name);
  278. if (vm.exception())
  279. return false;
  280. if (target_desc.has_value()) {
  281. if (!target_desc.value().attributes.is_configurable()) {
  282. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyHasExistingNonConfigurable);
  283. return false;
  284. }
  285. if (!m_target.is_extensible()) {
  286. if (!vm.exception())
  287. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyHasExistingNonExtensible);
  288. return false;
  289. }
  290. }
  291. }
  292. return trap_result.to_boolean();
  293. }
  294. Value ProxyObject::get(const PropertyName& name, Value receiver, AllowSideEffects allow_side_effects) const
  295. {
  296. auto& vm = this->vm();
  297. if (allow_side_effects == AllowSideEffects::No) {
  298. // Sorry, we're not going to call anything on this proxy.
  299. return js_string(vm, "<ProxyObject>");
  300. }
  301. if (m_is_revoked) {
  302. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked);
  303. return {};
  304. }
  305. if (receiver.is_empty())
  306. receiver = Value(const_cast<ProxyObject*>(this));
  307. auto trap = Value(&m_handler).get_method(global_object(), vm.names.get);
  308. if (vm.exception())
  309. return {};
  310. if (!trap)
  311. return m_target.get(name, receiver);
  312. auto trap_result = vm.call(*trap, Value(&m_handler), Value(&m_target), name.to_value(vm), receiver);
  313. if (vm.exception())
  314. return {};
  315. auto target_desc = m_target.get_own_property_descriptor(name);
  316. if (target_desc.has_value()) {
  317. if (vm.exception())
  318. return {};
  319. if (target_desc.value().is_data_descriptor() && !target_desc.value().attributes.is_writable() && !same_value(trap_result, target_desc.value().value)) {
  320. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyGetImmutableDataProperty);
  321. return {};
  322. }
  323. if (target_desc.value().is_accessor_descriptor() && target_desc.value().getter == nullptr && !trap_result.is_undefined()) {
  324. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyGetNonConfigurableAccessor);
  325. return {};
  326. }
  327. }
  328. return trap_result;
  329. }
  330. bool ProxyObject::put(const PropertyName& name, Value value, Value receiver)
  331. {
  332. auto& vm = this->vm();
  333. if (m_is_revoked) {
  334. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked);
  335. return false;
  336. }
  337. if (receiver.is_empty())
  338. receiver = Value(const_cast<ProxyObject*>(this));
  339. auto trap = Value(&m_handler).get_method(global_object(), vm.names.set);
  340. if (vm.exception())
  341. return false;
  342. if (!trap)
  343. return m_target.put(name, value, receiver);
  344. auto trap_result = vm.call(*trap, Value(&m_handler), Value(&m_target), name.to_value(vm), value, receiver);
  345. if (vm.exception() || !trap_result.to_boolean())
  346. return false;
  347. auto target_desc = m_target.get_own_property_descriptor(name);
  348. if (vm.exception())
  349. return false;
  350. if (target_desc.has_value() && !target_desc.value().attributes.is_configurable()) {
  351. if (target_desc.value().is_data_descriptor() && !target_desc.value().attributes.is_writable() && !same_value(value, target_desc.value().value)) {
  352. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxySetImmutableDataProperty);
  353. return false;
  354. }
  355. if (target_desc.value().is_accessor_descriptor() && !target_desc.value().setter) {
  356. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxySetNonConfigurableAccessor);
  357. }
  358. }
  359. return true;
  360. }
  361. bool ProxyObject::delete_property(PropertyName const& name, bool force_throw_exception)
  362. {
  363. auto& vm = this->vm();
  364. if (m_is_revoked) {
  365. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked);
  366. return false;
  367. }
  368. auto trap = Value(&m_handler).get_method(global_object(), vm.names.deleteProperty);
  369. if (vm.exception())
  370. return false;
  371. if (!trap)
  372. return m_target.delete_property(name, force_throw_exception);
  373. auto trap_result = vm.call(*trap, Value(&m_handler), Value(&m_target), name.to_value(vm));
  374. if (vm.exception())
  375. return false;
  376. if (!trap_result.to_boolean())
  377. return false;
  378. auto target_desc = m_target.get_own_property_descriptor(name);
  379. if (vm.exception())
  380. return false;
  381. if (!target_desc.has_value())
  382. return true;
  383. if (!target_desc.value().attributes.is_configurable()) {
  384. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyDeleteNonConfigurable);
  385. return false;
  386. }
  387. return true;
  388. }
  389. void ProxyObject::visit_edges(Cell::Visitor& visitor)
  390. {
  391. Function::visit_edges(visitor);
  392. visitor.visit(&m_target);
  393. visitor.visit(&m_handler);
  394. }
  395. Value ProxyObject::call()
  396. {
  397. auto& vm = this->vm();
  398. if (!is_function()) {
  399. vm.throw_exception<TypeError>(global_object(), ErrorType::NotAFunction, Value(this).to_string_without_side_effects());
  400. return {};
  401. }
  402. if (m_is_revoked) {
  403. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked);
  404. return {};
  405. }
  406. auto trap = Value(&m_handler).get_method(global_object(), vm.names.apply);
  407. if (vm.exception())
  408. return {};
  409. if (!trap)
  410. return static_cast<Function&>(m_target).call();
  411. MarkedValueList arguments(heap());
  412. arguments.append(Value(&m_target));
  413. arguments.append(Value(&m_handler));
  414. // FIXME: Pass global object
  415. auto arguments_array = Array::create(global_object());
  416. vm.for_each_argument([&](auto& argument) {
  417. arguments_array->indexed_properties().append(argument);
  418. });
  419. arguments.append(arguments_array);
  420. return vm.call(*trap, Value(&m_handler), move(arguments));
  421. }
  422. Value ProxyObject::construct(Function& new_target)
  423. {
  424. auto& vm = this->vm();
  425. if (!is_function()) {
  426. vm.throw_exception<TypeError>(global_object(), ErrorType::NotAConstructor, Value(this).to_string_without_side_effects());
  427. return {};
  428. }
  429. if (m_is_revoked) {
  430. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyRevoked);
  431. return {};
  432. }
  433. auto trap = Value(&m_handler).get_method(global_object(), vm.names.construct);
  434. if (vm.exception())
  435. return {};
  436. if (!trap)
  437. return static_cast<Function&>(m_target).construct(new_target);
  438. MarkedValueList arguments(vm.heap());
  439. arguments.append(Value(&m_target));
  440. auto arguments_array = Array::create(global_object());
  441. vm.for_each_argument([&](auto& argument) {
  442. arguments_array->indexed_properties().append(argument);
  443. });
  444. arguments.append(arguments_array);
  445. arguments.append(Value(&new_target));
  446. auto result = vm.call(*trap, Value(&m_handler), move(arguments));
  447. if (!result.is_object()) {
  448. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyConstructBadReturnType);
  449. return {};
  450. }
  451. return result;
  452. }
  453. const FlyString& ProxyObject::name() const
  454. {
  455. VERIFY(is_function());
  456. return static_cast<Function&>(m_target).name();
  457. }
  458. FunctionEnvironmentRecord* ProxyObject::create_environment_record(Function& function_being_invoked)
  459. {
  460. VERIFY(is_function());
  461. return static_cast<Function&>(m_target).create_environment_record(function_being_invoked);
  462. }
  463. }