ProxyObject.cpp 37 KB

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