ProxyObject.cpp 36 KB

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