ProxyConstructor.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using Jint.Collections;
  2. using Jint.Native.Function;
  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. public sealed class ProxyConstructor : FunctionInstance, IConstructor
  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 ClrFunctionInstance(_engine, "revocable", Revocable, 2, PropertyFlag.Configurable), true, true, true)
  29. };
  30. SetProperties(properties);
  31. }
  32. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  33. {
  34. ExceptionHelper.ThrowTypeError(_realm, "Constructor Proxy requires 'new'");
  35. return null;
  36. }
  37. ObjectInstance IConstructor.Construct(JsValue[] arguments, JsValue newTarget) => Construct(arguments);
  38. /// <summary>
  39. /// https://www.ecma-international.org/ecma-262/6.0/index.html#sec-proxy-object-internal-methods-and-internal-slots-construct-argumentslist-newtarget
  40. /// </summary>
  41. private ObjectInstance Construct(JsValue[] arguments)
  42. {
  43. var target = arguments.At(0);
  44. var handler = arguments.At(1);
  45. if (!target.IsObject() || !handler.IsObject())
  46. {
  47. ExceptionHelper.ThrowTypeError(_realm, "Cannot create proxy with a non-object as target or handler");
  48. }
  49. return Construct(target.AsObject(), handler.AsObject());
  50. }
  51. protected internal override ObjectInstance GetPrototypeOf()
  52. {
  53. return _realm.Intrinsics.Function.Prototype;
  54. }
  55. /// <summary>
  56. /// https://tc39.es/ecma262/#sec-proxy-target-handler
  57. /// </summary>
  58. public ProxyInstance Construct(ObjectInstance target, ObjectInstance handler)
  59. {
  60. if (target is ProxyInstance targetProxy && targetProxy._handler is null)
  61. {
  62. ExceptionHelper.ThrowTypeError(_realm);
  63. }
  64. if (handler is ProxyInstance handlerProxy && handlerProxy._handler is null)
  65. {
  66. ExceptionHelper.ThrowTypeError(_realm);
  67. }
  68. var instance = new ProxyInstance(Engine, target, handler);
  69. return instance;
  70. }
  71. private JsValue Revocable(JsValue thisObject, JsValue[] arguments)
  72. {
  73. var p = Construct(arguments);
  74. System.Func<JsValue, JsValue[], JsValue> revoke = (thisObject, arguments) =>
  75. {
  76. var proxy = (ProxyInstance) p;
  77. proxy._handler = null;
  78. proxy._target = null;
  79. return Undefined;
  80. };
  81. var result = _realm.Intrinsics.Object.Construct(System.Array.Empty<JsValue>());
  82. result.DefineOwnProperty(PropertyRevoke, new PropertyDescriptor(new ClrFunctionInstance(_engine, name: null, revoke, 0, PropertyFlag.Configurable), PropertyFlag.ConfigurableEnumerableWritable));
  83. result.DefineOwnProperty(PropertyProxy, new PropertyDescriptor(p, PropertyFlag.ConfigurableEnumerableWritable));
  84. return result;
  85. }
  86. }
  87. }