ProxyObject.cpp 38 KB

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