Proxy.hx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /**
  25. The `Proxy` object is used to define custom behavior for fundamental operations
  26. (e.g. property lookup, assignment, enumeration, function invocation, etc).
  27. 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/).
  28. **/
  29. @:native("Proxy")
  30. extern class Proxy<T:{}> {
  31. @:pure function new(target:T, handler:ProxyHandler<T>);
  32. /**
  33. Creates a revocable `Proxy` object.
  34. **/
  35. @:pure static function revocable<T:{}>(target:T, handler:ProxyHandler<T>):RevocableProxy<T>;
  36. }
  37. typedef ProxyHandler<T:{}> = {
  38. /**
  39. A trap for `Object.getPrototypeOf`.
  40. **/
  41. var ?getPrototypeOf:(target:T) -> Null<{}>;
  42. /**
  43. A trap for `Object.setPrototypeOf`.
  44. **/
  45. var ?setPrototypeOf:(target:T, prototype:Null<{}>) -> Bool;
  46. /**
  47. A trap for `Object.isExtensible`.
  48. **/
  49. var ?isExtensible:(target:T) -> Bool;
  50. /**
  51. A trap for `Object.preventExtensions`.
  52. **/
  53. var ?preventExtensions:(target:T) -> Bool;
  54. /**
  55. A trap for `Object.getOwnPropertyDescriptor`.
  56. **/
  57. var ?getOwnPropertyDescriptor:(target:T, prop:String) -> Null<ObjectPropertyDescriptor>;
  58. /**
  59. A trap for `Object.defineProperty`.
  60. **/
  61. var ?defineProperty:(target:T, property:String, descriptor:ObjectPropertyDescriptor) -> Bool;
  62. /**
  63. A trap for the `in` operator.
  64. **/
  65. var ?has:(target:T, prop:String) -> Bool;
  66. /**
  67. A trap for getting property values.
  68. **/
  69. var ?get:(target:T, property:String, receiver:Null<{}>) -> Any;
  70. /**
  71. A trap for setting property values.
  72. **/
  73. var ?set:(target:T, property:String, value:Any, receiver:Null<{}>) -> Bool;
  74. /**
  75. A trap for the `delete` operator.
  76. **/
  77. var ?deleteProperty:(target:T, property:String) -> Bool;
  78. /**
  79. A trap for `Object.getOwnPropertyNames` and `Object.getOwnPropertySymbols`.
  80. **/
  81. var ?ownKeys:(target:T) -> Array<String>;
  82. /**
  83. A trap a function call.
  84. **/
  85. var ?apply:(target:T, thisArg:{}, argumentsList:Array<Any>) -> Any;
  86. /**
  87. A trap for the `new` operator.
  88. **/
  89. var ?construct:(target:Class<T>, argumentsList:Array<Any>, newTarget:Class<Any>) -> Void;
  90. }
  91. typedef RevocableProxy<T:{}> = {
  92. /**
  93. A Proxy object created with `new Proxy(target, handler)` call.
  94. **/
  95. final proxy:Proxy<T>;
  96. /**
  97. A function with no argument to invalidate (switch off) the `proxy`.
  98. **/
  99. function revoke():Void;
  100. }