ProxyObject.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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. // 5. Let trap be ? GetMethod(handler, "has").
  305. auto trap = TRY(Value(m_handler).get_method(vm, vm.names.has));
  306. // 6. If trap is undefined, then
  307. if (!trap) {
  308. // a. Return ? target.[[HasProperty]](P).
  309. return m_target->internal_has_property(property_key);
  310. }
  311. // 7. Let booleanTrapResult be ToBoolean(? Call(trap, handler, « target, P »)).
  312. auto trap_result = TRY(call(vm, *trap, m_handler, m_target, property_key_to_value(vm, property_key))).to_boolean();
  313. // 8. If booleanTrapResult is false, then
  314. if (!trap_result) {
  315. // a. Let targetDesc be ? target.[[GetOwnProperty]](P).
  316. auto target_descriptor = TRY(m_target->internal_get_own_property(property_key));
  317. // b. If targetDesc is not undefined, then
  318. if (target_descriptor.has_value()) {
  319. // i. If targetDesc.[[Configurable]] is false, throw a TypeError exception.
  320. if (!*target_descriptor->configurable)
  321. return vm.throw_completion<TypeError>(ErrorType::ProxyHasExistingNonConfigurable);
  322. // ii. Let extensibleTarget be ? IsExtensible(target).
  323. auto extensible_target = TRY(m_target->is_extensible());
  324. // iii. If extensibleTarget is false, throw a TypeError exception.
  325. if (!extensible_target)
  326. return vm.throw_completion<TypeError>(ErrorType::ProxyHasExistingNonExtensible);
  327. }
  328. }
  329. // 9. Return booleanTrapResult.
  330. return trap_result;
  331. }
  332. // 10.5.8 [[Get]] ( P, Receiver ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-get-p-receiver
  333. ThrowCompletionOr<Value> ProxyObject::internal_get(PropertyKey const& property_key, Value receiver) const
  334. {
  335. VERIFY(!receiver.is_empty());
  336. auto& vm = this->vm();
  337. VERIFY(property_key.is_valid());
  338. VERIFY(!receiver.is_empty());
  339. // 1. Let handler be O.[[ProxyHandler]].
  340. // 2. If handler is null, throw a TypeError exception.
  341. if (m_is_revoked)
  342. return vm.throw_completion<TypeError>(ErrorType::ProxyRevoked);
  343. // 3. Assert: Type(handler) is Object.
  344. // 4. Let target be O.[[ProxyTarget]].
  345. // NOTE: We need to protect ourselves from a Proxy with the handler's prototype set to the
  346. // Proxy itself, which would by default bounce between these functions indefinitely and lead to
  347. // a stack overflow when the Proxy's (p) or Proxy handler's (h) Object::get() is called and the
  348. // handler doesn't have a `get` trap:
  349. //
  350. // 1. p -> ProxyObject::internal_get() <- you are here
  351. // 2. h -> Value::get_method()
  352. // 3. h -> Value::get()
  353. // 4. h -> Object::internal_get()
  354. // 5. h -> Object::internal_get_prototype_of() (result is p)
  355. // 6. goto 1
  356. //
  357. // In JS code: `h = {}; p = new Proxy({}, h); h.__proto__ = p; p.foo // or h.foo`
  358. if (vm.did_reach_stack_space_limit())
  359. return vm.throw_completion<InternalError>(ErrorType::CallStackSizeExceeded);
  360. // 5. Let trap be ? GetMethod(handler, "get").
  361. auto trap = TRY(Value(m_handler).get_method(vm, vm.names.get));
  362. // 6. If trap is undefined, then
  363. if (!trap) {
  364. // a. Return ? target.[[Get]](P, Receiver).
  365. return m_target->internal_get(property_key, receiver);
  366. }
  367. // 7. Let trapResult be ? Call(trap, handler, « target, P, Receiver »).
  368. auto trap_result = TRY(call(vm, *trap, m_handler, m_target, property_key_to_value(vm, property_key), receiver));
  369. // 8. Let targetDesc be ? target.[[GetOwnProperty]](P).
  370. auto target_descriptor = TRY(m_target->internal_get_own_property(property_key));
  371. // 9. If targetDesc is not undefined and targetDesc.[[Configurable]] is false, then
  372. if (target_descriptor.has_value() && !*target_descriptor->configurable) {
  373. // a. If IsDataDescriptor(targetDesc) is true and targetDesc.[[Writable]] is false, then
  374. if (target_descriptor->is_data_descriptor() && !*target_descriptor->writable) {
  375. // i. If SameValue(trapResult, targetDesc.[[Value]]) is false, throw a TypeError exception.
  376. if (!same_value(trap_result, *target_descriptor->value))
  377. return vm.throw_completion<TypeError>(ErrorType::ProxyGetImmutableDataProperty);
  378. }
  379. // b. If IsAccessorDescriptor(targetDesc) is true and targetDesc.[[Get]] is undefined, then
  380. if (target_descriptor->is_accessor_descriptor() && !*target_descriptor->get) {
  381. // i. If trapResult is not undefined, throw a TypeError exception.
  382. if (!trap_result.is_undefined())
  383. return vm.throw_completion<TypeError>(ErrorType::ProxyGetNonConfigurableAccessor);
  384. }
  385. }
  386. // 10. Return trapResult.
  387. return trap_result;
  388. }
  389. // 10.5.9 [[Set]] ( P, V, Receiver ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-set-p-v-receiver
  390. ThrowCompletionOr<bool> ProxyObject::internal_set(PropertyKey const& property_key, Value value, Value receiver)
  391. {
  392. auto& vm = this->vm();
  393. VERIFY(property_key.is_valid());
  394. VERIFY(!value.is_empty());
  395. VERIFY(!receiver.is_empty());
  396. // 1. Let handler be O.[[ProxyHandler]].
  397. // 2. If handler is null, throw a TypeError exception.
  398. if (m_is_revoked)
  399. return vm.throw_completion<TypeError>(ErrorType::ProxyRevoked);
  400. // 3. Assert: Type(handler) is Object.
  401. // 4. Let target be O.[[ProxyTarget]].
  402. // 5. Let trap be ? GetMethod(handler, "set").
  403. auto trap = TRY(Value(m_handler).get_method(vm, vm.names.set));
  404. // 6. If trap is undefined, then
  405. if (!trap) {
  406. // a. Return ? target.[[Set]](P, V, Receiver).
  407. return m_target->internal_set(property_key, value, receiver);
  408. }
  409. // 7. Let booleanTrapResult be ToBoolean(? Call(trap, handler, « target, P, V, Receiver »)).
  410. auto trap_result = TRY(call(vm, *trap, m_handler, m_target, property_key_to_value(vm, property_key), value, receiver)).to_boolean();
  411. // 8. If booleanTrapResult is false, return false.
  412. if (!trap_result)
  413. return false;
  414. // 9. Let targetDesc be ? target.[[GetOwnProperty]](P).
  415. auto target_descriptor = TRY(m_target->internal_get_own_property(property_key));
  416. // 10. If targetDesc is not undefined and targetDesc.[[Configurable]] is false, then
  417. if (target_descriptor.has_value() && !*target_descriptor->configurable) {
  418. // a. If IsDataDescriptor(targetDesc) is true and targetDesc.[[Writable]] is false, then
  419. if (target_descriptor->is_data_descriptor() && !*target_descriptor->writable) {
  420. // i. If SameValue(V, targetDesc.[[Value]]) is false, throw a TypeError exception.
  421. if (!same_value(value, *target_descriptor->value))
  422. return vm.throw_completion<TypeError>(ErrorType::ProxySetImmutableDataProperty);
  423. }
  424. // b. If IsAccessorDescriptor(targetDesc) is true, then
  425. if (target_descriptor->is_accessor_descriptor()) {
  426. // i. If targetDesc.[[Set]] is undefined, throw a TypeError exception.
  427. if (!*target_descriptor->set)
  428. return vm.throw_completion<TypeError>(ErrorType::ProxySetNonConfigurableAccessor);
  429. }
  430. }
  431. // 11. Return true.
  432. return true;
  433. }
  434. // 10.5.10 [[Delete]] ( P ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-delete-p
  435. ThrowCompletionOr<bool> ProxyObject::internal_delete(PropertyKey const& property_key)
  436. {
  437. auto& vm = this->vm();
  438. VERIFY(property_key.is_valid());
  439. // 1. Let handler be O.[[ProxyHandler]].
  440. // 2. If handler is null, throw a TypeError exception.
  441. if (m_is_revoked)
  442. return vm.throw_completion<TypeError>(ErrorType::ProxyRevoked);
  443. // 3. Assert: Type(handler) is Object.
  444. // 4. Let target be O.[[ProxyTarget]].
  445. // 5. Let trap be ? GetMethod(handler, "deleteProperty").
  446. auto trap = TRY(Value(m_handler).get_method(vm, vm.names.deleteProperty));
  447. // 6. If trap is undefined, then
  448. if (!trap) {
  449. // a. Return ? target.[[Delete]](P).
  450. return m_target->internal_delete(property_key);
  451. }
  452. // 7. Let booleanTrapResult be ToBoolean(? Call(trap, handler, « target, P »)).
  453. auto trap_result = TRY(call(vm, *trap, m_handler, m_target, property_key_to_value(vm, property_key))).to_boolean();
  454. // 8. If booleanTrapResult is false, return false.
  455. if (!trap_result)
  456. return false;
  457. // 9. Let targetDesc be ? target.[[GetOwnProperty]](P).
  458. auto target_descriptor = TRY(m_target->internal_get_own_property(property_key));
  459. // 10. If targetDesc is undefined, return true.
  460. if (!target_descriptor.has_value())
  461. return true;
  462. // 11. If targetDesc.[[Configurable]] is false, throw a TypeError exception.
  463. if (!*target_descriptor->configurable)
  464. return vm.throw_completion<TypeError>(ErrorType::ProxyDeleteNonConfigurable);
  465. // 12. Let extensibleTarget be ? IsExtensible(target).
  466. auto extensible_target = TRY(m_target->is_extensible());
  467. // 13. If extensibleTarget is false, throw a TypeError exception.
  468. if (!extensible_target)
  469. return vm.throw_completion<TypeError>(ErrorType::ProxyDeleteNonExtensible);
  470. // 14. Return true.
  471. return true;
  472. }
  473. // 10.5.11 [[OwnPropertyKeys]] ( ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-ownpropertykeys
  474. ThrowCompletionOr<MarkedVector<Value>> ProxyObject::internal_own_property_keys() const
  475. {
  476. auto& vm = this->vm();
  477. // 1. Let handler be O.[[ProxyHandler]].
  478. // 2. If handler is null, throw a TypeError exception.
  479. if (m_is_revoked)
  480. return vm.throw_completion<TypeError>(ErrorType::ProxyRevoked);
  481. // 3. Assert: Type(handler) is Object.
  482. // 4. Let target be O.[[ProxyTarget]].
  483. // 5. Let trap be ? GetMethod(handler, "ownKeys").
  484. auto trap = TRY(Value(m_handler).get_method(vm, vm.names.ownKeys));
  485. // 6. If trap is undefined, then
  486. if (!trap) {
  487. // a. Return ? target.[[OwnPropertyKeys]]().
  488. return m_target->internal_own_property_keys();
  489. }
  490. // 7. Let trapResultArray be ? Call(trap, handler, « target »).
  491. auto trap_result_array = TRY(call(vm, *trap, m_handler, m_target));
  492. // 8. Let trapResult be ? CreateListFromArrayLike(trapResultArray, « String, Symbol »).
  493. HashTable<PropertyKey> unique_keys;
  494. auto trap_result = TRY(create_list_from_array_like(vm, trap_result_array, [&](auto value) -> ThrowCompletionOr<void> {
  495. if (!value.is_string() && !value.is_symbol())
  496. return vm.throw_completion<TypeError>(ErrorType::ProxyOwnPropertyKeysNotStringOrSymbol);
  497. auto property_key = MUST(value.to_property_key(vm));
  498. unique_keys.set(property_key, AK::HashSetExistingEntryBehavior::Keep);
  499. return {};
  500. }));
  501. // 9. If trapResult contains any duplicate entries, throw a TypeError exception.
  502. if (unique_keys.size() != trap_result.size())
  503. return vm.throw_completion<TypeError>(ErrorType::ProxyOwnPropertyKeysDuplicates);
  504. // 10. Let extensibleTarget be ? IsExtensible(target).
  505. auto extensible_target = TRY(m_target->is_extensible());
  506. // 11. Let targetKeys be ? target.[[OwnPropertyKeys]]().
  507. auto target_keys = TRY(m_target->internal_own_property_keys());
  508. // 12. Assert: targetKeys is a List of property keys.
  509. // 13. Assert: targetKeys contains no duplicate entries.
  510. // 14. Let targetConfigurableKeys be a new empty List.
  511. auto target_configurable_keys = MarkedVector<Value> { heap() };
  512. // 15. Let targetNonconfigurableKeys be a new empty List.
  513. auto target_nonconfigurable_keys = MarkedVector<Value> { heap() };
  514. // 16. For each element key of targetKeys, do
  515. for (auto& key : target_keys) {
  516. auto property_key = MUST(PropertyKey::from_value(vm, key));
  517. // a. Let desc be ? target.[[GetOwnProperty]](key).
  518. auto descriptor = TRY(m_target->internal_get_own_property(property_key));
  519. // b. If desc is not undefined and desc.[[Configurable]] is false, then
  520. if (descriptor.has_value() && !*descriptor->configurable) {
  521. // i. Append key as an element of targetNonconfigurableKeys.
  522. target_nonconfigurable_keys.append(key);
  523. }
  524. // c. Else,
  525. else {
  526. // i. Append key as an element of targetConfigurableKeys.
  527. target_configurable_keys.append(key);
  528. }
  529. }
  530. // 17. If extensibleTarget is true and targetNonconfigurableKeys is empty, then
  531. if (extensible_target && target_nonconfigurable_keys.is_empty()) {
  532. // a. Return trapResult.
  533. return { move(trap_result) };
  534. }
  535. // 18. Let uncheckedResultKeys be a List whose elements are the elements of trapResult.
  536. auto unchecked_result_keys = MarkedVector<Value> { heap() };
  537. unchecked_result_keys.extend(trap_result);
  538. // 19. For each element key of targetNonconfigurableKeys, do
  539. for (auto& key : target_nonconfigurable_keys) {
  540. // a. If key is not an element of uncheckedResultKeys, throw a TypeError exception.
  541. if (!unchecked_result_keys.contains_slow(key))
  542. return vm.throw_completion<TypeError>(ErrorType::ProxyOwnPropertyKeysSkippedNonconfigurableProperty, TRY_OR_THROW_OOM(vm, key.to_string_without_side_effects()));
  543. // b. Remove key from uncheckedResultKeys.
  544. unchecked_result_keys.remove_first_matching([&](auto& value) {
  545. return same_value(value, key);
  546. });
  547. }
  548. // 20. If extensibleTarget is true, return trapResult.
  549. if (extensible_target)
  550. return { move(trap_result) };
  551. // 21. For each element key of targetConfigurableKeys, do
  552. for (auto& key : target_configurable_keys) {
  553. // a. If key is not an element of uncheckedResultKeys, throw a TypeError exception.
  554. if (!unchecked_result_keys.contains_slow(key))
  555. return vm.throw_completion<TypeError>(ErrorType::ProxyOwnPropertyKeysNonExtensibleSkippedProperty, TRY_OR_THROW_OOM(vm, key.to_string_without_side_effects()));
  556. // b. Remove key from uncheckedResultKeys.
  557. unchecked_result_keys.remove_first_matching([&](auto& value) {
  558. return same_value(value, key);
  559. });
  560. }
  561. // 22. If uncheckedResultKeys is not empty, throw a TypeError exception.
  562. if (!unchecked_result_keys.is_empty())
  563. return vm.throw_completion<TypeError>(ErrorType::ProxyOwnPropertyKeysNonExtensibleNewProperty, TRY_OR_THROW_OOM(vm, unchecked_result_keys[0].to_string_without_side_effects()));
  564. // 23. Return trapResult.
  565. return { move(trap_result) };
  566. }
  567. // 10.5.12 [[Call]] ( thisArgument, argumentsList ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-call-thisargument-argumentslist
  568. ThrowCompletionOr<Value> ProxyObject::internal_call(Value this_argument, MarkedVector<Value> arguments_list)
  569. {
  570. auto& vm = this->vm();
  571. auto& realm = *vm.current_realm();
  572. // 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.
  573. VERIFY(is_function());
  574. // 1. Let handler be O.[[ProxyHandler]].
  575. // 2. If handler is null, throw a TypeError exception.
  576. if (m_is_revoked)
  577. return vm.throw_completion<TypeError>(ErrorType::ProxyRevoked);
  578. // 3. Assert: Type(handler) is Object.
  579. // 4. Let target be O.[[ProxyTarget]].
  580. // 5. Let trap be ? GetMethod(handler, "apply").
  581. auto trap = TRY(Value(m_handler).get_method(vm, vm.names.apply));
  582. // 6. If trap is undefined, then
  583. if (!trap) {
  584. // a. Return ? Call(target, thisArgument, argumentsList).
  585. return call(vm, m_target, this_argument, move(arguments_list));
  586. }
  587. // 7. Let argArray be CreateArrayFromList(argumentsList).
  588. auto arguments_array = Array::create_from(realm, arguments_list);
  589. // 8. Return ? Call(trap, handler, « target, thisArgument, argArray »).
  590. return call(vm, trap, m_handler, m_target, this_argument, arguments_array);
  591. }
  592. bool ProxyObject::has_constructor() const
  593. {
  594. // Note: A Proxy exotic object only has a [[Construct]] internal method if the initial value of
  595. // its [[ProxyTarget]] internal slot is an object that has a [[Construct]] internal method.
  596. if (!is_function())
  597. return false;
  598. return static_cast<FunctionObject&>(*m_target).has_constructor();
  599. }
  600. // 10.5.13 [[Construct]] ( argumentsList, newTarget ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-construct-argumentslist-newtarget
  601. ThrowCompletionOr<NonnullGCPtr<Object>> ProxyObject::internal_construct(MarkedVector<Value> arguments_list, FunctionObject& new_target)
  602. {
  603. auto& vm = this->vm();
  604. auto& realm = *vm.current_realm();
  605. // 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.
  606. VERIFY(is_function());
  607. // 1. Let handler be O.[[ProxyHandler]].
  608. // 2. If handler is null, throw a TypeError exception.
  609. if (m_is_revoked)
  610. return vm.throw_completion<TypeError>(ErrorType::ProxyRevoked);
  611. // 3. Assert: Type(handler) is Object.
  612. // 4. Let target be O.[[ProxyTarget]].
  613. // 5. Assert: IsConstructor(target) is true.
  614. // 6. Let trap be ? GetMethod(handler, "construct").
  615. auto trap = TRY(Value(m_handler).get_method(vm, vm.names.construct));
  616. // 7. If trap is undefined, then
  617. if (!trap) {
  618. // a. Return ? Construct(target, argumentsList, newTarget).
  619. return construct(vm, static_cast<FunctionObject&>(*m_target), move(arguments_list), &new_target);
  620. }
  621. // 8. Let argArray be CreateArrayFromList(argumentsList).
  622. auto arguments_array = Array::create_from(realm, arguments_list);
  623. // 9. Let newObj be ? Call(trap, handler, « target, argArray, newTarget »).
  624. auto new_object = TRY(call(vm, trap, m_handler, m_target, arguments_array, &new_target));
  625. // 10. If Type(newObj) is not Object, throw a TypeError exception.
  626. if (!new_object.is_object())
  627. return vm.throw_completion<TypeError>(ErrorType::ProxyConstructBadReturnType);
  628. // 11. Return newObj.
  629. return new_object.as_object();
  630. }
  631. void ProxyObject::visit_edges(Cell::Visitor& visitor)
  632. {
  633. Base::visit_edges(visitor);
  634. visitor.visit(m_target);
  635. visitor.visit(m_handler);
  636. }
  637. DeprecatedFlyString const& ProxyObject::name() const
  638. {
  639. VERIFY(is_function());
  640. return static_cast<FunctionObject&>(*m_target).name();
  641. }
  642. }