ProxyObject.cpp 38 KB

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