ProxyObject.cpp 36 KB

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