Proxy.hx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package js.lib;
  23. import js.lib.Object;
  24. import haxe.extern.EitherType;
  25. /**
  26. The `Proxy` object is used to define custom behavior for fundamental operations
  27. (e.g. property lookup, assignment, enumeration, function invocation, etc).
  28. Documentation [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) by [Mozilla Contributors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy$history), licensed under [CC-BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/).
  29. **/
  30. @:native("Proxy")
  31. extern class Proxy<T:{}> {
  32. @:pure function new(target:T, handler:ProxyHandler<T>);
  33. /**
  34. Creates a revocable `Proxy` object.
  35. **/
  36. @:pure static function revocable<T:{}>(target:T, handler:ProxyHandler<T>):RevocableProxy<T>;
  37. }
  38. typedef ProxyHandler<T:{}> = {
  39. /**
  40. A trap for `Object.getPrototypeOf`.
  41. **/
  42. var ?getPrototypeOf:(target:T) -> Null<{}>;
  43. /**
  44. A trap for `Object.setPrototypeOf`.
  45. **/
  46. var ?setPrototypeOf:(target:T, prototype:Null<{}>) -> Bool;
  47. /**
  48. A trap for `Object.isExtensible`.
  49. **/
  50. var ?isExtensible:(target:T) -> Bool;
  51. /**
  52. A trap for `Object.preventExtensions`.
  53. **/
  54. var ?preventExtensions:(target:T) -> Bool;
  55. /**
  56. A trap for `Object.getOwnPropertyDescriptor`.
  57. **/
  58. var ?getOwnPropertyDescriptor:(target:T, prop:EitherType<String, Symbol>) -> Null<ObjectPropertyDescriptor>;
  59. /**
  60. A trap for `Object.defineProperty`.
  61. **/
  62. var ?defineProperty:(target:T, property:EitherType<String, Symbol>, descriptor:ObjectPropertyDescriptor) -> Bool;
  63. /**
  64. A trap for the `in` operator.
  65. **/
  66. var ?has:(target:T, prop:EitherType<String, EitherType<Int, Symbol>>) -> Bool;
  67. /**
  68. A trap for getting property values.
  69. **/
  70. var ?get:(target:T, property:EitherType<String, EitherType<Int, Symbol>>, receiver:Null<{}>) -> Any;
  71. /**
  72. A trap for setting property values.
  73. **/
  74. var ?set:(target:T, property:EitherType<String, EitherType<Int, Symbol>>, value:Any, receiver:Null<{}>) -> Bool;
  75. /**
  76. A trap for the `delete` operator.
  77. **/
  78. var ?deleteProperty:(target:T, property:EitherType<String, EitherType<Int, Symbol>>) -> Bool;
  79. /**
  80. A trap for `Object.getOwnPropertyNames` and `Object.getOwnPropertySymbols`.
  81. **/
  82. var ?ownKeys:(target:T) -> Array<String>;
  83. /**
  84. A trap a function call.
  85. **/
  86. var ?apply:(target:T, thisArg:{}, argumentsList:Array<Any>) -> Any;
  87. /**
  88. A trap for the `new` operator.
  89. **/
  90. var ?construct:(target:Class<T>, argumentsList:Array<Any>, newTarget:Class<Any>) -> Void;
  91. }
  92. typedef RevocableProxy<T:{}> = {
  93. /**
  94. A Proxy object created with `new Proxy(target, handler)` call.
  95. **/
  96. final proxy:Proxy<T>;
  97. /**
  98. A function with no argument to invalidate (switch off) the `proxy`.
  99. **/
  100. function revoke():Void;
  101. }