ProxyObject.cpp 37 KB

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