Host.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Jint.Native.Global;
  2. using Jint.Native.Object;
  3. using Jint.Runtime.Environments;
  4. namespace Jint.Runtime
  5. {
  6. public class Host
  7. {
  8. protected Engine Engine { get; private set; }
  9. /// <summary>
  10. /// Initializes the host.
  11. /// </summary>
  12. public void Initialize(Engine engine)
  13. {
  14. Engine = engine;
  15. InitializeHostDefinedRealm();
  16. PostInitialize();
  17. }
  18. protected virtual void PostInitialize()
  19. {
  20. }
  21. /// <summary>
  22. /// https://tc39.es/ecma262/#sec-initializehostdefinedrealm
  23. /// </summary>
  24. protected virtual void InitializeHostDefinedRealm()
  25. {
  26. var realm = CreateRealm();
  27. var newContext = new ExecutionContext(
  28. lexicalEnvironment: realm.GlobalEnv,
  29. variableEnvironment: realm.GlobalEnv,
  30. privateEnvironment: null,
  31. realm: realm,
  32. function: null);
  33. Engine.EnterExecutionContext(newContext);
  34. }
  35. protected virtual GlobalEnvironmentRecord CreateGlobalEnvironment(ObjectInstance globalObject)
  36. {
  37. return JintEnvironment.NewGlobalEnvironment(Engine, globalObject, globalObject);
  38. }
  39. protected virtual ObjectInstance CreateGlobalObject(Realm realm)
  40. {
  41. var globalObject = new GlobalObject(Engine, realm);
  42. // Because the properties might need some of the built-in object
  43. // their configuration is delayed to a later step
  44. // trigger initialization
  45. globalObject.EnsureInitialized();
  46. return globalObject;
  47. }
  48. /// <summary>
  49. /// https://tc39.es/ecma262/#sec-createrealm
  50. /// </summary>
  51. protected internal virtual Realm CreateRealm()
  52. {
  53. var realmRec = new Realm();
  54. Engine._realmInConstruction = realmRec;
  55. CreateIntrinsics(realmRec);
  56. var globalObject = CreateGlobalObject(realmRec);
  57. var globalEnv = CreateGlobalEnvironment(globalObject);
  58. realmRec.GlobalEnv = globalEnv;
  59. realmRec.GlobalObject = globalObject;
  60. Engine._realmInConstruction = null;
  61. return realmRec;
  62. }
  63. /// <summary>
  64. /// https://tc39.es/ecma262/#sec-createintrinsics
  65. /// </summary>
  66. protected virtual void CreateIntrinsics(Realm realmRec)
  67. {
  68. var intrinsics = new Intrinsics(Engine, realmRec);
  69. realmRec.Intrinsics = intrinsics;
  70. }
  71. /// <summary>
  72. /// https://tc39.es/ecma262/#sec-hostensurecancompilestrings
  73. /// </summary>
  74. /// <param name="callerRealm"></param>
  75. /// <param name="evalRealm"></param>
  76. public virtual void EnsureCanCompileStrings(Realm callerRealm, Realm evalRealm)
  77. {
  78. }
  79. }
  80. }