ProxyConstructor.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #pragma warning disable CA1859 // Use concrete types when possible for improved performance -- most of constructor methods return JsValue
  2. using Jint.Collections;
  3. using Jint.Native.Object;
  4. using Jint.Runtime;
  5. using Jint.Runtime.Descriptors;
  6. using Jint.Runtime.Interop;
  7. namespace Jint.Native.Proxy
  8. {
  9. /// <summary>
  10. /// https://tc39.es/ecma262/#sec-proxy-constructor
  11. /// </summary>
  12. internal sealed class ProxyConstructor : Constructor
  13. {
  14. private static readonly JsString _name = new JsString("Proxy");
  15. private static readonly JsString PropertyProxy = new JsString("proxy");
  16. private static readonly JsString PropertyRevoke = new JsString("revoke");
  17. internal ProxyConstructor(
  18. Engine engine,
  19. Realm realm)
  20. : base(engine, realm, _name)
  21. {
  22. _length = new PropertyDescriptor(2, PropertyFlag.Configurable);
  23. }
  24. protected override void Initialize()
  25. {
  26. var properties = new PropertyDictionary(1, checkExistingKeys: false)
  27. {
  28. ["revocable"] = new PropertyDescriptor(new ClrFunction(_engine, "revocable", Revocable, 2, PropertyFlag.Configurable), true, true, true)
  29. };
  30. SetProperties(properties);
  31. }
  32. public override ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  33. {
  34. if (newTarget.IsUndefined())
  35. {
  36. ExceptionHelper.ThrowTypeError(_realm);
  37. }
  38. return Construct(arguments.At(0), arguments.At(1));
  39. }
  40. protected internal override ObjectInstance? GetPrototypeOf()
  41. {
  42. return _realm.Intrinsics.Function.Prototype;
  43. }
  44. /// <summary>
  45. /// https://tc39.es/ecma262/#sec-proxy-target-handler
  46. /// </summary>
  47. public JsProxy Construct(JsValue target, JsValue handler)
  48. {
  49. return ProxyCreate(target, handler);
  50. }
  51. /// <summary>
  52. /// https://tc39.es/ecma262/#sec-proxy.revocable
  53. /// </summary>
  54. private JsValue Revocable(JsValue thisObject, JsValue[] arguments)
  55. {
  56. var p = ProxyCreate(arguments.At(0), arguments.At(1));
  57. JsValue Revoke(JsValue thisObject, JsValue[] arguments)
  58. {
  59. p._handler = null;
  60. p._target = null!;
  61. return Undefined;
  62. }
  63. var result = _realm.Intrinsics.Object.Construct(System.Array.Empty<JsValue>());
  64. result.DefineOwnProperty(PropertyRevoke, new PropertyDescriptor(new ClrFunction(_engine, name: "", Revoke, 0, PropertyFlag.Configurable), PropertyFlag.ConfigurableEnumerableWritable));
  65. result.DefineOwnProperty(PropertyProxy, new PropertyDescriptor(p, PropertyFlag.ConfigurableEnumerableWritable));
  66. return result;
  67. }
  68. /// <summary>
  69. /// https://tc39.es/ecma262/#sec-proxycreate
  70. /// </summary>
  71. private JsProxy ProxyCreate(JsValue target, JsValue handler)
  72. {
  73. if (target is not ObjectInstance targetObject)
  74. {
  75. ExceptionHelper.ThrowTypeError(_realm, "Cannot create proxy with a non-object as target");
  76. return null;
  77. }
  78. if (handler is not ObjectInstance targetHandler)
  79. {
  80. ExceptionHelper.ThrowTypeError(_realm, "Cannot create proxy with a non-object as handler");
  81. return null;
  82. }
  83. var p = new JsProxy(Engine, targetObject, targetHandler);
  84. return p;
  85. }
  86. }
  87. }