ProxyObject.cpp 37 KB

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