ProxyObject.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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. // 6. Let trap be ? GetMethod(handler, "get").
  469. auto trap = Value(&m_handler).get_method(global_object, vm.names.get);
  470. if (vm.exception())
  471. return {};
  472. // 7. If trap is undefined, then
  473. if (!trap) {
  474. // a. Return ? target.[[Get]](P, Receiver).
  475. return m_target.internal_get(property_name, receiver);
  476. }
  477. // 8. Let trapResult be ? Call(trap, handler, « target, P, Receiver »).
  478. auto trap_result = vm.call(*trap, &m_handler, &m_target, property_name_to_value(vm, property_name), receiver);
  479. if (vm.exception())
  480. return {};
  481. // 9. Let targetDesc be ? target.[[GetOwnProperty]](P).
  482. auto target_descriptor = m_target.internal_get_own_property(property_name);
  483. if (vm.exception())
  484. return {};
  485. // 10. If targetDesc is not undefined and targetDesc.[[Configurable]] is false, then
  486. if (target_descriptor.has_value() && !*target_descriptor->configurable) {
  487. // a. If IsDataDescriptor(targetDesc) is true and targetDesc.[[Writable]] is false, then
  488. if (target_descriptor->is_data_descriptor() && !*target_descriptor->writable) {
  489. // i. If SameValue(trapResult, targetDesc.[[Value]]) is false, throw a TypeError exception.
  490. if (!same_value(trap_result, *target_descriptor->value)) {
  491. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyGetImmutableDataProperty);
  492. return {};
  493. }
  494. }
  495. // b. If IsAccessorDescriptor(targetDesc) is true and targetDesc.[[Get]] is undefined, then
  496. if (target_descriptor->is_accessor_descriptor() && !*target_descriptor->get) {
  497. // i. If trapResult is not undefined, throw a TypeError exception.
  498. if (!trap_result.is_undefined()) {
  499. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyGetNonConfigurableAccessor);
  500. return {};
  501. }
  502. }
  503. }
  504. // 11. Return trapResult.
  505. return trap_result;
  506. }
  507. // 10.5.9 [[Set]] ( P, V, Receiver ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-set-p-v-receiver
  508. bool ProxyObject::internal_set(PropertyName const& property_name, Value value, Value receiver)
  509. {
  510. VERIFY(!value.is_empty());
  511. VERIFY(!receiver.is_empty());
  512. auto& vm = this->vm();
  513. auto& global_object = this->global_object();
  514. // 1. Assert: IsPropertyKey(P) is true.
  515. VERIFY(property_name.is_valid());
  516. // 2. Let handler be O.[[ProxyHandler]].
  517. // 3. If handler is null, throw a TypeError exception.
  518. if (m_is_revoked) {
  519. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyRevoked);
  520. return {};
  521. }
  522. // 4. Assert: Type(handler) is Object.
  523. // 5. Let target be O.[[ProxyTarget]].
  524. // 6. Let trap be ? GetMethod(handler, "set").
  525. auto trap = Value(&m_handler).get_method(global_object, vm.names.set);
  526. if (vm.exception())
  527. return {};
  528. // 7. If trap is undefined, then
  529. if (!trap) {
  530. // a. Return ? target.[[Set]](P, V, Receiver).
  531. return m_target.internal_set(property_name, value, receiver);
  532. }
  533. // 8. Let booleanTrapResult be ! ToBoolean(? Call(trap, handler, « target, P, V, Receiver »)).
  534. auto trap_result = vm.call(*trap, &m_handler, &m_target, property_name_to_value(vm, property_name), value, receiver);
  535. if (vm.exception())
  536. return {};
  537. // 9. If booleanTrapResult is false, return false.
  538. if (!trap_result.to_boolean())
  539. return false;
  540. // 10. Let targetDesc be ? target.[[GetOwnProperty]](P).
  541. auto target_descriptor = m_target.internal_get_own_property(property_name);
  542. if (vm.exception())
  543. return {};
  544. // 11. If targetDesc is not undefined and targetDesc.[[Configurable]] is false, then
  545. if (target_descriptor.has_value() && !*target_descriptor->configurable) {
  546. // a. If IsDataDescriptor(targetDesc) is true and targetDesc.[[Writable]] is false, then
  547. if (target_descriptor->is_data_descriptor() && !*target_descriptor->writable) {
  548. // i. If SameValue(V, targetDesc.[[Value]]) is false, throw a TypeError exception.
  549. if (!same_value(value, *target_descriptor->value)) {
  550. vm.throw_exception<TypeError>(global_object, ErrorType::ProxySetImmutableDataProperty);
  551. return {};
  552. }
  553. }
  554. // b. If IsAccessorDescriptor(targetDesc) is true, then
  555. if (target_descriptor->is_accessor_descriptor()) {
  556. // i. If targetDesc.[[Set]] is undefined, throw a TypeError exception.
  557. if (!*target_descriptor->set) {
  558. vm.throw_exception<TypeError>(global_object, ErrorType::ProxySetNonConfigurableAccessor);
  559. return {};
  560. }
  561. }
  562. }
  563. // 12. Return true.
  564. return true;
  565. }
  566. // 10.5.10 [[Delete]] ( P ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-delete-p
  567. bool ProxyObject::internal_delete(PropertyName const& property_name)
  568. {
  569. auto& vm = this->vm();
  570. auto& global_object = this->global_object();
  571. // 1. Assert: IsPropertyKey(P) is true.
  572. VERIFY(property_name.is_valid());
  573. // 2. Let handler be O.[[ProxyHandler]].
  574. // 3. If handler is null, throw a TypeError exception.
  575. if (m_is_revoked) {
  576. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyRevoked);
  577. return {};
  578. }
  579. // 4. Assert: Type(handler) is Object.
  580. // 5. Let target be O.[[ProxyTarget]].
  581. // 6. Let trap be ? GetMethod(handler, "deleteProperty").
  582. auto trap = Value(&m_handler).get_method(global_object, vm.names.deleteProperty);
  583. if (vm.exception())
  584. return {};
  585. // 7. If trap is undefined, then
  586. if (!trap) {
  587. // a. Return ? target.[[Delete]](P).
  588. return m_target.internal_delete(property_name);
  589. }
  590. // 8. Let booleanTrapResult be ! ToBoolean(? Call(trap, handler, « target, P »)).
  591. auto trap_result = vm.call(*trap, &m_handler, &m_target, property_name_to_value(vm, property_name));
  592. if (vm.exception())
  593. return {};
  594. // 9. If booleanTrapResult is false, return false.
  595. if (!trap_result.to_boolean())
  596. return false;
  597. // 10. Let targetDesc be ? target.[[GetOwnProperty]](P).
  598. auto target_descriptor = m_target.internal_get_own_property(property_name);
  599. if (vm.exception())
  600. return {};
  601. // 11. If targetDesc is undefined, return true.
  602. if (!target_descriptor.has_value())
  603. return true;
  604. // 12. If targetDesc.[[Configurable]] is false, throw a TypeError exception.
  605. if (!*target_descriptor->configurable) {
  606. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyDeleteNonConfigurable);
  607. return {};
  608. }
  609. // 13. Let extensibleTarget be ? IsExtensible(target).
  610. auto extensible_target = m_target.is_extensible();
  611. if (vm.exception())
  612. return {};
  613. // 14. If extensibleTarget is false, throw a TypeError exception.
  614. if (!extensible_target) {
  615. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyDeleteNonExtensible);
  616. return {};
  617. }
  618. // 15. Return true.
  619. return true;
  620. }
  621. // 10.5.11 [[OwnPropertyKeys]] ( ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-ownpropertykeys
  622. MarkedValueList ProxyObject::internal_own_property_keys() const
  623. {
  624. auto& vm = this->vm();
  625. auto& global_object = this->global_object();
  626. // 1. Let handler be O.[[ProxyHandler]].
  627. // 2. If handler is null, throw a TypeError exception.
  628. if (m_is_revoked) {
  629. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyRevoked);
  630. return MarkedValueList { heap() };
  631. }
  632. // 3. Assert: Type(handler) is Object.
  633. // 4. Let target be O.[[ProxyTarget]].
  634. // 5. Let trap be ? GetMethod(handler, "ownKeys").
  635. auto trap = Value(&m_handler).get_method(global_object, vm.names.ownKeys);
  636. if (vm.exception())
  637. return MarkedValueList { heap() };
  638. // 6. If trap is undefined, then
  639. if (!trap) {
  640. // a. Return ? target.[[OwnPropertyKeys]]().
  641. return m_target.internal_own_property_keys();
  642. }
  643. // 7. Let trapResultArray be ? Call(trap, handler, « target »).
  644. auto trap_result_array = vm.call(*trap, &m_handler, &m_target);
  645. if (vm.exception())
  646. return MarkedValueList { heap() };
  647. // 8. Let trapResult be ? CreateListFromArrayLike(trapResultArray, « String, Symbol »).
  648. HashTable<StringOrSymbol> unique_keys;
  649. auto trap_result = create_list_from_array_like(global_object, trap_result_array, [&](auto value) {
  650. auto& vm = global_object.vm();
  651. if (!value.is_string() && !value.is_symbol()) {
  652. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyOwnPropertyKeysNotStringOrSymbol);
  653. return;
  654. }
  655. auto property_key = value.to_property_key(global_object);
  656. VERIFY(!vm.exception());
  657. unique_keys.set(property_key, AK::HashSetExistingEntryBehavior::Keep);
  658. });
  659. // 9. If trapResult contains any duplicate entries, throw a TypeError exception.
  660. if (unique_keys.size() != trap_result.size()) {
  661. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyOwnPropertyKeysDuplicates);
  662. return MarkedValueList { heap() };
  663. }
  664. // 10. Let extensibleTarget be ? IsExtensible(target).
  665. auto extensible_target = m_target.is_extensible();
  666. if (vm.exception())
  667. return MarkedValueList { heap() };
  668. // 11. Let targetKeys be ? target.[[OwnPropertyKeys]]().
  669. auto target_keys = m_target.internal_own_property_keys();
  670. if (vm.exception())
  671. return MarkedValueList { heap() };
  672. // 12. Assert: targetKeys is a List whose elements are only String and Symbol values.
  673. // 13. Assert: targetKeys contains no duplicate entries.
  674. // 14. Let targetConfigurableKeys be a new empty List.
  675. auto target_configurable_keys = MarkedValueList { heap() };
  676. // 15. Let targetNonconfigurableKeys be a new empty List.
  677. auto target_nonconfigurable_keys = MarkedValueList { heap() };
  678. // 16. For each element key of targetKeys, do
  679. for (auto& key : target_keys) {
  680. // a. Let desc be ? target.[[GetOwnProperty]](key).
  681. auto descriptor = m_target.internal_get_own_property(PropertyName::from_value(global_object, key));
  682. // b. If desc is not undefined and desc.[[Configurable]] is false, then
  683. if (descriptor.has_value() && !*descriptor->configurable) {
  684. // i. Append key as an element of targetNonconfigurableKeys.
  685. target_nonconfigurable_keys.append(key);
  686. }
  687. // c. Else,
  688. else {
  689. // i. Append key as an element of targetConfigurableKeys.
  690. target_configurable_keys.append(key);
  691. }
  692. }
  693. // 17. If extensibleTarget is true and targetNonconfigurableKeys is empty, then
  694. if (extensible_target && target_nonconfigurable_keys.is_empty()) {
  695. // a. Return trapResult.
  696. return trap_result;
  697. }
  698. // 18. Let uncheckedResultKeys be a List whose elements are the elements of trapResult.
  699. auto unchecked_result_keys = MarkedValueList { heap() };
  700. unchecked_result_keys.extend(trap_result);
  701. // 19. For each element key of targetNonconfigurableKeys, do
  702. for (auto& key : target_nonconfigurable_keys) {
  703. // a. If key is not an element of uncheckedResultKeys, throw a TypeError exception.
  704. if (!unchecked_result_keys.contains_slow(key)) {
  705. vm.throw_exception<TypeError>(global_object, ErrorType::FixmeAddAnErrorString);
  706. return MarkedValueList { heap() };
  707. }
  708. // b. Remove key from uncheckedResultKeys.
  709. unchecked_result_keys.remove_first_matching([&](auto& value) {
  710. return same_value(value, key);
  711. });
  712. }
  713. // 20. If extensibleTarget is true, return trapResult.
  714. if (extensible_target)
  715. return trap_result;
  716. // 21. For each element key of targetConfigurableKeys, do
  717. for (auto& key : target_configurable_keys) {
  718. // a. If key is not an element of uncheckedResultKeys, throw a TypeError exception.
  719. if (!unchecked_result_keys.contains_slow(key)) {
  720. vm.throw_exception<TypeError>(global_object, ErrorType::FixmeAddAnErrorString);
  721. return MarkedValueList { heap() };
  722. }
  723. // b. Remove key from uncheckedResultKeys.
  724. unchecked_result_keys.remove_first_matching([&](auto& value) {
  725. return same_value(value, key);
  726. });
  727. }
  728. // 22. If uncheckedResultKeys is not empty, throw a TypeError exception.
  729. if (!unchecked_result_keys.is_empty()) {
  730. vm.throw_exception<TypeError>(global_object, ErrorType::FixmeAddAnErrorString);
  731. return MarkedValueList { heap() };
  732. }
  733. // 23. Return trapResult.
  734. return trap_result;
  735. }
  736. // 10.5.12 [[Call]] ( thisArgument, argumentsList ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-call-thisargument-argumentslist
  737. Value ProxyObject::call()
  738. {
  739. auto& vm = this->vm();
  740. auto& global_object = this->global_object();
  741. auto this_argument = vm.this_value(global_object);
  742. // 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.
  743. if (!is_function()) {
  744. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, Value(this).to_string_without_side_effects());
  745. return {};
  746. }
  747. // 1. Let handler be O.[[ProxyHandler]].
  748. // 2. If handler is null, throw a TypeError exception.
  749. if (m_is_revoked) {
  750. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyRevoked);
  751. return {};
  752. }
  753. // 3. Assert: Type(handler) is Object.
  754. // 4. Let target be O.[[ProxyTarget]].
  755. // 5. Let trap be ? GetMethod(handler, "apply").
  756. auto trap = Value(&m_handler).get_method(global_object, vm.names.apply);
  757. if (vm.exception())
  758. return {};
  759. // 6. If trap is undefined, then
  760. if (!trap) {
  761. // a. Return ? Call(target, thisArgument, argumentsList).
  762. return static_cast<FunctionObject&>(m_target).call();
  763. }
  764. // 7. Let argArray be ! CreateArrayFromList(argumentsList).
  765. auto arguments_array = Array::create(global_object, 0);
  766. vm.for_each_argument([&](auto& argument) {
  767. arguments_array->indexed_properties().append(argument);
  768. });
  769. // 8. Return ? Call(trap, handler, « target, thisArgument, argArray »).
  770. return vm.call(*trap, &m_handler, &m_target, this_argument, arguments_array);
  771. }
  772. // 10.5.13 [[Construct]] ( argumentsList, newTarget ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-construct-argumentslist-newtarget
  773. Value ProxyObject::construct(FunctionObject& new_target)
  774. {
  775. auto& vm = this->vm();
  776. auto& global_object = this->global_object();
  777. // 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.
  778. if (!is_function()) {
  779. vm.throw_exception<TypeError>(global_object, ErrorType::NotAConstructor, Value(this).to_string_without_side_effects());
  780. return {};
  781. }
  782. // 1. Let handler be O.[[ProxyHandler]].
  783. // 2. If handler is null, throw a TypeError exception.
  784. if (m_is_revoked) {
  785. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyRevoked);
  786. return {};
  787. }
  788. // 3. Assert: Type(handler) is Object.
  789. // 4. Let target be O.[[ProxyTarget]].
  790. // 5. Assert: IsConstructor(target) is true.
  791. // 6. Let trap be ? GetMethod(handler, "construct").
  792. auto trap = Value(&m_handler).get_method(global_object, vm.names.construct);
  793. if (vm.exception())
  794. return {};
  795. // 7. If trap is undefined, then
  796. if (!trap) {
  797. // a. Return ? Construct(target, argumentsList, newTarget).
  798. return static_cast<FunctionObject&>(m_target).construct(new_target);
  799. }
  800. // 8. Let argArray be ! CreateArrayFromList(argumentsList).
  801. auto arguments_array = Array::create(global_object, 0);
  802. vm.for_each_argument([&](auto& argument) {
  803. arguments_array->indexed_properties().append(argument);
  804. });
  805. // 9. Let newObj be ? Call(trap, handler, « target, argArray, newTarget »).
  806. auto result = vm.call(*trap, &m_handler, &m_target, arguments_array, &new_target);
  807. // 10. If Type(newObj) is not Object, throw a TypeError exception.
  808. if (!result.is_object()) {
  809. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyConstructBadReturnType);
  810. return {};
  811. }
  812. // 11. Return newObj.
  813. return result;
  814. }
  815. void ProxyObject::visit_edges(Cell::Visitor& visitor)
  816. {
  817. FunctionObject::visit_edges(visitor);
  818. visitor.visit(&m_target);
  819. visitor.visit(&m_handler);
  820. }
  821. const FlyString& ProxyObject::name() const
  822. {
  823. VERIFY(is_function());
  824. return static_cast<FunctionObject&>(m_target).name();
  825. }
  826. FunctionEnvironment* ProxyObject::create_environment(FunctionObject& function_being_invoked)
  827. {
  828. VERIFY(is_function());
  829. return static_cast<FunctionObject&>(m_target).create_environment(function_being_invoked);
  830. }
  831. }