ProxyObject.cpp 18 KB

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