ProxyObject.cpp 38 KB

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