ShadowRealmConstructor.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using Jint.Native.Function;
  2. using Jint.Native.Object;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. using Jint.Runtime.Environments;
  6. namespace Jint.Native.ShadowRealm;
  7. /// <summary>
  8. /// https://tc39.es/proposal-shadowrealm/#sec-properties-of-the-shadowRealm-constructor
  9. /// </summary>
  10. public sealed class ShadowRealmConstructor : Constructor
  11. {
  12. private static readonly JsString _functionName = new("ShadowRealm");
  13. internal ShadowRealmConstructor(
  14. Engine engine,
  15. Realm realm,
  16. FunctionPrototype functionPrototype,
  17. ObjectPrototype objectPrototype)
  18. : base(engine, realm, _functionName)
  19. {
  20. _prototype = functionPrototype;
  21. PrototypeObject = new ShadowRealmPrototype(engine, realm, this, objectPrototype);
  22. _length = new PropertyDescriptor(0, PropertyFlag.Configurable);
  23. _prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
  24. }
  25. private ShadowRealmPrototype PrototypeObject { get; }
  26. public ShadowRealm Construct()
  27. {
  28. return Construct(PrototypeObject);
  29. }
  30. private ShadowRealm Construct(JsValue newTarget)
  31. {
  32. var realmRec = _engine._host.CreateRealm();
  33. var o = OrdinaryCreateFromConstructor(
  34. newTarget,
  35. static intrinsics => intrinsics.ShadowRealm.PrototypeObject,
  36. static (engine, _, realmRec) =>
  37. {
  38. var context = new ExecutionContext(
  39. scriptOrModule: null,
  40. lexicalEnvironment: realmRec!.GlobalEnv,
  41. variableEnvironment: realmRec.GlobalEnv,
  42. privateEnvironment: null,
  43. realm: realmRec,
  44. function: null);
  45. return new ShadowRealm(engine, context, realmRec);
  46. },
  47. realmRec);
  48. // this are currently handled as part of realm construction
  49. // SetRealmGlobalObject(realmRec, Undefined, Undefined);
  50. // SetDefaultGlobalBindings(o._ShadowRealm);
  51. _engine._host.InitializeShadowRealm(o._shadowRealm);
  52. return o;
  53. }
  54. public override ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  55. {
  56. if (newTarget.IsUndefined())
  57. {
  58. ExceptionHelper.ThrowTypeError(_realm);
  59. }
  60. return Construct(newTarget);
  61. }
  62. }