ProxyObject.cpp 36 KB

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