ProxyObject.cpp 38 KB

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