2
0

ProxyConstructor.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. internal 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. protected internal 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)
  38. {
  39. if (newTarget.IsUndefined())
  40. {
  41. ExceptionHelper.ThrowTypeError(_realm);
  42. }
  43. return Construct(arguments.At(0), arguments.At(1));
  44. }
  45. protected internal override ObjectInstance? GetPrototypeOf()
  46. {
  47. return _realm.Intrinsics.Function.Prototype;
  48. }
  49. /// <summary>
  50. /// https://tc39.es/ecma262/#sec-proxy-target-handler
  51. /// </summary>
  52. public ProxyInstance Construct(JsValue target, JsValue handler)
  53. {
  54. return ProxyCreate(target, handler);
  55. }
  56. /// <summary>
  57. /// https://tc39.es/ecma262/#sec-proxy.revocable
  58. /// </summary>
  59. private JsValue Revocable(JsValue thisObject, JsValue[] arguments)
  60. {
  61. var p = ProxyCreate(arguments.At(0), arguments.At(1));
  62. JsValue Revoke(JsValue thisObject, JsValue[] arguments)
  63. {
  64. p._handler = null;
  65. p._target = null!;
  66. return Undefined;
  67. }
  68. var result = _realm.Intrinsics.Object.Construct(System.Array.Empty<JsValue>());
  69. result.DefineOwnProperty(PropertyRevoke, new PropertyDescriptor(new ClrFunctionInstance(_engine, name: "", Revoke, 0, PropertyFlag.Configurable), PropertyFlag.ConfigurableEnumerableWritable));
  70. result.DefineOwnProperty(PropertyProxy, new PropertyDescriptor(p, PropertyFlag.ConfigurableEnumerableWritable));
  71. return result;
  72. }
  73. /// <summary>
  74. /// https://tc39.es/ecma262/#sec-proxycreate
  75. /// </summary>
  76. private ProxyInstance ProxyCreate(JsValue target, JsValue handler)
  77. {
  78. if (target is not ObjectInstance targetObject)
  79. {
  80. ExceptionHelper.ThrowTypeError(_realm, "Cannot create proxy with a non-object as target");
  81. return null;
  82. }
  83. if (handler is not ObjectInstance targetHandler)
  84. {
  85. ExceptionHelper.ThrowTypeError(_realm, "Cannot create proxy with a non-object as handler");
  86. return null;
  87. }
  88. var p = new ProxyInstance(Engine, targetObject, targetHandler);
  89. return p;
  90. }
  91. }
  92. }