ProxyObject.cpp 36 KB

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