ProxyObject.cpp 38 KB

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