ProxyObject.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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/PropertyDescriptor.h>
  13. #include <LibJS/Runtime/ProxyObject.h>
  14. namespace JS {
  15. ProxyObject* ProxyObject::create(GlobalObject& global_object, Object& target, Object& handler)
  16. {
  17. return global_object.heap().allocate<ProxyObject>(global_object, target, handler, *global_object.object_prototype());
  18. }
  19. ProxyObject::ProxyObject(Object& target, Object& handler, Object& prototype)
  20. : FunctionObject(prototype)
  21. , m_target(target)
  22. , m_handler(handler)
  23. {
  24. }
  25. ProxyObject::~ProxyObject()
  26. {
  27. }
  28. static Value property_name_to_value(VM& vm, PropertyName const& name)
  29. {
  30. VERIFY(name.is_valid());
  31. if (name.is_symbol())
  32. return name.as_symbol();
  33. if (name.is_string())
  34. return js_string(vm, name.as_string());
  35. VERIFY(name.is_number());
  36. return js_string(vm, String::number(name.as_number()));
  37. }
  38. // 10.5.1 [[GetPrototypeOf]] ( ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-getprototypeof
  39. ThrowCompletionOr<Object*> ProxyObject::internal_get_prototype_of() const
  40. {
  41. auto& vm = this->vm();
  42. auto& global_object = this->global_object();
  43. // 1. Let handler be O.[[ProxyHandler]].
  44. // 2. If handler is null, throw a TypeError exception.
  45. if (m_is_revoked)
  46. return vm.throw_completion<TypeError>(global_object, ErrorType::ProxyRevoked);
  47. // 3. Assert: Type(handler) is Object.
  48. // 4. Let target be O.[[ProxyTarget]].
  49. // 5. Let trap be ? GetMethod(handler, "getPrototypeOf").
  50. auto trap = TRY(Value(&m_handler).get_method(global_object, vm.names.getPrototypeOf));
  51. // 6. If trap is undefined, then
  52. if (!trap) {
  53. // a. Return ? target.[[GetPrototypeOf]]().
  54. return TRY(m_target.internal_get_prototype_of());
  55. }
  56. // 7. Let handlerProto be ? Call(trap, handler, « target »).
  57. auto handler_proto = TRY(vm.call(*trap, &m_handler, &m_target));
  58. // 8. If Type(handlerProto) is neither Object nor Null, throw a TypeError exception.
  59. if (!handler_proto.is_object() && !handler_proto.is_null())
  60. return vm.throw_completion<TypeError>(global_object, ErrorType::ProxyGetPrototypeOfReturn);
  61. // 9. Let extensibleTarget be ? IsExtensible(target).
  62. auto extensible_target = m_target.is_extensible();
  63. if (auto* exception = vm.exception())
  64. return throw_completion(exception->value());
  65. // 10. If extensibleTarget is true, return handlerProto.
  66. if (extensible_target)
  67. return handler_proto.is_null() ? nullptr : &handler_proto.as_object();
  68. // 11. Let targetProto be ? target.[[GetPrototypeOf]]().
  69. auto* target_proto = TRY(m_target.internal_get_prototype_of());
  70. // 12. If SameValue(handlerProto, targetProto) is false, throw a TypeError exception.
  71. if (!same_value(handler_proto, target_proto))
  72. return vm.throw_completion<TypeError>(global_object, ErrorType::ProxyGetPrototypeOfNonExtensible);
  73. // 13. Return handlerProto.
  74. return handler_proto.is_null() ? nullptr : &handler_proto.as_object();
  75. }
  76. // 10.5.2 [[SetPrototypeOf]] ( V ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-setprototypeof-v
  77. bool ProxyObject::internal_set_prototype_of(Object* prototype)
  78. {
  79. auto& vm = this->vm();
  80. auto& global_object = this->global_object();
  81. // 1. Assert: Either Type(V) is Object or Type(V) is Null.
  82. // 2. Let handler be O.[[ProxyHandler]].
  83. // 3. If handler is null, throw a TypeError exception.
  84. if (m_is_revoked) {
  85. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyRevoked);
  86. return {};
  87. }
  88. // 4. Assert: Type(handler) is Object.
  89. // 5. Let target be O.[[ProxyTarget]].
  90. // 6. Let trap be ? GetMethod(handler, "setPrototypeOf").
  91. auto trap = TRY_OR_DISCARD(Value(&m_handler).get_method(global_object, vm.names.setPrototypeOf));
  92. // 7. If trap is undefined, then
  93. if (!trap) {
  94. // a. Return ? target.[[SetPrototypeOf]](V).
  95. return m_target.internal_set_prototype_of(prototype);
  96. }
  97. // 8. Let booleanTrapResult be ! ToBoolean(? Call(trap, handler, « target, V »)).
  98. auto trap_result = TRY_OR_DISCARD(vm.call(*trap, &m_handler, &m_target, prototype)).to_boolean();
  99. // 9. If booleanTrapResult is false, return false.
  100. if (!trap_result)
  101. return false;
  102. // 10. Let extensibleTarget be ? IsExtensible(target).
  103. auto extensible_target = m_target.is_extensible();
  104. if (vm.exception())
  105. return {};
  106. // 11. If extensibleTarget is true, return true.
  107. if (extensible_target)
  108. return true;
  109. // 12. Let targetProto be ? target.[[GetPrototypeOf]]().
  110. auto* target_proto = TRY_OR_DISCARD(m_target.internal_get_prototype_of());
  111. // 13. If SameValue(V, targetProto) is false, throw a TypeError exception.
  112. if (!same_value(prototype, target_proto)) {
  113. vm.throw_exception<TypeError>(global_object, ErrorType::ProxySetPrototypeOfNonExtensible);
  114. return {};
  115. }
  116. // 14. Return true.
  117. return true;
  118. }
  119. // 10.5.3 [[IsExtensible]] ( ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-isextensible
  120. bool ProxyObject::internal_is_extensible() const
  121. {
  122. auto& vm = this->vm();
  123. auto& global_object = this->global_object();
  124. // 1. Let handler be O.[[ProxyHandler]].
  125. // 2. If handler is null, throw a TypeError exception.
  126. if (m_is_revoked) {
  127. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyRevoked);
  128. return {};
  129. }
  130. // 3. Assert: Type(handler) is Object.
  131. // 4. Let target be O.[[ProxyTarget]].
  132. // 5. Let trap be ? GetMethod(handler, "isExtensible").
  133. auto trap = TRY_OR_DISCARD(Value(&m_handler).get_method(global_object, vm.names.isExtensible));
  134. // 6. If trap is undefined, then
  135. if (!trap) {
  136. // a. Return ? IsExtensible(target).
  137. return m_target.is_extensible();
  138. }
  139. // 7. Let booleanTrapResult be ! ToBoolean(? Call(trap, handler, « target »)).
  140. auto trap_result = TRY_OR_DISCARD(vm.call(*trap, &m_handler, &m_target)).to_boolean();
  141. // 8. Let targetResult be ? IsExtensible(target).
  142. auto target_result = m_target.is_extensible();
  143. if (vm.exception())
  144. return {};
  145. // 9. If SameValue(booleanTrapResult, targetResult) is false, throw a TypeError exception.
  146. if (trap_result != target_result) {
  147. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyIsExtensibleReturn);
  148. return {};
  149. }
  150. // 10. Return booleanTrapResult.
  151. return trap_result;
  152. }
  153. // 10.5.4 [[PreventExtensions]] ( ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-preventextensions
  154. bool ProxyObject::internal_prevent_extensions()
  155. {
  156. auto& vm = this->vm();
  157. auto& global_object = this->global_object();
  158. // 1. Let handler be O.[[ProxyHandler]].
  159. // 2. If handler is null, throw a TypeError exception.
  160. if (m_is_revoked) {
  161. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyRevoked);
  162. return {};
  163. }
  164. // 3. Assert: Type(handler) is Object.
  165. // 4. Let target be O.[[ProxyTarget]].
  166. // 5. Let trap be ? GetMethod(handler, "preventExtensions").
  167. auto trap = TRY_OR_DISCARD(Value(&m_handler).get_method(global_object, vm.names.preventExtensions));
  168. // 6. If trap is undefined, then
  169. if (!trap) {
  170. // a. Return ? target.[[PreventExtensions]]().
  171. return m_target.internal_prevent_extensions();
  172. }
  173. // 7. Let booleanTrapResult be ! ToBoolean(? Call(trap, handler, « target »)).
  174. auto trap_result = TRY_OR_DISCARD(vm.call(*trap, &m_handler, &m_target)).to_boolean();
  175. // 8. If booleanTrapResult is true, then
  176. if (trap_result) {
  177. // a. Let extensibleTarget be ? IsExtensible(target).
  178. auto extensible_target = m_target.is_extensible();
  179. if (vm.exception())
  180. return {};
  181. // b. If extensibleTarget is true, throw a TypeError exception.
  182. if (extensible_target) {
  183. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyPreventExtensionsReturn);
  184. return {};
  185. }
  186. }
  187. // 9. Return booleanTrapResult.
  188. return trap_result;
  189. }
  190. // 10.5.5 [[GetOwnProperty]] ( P ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-getownproperty-p
  191. Optional<PropertyDescriptor> ProxyObject::internal_get_own_property(const PropertyName& property_name) const
  192. {
  193. auto& vm = this->vm();
  194. auto& global_object = this->global_object();
  195. // 1. Assert: IsPropertyKey(P) is true.
  196. VERIFY(property_name.is_valid());
  197. // 2. Let handler be O.[[ProxyHandler]].
  198. // 3. If handler is null, throw a TypeError exception.
  199. if (m_is_revoked) {
  200. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyRevoked);
  201. return {};
  202. }
  203. // 4. Assert: Type(handler) is Object.
  204. // 5. Let target be O.[[ProxyTarget]].
  205. // 6. Let trap be ? GetMethod(handler, "getOwnPropertyDescriptor").
  206. auto trap = TRY_OR_DISCARD(Value(&m_handler).get_method(global_object, vm.names.getOwnPropertyDescriptor));
  207. // 7. If trap is undefined, then
  208. if (!trap) {
  209. // a. Return ? target.[[GetOwnProperty]](P).
  210. return m_target.internal_get_own_property(property_name);
  211. }
  212. // 8. Let trapResultObj be ? Call(trap, handler, « target, P »).
  213. auto trap_result = TRY_OR_DISCARD(vm.call(*trap, &m_handler, &m_target, property_name_to_value(vm, property_name)));
  214. // 9. If Type(trapResultObj) is neither Object nor Undefined, throw a TypeError exception.
  215. if (!trap_result.is_object() && !trap_result.is_undefined()) {
  216. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyGetOwnDescriptorReturn);
  217. return {};
  218. }
  219. // 10. Let targetDesc be ? target.[[GetOwnProperty]](P).
  220. auto target_descriptor = m_target.internal_get_own_property(property_name);
  221. if (vm.exception())
  222. return {};
  223. // 11. If trapResultObj is undefined, then
  224. if (trap_result.is_undefined()) {
  225. // a. If targetDesc is undefined, return undefined.
  226. if (!target_descriptor.has_value())
  227. return {};
  228. // b. If targetDesc.[[Configurable]] is false, throw a TypeError exception.
  229. if (!*target_descriptor->configurable) {
  230. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyGetOwnDescriptorNonConfigurable);
  231. return {};
  232. }
  233. // c. Let extensibleTarget be ? IsExtensible(target).
  234. auto extensible_target = m_target.is_extensible();
  235. if (vm.exception())
  236. return {};
  237. // d. If extensibleTarget is false, throw a TypeError exception.
  238. if (!extensible_target) {
  239. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyGetOwnDescriptorUndefinedReturn);
  240. return {};
  241. }
  242. // e. Return undefined.
  243. return {};
  244. }
  245. // 12. Let extensibleTarget be ? IsExtensible(target).
  246. auto extensible_target = m_target.is_extensible();
  247. if (vm.exception())
  248. return {};
  249. // 13. Let resultDesc be ? ToPropertyDescriptor(trapResultObj).
  250. auto result_desc = to_property_descriptor(global_object, trap_result);
  251. if (vm.exception())
  252. return {};
  253. // 14. Call CompletePropertyDescriptor(resultDesc).
  254. result_desc.complete();
  255. // 15. Let valid be IsCompatiblePropertyDescriptor(extensibleTarget, resultDesc, targetDesc).
  256. auto valid = is_compatible_property_descriptor(extensible_target, result_desc, target_descriptor);
  257. // 16. If valid is false, throw a TypeError exception.
  258. if (!valid) {
  259. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyGetOwnDescriptorInvalidDescriptor);
  260. return {};
  261. }
  262. // 17. If resultDesc.[[Configurable]] is false, then
  263. if (!*result_desc.configurable) {
  264. // a. If targetDesc is undefined or targetDesc.[[Configurable]] is true, then
  265. if (!target_descriptor.has_value() || *target_descriptor->configurable) {
  266. // i. Throw a TypeError exception.
  267. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyGetOwnDescriptorInvalidNonConfig);
  268. return {};
  269. }
  270. // b. If resultDesc has a [[Writable]] field and resultDesc.[[Writable]] is false, then
  271. if (result_desc.writable.has_value() && !*result_desc.writable) {
  272. // i. If targetDesc.[[Writable]] is true, throw a TypeError exception.
  273. if (*target_descriptor->writable) {
  274. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyGetOwnDescriptorNonConfigurableNonWritable);
  275. return {};
  276. }
  277. }
  278. }
  279. // 18. Return resultDesc.
  280. return result_desc;
  281. }
  282. // 10.5.6 [[DefineOwnProperty]] ( P, Desc ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-defineownproperty-p-desc
  283. bool ProxyObject::internal_define_own_property(PropertyName const& property_name, PropertyDescriptor const& property_descriptor)
  284. {
  285. auto& vm = this->vm();
  286. auto& global_object = this->global_object();
  287. // 1. Assert: IsPropertyKey(P) is true.
  288. VERIFY(property_name.is_valid());
  289. // 2. Let handler be O.[[ProxyHandler]].
  290. // 3. If handler is null, throw a TypeError exception.
  291. if (m_is_revoked) {
  292. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyRevoked);
  293. return {};
  294. }
  295. // 4. Assert: Type(handler) is Object.
  296. // 5. Let target be O.[[ProxyTarget]].
  297. // 6. Let trap be ? GetMethod(handler, "defineProperty").
  298. auto trap = TRY_OR_DISCARD(Value(&m_handler).get_method(global_object, vm.names.defineProperty));
  299. // 7. If trap is undefined, then
  300. if (!trap) {
  301. // a. Return ? target.[[DefineOwnProperty]](P, Desc).
  302. return m_target.internal_define_own_property(property_name, property_descriptor);
  303. }
  304. // 8. Let descObj be FromPropertyDescriptor(Desc).
  305. auto descriptor_object = from_property_descriptor(global_object, property_descriptor);
  306. // 9. Let booleanTrapResult be ! ToBoolean(? Call(trap, handler, « target, P, descObj »)).
  307. auto trap_result = TRY_OR_DISCARD(vm.call(*trap, &m_handler, &m_target, property_name_to_value(vm, property_name), descriptor_object)).to_boolean();
  308. // 10. If booleanTrapResult is false, return false.
  309. if (!trap_result)
  310. return false;
  311. // 11. Let targetDesc be ? target.[[GetOwnProperty]](P).
  312. auto target_descriptor = m_target.internal_get_own_property(property_name);
  313. if (vm.exception())
  314. return {};
  315. // 12. Let extensibleTarget be ? IsExtensible(target).
  316. auto extensible_target = m_target.is_extensible();
  317. if (vm.exception())
  318. return {};
  319. // 14. Else, let settingConfigFalse be false.
  320. bool setting_config_false = false;
  321. // 13. If Desc has a [[Configurable]] field and if Desc.[[Configurable]] is false, then
  322. if (property_descriptor.configurable.has_value() && !*property_descriptor.configurable) {
  323. // a. Let settingConfigFalse be true.
  324. setting_config_false = true;
  325. }
  326. // 15. If targetDesc is undefined, then
  327. if (!target_descriptor.has_value()) {
  328. // a. If extensibleTarget is false, throw a TypeError exception.
  329. if (!extensible_target) {
  330. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyDefinePropNonExtensible);
  331. return {};
  332. }
  333. // b. If settingConfigFalse is true, throw a TypeError exception.
  334. if (setting_config_false) {
  335. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyDefinePropNonConfigurableNonExisting);
  336. return {};
  337. }
  338. }
  339. // 16. Else,
  340. else {
  341. // a. If IsCompatiblePropertyDescriptor(extensibleTarget, Desc, targetDesc) is false, throw a TypeError exception.
  342. if (!is_compatible_property_descriptor(extensible_target, property_descriptor, target_descriptor)) {
  343. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyDefinePropIncompatibleDescriptor);
  344. return {};
  345. }
  346. // b. If settingConfigFalse is true and targetDesc.[[Configurable]] is true, throw a TypeError exception.
  347. if (setting_config_false && *target_descriptor->configurable) {
  348. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyDefinePropExistingConfigurable);
  349. return {};
  350. }
  351. // c. If IsDataDescriptor(targetDesc) is true, targetDesc.[[Configurable]] is false, and targetDesc.[[Writable]] is true, then
  352. if (target_descriptor->is_data_descriptor() && !*target_descriptor->configurable && *target_descriptor->writable) {
  353. // i. If Desc has a [[Writable]] field and Desc.[[Writable]] is false, throw a TypeError exception.
  354. if (property_descriptor.writable.has_value() && !*property_descriptor.writable) {
  355. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyDefinePropNonWritable);
  356. return {};
  357. }
  358. }
  359. }
  360. // 17. Return true.
  361. return true;
  362. }
  363. // 10.5.7 [[HasProperty]] ( P ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-hasproperty-p
  364. bool ProxyObject::internal_has_property(PropertyName const& property_name) const
  365. {
  366. auto& vm = this->vm();
  367. auto& global_object = this->global_object();
  368. // 1. Assert: IsPropertyKey(P) is true.
  369. VERIFY(property_name.is_valid());
  370. // 2. Let handler be O.[[ProxyHandler]].
  371. // 3. If handler is null, throw a TypeError exception.
  372. if (m_is_revoked) {
  373. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyRevoked);
  374. return {};
  375. }
  376. // 4. Assert: Type(handler) is Object.
  377. // 5. Let target be O.[[ProxyTarget]].
  378. // 6. Let trap be ? GetMethod(handler, "has").
  379. auto trap = TRY_OR_DISCARD(Value(&m_handler).get_method(global_object, vm.names.has));
  380. // 7. If trap is undefined, then
  381. if (!trap) {
  382. // a. Return ? target.[[HasProperty]](P).
  383. return m_target.internal_has_property(property_name);
  384. }
  385. // 8. Let booleanTrapResult be ! ToBoolean(? Call(trap, handler, « target, P »)).
  386. auto trap_result = TRY_OR_DISCARD(vm.call(*trap, &m_handler, &m_target, property_name_to_value(vm, property_name))).to_boolean();
  387. // 9. If booleanTrapResult is false, then
  388. if (!trap_result) {
  389. // a. Let targetDesc be ? target.[[GetOwnProperty]](P).
  390. auto target_descriptor = m_target.internal_get_own_property(property_name);
  391. if (vm.exception())
  392. return {};
  393. // b. If targetDesc is not undefined, then
  394. if (target_descriptor.has_value()) {
  395. // i. If targetDesc.[[Configurable]] is false, throw a TypeError exception.
  396. if (!*target_descriptor->configurable) {
  397. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyHasExistingNonConfigurable);
  398. return {};
  399. }
  400. // ii. Let extensibleTarget be ? IsExtensible(target).
  401. auto extensible_target = m_target.is_extensible();
  402. if (vm.exception())
  403. return {};
  404. // iii. If extensibleTarget is false, throw a TypeError exception.
  405. if (!extensible_target) {
  406. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyHasExistingNonExtensible);
  407. return false;
  408. }
  409. }
  410. }
  411. // 10. Return booleanTrapResult.
  412. return trap_result;
  413. }
  414. // 10.5.8 [[Get]] ( P, Receiver ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-get-p-receiver
  415. Value ProxyObject::internal_get(PropertyName const& property_name, Value receiver) const
  416. {
  417. VERIFY(!receiver.is_empty());
  418. auto& vm = this->vm();
  419. auto& global_object = this->global_object();
  420. // 1. Assert: IsPropertyKey(P) is true.
  421. VERIFY(property_name.is_valid());
  422. // 2. Let handler be O.[[ProxyHandler]].
  423. // 3. If handler is null, throw a TypeError exception.
  424. if (m_is_revoked) {
  425. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyRevoked);
  426. return {};
  427. }
  428. // 4. Assert: Type(handler) is Object.
  429. // 5. Let target be O.[[ProxyTarget]].
  430. // NOTE: We need to protect ourselves from a Proxy with the handler's prototype set to the
  431. // Proxy itself, which would by default bounce between these functions indefinitely and lead to
  432. // a stack overflow when the Proxy's (p) or Proxy handler's (h) Object::get() is called and the
  433. // handler doesn't have a `get` trap:
  434. //
  435. // 1. p -> ProxyObject::internal_get() <- you are here
  436. // 2. h -> Value::get_method()
  437. // 3. h -> Value::get()
  438. // 4. h -> Object::internal_get()
  439. // 5. h -> Object::internal_get_prototype_of() (result is p)
  440. // 6. goto 1
  441. //
  442. // In JS code: `h = {}; p = new Proxy({}, h); h.__proto__ = p; p.foo // or h.foo`
  443. if (vm.did_reach_stack_space_limit()) {
  444. vm.throw_exception<Error>(global_object, ErrorType::CallStackSizeExceeded);
  445. return {};
  446. }
  447. // 6. Let trap be ? GetMethod(handler, "get").
  448. auto trap = TRY_OR_DISCARD(Value(&m_handler).get_method(global_object, vm.names.get));
  449. // 7. If trap is undefined, then
  450. if (!trap) {
  451. // a. Return ? target.[[Get]](P, Receiver).
  452. return m_target.internal_get(property_name, receiver);
  453. }
  454. // 8. Let trapResult be ? Call(trap, handler, « target, P, Receiver »).
  455. auto trap_result = TRY_OR_DISCARD(vm.call(*trap, &m_handler, &m_target, property_name_to_value(vm, property_name), receiver));
  456. // 9. Let targetDesc be ? target.[[GetOwnProperty]](P).
  457. auto target_descriptor = m_target.internal_get_own_property(property_name);
  458. if (vm.exception())
  459. return {};
  460. // 10. If targetDesc is not undefined and targetDesc.[[Configurable]] is false, then
  461. if (target_descriptor.has_value() && !*target_descriptor->configurable) {
  462. // a. If IsDataDescriptor(targetDesc) is true and targetDesc.[[Writable]] is false, then
  463. if (target_descriptor->is_data_descriptor() && !*target_descriptor->writable) {
  464. // i. If SameValue(trapResult, targetDesc.[[Value]]) is false, throw a TypeError exception.
  465. if (!same_value(trap_result, *target_descriptor->value)) {
  466. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyGetImmutableDataProperty);
  467. return {};
  468. }
  469. }
  470. // b. If IsAccessorDescriptor(targetDesc) is true and targetDesc.[[Get]] is undefined, then
  471. if (target_descriptor->is_accessor_descriptor() && !*target_descriptor->get) {
  472. // i. If trapResult is not undefined, throw a TypeError exception.
  473. if (!trap_result.is_undefined()) {
  474. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyGetNonConfigurableAccessor);
  475. return {};
  476. }
  477. }
  478. }
  479. // 11. Return trapResult.
  480. return trap_result;
  481. }
  482. // 10.5.9 [[Set]] ( P, V, Receiver ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-set-p-v-receiver
  483. bool ProxyObject::internal_set(PropertyName const& property_name, Value value, Value receiver)
  484. {
  485. VERIFY(!value.is_empty());
  486. VERIFY(!receiver.is_empty());
  487. auto& vm = this->vm();
  488. auto& global_object = this->global_object();
  489. // 1. Assert: IsPropertyKey(P) is true.
  490. VERIFY(property_name.is_valid());
  491. // 2. Let handler be O.[[ProxyHandler]].
  492. // 3. If handler is null, throw a TypeError exception.
  493. if (m_is_revoked) {
  494. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyRevoked);
  495. return {};
  496. }
  497. // 4. Assert: Type(handler) is Object.
  498. // 5. Let target be O.[[ProxyTarget]].
  499. // 6. Let trap be ? GetMethod(handler, "set").
  500. auto trap = TRY_OR_DISCARD(Value(&m_handler).get_method(global_object, vm.names.set));
  501. // 7. If trap is undefined, then
  502. if (!trap) {
  503. // a. Return ? target.[[Set]](P, V, Receiver).
  504. return m_target.internal_set(property_name, value, receiver);
  505. }
  506. // 8. Let booleanTrapResult be ! ToBoolean(? Call(trap, handler, « target, P, V, Receiver »)).
  507. auto trap_result = TRY_OR_DISCARD(vm.call(*trap, &m_handler, &m_target, property_name_to_value(vm, property_name), value, receiver)).to_boolean();
  508. // 9. If booleanTrapResult is false, return false.
  509. if (!trap_result)
  510. return false;
  511. // 10. Let targetDesc be ? target.[[GetOwnProperty]](P).
  512. auto target_descriptor = m_target.internal_get_own_property(property_name);
  513. if (vm.exception())
  514. return {};
  515. // 11. If targetDesc is not undefined and targetDesc.[[Configurable]] is false, then
  516. if (target_descriptor.has_value() && !*target_descriptor->configurable) {
  517. // a. If IsDataDescriptor(targetDesc) is true and targetDesc.[[Writable]] is false, then
  518. if (target_descriptor->is_data_descriptor() && !*target_descriptor->writable) {
  519. // i. If SameValue(V, targetDesc.[[Value]]) is false, throw a TypeError exception.
  520. if (!same_value(value, *target_descriptor->value)) {
  521. vm.throw_exception<TypeError>(global_object, ErrorType::ProxySetImmutableDataProperty);
  522. return {};
  523. }
  524. }
  525. // b. If IsAccessorDescriptor(targetDesc) is true, then
  526. if (target_descriptor->is_accessor_descriptor()) {
  527. // i. If targetDesc.[[Set]] is undefined, throw a TypeError exception.
  528. if (!*target_descriptor->set) {
  529. vm.throw_exception<TypeError>(global_object, ErrorType::ProxySetNonConfigurableAccessor);
  530. return {};
  531. }
  532. }
  533. }
  534. // 12. Return true.
  535. return true;
  536. }
  537. // 10.5.10 [[Delete]] ( P ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-delete-p
  538. bool ProxyObject::internal_delete(PropertyName const& property_name)
  539. {
  540. auto& vm = this->vm();
  541. auto& global_object = this->global_object();
  542. // 1. Assert: IsPropertyKey(P) is true.
  543. VERIFY(property_name.is_valid());
  544. // 2. Let handler be O.[[ProxyHandler]].
  545. // 3. If handler is null, throw a TypeError exception.
  546. if (m_is_revoked) {
  547. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyRevoked);
  548. return {};
  549. }
  550. // 4. Assert: Type(handler) is Object.
  551. // 5. Let target be O.[[ProxyTarget]].
  552. // 6. Let trap be ? GetMethod(handler, "deleteProperty").
  553. auto trap = TRY_OR_DISCARD(Value(&m_handler).get_method(global_object, vm.names.deleteProperty));
  554. // 7. If trap is undefined, then
  555. if (!trap) {
  556. // a. Return ? target.[[Delete]](P).
  557. return m_target.internal_delete(property_name);
  558. }
  559. // 8. Let booleanTrapResult be ! ToBoolean(? Call(trap, handler, « target, P »)).
  560. auto trap_result = TRY_OR_DISCARD(vm.call(*trap, &m_handler, &m_target, property_name_to_value(vm, property_name))).to_boolean();
  561. // 9. If booleanTrapResult is false, return false.
  562. if (!trap_result)
  563. return false;
  564. // 10. Let targetDesc be ? target.[[GetOwnProperty]](P).
  565. auto target_descriptor = m_target.internal_get_own_property(property_name);
  566. if (vm.exception())
  567. return {};
  568. // 11. If targetDesc is undefined, return true.
  569. if (!target_descriptor.has_value())
  570. return true;
  571. // 12. If targetDesc.[[Configurable]] is false, throw a TypeError exception.
  572. if (!*target_descriptor->configurable) {
  573. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyDeleteNonConfigurable);
  574. return {};
  575. }
  576. // 13. Let extensibleTarget be ? IsExtensible(target).
  577. auto extensible_target = m_target.is_extensible();
  578. if (vm.exception())
  579. return {};
  580. // 14. If extensibleTarget is false, throw a TypeError exception.
  581. if (!extensible_target) {
  582. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyDeleteNonExtensible);
  583. return {};
  584. }
  585. // 15. Return true.
  586. return true;
  587. }
  588. // 10.5.11 [[OwnPropertyKeys]] ( ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-ownpropertykeys
  589. MarkedValueList ProxyObject::internal_own_property_keys() const
  590. {
  591. auto& vm = this->vm();
  592. auto& global_object = this->global_object();
  593. // 1. Let handler be O.[[ProxyHandler]].
  594. // 2. If handler is null, throw a TypeError exception.
  595. if (m_is_revoked) {
  596. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyRevoked);
  597. return MarkedValueList { heap() };
  598. }
  599. // 3. Assert: Type(handler) is Object.
  600. // 4. Let target be O.[[ProxyTarget]].
  601. // 5. Let trap be ? GetMethod(handler, "ownKeys").
  602. auto trap_or_error = Value(&m_handler).get_method(global_object, vm.names.ownKeys);
  603. if (trap_or_error.is_error())
  604. return MarkedValueList { heap() };
  605. auto trap = trap_or_error.release_value();
  606. // 6. If trap is undefined, then
  607. if (!trap) {
  608. // a. Return ? target.[[OwnPropertyKeys]]().
  609. return m_target.internal_own_property_keys();
  610. }
  611. // 7. Let trapResultArray be ? Call(trap, handler, « target »).
  612. auto trap_result_array_or_error = vm.call(*trap, &m_handler, &m_target);
  613. if (trap_result_array_or_error.is_error())
  614. return MarkedValueList { heap() };
  615. auto trap_result_array = trap_result_array_or_error.release_value();
  616. // 8. Let trapResult be ? CreateListFromArrayLike(trapResultArray, « String, Symbol »).
  617. HashTable<StringOrSymbol> unique_keys;
  618. auto throw_completion_or_trap_result = create_list_from_array_like(global_object, trap_result_array, [&](auto value) -> ThrowCompletionOr<void> {
  619. auto& vm = global_object.vm();
  620. if (!value.is_string() && !value.is_symbol())
  621. return vm.throw_completion<TypeError>(global_object, ErrorType::ProxyOwnPropertyKeysNotStringOrSymbol);
  622. auto property_key = value.to_property_key(global_object);
  623. VERIFY(!vm.exception());
  624. unique_keys.set(property_key, AK::HashSetExistingEntryBehavior::Keep);
  625. return {};
  626. });
  627. // TODO: This becomes a lot nicer once this function returns a ThrowCompletionOr as well.
  628. if (throw_completion_or_trap_result.is_throw_completion())
  629. return MarkedValueList { heap() };
  630. auto trap_result = throw_completion_or_trap_result.release_value();
  631. // 9. If trapResult contains any duplicate entries, throw a TypeError exception.
  632. if (unique_keys.size() != trap_result.size()) {
  633. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyOwnPropertyKeysDuplicates);
  634. return MarkedValueList { heap() };
  635. }
  636. // 10. Let extensibleTarget be ? IsExtensible(target).
  637. auto extensible_target = m_target.is_extensible();
  638. if (vm.exception())
  639. return MarkedValueList { heap() };
  640. // 11. Let targetKeys be ? target.[[OwnPropertyKeys]]().
  641. auto target_keys = m_target.internal_own_property_keys();
  642. if (vm.exception())
  643. return MarkedValueList { heap() };
  644. // 12. Assert: targetKeys is a List whose elements are only String and Symbol values.
  645. // 13. Assert: targetKeys contains no duplicate entries.
  646. // 14. Let targetConfigurableKeys be a new empty List.
  647. auto target_configurable_keys = MarkedValueList { heap() };
  648. // 15. Let targetNonconfigurableKeys be a new empty List.
  649. auto target_nonconfigurable_keys = MarkedValueList { heap() };
  650. // 16. For each element key of targetKeys, do
  651. for (auto& key : target_keys) {
  652. // a. Let desc be ? target.[[GetOwnProperty]](key).
  653. auto descriptor = m_target.internal_get_own_property(PropertyName::from_value(global_object, key));
  654. // b. If desc is not undefined and desc.[[Configurable]] is false, then
  655. if (descriptor.has_value() && !*descriptor->configurable) {
  656. // i. Append key as an element of targetNonconfigurableKeys.
  657. target_nonconfigurable_keys.append(key);
  658. }
  659. // c. Else,
  660. else {
  661. // i. Append key as an element of targetConfigurableKeys.
  662. target_configurable_keys.append(key);
  663. }
  664. }
  665. // 17. If extensibleTarget is true and targetNonconfigurableKeys is empty, then
  666. if (extensible_target && target_nonconfigurable_keys.is_empty()) {
  667. // a. Return trapResult.
  668. return trap_result;
  669. }
  670. // 18. Let uncheckedResultKeys be a List whose elements are the elements of trapResult.
  671. auto unchecked_result_keys = MarkedValueList { heap() };
  672. unchecked_result_keys.extend(trap_result);
  673. // 19. For each element key of targetNonconfigurableKeys, do
  674. for (auto& key : target_nonconfigurable_keys) {
  675. // a. If key is not an element of uncheckedResultKeys, throw a TypeError exception.
  676. if (!unchecked_result_keys.contains_slow(key)) {
  677. vm.throw_exception<TypeError>(global_object, ErrorType::FixmeAddAnErrorString);
  678. return MarkedValueList { heap() };
  679. }
  680. // b. Remove key from uncheckedResultKeys.
  681. unchecked_result_keys.remove_first_matching([&](auto& value) {
  682. return same_value(value, key);
  683. });
  684. }
  685. // 20. If extensibleTarget is true, return trapResult.
  686. if (extensible_target)
  687. return trap_result;
  688. // 21. For each element key of targetConfigurableKeys, do
  689. for (auto& key : target_configurable_keys) {
  690. // a. If key is not an element of uncheckedResultKeys, throw a TypeError exception.
  691. if (!unchecked_result_keys.contains_slow(key)) {
  692. vm.throw_exception<TypeError>(global_object, ErrorType::FixmeAddAnErrorString);
  693. return MarkedValueList { heap() };
  694. }
  695. // b. Remove key from uncheckedResultKeys.
  696. unchecked_result_keys.remove_first_matching([&](auto& value) {
  697. return same_value(value, key);
  698. });
  699. }
  700. // 22. If uncheckedResultKeys is not empty, throw a TypeError exception.
  701. if (!unchecked_result_keys.is_empty()) {
  702. vm.throw_exception<TypeError>(global_object, ErrorType::FixmeAddAnErrorString);
  703. return MarkedValueList { heap() };
  704. }
  705. // 23. Return trapResult.
  706. return trap_result;
  707. }
  708. // 10.5.12 [[Call]] ( thisArgument, argumentsList ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-call-thisargument-argumentslist
  709. Value ProxyObject::call()
  710. {
  711. auto& vm = this->vm();
  712. auto& global_object = this->global_object();
  713. auto this_argument = vm.this_value(global_object);
  714. // A Proxy exotic object only has a [[Call]] internal method if the initial value of its [[ProxyTarget]] internal slot is an object that has a [[Call]] internal method.
  715. if (!is_function()) {
  716. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, Value(this).to_string_without_side_effects());
  717. return {};
  718. }
  719. // 1. Let handler be O.[[ProxyHandler]].
  720. // 2. If handler is null, throw a TypeError exception.
  721. if (m_is_revoked) {
  722. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyRevoked);
  723. return {};
  724. }
  725. // 3. Assert: Type(handler) is Object.
  726. // 4. Let target be O.[[ProxyTarget]].
  727. // 5. Let trap be ? GetMethod(handler, "apply").
  728. auto trap = TRY_OR_DISCARD(Value(&m_handler).get_method(global_object, vm.names.apply));
  729. // 6. If trap is undefined, then
  730. if (!trap) {
  731. // a. Return ? Call(target, thisArgument, argumentsList).
  732. return static_cast<FunctionObject&>(m_target).call();
  733. }
  734. // 7. Let argArray be ! CreateArrayFromList(argumentsList).
  735. auto arguments_array = Array::create(global_object, 0);
  736. vm.for_each_argument([&](auto& argument) {
  737. arguments_array->indexed_properties().append(argument);
  738. });
  739. // 8. Return ? Call(trap, handler, « target, thisArgument, argArray »).
  740. return TRY_OR_DISCARD(vm.call(*trap, &m_handler, &m_target, this_argument, arguments_array));
  741. }
  742. // 10.5.13 [[Construct]] ( argumentsList, newTarget ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-construct-argumentslist-newtarget
  743. Value ProxyObject::construct(FunctionObject& new_target)
  744. {
  745. auto& vm = this->vm();
  746. auto& global_object = this->global_object();
  747. // A Proxy exotic object only has a [[Construct]] internal method if the initial value of its [[ProxyTarget]] internal slot is an object that has a [[Construct]] internal method.
  748. if (!is_function()) {
  749. vm.throw_exception<TypeError>(global_object, ErrorType::NotAConstructor, Value(this).to_string_without_side_effects());
  750. return {};
  751. }
  752. // 1. Let handler be O.[[ProxyHandler]].
  753. // 2. If handler is null, throw a TypeError exception.
  754. if (m_is_revoked) {
  755. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyRevoked);
  756. return {};
  757. }
  758. // 3. Assert: Type(handler) is Object.
  759. // 4. Let target be O.[[ProxyTarget]].
  760. // 5. Assert: IsConstructor(target) is true.
  761. // 6. Let trap be ? GetMethod(handler, "construct").
  762. auto trap = TRY_OR_DISCARD(Value(&m_handler).get_method(global_object, vm.names.construct));
  763. // 7. If trap is undefined, then
  764. if (!trap) {
  765. // a. Return ? Construct(target, argumentsList, newTarget).
  766. return static_cast<FunctionObject&>(m_target).construct(new_target);
  767. }
  768. // 8. Let argArray be ! CreateArrayFromList(argumentsList).
  769. auto arguments_array = Array::create(global_object, 0);
  770. vm.for_each_argument([&](auto& argument) {
  771. arguments_array->indexed_properties().append(argument);
  772. });
  773. // 9. Let newObj be ? Call(trap, handler, « target, argArray, newTarget »).
  774. auto result = TRY_OR_DISCARD(vm.call(*trap, &m_handler, &m_target, arguments_array, &new_target));
  775. // 10. If Type(newObj) is not Object, throw a TypeError exception.
  776. if (!result.is_object()) {
  777. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyConstructBadReturnType);
  778. return {};
  779. }
  780. // 11. Return newObj.
  781. return result;
  782. }
  783. void ProxyObject::visit_edges(Cell::Visitor& visitor)
  784. {
  785. Base::visit_edges(visitor);
  786. visitor.visit(&m_target);
  787. visitor.visit(&m_handler);
  788. }
  789. const FlyString& ProxyObject::name() const
  790. {
  791. VERIFY(is_function());
  792. return static_cast<FunctionObject&>(m_target).name();
  793. }
  794. FunctionEnvironment* ProxyObject::create_environment(FunctionObject& function_being_invoked)
  795. {
  796. VERIFY(is_function());
  797. return static_cast<FunctionObject&>(m_target).create_environment(function_being_invoked);
  798. }
  799. }