ProxyObject.cpp 36 KB

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