ProxyObject.cpp 36 KB

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