Proxy.handler-has.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. load("test-common.js");
  2. try {
  3. assert("foo" in new Proxy({}, { has: null }) === false);
  4. assert("foo" in new Proxy({}, { has: undefined}) === false);
  5. assert("foo" in new Proxy({}, {}) === false);
  6. let o = {};
  7. let p = new Proxy(o, {
  8. has(target, prop) {
  9. assert(target === o);
  10. assert(prop === "foo");
  11. return true;
  12. }
  13. });
  14. "foo" in p;
  15. p = new Proxy(o, {
  16. has(target, prop) {
  17. if (target.checkedFoo)
  18. return true;
  19. if (prop === "foo")
  20. target.checkedFoo = true;
  21. return false;
  22. }
  23. });
  24. assert("foo" in p === false);
  25. assert("foo" in p === true);
  26. // Invariants
  27. o = {};
  28. Object.defineProperty(o, "foo", { configurable: false });
  29. Object.defineProperty(o, "bar", { value: 10, configurable: true });
  30. p = new Proxy(o, {
  31. has() {
  32. return false;
  33. }
  34. });
  35. assertThrowsError(() => {
  36. "foo" in p;
  37. }, {
  38. error: TypeError,
  39. message: "Proxy handler's has trap violates invariant: a property cannot be reported as non-existent if it exists on the target as a non-configurable property",
  40. });
  41. Object.preventExtensions(o);
  42. assertThrowsError(() => {
  43. "bar" in p;
  44. }, {
  45. error: TypeError,
  46. message: "Proxy handler's has trap violates invariant: a property cannot be reported as non-existent if it exists on the target and the target is non-extensible",
  47. });
  48. console.log("PASS");
  49. } catch (e) {
  50. console.log("FAIL: " + e);
  51. }