Engine.Modules.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  4. using Esprima;
  5. using Jint.Native;
  6. using Jint.Native.Object;
  7. using Jint.Native.Promise;
  8. using Jint.Runtime;
  9. using Jint.Runtime.Interpreter;
  10. using Jint.Runtime.Modules;
  11. namespace Jint
  12. {
  13. public partial class Engine
  14. {
  15. internal IModuleLoader ModuleLoader { get; set; }
  16. private readonly Dictionary<string, ModuleRecord> _modules = new();
  17. private readonly Dictionary<string, ModuleBuilder> _builders = new();
  18. /// <summary>
  19. /// https://tc39.es/ecma262/#sec-getactivescriptormodule
  20. /// </summary>
  21. internal IScriptOrModule? GetActiveScriptOrModule()
  22. {
  23. return _executionContexts?.GetActiveScriptOrModule();
  24. }
  25. internal ModuleRecord LoadModule(string? referencingModuleLocation, string specifier)
  26. {
  27. var moduleResolution = ModuleLoader.Resolve(referencingModuleLocation, specifier);
  28. if (_modules.TryGetValue(moduleResolution.Key, out var module))
  29. {
  30. return module;
  31. }
  32. if (_builders.TryGetValue(specifier, out var moduleBuilder))
  33. {
  34. module = LoadFromBuilder(specifier, moduleBuilder, moduleResolution);
  35. }
  36. else
  37. {
  38. module = LoaderFromModuleLoader(moduleResolution);
  39. }
  40. return module;
  41. }
  42. private CyclicModuleRecord LoadFromBuilder(string specifier, ModuleBuilder moduleBuilder, ResolvedSpecifier moduleResolution)
  43. {
  44. var parsedModule = moduleBuilder.Parse();
  45. var module = new BuilderModuleRecord(this, Realm, parsedModule, null, false);
  46. _modules[moduleResolution.Key] = module;
  47. moduleBuilder.BindExportedValues(module);
  48. _builders.Remove(specifier);
  49. return module;
  50. }
  51. private CyclicModuleRecord LoaderFromModuleLoader(ResolvedSpecifier moduleResolution)
  52. {
  53. var parsedModule = ModuleLoader.LoadModule(this, moduleResolution);
  54. var module = new SourceTextModuleRecord(this, Realm, parsedModule, moduleResolution.Uri?.LocalPath, false);
  55. _modules[moduleResolution.Key] = module;
  56. return module;
  57. }
  58. public void AddModule(string specifier, string code)
  59. {
  60. var moduleBuilder = new ModuleBuilder(this, specifier);
  61. moduleBuilder.AddSource(code);
  62. AddModule(specifier, moduleBuilder);
  63. }
  64. public void AddModule(string specifier, Action<ModuleBuilder> buildModule)
  65. {
  66. var moduleBuilder = new ModuleBuilder(this, specifier);
  67. buildModule(moduleBuilder);
  68. AddModule(specifier, moduleBuilder);
  69. }
  70. public void AddModule(string specifier, ModuleBuilder moduleBuilder)
  71. {
  72. _builders.Add(specifier, moduleBuilder);
  73. }
  74. public ObjectInstance ImportModule(string specifier)
  75. {
  76. return ImportModule(specifier, null);
  77. }
  78. internal ObjectInstance ImportModule(string specifier, string? referencingModuleLocation)
  79. {
  80. var moduleResolution = ModuleLoader.Resolve(referencingModuleLocation, specifier);
  81. if (!_modules.TryGetValue(moduleResolution.Key, out var module))
  82. {
  83. module = LoadModule(null, specifier);
  84. }
  85. if (module is not CyclicModuleRecord cyclicModule)
  86. {
  87. LinkModule(specifier, module);
  88. EvaluateModule(specifier, module);
  89. }
  90. else if (cyclicModule.Status == ModuleStatus.Unlinked)
  91. {
  92. LinkModule(specifier, cyclicModule);
  93. if (cyclicModule.Status == ModuleStatus.Linked)
  94. {
  95. ExecuteWithConstraints(true, () => EvaluateModule(specifier, cyclicModule));
  96. }
  97. if (cyclicModule.Status != ModuleStatus.Evaluated)
  98. {
  99. ExceptionHelper.ThrowNotSupportedException($"Error while evaluating module: Module is in an invalid state: '{cyclicModule.Status}'");
  100. }
  101. }
  102. RunAvailableContinuations();
  103. return ModuleRecord.GetModuleNamespace(module);
  104. }
  105. private static void LinkModule(string specifier, ModuleRecord module)
  106. {
  107. module.Link();
  108. }
  109. private JsValue EvaluateModule(string specifier, ModuleRecord cyclicModule)
  110. {
  111. var ownsContext = _activeEvaluationContext is null;
  112. _activeEvaluationContext ??= new EvaluationContext(this);
  113. JsValue evaluationResult;
  114. try
  115. {
  116. evaluationResult = cyclicModule.Evaluate();
  117. }
  118. finally
  119. {
  120. if (ownsContext)
  121. {
  122. _activeEvaluationContext = null;
  123. }
  124. }
  125. // This should instead be returned and resolved in ImportModule(specifier) only so Host.ImportModuleDynamically can use this promise
  126. if (evaluationResult is not PromiseInstance promise)
  127. {
  128. ExceptionHelper.ThrowInvalidOperationException($"Error while evaluating module: Module evaluation did not return a promise: {evaluationResult.Type}");
  129. }
  130. else if (promise.State == PromiseState.Rejected)
  131. {
  132. ExceptionHelper.ThrowJavaScriptException(this, promise.Value, new Completion(CompletionType.Throw, promise.Value, null, new Location(new Position(), new Position(), specifier)));
  133. }
  134. else if (promise.State != PromiseState.Fulfilled)
  135. {
  136. ExceptionHelper.ThrowInvalidOperationException($"Error while evaluating module: Module evaluation did not return a fulfilled promise: {promise.State}");
  137. }
  138. return evaluationResult;
  139. }
  140. }
  141. }