ProxyObject.cpp 38 KB

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